Skip to content

Commit

Permalink
add/update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLark86 committed Jun 18, 2024
1 parent b5dc464 commit 49e06f2
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 5 deletions.
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ requests-mock==1.11.0
responses
pytest
pytest-env
pytest-asyncio
python3-saml>=1.9,<1.17
typing_extensions>=3.7.4
moto[sqs]<5.0
Expand Down
1 change: 1 addition & 0 deletions mypy-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ types-Werkzeug
types-jwt
typing-extensions
boto3-stubs[s3,sqs]
motor-types
51 changes: 51 additions & 0 deletions superdesk/tests/asyncio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2024 Sourcefabric z.u. and contributors.
#
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

from typing import Dict, Any
import unittest
from dataclasses import dataclass

from superdesk.core.app import SuperdeskAsyncApp

from . import setup_config


@dataclass
class WSGI:
config: Dict[str, Any]


class AsyncTestCase(unittest.IsolatedAsyncioTestCase):
app: SuperdeskAsyncApp
app_config: Dict[str, Any] = {}

def setup(self):
self.app_config = setup_config(self.app_config)
self.app = SuperdeskAsyncApp(WSGI(config=self.app_config))
self.app.start()

for resource_config in self.app.mongo.get_all_resource_configs():
client, db = self.app.mongo.get_client(resource_config.name)
client.drop_database(db)

def teardown(self):
self.app.stop()

async def asyncSetUp(self):
self.app_config = setup_config(self.app_config)
self.app = SuperdeskAsyncApp(WSGI(config=self.app_config))
self.app.start()

for resource_config in self.app.mongo.get_all_resource_configs():
client, db = self.app.mongo.get_client_async(resource_config.name)
await client.drop_database(db)

async def asyncTearDown(self):
self.app.stop()
7 changes: 2 additions & 5 deletions tests/core/app_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from typing import Dict, Any
from dataclasses import dataclass
from unittest import TestCase, mock
from superdesk.core.app import SuperdeskAsyncApp


@dataclass
class WSGI:
config: Dict[str, Any]
from superdesk.core.app import SuperdeskAsyncApp
from superdesk.tests.asyncio import WSGI


class SuperdeskAsyncAppTestCase(TestCase):
Expand Down
28 changes: 28 additions & 0 deletions tests/core/modules/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from superdesk.core.module import Module, SuperdeskAsyncApp
from superdesk.core.mongo import MongoResourceConfig, MongoIndexOptions


user_mongo_resource = MongoResourceConfig(
name="users",
indexes=[
MongoIndexOptions(
name="users_name_1",
keys=[("first_name", 1)],
),
MongoIndexOptions(
name="combined_name_1",
keys=[("first_name", 1), ("last_name", -1)],
background=False,
unique=False,
sparse=False,
collation={"locale": "en", "strength": 1},
),
],
)


def init(app: SuperdeskAsyncApp):
app.mongo.register_resource_config(user_mongo_resource)


module = Module(name="tests.users", init=init)
87 changes: 87 additions & 0 deletions tests/core/mongo_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from pymongo import MongoClient
from pymongo.database import Database
from pymongo.errors import DuplicateKeyError
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase

from superdesk.tests.asyncio import AsyncTestCase
from .modules.users import user_mongo_resource


class MongoClientTestCase(AsyncTestCase):
app_config = {"MODULES": ["tests.core.modules.users"]}

def test_mongo_resource_registration(self):
assert self.app.mongo.get_resource_config("users") == user_mongo_resource

with self.assertRaises(KeyError):
self.app.mongo.get_resource_config("profiles")

# Test immutable resource config
modified_resource_config = self.app.mongo.get_resource_config("users")
modified_resource_config.prefix = "MONGO_MODIFIED"
assert self.app.mongo.get_resource_config("users") != modified_resource_config

def test_get_mongo_clients(self):
client, db = self.app.mongo.get_client("users")
assert isinstance(client, MongoClient)
assert isinstance(db, Database)

client, db = self.app.mongo.get_client_async("users")
assert isinstance(client, AsyncIOMotorClient)
assert isinstance(db, AsyncIOMotorDatabase)

def test_collection_operations(self):
collection = self.app.mongo.get_db("users").get_collection("users")

assert collection.find_one({"_id": "user_1"}) is None

collection.insert_one({"_id": "user_1", "name": "John"})
assert collection.find_one({"_id": "user_1"})["name"] == "John"

with self.assertRaises(DuplicateKeyError):
collection.insert_one({"_id": "user_1", "name": "Bar"})

collection.update_one({"_id": "user_1"}, {"$set": {"name": "Foo"}})
assert collection.find_one({"_id": "user_1"})["name"] == "Foo"

collection.delete_one({"_id": "user_1"})
assert collection.find_one({"_id": "user_1"}) is None

async def test_collection_operations_async(self):
collection = self.app.mongo.get_db_async("users").get_collection("users")

assert (await collection.find_one({"_id": "user_1"})) is None

await collection.insert_one({"_id": "user_1", "name": "John"})
assert (await collection.find_one({"_id": "user_1"}))["name"] == "John"

with self.assertRaises(DuplicateKeyError):
await collection.insert_one({"_id": "user_1", "name": "Bar"})

await collection.update_one({"_id": "user_1"}, {"$set": {"name": "Foo"}})
assert (await collection.find_one({"_id": "user_1"}))["name"] == "Foo"

await collection.delete_one({"_id": "user_1"})
assert (await collection.find_one({"_id": "user_1"})) is None

def test_init_indexes(self):
db = self.app.mongo.get_db("users")

indexes = db.get_collection("users").index_information()
self.assertIsNone(indexes.get("users_name_1"))
self.assertIsNone(indexes.get("combined_name_1"))

self.app.mongo.create_indexes_for_all_resources()
indexes = db.get_collection("users").index_information()
self.assertEqual(indexes["users_name_1"]["key"], [("first_name", 1)])
self.assertEqual(indexes["users_name_1"]["unique"], True)
self.assertEqual(indexes["users_name_1"]["background"], True)
self.assertEqual(indexes["users_name_1"]["sparse"], True)

self.assertEqual(indexes["combined_name_1"]["key"], [("first_name", 1), ("last_name", -1)])
self.assertNotEqual(indexes["combined_name_1"].get("unique"), True)
self.assertEqual(indexes["combined_name_1"]["background"], False)
self.assertEqual(indexes["combined_name_1"]["sparse"], False)
# ``collation`` uses an ``bson.son.SON` instance, so use that for testing here
self.assertEqual(indexes["combined_name_1"]["collation"].get("locale"), "en")
self.assertEqual(indexes["combined_name_1"]["collation"].get("strength"), 1)

0 comments on commit 49e06f2

Please sign in to comment.