Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLark86 committed Aug 30, 2024
1 parent 1669efa commit 2664e79
Show file tree
Hide file tree
Showing 6 changed files with 449 additions and 13 deletions.
3 changes: 3 additions & 0 deletions superdesk/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ def get_fixture_path(self, filename):
rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
return os.path.join(rootpath, "features", "steps", "fixtures", filename)

def assertDictContains(self, source: dict, contains: dict):
self.assertDictEqual({key: val for key, val in source.items() if key in contains}, contains)


class TestClient(QuartClient):
def model_instance_to_json(self, model_instance: ResourceModel):
Expand Down
15 changes: 15 additions & 0 deletions tests/core/modules/content/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from superdesk.core.module import Module

from .model import Content
from .resources import ContentResourceService, content_model_config

__all__ = [
"Content",
"ContentResourceService",
"content_model_config",
]

module = Module(
name="tests.content",
resources=[content_model_config],
)
7 changes: 7 additions & 0 deletions tests/core/modules/content/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from superdesk.core.resources import ResourceModel, ModelWithVersions


class Content(ResourceModel, ModelWithVersions):
guid: str
lock_user: str | None = None
headline: str | None = None
41 changes: 41 additions & 0 deletions tests/core/modules/content/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from superdesk.core.resources import (
ResourceConfig,
RestEndpointConfig,
AsyncResourceService,
MongoResourceConfig,
MongoIndexOptions,
ElasticResourceConfig,
)
from .model import Content


class ContentResourceService(AsyncResourceService[Content]):
resource_name = "content_async"


content_model_config = ResourceConfig(
name="content_async",
data_class=Content,
service=ContentResourceService,
versioning=True,
ignore_fields_in_versions=["lock_user"],
rest_endpoints=RestEndpointConfig(),
mongo=MongoResourceConfig(
indexes=[
MongoIndexOptions(
name="guid",
keys=[("guid", 1)],
background=True,
unique=False,
),
],
version_indexes=[
MongoIndexOptions(
name="_id_document_1",
keys=[("_id_document", 1)],
background=True,
unique=False,
)
],
),
)
47 changes: 34 additions & 13 deletions tests/core/resource_endpoints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from superdesk.tests import AsyncFlaskTestCase

from .modules.users import UserResourceService
from .fixtures.users import john_doe
from .fixtures.users import john_doe, jane_doe


NOW = utcnow()
Expand Down Expand Up @@ -178,18 +178,15 @@ async def test_hateoas(self):
},
"self": {
"href": "api/users_async",
"title": "users_async",
},
"next": {
"href": "api/users_async?max_results=25&page=2",
"title": "next page",
"title": "User",
},
},
)

# Now add a user, and make sure hateoas & links are reflected
test_user = john_doe()
response = await self.test_client.post("/api/users_async", json=test_user)
# Now add 2 users, and make sure hateoas & links are reflected
response = await self.test_client.post("/api/users_async", json=john_doe())
assert response.status_code == 201
response = await self.test_client.post("/api/users_async", json=jane_doe())
assert response.status_code == 201

response = await self.test_client.get("/api/users_async")
Expand All @@ -199,7 +196,7 @@ async def test_hateoas(self):
{
"max_results": 25,
"page": 1,
"total": 1,
"total": 2,
},
)
self.assertEqual(
Expand All @@ -211,14 +208,38 @@ async def test_hateoas(self):
},
"self": {
"href": "api/users_async?max_results=25",
"title": "users_async",
"title": "User",
},
},
)

response = await self.test_client.get("/api/users_async?max_results=1")
json_data = await response.get_json()
self.assertEqual(
json_data["_meta"],
{
"max_results": 1,
"page": 1,
"total": 2,
},
)
self.assertEqual(
json_data["_links"],
{
"parent": {
"href": "/",
"title": "home",
},
"self": {
"href": "api/users_async?max_results=1",
"title": "User",
},
"next": {
"href": "api/users_async?max_results=25&page=2",
"href": "api/users_async?max_results=1&page=2",
"title": "next page",
},
"last": {
"href": "api/users_async?max_results=25",
"href": "api/users_async?max_results=1&page=2",
"title": "last page",
},
},
Expand Down
Loading

0 comments on commit 2664e79

Please sign in to comment.