cleaning up tests

This commit is contained in:
Dan Funk 2020-09-15 11:57:56 -04:00
parent b7dc2d56bf
commit f2d809d9d9
5 changed files with 27 additions and 9 deletions

0
tests/__init__.py Normal file
View File

View File

@ -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."))
@ -33,3 +40,10 @@ class BaseTest(unittest.TestCase):
cls.ctx.pop()
db.drop_all()
pass
def setUp(self):
pass
def tearDown(self):
db.session.query(Sample).delete()
db.session.commit()

View File

@ -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)

View File

@ -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()))