Skip to content

Commit

Permalink
tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Krist committed Dec 31, 2024
1 parent 7de480b commit c1ede9c
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 321 deletions.
157 changes: 78 additions & 79 deletions tests/test_communities/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
import os

from thesis.proxies import current_service
import pytest
import yaml
from flask_security import login_user
Expand Down Expand Up @@ -47,8 +47,8 @@
from oarepo_communities.services.permissions.policy import (
CommunityDefaultWorkflowPermissions,
)
from tests.test_communities.utils import link_api2testclient

from tests.test_communities.utils import link2testclient
from deepmerge import always_merger

@pytest.fixture(scope="function")
def sample_metadata_list():
Expand Down Expand Up @@ -545,8 +545,6 @@ def create_community(slug, community_owner, workflow="default"):

return create_community


# -----
from invenio_requests.customizations import RequestType
from invenio_requests.proxies import current_requests

Expand All @@ -557,58 +555,6 @@ def requests_service(app):

return current_requests.requests_service


@pytest.fixture()
def create_publish_request(requests_service):
"""Request Factory fixture."""

def _create_request(identity, receiver, **kwargs):
"""Create a request."""
RequestType.allowed_receiver_ref_types = ["community"]
RequestType.needs_context = {
"community_permission_name": "can_publish",
}
# Need to use the service to get the id
item = requests_service.create(
identity=identity,
data={},
request_type=RequestType,
receiver=receiver,
**kwargs,
)
return item._request

return _create_request


# -----
from thesis.proxies import current_service


@pytest.fixture(scope="function")
def sample_draft(app, db, input_data, community):
input_data["community_id"] = community.id
draft_item = current_service.create(system_identity, input_data)
ThesisDraft.index.refresh()
return ThesisDraft.pid.resolve(draft_item.id, registered_only=False)


@pytest.fixture
def request_data_factory():
def _request_data(
community_id, topic_type, topic_id, request_type, creator=None, payload=None
):
input_data = {
"request_type": request_type,
"topic": {topic_type: topic_id},
}
if payload:
input_data["payload"] = payload
return input_data

return _request_data


@pytest.fixture
def ui_serialized_community_role():
def _ui_serialized_community(community_id):
Expand Down Expand Up @@ -693,8 +639,8 @@ def get_request_type():
def _get_request_type(request_types_json, request_type):
selected_entry = [
entry for entry in request_types_json if entry["type_id"] == request_type
][0]
return selected_entry
]
return selected_entry[0] if selected_entry else None

return _get_request_type

Expand All @@ -707,49 +653,102 @@ def get_request_link(get_request_type):

def _create_request_from_link(request_types_json, request_type):
selected_entry = get_request_type(request_types_json, request_type)
return selected_entry["links"]["actions"]["create"]
return selected_entry["links"]["actions"]["create"] if selected_entry else None

return _create_request_from_link


@pytest.fixture()
def clear_babel_context():

# force babel reinitialization when language is switched
def _clear_babel_context():
try:
from flask import g
from flask_babel import SimpleNamespace

g._flask_babel = SimpleNamespace()
except ImportError:
return

return _clear_babel_context


@pytest.fixture()
def request_type_additional_data():
return {"publish_draft": {"payload": {"version": "1.0"}}}


@pytest.fixture
def create_request_by_link(get_request_link):
def _create_request(client, record, request_type):
def create_request_by_link(get_request_link, request_type_additional_data):
def _create_request(
client, record, request_type, additional_data=None, **request_kwargs
):
if additional_data is None:
additional_data = {}
applicable_requests = client.get(
link_api2testclient(record.json["links"]["applicable-requests"])
link2testclient(record.json["links"]["applicable-requests"])
).json["hits"]["hits"]
create_link = link_api2testclient(
create_link = link2testclient(
get_request_link(applicable_requests, request_type)
)
create_response = client.post(create_link)
if request_type in request_type_additional_data:
additional_data = always_merger.merge(
additional_data, request_type_additional_data[request_type]
)
if not additional_data:
create_response = client.post(create_link, **request_kwargs)
else:
create_response = client.post(
create_link, json=additional_data, **request_kwargs
)
return create_response

return _create_request



@pytest.fixture
def submit_request_by_link(create_request_by_link):
def _submit_request(client, record, request_type):
create_response = create_request_by_link(client, record, request_type)
def _submit_request(
client,
record,
request_type,
create_additional_data=None,
submit_additional_data=None,
):
create_response = create_request_by_link(
client, record, request_type, additional_data=create_additional_data
)
submit_response = client.post(
link_api2testclient(create_response.json["links"]["actions"]["submit"])
link2testclient(create_response.json["links"]["actions"]["submit"]),
json=submit_additional_data,
)
return submit_response

return _submit_request

@pytest.fixture
def draft_in_community():
def _draft_in_community(client, comm_id, custom_workflow=None):
if custom_workflow:
return client.post(
f"/communities/{comm_id}/thesis",
json={"parent": {"workflow": custom_workflow}},
)

@pytest.fixture()
def clear_babel_context():
return client.post(f"/communities/{comm_id}/thesis", json={})
return _draft_in_community

# force babel reinitialization when language is switched
def _clear_babel_context():
try:
from flask import g
from flask_babel import SimpleNamespace
@pytest.fixture
def published_record_in_community(record_service, draft_in_community):
# skip the request approval
def _published_record_in_community(client, community_id, custom_workflow=None):
response = draft_in_community(client, community_id, custom_workflow)
record_item = record_service.publish(system_identity, response.json["id"])
ret = client.get(f"/thesis/{record_item['id']}")
return ret
return _published_record_in_community

g._flask_babel = SimpleNamespace()
except ImportError:
return

return _clear_babel_context
# todo - idea - oarepo-pytest library or something for these fixtures so we don't have to redefine them each time?
43 changes: 15 additions & 28 deletions tests/test_communities/test_community_records.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from thesis.records.api import ThesisDraft, ThesisRecord

from tests.test_communities.utils import published_record_in_community


def test_create_record_in_community(
logged_client,
community_owner,
Expand Down Expand Up @@ -45,6 +42,7 @@ def test_search(
community_owner,
community_reader,
community_with_workflow_factory,
published_record_in_community,
record_service,
search_clear,
):
Expand All @@ -53,12 +51,8 @@ def test_search(
community_1 = community_with_workflow_factory("comm1", community_owner)
community_2 = community_with_workflow_factory("comm2", community_owner)

record1 = published_record_in_community(
owner_client, community_1.id, record_service, community_owner
)
record2 = published_record_in_community(
owner_client, community_2.id, record_service, community_owner
)
record1 = published_record_in_community(owner_client, community_1.id)
record2 = published_record_in_community(owner_client, community_2.id)

ThesisRecord.index.refresh()
ThesisDraft.index.refresh()
Expand All @@ -76,8 +70,8 @@ def test_search(
assert len(response_draft1.json["hits"]["hits"]) == 1
assert len(response_draft2.json["hits"]["hits"]) == 1

assert response_record1.json["hits"]["hits"][0]["id"] == record1["id"]
assert response_record2.json["hits"]["hits"][0]["id"] == record2["id"]
assert response_record1.json["hits"]["hits"][0]["id"] == record1.json["id"]
assert response_record2.json["hits"]["hits"][0]["id"] == record2.json["id"]


# todo tests for search links
Expand All @@ -88,6 +82,7 @@ def test_search_model(
community_owner,
community_reader,
community_with_workflow_factory,
published_record_in_community,
record_service,
search_clear,
):
Expand All @@ -96,12 +91,8 @@ def test_search_model(
community_1 = community_with_workflow_factory("comm1", community_owner)
community_2 = community_with_workflow_factory("comm2", community_owner)

record1 = published_record_in_community(
owner_client, community_1.id, record_service, community_owner
)
record2 = published_record_in_community(
owner_client, community_2.id, record_service, community_owner
)
record1 = published_record_in_community(owner_client, community_1.id)
record2 = published_record_in_community(owner_client, community_2.id)

ThesisRecord.index.refresh()
ThesisDraft.index.refresh()
Expand All @@ -112,8 +103,8 @@ def test_search_model(
assert len(response_record1.json["hits"]["hits"]) == 1
assert len(response_record2.json["hits"]["hits"]) == 1

assert response_record1.json["hits"]["hits"][0]["id"] == record1["id"]
assert response_record2.json["hits"]["hits"][0]["id"] == record2["id"]
assert response_record1.json["hits"]["hits"][0]["id"] == record1.json["id"]
assert response_record2.json["hits"]["hits"][0]["id"] == record2.json["id"]


def test_user_search(
Expand Down Expand Up @@ -192,6 +183,7 @@ def test_search_links(
community_owner,
community_reader,
community_with_workflow_factory,
published_record_in_community,
record_service,
search_clear,
site_hostname="127.0.0.1:5000",
Expand All @@ -201,9 +193,7 @@ def test_search_links(
community_1 = community_with_workflow_factory("comm1", community_owner)

for _ in range(30):
published_record_in_community(
owner_client, community_1.id, record_service, community_owner
)
published_record_in_community(owner_client, community_1.id)
ThesisRecord.index.refresh()

def check_links(model_suffix):
Expand Down Expand Up @@ -251,6 +241,7 @@ def test_search_ui_serialization(
community_owner,
community_reader,
community_with_workflow_factory,
published_record_in_community,
record_service,
inviter,
search_clear,
Expand All @@ -262,12 +253,8 @@ def test_search_ui_serialization(
community_2 = community_with_workflow_factory("comm2", community_owner)
inviter("2", community_1.id, "reader")

record1 = published_record_in_community(
owner_client, community_1.id, record_service, community_owner
)
record2 = published_record_in_community(
owner_client, community_2.id, record_service, community_owner
)
record1 = published_record_in_community(owner_client, community_1.id)
record2 = published_record_in_community(owner_client, community_2.id)

ThesisRecord.index.refresh()
ThesisDraft.index.refresh()
Expand Down
Loading

0 comments on commit c1ede9c

Please sign in to comment.