Skip to content

Commit f6c5a72

Browse files
committed
Add a cli db test
1 parent 7336f0d commit f6c5a72

File tree

4 files changed

+122
-23
lines changed

4 files changed

+122
-23
lines changed

trolldb/cli.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Main interface."""
2+
3+
from posttroll.message import Message
4+
from posttroll.subscriber import create_subscriber_from_dict_config
5+
6+
from trolldb.database.mongodb import MongoDB, mongodb_context
7+
from trolldb.test_utils.common import test_app_config
8+
9+
10+
async def record_messages(subscriber_config):
11+
"""Record the metadata of messages into the database."""
12+
async with mongodb_context(test_app_config.database):
13+
sub = create_subscriber_from_dict_config(subscriber_config)
14+
collection = await MongoDB.get_collection("mock_database", "mock_collection")
15+
for m in sub.recv():
16+
msg = Message.decode(m)
17+
match msg.type:
18+
case "file":
19+
collection.insert_one(msg.data)
20+
case "delete":
21+
deletion_result = await collection.delete_many({"uri": msg.data["uri"]})
22+
if deletion_result.deleted_count != 1:
23+
raise ValueError("Multiple deletions!") # Replace with logging
24+
case _:
25+
raise KeyError(f"Don't know what to do with {msg.type} message.") # Replace with logging

trolldb/tests/tests_database/conftest.py trolldb/tests/conftest.py

+17
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,32 @@
33
This module provides fixtures for running a Mongo DB instance in test mode and filling the database with test data.
44
"""
55

6+
67
import pytest
78
import pytest_asyncio
89

10+
from trolldb.api.api import server_process_context
911
from trolldb.database.mongodb import mongodb_context
1012
from trolldb.test_utils.common import test_app_config
1113
from trolldb.test_utils.mongodb_database import TestDatabase
1214
from trolldb.test_utils.mongodb_instance import mongodb_instance_server_process_context
1315

1416

17+
@pytest.fixture(scope="session")
18+
def _run_mongodb_server_instance():
19+
"""Documentation to be added!"""
20+
with mongodb_instance_server_process_context():
21+
yield
22+
23+
24+
@pytest.fixture(scope="session")
25+
def _test_server_fixture(_run_mongodb_server_instance):
26+
"""Documentation to be added!"""
27+
TestDatabase.prepare()
28+
with server_process_context(test_app_config, startup_time=2000):
29+
yield
30+
31+
1532
@pytest.fixture(scope="session")
1633
def _run_mongodb_server_instance():
1734
"""Runs the MongoDB instance in test mode using a context manager.

trolldb/tests/test_db.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""Tests for the message recording into database."""
2+
3+
import pytest
4+
from posttroll.message import Message
5+
from posttroll.testing import patched_subscriber_recv
6+
7+
from trolldb.cli import record_messages
8+
from trolldb.database.mongodb import MongoDB, mongodb_context
9+
from trolldb.test_utils.common import test_app_config
10+
from trolldb.test_utils.mongodb_database import TestDatabase
11+
from trolldb.test_utils.mongodb_instance import mongodb_instance_server_process_context
12+
13+
FILENAME = "20191103_153936-s1b-ew-hh.tiff"
14+
15+
@pytest.fixture()
16+
def tmp_filename(tmp_path):
17+
"""Create a filename for the messages."""
18+
return tmp_path / FILENAME
19+
20+
@pytest.fixture()
21+
def file_message(tmp_filename):
22+
"""Create a string for a file message."""
23+
return ('pytroll://segment/raster/L2/SAR file [email protected] 2019-11-05T13:00:10.366023 v1.01 '
24+
'application/json {"platform_name": "S1B", "scan_mode": "EW", "type": "GRDM", "data_source": "1SDH", '
25+
'"start_time": "2019-11-03T15:39:36.543000", "end_time": "2019-11-03T15:40:40.821000", "orbit_number": '
26+
'18765, "random_string1": "0235EA", "random_string2": "747D", "uri": '
27+
f'"{str(tmp_filename)}", "uid": "20191103_153936-s1b-ew-hh.tiff", '
28+
'"polarization": "hh", "sensor": "sar-c", "format": "GeoTIFF", "pass_direction": "ASCENDING"}')
29+
30+
31+
@pytest.fixture()
32+
def del_message(tmp_filename):
33+
"""Create a string for a delete message."""
34+
return ('pytroll://segment/raster/L2/SAR delete [email protected] 2019-11-05T13:00:10.366023 v1.01 '
35+
'application/json {"platform_name": "S1B", "scan_mode": "EW", "type": "GRDM", "data_source": "1SDH", '
36+
'"start_time": "2019-11-03T15:39:36.543000", "end_time": "2019-11-03T15:40:40.821000", "orbit_number": '
37+
'18765, "random_string1": "0235EA", "random_string2": "747D", "uri": '
38+
f'"{str(tmp_filename)}", "uid": "20191103_153936-s1b-ew-hh.tiff", '
39+
'"polarization": "hh", "sensor": "sar-c", "format": "GeoTIFF", "pass_direction": "ASCENDING"}')
40+
41+
42+
async def test_record_adds_message(tmp_path, file_message, tmp_filename):
43+
"""Test that message recording adds a message to the database."""
44+
msg = Message.decode(file_message)
45+
46+
subscriber_config = dict(nameserver=False, addresses=[f"ipc://{str(tmp_path)}/in.ipc"], port=3000)
47+
48+
with mongodb_instance_server_process_context():
49+
TestDatabase.prepare()
50+
with patched_subscriber_recv([file_message]):
51+
52+
await record_messages(subscriber_config)
53+
54+
async with mongodb_context(test_app_config.database):
55+
collection = await MongoDB.get_collection("mock_database", "mock_collection")
56+
57+
result = await collection.find_one(dict(scan_mode="EW"))
58+
result.pop("_id")
59+
assert result == msg.data
60+
61+
deletion_result = await collection.delete_many({"uri": str(tmp_filename)})
62+
63+
assert deletion_result.deleted_count == 1
64+
65+
66+
async def test_record_deletes_message(tmp_path, file_message, del_message):
67+
"""Test that message recording can delete a record in the database."""
68+
subscriber_config = dict(nameserver=False, addresses=[f"ipc://{str(tmp_path)}/in.ipc"], port=3000)
69+
70+
with mongodb_instance_server_process_context():
71+
TestDatabase.prepare()
72+
73+
with patched_subscriber_recv([file_message, del_message]):
74+
75+
await record_messages(subscriber_config)
76+
77+
async with mongodb_context(test_app_config.database):
78+
collection = await MongoDB.get_collection("mock_database", "mock_collection")
79+
result = await collection.find_one(dict(scan_mode="EW"))
80+
assert result is None

trolldb/tests/tests_api/conftest.py

-23
This file was deleted.

0 commit comments

Comments
 (0)