From f2d809d9d9e4cdb88d287e1aac3cd2b2bb71e186 Mon Sep 17 00:00:00 2001 From: Dan Funk Date: Tue, 15 Sep 2020 11:57:56 -0400 Subject: [PATCH] cleaning up tests --- tests/__init__.py | 0 tests/base_test.py | 18 ++++++++++++++++-- ...ervice_test.py => test_firebase_service.py} | 0 ...ivy_service_test.py => test_ivy_service.py} | 8 ++++++-- ..._service_test.py => test_sample_service.py} | 10 +++++----- 5 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 tests/__init__.py rename tests/services/{firebase_service_test.py => test_firebase_service.py} (100%) rename tests/services/{ivy_service_test.py => test_ivy_service.py} (65%) rename tests/services/{sample_service_test.py => test_sample_service.py} (90%) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/base_test.py b/tests/base_test.py index d30f9ee..29e1726 100644 --- a/tests/base_test.py +++ b/tests/base_test.py @@ -4,8 +4,11 @@ import os import unittest os.environ["TESTING"] = "true" +from communicator.models import Sample + + from communicator import app, db -# UNCOMMENT THIS FOR DEBUGGING SQL ALCHEMY QUERIES + import logging logging.basicConfig() @@ -15,6 +18,10 @@ class BaseTest(unittest.TestCase): efficiently when we have a database in place. """ + firebase_file = os.path.join(app.instance_path, '..', 'tests', 'data', 'firebase_data.json') + ivy_file = os.path.join(app.instance_path, '..', 'tests', 'data', 'results.csv') + + if not app.config['TESTING']: raise (Exception("INVALID TEST CONFIGURATION. This is almost always in import order issue." "The first class to import in each test should be the base_test.py file.")) @@ -32,4 +39,11 @@ class BaseTest(unittest.TestCase): def tearDownClass(cls): cls.ctx.pop() db.drop_all() - pass \ No newline at end of file + pass + + def setUp(self): + pass + + def tearDown(self): + db.session.query(Sample).delete() + db.session.commit() diff --git a/tests/services/firebase_service_test.py b/tests/services/test_firebase_service.py similarity index 100% rename from tests/services/firebase_service_test.py rename to tests/services/test_firebase_service.py diff --git a/tests/services/ivy_service_test.py b/tests/services/test_ivy_service.py similarity index 65% rename from tests/services/ivy_service_test.py rename to tests/services/test_ivy_service.py index 614d25b..eac2e48 100644 --- a/tests/services/ivy_service_test.py +++ b/tests/services/test_ivy_service.py @@ -1,4 +1,7 @@ +import os import unittest + +from communicator import app from tests.base_test import BaseTest from communicator.errors import CommError @@ -8,12 +11,13 @@ from communicator.services.ivy_service import IvyService class IvyServiceTest(BaseTest): def test_read_file_and_build_records(self): - records = IvyService.samples_from_ivy_file('../data/results.csv') + records = IvyService.samples_from_ivy_file(self.ivy_file) self.assertEquals("987654321", records[0].student_id) self.assertEquals("testpositive@virginia.edu", records[1].email) self.assertEquals("1142270225", records[2].result_code) def test_invalid_file(self): with self.assertRaises(CommError): - IvyService.samples_from_ivy_file('../data/incorrect.csv') + ivy_incorrect_file = os.path.join(app.instance_path, '..', 'tests', 'data', 'incorrect.csv') + IvyService.samples_from_ivy_file(ivy_incorrect_file) diff --git a/tests/services/sample_service_test.py b/tests/services/test_sample_service.py similarity index 90% rename from tests/services/sample_service_test.py rename to tests/services/test_sample_service.py index 9a60f59..9c1e2ff 100644 --- a/tests/services/sample_service_test.py +++ b/tests/services/test_sample_service.py @@ -1,10 +1,11 @@ import json +import os import unittest from dateutil import parser from tests.base_test import BaseTest -from communicator import db +from communicator import db, app from communicator.models.sample import Sample from communicator.services.ivy_service import IvyService from communicator.services.sample_service import SampleService @@ -14,8 +15,7 @@ class IvyServiceTest(BaseTest): def get_firebase_records(self): - file_name = '../data/firebase_data.json' - with open(file_name, 'r') as fb_file: + with open(self.firebase_file, 'r') as fb_file: raw_data = json.load(fb_file) samples = [] for d in raw_data: @@ -40,7 +40,7 @@ class IvyServiceTest(BaseTest): self.assertEquals(4, len(db.session.query(Sample).all())) - ivy_samples = IvyService.samples_from_ivy_file('../data/results.csv') + ivy_samples = IvyService.samples_from_ivy_file(self.ivy_file) service.add_or_update_records(ivy_samples) # There are 6 records in ivy, but three records that should match up, giving seven total @@ -56,7 +56,7 @@ class IvyServiceTest(BaseTest): self.assertEquals(0, len(db.session.query(Sample).all())) - ivy_samples = IvyService.samples_from_ivy_file('../data/results.csv') + ivy_samples = IvyService.samples_from_ivy_file(self.ivy_file) service.add_or_update_records(ivy_samples) self.assertEquals(6, len(db.session.query(Sample).all()))