Skip to content

Commit 2f06368

Browse files
committed
Add unit tests
1 parent 0239784 commit 2f06368

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .processing_utils import *

tagbase_server/tagbase_server/utils/processing_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import logging
32
from datetime import datetime as dt
43
from io import StringIO

tagbase_server/test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pytest~=7.2.1
22
pytest-cov>=2.8.1
33
pytest-randomly==3.12.0
44
Flask-Testing==0.8.1
5+
mock==5.0.1

0 commit comments

Comments
 (0)