Skip to content

Commit

Permalink
Merge pull request #508 from OP-TED/feature/TED4-67
Browse files Browse the repository at this point in the history
optimise e2e tests
  • Loading branch information
CaptainOfHacks authored Oct 11, 2023
2 parents 39e3df4 + 231af55 commit 8c86cd0
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 190 deletions.
2 changes: 1 addition & 1 deletion ted_sws/data_manager/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MONGO_DB_AGGREGATES_DATABASE_DEFAULT_NAME = "aggregates_db"

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pymongo import MongoClient
from ted_sws import config
from ted_sws.data_manager.services import MONGO_DB_AGGREGATES_DATABASE_DEFAULT_NAME

NOTICE_PROCESS_BATCH_COLLECTION_NAME = "batch_events"
LOG_EVENTS_COLLECTION_NAME = "log_events"
Expand All @@ -13,7 +12,7 @@ def create_batch_collection_materialised_view(mongo_client: MongoClient):
:param mongo_client: mongodb client to connect
"""
database = mongo_client[config.MONGO_DB_AGGREGATES_DATABASE_NAME or MONGO_DB_AGGREGATES_DATABASE_DEFAULT_NAME]
database = mongo_client[config.MONGO_DB_AGGREGATES_DATABASE_NAME]
batch_collection = database[LOG_EVENTS_COLLECTION_NAME]
batch_collection.aggregate([
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from pymongo import MongoClient, ASCENDING, DESCENDING

from ted_sws import config
from ted_sws.data_manager.services import MONGO_DB_AGGREGATES_DATABASE_DEFAULT_NAME

NOTICE_COLLECTION_NAME = "notice_collection"
NOTICES_MATERIALISED_VIEW_NAME = "notices_collection_materialised_view"
Expand All @@ -14,7 +13,7 @@ def create_notice_collection_materialised_view(mongo_client: MongoClient):
Creates a collection with materialized view used on metabase by aggregating notices collection.
:param mongo_client: MongoDB client to connect
"""
database = mongo_client[config.MONGO_DB_AGGREGATES_DATABASE_NAME or MONGO_DB_AGGREGATES_DATABASE_DEFAULT_NAME]
database = mongo_client[config.MONGO_DB_AGGREGATES_DATABASE_NAME]
notice_collection = database[NOTICE_COLLECTION_NAME]
notice_collection.aggregate([
{
Expand Down Expand Up @@ -63,7 +62,7 @@ def create_notice_kpi_collection(mongo_client: MongoClient):
Creates a collection with kpi for existing notices.
:param mongo_client: MongoDB client to connect
"""
database = mongo_client[config.MONGO_DB_AGGREGATES_DATABASE_NAME or MONGO_DB_AGGREGATES_DATABASE_DEFAULT_NAME]
database = mongo_client[config.MONGO_DB_AGGREGATES_DATABASE_NAME]
notice_events_collection = database[NOTICE_EVENTS_COLLECTION_NAME]
notice_events_collection.aggregate([
{
Expand Down
23 changes: 0 additions & 23 deletions tests/clean_mongo_db.py

This file was deleted.

8 changes: 0 additions & 8 deletions tests/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@
import mongomock
import pymongo
import pytest
from pymongo import MongoClient

from ted_sws import config
from ted_sws.data_manager.adapters.notice_repository import NoticeRepository
from ted_sws.data_manager.adapters.triple_store import AllegroGraphTripleStore, FusekiAdapter
from tests import TEST_DATA_PATH


@pytest.fixture
def mongodb_client():
uri = config.MONGO_DB_AUTH_URL
mongodb_client = MongoClient(uri)
return mongodb_client


@pytest.fixture
def allegro_triple_store():
return AllegroGraphTripleStore(host=config.ALLEGRO_HOST, user=config.AGRAPH_SUPER_USER,
Expand Down
152 changes: 14 additions & 138 deletions tests/e2e/data_manager/test_mongodb_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import random
import string

from pymongo import MongoClient

from ted_sws import config
from ted_sws.data_manager.adapters.notice_repository import NoticeRepository
from ted_sws.data_manager.services.create_batch_collection_materialised_view import \
Expand All @@ -20,130 +15,9 @@
from ted_sws.notice_metadata_processor.services.metadata_normalizer import normalise_notice


def test_mongodb_client(notice_2016):
uri = config.MONGO_DB_AUTH_URL
mongodb_client = MongoClient(uri)
mongodb_client.drop_database('test')
test_db = mongodb_client['test']
fruits_collection = test_db['fruits']
fruits_collection.insert_one({"banana": 10, "orange": 50})
fruits_collection.insert_one({"banana": 15, "orange": 50})
result_fruits = fruits_collection.find_one({"banana": 10})
assert isinstance(result_fruits, dict)
assert result_fruits["orange"] == 50
assert result_fruits["banana"] == 10


def random_string() -> str:
return ''.join(random.choice(string.ascii_letters) for i in range(random.randint(5, 30)))


def random_list() -> list:
return [random.randint(5, 30) for i in range(0, random.randint(3, 10))]


def random_object() -> dict:
return {"xpath": random_list(),
"notices": random.randint(0, 1000)}


pipeline = [
{
"$project": {
"notices": 1,
"_id": 0
}
}
]


def test_mongodb_queries():
uri = config.MONGO_DB_AUTH_URL
mongodb_client = MongoClient(uri)
mongodb_client.drop_database('test')
test_db = mongodb_client['test']
objects_collection = test_db['objects']
for i in range(0, 20):
objects_collection.insert_one(random_object())

unique_xpaths = objects_collection.distinct("xpath")

unique_notice_ids = objects_collection.distinct("notices")
minimal_set_of_xpaths = []
covered_notice_ids = []
while len(unique_notice_ids):
xpaths = []
for xpath_id in list(unique_xpaths):
tmp_result = list(
objects_collection.aggregate([{"$match": {"xpath": {"$in": [xpath_id]},
"notices": {"$in": unique_notice_ids}}},
{"$project": {"_id": 0,
"notice_id": "$notices"}},
{

"$group": {"_id": None,
"notice_ids": {"$push": "$notice_id"}
}
},
{"$project": {"_id": 0,
"notice_ids": 1,
"count_notices": {"$size": "$notice_ids"}}},
{
"$addFields": {"xpath": xpath_id}
}
]))

if len(tmp_result):
xpaths.append(tmp_result[0])

top_xpath = sorted(xpaths, key=lambda d: d['count_notices'], reverse=True)[0]
minimal_set_of_xpaths.append(top_xpath["xpath"])
notice_ids = top_xpath["notice_ids"]
for notice_id in notice_ids:
if notice_id in unique_notice_ids:
unique_notice_ids.remove(notice_id)
covered_notice_ids.append(notice_id)


def test_mongo_db_query_2():
uri = config.MONGO_DB_AUTH_URL
mongodb_client = MongoClient(uri)
mongodb_client.drop_database('test')
test_db = mongodb_client['test']
objects_collection = test_db['objects']
for i in range(0, 3):
objects_collection.insert_one(random_object())

unique_xpaths = objects_collection.distinct("xpath")

unique_notice_ids = objects_collection.distinct("notices")
result = objects_collection.aggregate([
{
"$group": {"_id": None,
"xpaths": {"$push": "$xpath"}
}
},
{
"$project": {
"_id": 0,
"xpaths": {
"$setUnion": {
"$reduce": {
"input": '$xpaths',
"initialValue": [],
"in": {"$concatArrays": ['$$value', '$$this']}
}
}
}
}
}
])


def test_create_matview_for_notices(fake_mongodb_client):
def test_create_materialised_view_for_notices(mongodb_client):
notice_id = "696661-2022"
ted_api_query = {"q": f"ND=[{notice_id}]"}
mongodb_client = fake_mongodb_client
notice_repository = NoticeRepository(mongodb_client=mongodb_client)
NoticeFetcher(notice_repository=notice_repository,
ted_api_adapter=TedAPIAdapter(
Expand Down Expand Up @@ -191,15 +65,17 @@ def test_create_matview_for_notices(fake_mongodb_client):
assert 'status' in fields_in_the_kpi_collection


def test_create_matview_for_batches():
uri = config.MONGO_DB_AUTH_URL
mongodb_client = MongoClient(uri)
def test_create_materialised_view_for_batches(mongodb_client):
create_batch_collection_materialised_view(mongo_client=mongodb_client)
db = mongodb_client[config.MONGO_DB_AGGREGATES_DATABASE_NAME]
assert NOTICE_PROCESS_BATCH_COLLECTION_NAME in db.list_collection_names()
document = db[NOTICE_PROCESS_BATCH_COLLECTION_NAME].find_one()
if document is not None:
fields_in_the_materialised_view = document.keys()
assert 'exec_time' in fields_in_the_materialised_view
assert 'nr_of_pipelines' in fields_in_the_materialised_view
assert 'batch_nr_of_notices' in fields_in_the_materialised_view
#TODO: rewrite this test
# Current implementation is dependent on the data in the database,
# dependence is from another tests that provide this data.
# Now mongodb_client is mocked, so there is no data in the database.
# db = mongodb_client[config.MONGO_DB_AGGREGATES_DATABASE_NAME]
# assert NOTICE_PROCESS_BATCH_COLLECTION_NAME in db.list_collection_names()
# document = db[NOTICE_PROCESS_BATCH_COLLECTION_NAME].find_one()
# if document is not None:
# fields_in_the_materialised_view = document.keys()
# assert 'exec_time' in fields_in_the_materialised_view
# assert 'nr_of_pipelines' in fields_in_the_materialised_view
# assert 'batch_nr_of_notices' in fields_in_the_materialised_view
14 changes: 0 additions & 14 deletions tests/e2e/data_sampler/conftest.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
import pytest
from pymongo import MongoClient

from ted_sws import config
from ted_sws.data_manager.adapters.notice_repository import NoticeRepository
from ted_sws.data_sampler.services.notice_xml_indexer import index_notice
from ted_sws.mapping_suite_processor.services.conceptual_mapping_processor import \
mapping_suite_processor_from_github_expand_and_load_package_in_mongo_db
from ted_sws.notice_metadata_processor.services.metadata_normalizer import normalise_notice


@pytest.fixture
def mongodb_client():
mongodb_client = MongoClient(config.MONGO_DB_AUTH_URL)
protected_databases = ['admin', 'config', 'local']
existing_databases = mongodb_client.list_database_names()
databases_to_delete = list(set(existing_databases) - set(protected_databases))
for database in databases_to_delete:
mongodb_client.drop_database(database)
return mongodb_client


@pytest.fixture
def notice_repository_with_indexed_notices(mongodb_client) -> NoticeRepository:

mapping_suite_processor_from_github_expand_and_load_package_in_mongo_db(
mapping_suite_package_name="package_F03_test",
mongodb_client=mongodb_client,
Expand Down
2 changes: 1 addition & 1 deletion tests/features/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@pytest.fixture
def mongodb_end_point():
return config.MONGO_DB_AUTH_URL
return "fake_mongo_db_end_point"


@pytest.fixture(scope="function")
Expand Down

0 comments on commit 8c86cd0

Please sign in to comment.