|
| 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 |
0 commit comments