|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import psycopg2 |
| 7 | +import tagbase_server.utils.processing_utils as pu |
| 8 | + |
| 9 | + |
| 10 | +class TestIngest(unittest.TestCase): |
| 11 | + PG_VERSION = "postgres:9.5" |
| 12 | + SAMPLE_METADATA_LINES = [ |
| 13 | + "// global attributes:", |
| 14 | + "// etag device attributes:", |
| 15 | + ":instrument_name = \"159903_2012_117464\"", |
| 16 | + ":instrument_type = \"s\"", |
| 17 | + ":manufacturer = \"Wildlife\"", |
| 18 | + ":model = \"SPOT\"", |
| 19 | + ":owner_contact = \"a@a.org\"", |
| 20 | + ":person_owner = \"foo bar\"", |
| 21 | + ":ptt = \"117464\"", |
| 22 | + ] |
| 23 | + |
| 24 | + fake_submission_id = 1 |
| 25 | + fake_submission_filename = "test_file" |
| 26 | + |
| 27 | + @mock.patch("psycopg2.connect") |
| 28 | + def test_processing_file_metadata_with_existing_attributes(self, mock_connect): |
| 29 | + metadata_attribs_in_db = [[1, 'instrument_name'], [2, 'model']] |
| 30 | + mock_con = ( |
| 31 | + mock_connect.return_value |
| 32 | + ) # result of psycopg2.connect(**connection_stuff) |
| 33 | + mock_cur = ( |
| 34 | + mock_con.cursor.return_value |
| 35 | + ) # result of con.cursor(cursor_factory=DictCursor) |
| 36 | + mock_cur.fetchall.return_value = ( |
| 37 | + metadata_attribs_in_db |
| 38 | + ) # return this when calling cur.fetchall() |
| 39 | + |
| 40 | + conn = psycopg2.connect( |
| 41 | + dbname="test", |
| 42 | + user="test", |
| 43 | + host="localhost", |
| 44 | + port="32780", |
| 45 | + password="test", |
| 46 | + ) |
| 47 | + cur = conn.cursor() |
| 48 | + |
| 49 | + metadata = [] |
| 50 | + processed_lines = pu.process_global_attributes( |
| 51 | + TestIngest.SAMPLE_METADATA_LINES, |
| 52 | + cur, |
| 53 | + TestIngest.fake_submission_id, |
| 54 | + metadata, |
| 55 | + TestIngest.fake_submission_filename, |
| 56 | + ) |
| 57 | + assert len(TestIngest.SAMPLE_METADATA_LINES), processed_lines + 1 |
| 58 | + assert len(metadata_attribs_in_db), len(metadata) |
| 59 | + assert metadata[0][2], "159903_2012_117464" |
| 60 | + assert metadata[1][2], "SPOT" |
| 61 | + |
| 62 | + @mock.patch("psycopg2.connect") |
| 63 | + def test_processing_file_metadata_without_attributes(self, mock_connect): |
| 64 | + metadata_attribs_in_db = [] |
| 65 | + mock_con = ( |
| 66 | + mock_connect.return_value |
| 67 | + ) # result of psycopg2.connect(**connection_stuff) |
| 68 | + mock_cur = ( |
| 69 | + mock_con.cursor.return_value |
| 70 | + ) # result of con.cursor(cursor_factory=DictCursor) |
| 71 | + mock_cur.fetchall.return_value = ( |
| 72 | + metadata_attribs_in_db |
| 73 | + ) # return this when calling cur.fetchall() |
| 74 | + |
| 75 | + conn = psycopg2.connect( |
| 76 | + dbname="test", |
| 77 | + user="test", |
| 78 | + host="localhost", |
| 79 | + port="32780", |
| 80 | + password="test", |
| 81 | + ) |
| 82 | + cur = conn.cursor() |
| 83 | + |
| 84 | + metadata = [] |
| 85 | + processed_lines = pu.process_global_attributes( |
| 86 | + TestIngest.SAMPLE_METADATA_LINES, |
| 87 | + cur, |
| 88 | + TestIngest.fake_submission_id, |
| 89 | + metadata, |
| 90 | + TestIngest.fake_submission_filename, |
| 91 | + ) |
| 92 | + assert len(TestIngest.SAMPLE_METADATA_LINES), processed_lines + 1 |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + unittest.main() |
0 commit comments