-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
143 lines (105 loc) · 5.54 KB
/
tests.py
File metadata and controls
143 lines (105 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import unittest, sys, os, pyodbc, datetime, sqlalchemy, xml
from sqlalchemy.orm import sessionmaker
import Reports, EmergeXMLParser
SQLite_test_DB = 'test_files/temp.db'
db_connection = 'sqlite:///' + SQLite_test_DB
class TestCreateTables(unittest.TestCase):
def setUp(self):
self.xml_file = 'test_files/sample_report.xml'
# delete the database
if os.path.exists(SQLite_test_DB): os.remove(SQLite_test_DB)
# create the parser for testing
self.parser = EmergeXMLParser.XMLParser(db_connection)
# create our own engine for test queries
self.engine = sqlalchemy.create_engine(db_connection)
Session = sessionmaker(bind=self.engine)
self.session = Session()
# drop any existing tables before creating new, empty tables
def test_drop_before_create(self):
# drop the tables, then create them
self.parser.create_tables(True)
# queries to the tables should produce no rows since the tables were dropped before created
rows = self.session.query(Reports.Report).count()
self.assertEqual(rows , 0)
rows = self.session.query(Reports.InterpretedDiseases).count()
self.assertEqual(rows , 0)
# try to create tables even if they already exist
def test_create_without_drop(self):
# Drop and Create new tables
self.parser.create_tables(True)
# Add some rows to the tables
self.parser.parse(self.xml_file)
# Create the tables again, but without dropping them first
self.parser.create_tables(False)
# since the table already exist, and they weren't dropped, the rows should still be there
rows = self.session.query(Reports.Report).count()
self.assertEqual(rows , 1)
rows = self.session.query(Reports.InterpretedDiseases).count()
self.assertEqual(rows , 2)
# Test if parsing works even if the tables didn't exist
class TestNoTables(unittest.TestCase):
def setUp(self):
self.xml_file = 'test_files/sample_report.xml'
# delete the database
if os.path.exists(SQLite_test_DB): os.remove(SQLite_test_DB)
# create the parser for testing
self.parser = EmergeXMLParser.XMLParser(db_connection)
# create our own engine for test queries
self.engine = sqlalchemy.create_engine(db_connection)
Session = sessionmaker(bind=self.engine)
self.session = Session()
def test_insert_before_tables_created(self):
# try to parse xml before tables have been created
# the tables should automatically be created if they didn't exist
self.parser.parse(self.xml_file)
self.assertTrue(Reports.Report.__table__.exists(bind=self.engine))
def test_insert_if_a_table_is_missing(self):
# drop and create new tables
self.parser.create_tables(True)
# drop one of the tables
Reports.InterpretedDiseases.__table__.drop(bind=self.engine)
self.assertFalse(Reports.InterpretedDiseases.__table__.exists(bind=self.engine))
# try to parse xml even though a table has been dropped
self.parser.parse(self.xml_file)
# check to see if the table has been re-created
self.assertTrue(Reports.InterpretedDiseases.__table__.exists(bind=self.engine))
#class TestMissingValuesInXMLFile(unittest.TestCase):
# this is already done by the next test since multiple_children has many missing values
# Test to see if each child creates a row in the corresponding table
class TestMultipleChildren(unittest.TestCase):
def setUp(self):
self.xml_file = 'test_files/multiple_children.xml'
# delete the database
if os.path.exists(SQLite_test_DB): os.remove(SQLite_test_DB)
# create the parser for testing
self.parser = EmergeXMLParser.XMLParser(db_connection)
# create our own engine for test queries
self.engine = sqlalchemy.create_engine(db_connection)
Session = sessionmaker(bind=self.engine)
self.session = Session()
def test_the_tables_can_have_multiple_children(self):
self.parser.parse(self.xml_file)
self.assertEqual(self.session.query(Reports.Report).count(), 1)
self.assertEqual(self.session.query(Reports.InterpretedDiseases).count(), 2)
self.assertEqual(self.session.query(Reports.PatientDiseases).count(), 3)
self.assertEqual(self.session.query(Reports.ReportAssays).count(), 2)
self.assertEqual(self.session.query(Reports.Physicians).count(), 2)
self.assertEqual(self.session.query(Reports.ReportVariants).count(), 2)
self.assertEqual(self.session.query(Reports.NestedVariants).count(), 0)
self.assertEqual(self.session.query(Reports.Specimens).count(), 2)
# I ended up adding try/except in parse_xml_file.py to test for bad xml
# this is probably a dumb test since I'm really just testing the xml module
# but, hey, it helped me figure out xml parse errors
class TestMalformedXML(unittest.TestCase):
def setUp(self):
self.xml_file = 'test_files/malformed.xml'
# delete the database
if os.path.exists(SQLite_test_DB): os.remove(SQLite_test_DB)
# create the parser for testing
self.parser = EmergeXMLParser.XMLParser(db_connection)
def test_malformed(self):
with self.assertRaises(xml.etree.ElementTree.ParseError):
self.parser.parse(self.xml_file)
if __name__ == '__main__':
unittest.main()
# with self.assertRaises(TypeError):