forked from superdesk/superdesk-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5dc464
commit 49e06f2
Showing
6 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ types-Werkzeug | |
types-jwt | ||
typing-extensions | ||
boto3-stubs[s3,sqs] | ||
motor-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |