Skip to content

Commit 5479eaf

Browse files
committed
Add unit tests
1 parent 0239784 commit 5479eaf

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
# result of psycopg2.connect(**connection_stuff)
31+
mock_con = (
32+
mock_connect.return_value
33+
)
34+
# result of con.cursor(cursor_factory=DictCursor)
35+
mock_cur = (
36+
mock_con.cursor.return_value
37+
)
38+
# return this when calling cur.fetchall()
39+
mock_cur.fetchall.return_value = (
40+
metadata_attribs_in_db
41+
)
42+
43+
conn = psycopg2.connect(
44+
dbname="test",
45+
user="test",
46+
host="localhost",
47+
port="32780",
48+
password="test",
49+
)
50+
cur = conn.cursor()
51+
52+
metadata = []
53+
processed_lines = pu.process_global_attributes(
54+
TestIngest.SAMPLE_METADATA_LINES,
55+
cur,
56+
TestIngest.fake_submission_id,
57+
metadata,
58+
TestIngest.fake_submission_filename,
59+
)
60+
assert len(TestIngest.SAMPLE_METADATA_LINES), processed_lines + 1
61+
assert len(metadata_attribs_in_db), len(metadata)
62+
assert metadata[0][2], "159903_2012_117464"
63+
assert metadata[1][2], "SPOT"
64+
65+
@mock.patch("psycopg2.connect")
66+
def test_processing_file_metadata_without_attributes(self, mock_connect):
67+
metadata_attribs_in_db = []
68+
# result of psycopg2.connect(**connection_stuff)
69+
mock_con = (
70+
mock_connect.return_value
71+
)
72+
# result of con.cursor(cursor_factory=DictCursor)
73+
mock_cur = (
74+
mock_con.cursor.return_value
75+
)
76+
# return this when calling cur.fetchall()
77+
mock_cur.fetchall.return_value = (
78+
metadata_attribs_in_db
79+
)
80+
81+
conn = psycopg2.connect(
82+
dbname="test",
83+
user="test",
84+
host="localhost",
85+
port="32780",
86+
password="test",
87+
)
88+
cur = conn.cursor()
89+
90+
metadata = []
91+
processed_lines = pu.process_global_attributes(
92+
TestIngest.SAMPLE_METADATA_LINES,
93+
cur,
94+
TestIngest.fake_submission_id,
95+
metadata,
96+
TestIngest.fake_submission_filename,
97+
)
98+
assert len(TestIngest.SAMPLE_METADATA_LINES), processed_lines + 1
99+
100+
101+
if __name__ == "__main__":
102+
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)