|
1
|
|
|
import sqlite3 |
|
|
|
|
|
|
2
|
|
|
from pathlib import Path |
|
3
|
|
|
import logging |
|
4
|
|
|
import os |
|
5
|
|
|
import sys |
|
6
|
|
|
from LocalDB.query import check_value, update_entry, create_entry |
|
|
|
|
|
|
7
|
|
|
from LocalDB.create_CNBP import create_localDB_CNBP |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
11
|
|
|
|
|
12
|
|
|
def test_CreateSubject(): |
|
|
|
|
|
|
13
|
|
|
logger = logging.getLogger('UT_CreateSubject') |
|
14
|
|
|
PathString = "TestCNBPQuery.sqlite" |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
# if SQL already exist, quit script. |
|
17
|
|
|
SQLPath = Path(PathString) |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
# check if path is a file and exist. |
|
20
|
|
|
if SQLPath.is_file(): |
|
21
|
|
|
logger.info('Test SQLite database file already exist. Gonna mess with it!') |
|
22
|
|
|
''''Delete current database! During testing only''' |
|
|
|
|
|
|
23
|
|
|
os.remove(PathString) |
|
24
|
|
|
|
|
25
|
|
|
# Create the database |
|
26
|
|
|
assert create_localDB_CNBP(PathString) |
|
27
|
|
|
logger.info('Test SQLite database successfully created. Gonna mess with it!') |
|
28
|
|
|
|
|
29
|
|
|
tableName = 'id_table' # All CNBP database should have this table name. |
|
|
|
|
|
|
30
|
|
|
MRNColumn = "MRN" |
|
|
|
|
|
|
31
|
|
|
CNBPIDColumn = "CNBPID" |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
create_entry(PathString, tableName, MRNColumn, 291033) |
|
34
|
|
|
logger.info('Test SQLite database successfully inserted with mock records. Gonna check!') |
|
35
|
|
|
|
|
36
|
|
|
# Populate the table with some fake records. |
|
37
|
|
|
ConnectedDatabase = sqlite3.connect(PathString) |
|
|
|
|
|
|
38
|
|
|
c = ConnectedDatabase.cursor() |
|
|
|
|
|
|
39
|
|
|
c.execute( |
|
40
|
|
|
'SELECT * FROM {tablename} WHERE {columnname}="{columnvalue}"'. |
|
41
|
|
|
format(tablename=tableName, columnname=MRNColumn,columnvalue=291033)) |
|
|
|
|
|
|
42
|
|
|
ResultRows = c.fetchall() |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
assert len(ResultRows) > 0 |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
# Closing the connection to the database file |
|
47
|
|
|
ConnectedDatabase.close() |
|
48
|
|
|
|
|
49
|
|
|
# Remove test data base created |
|
50
|
|
|
os.remove(PathString) |
|
51
|
|
|
|
|
52
|
|
|
return True |
|
53
|
|
|
|
|
54
|
|
|
def test_CheckSubjectExist(): |
|
|
|
|
|
|
55
|
|
|
logger = logging.getLogger('UT_CheckSubjectExist') |
|
56
|
|
|
PathString = "TestCNBPQuery.sqlite" |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
# if SQL already exist, quit script. |
|
59
|
|
|
SQLPath = Path(PathString) |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
# check if path is a file and exist. |
|
62
|
|
|
if SQLPath.is_file(): |
|
63
|
|
|
logger.info('Test SQLite database file already exist. Gonna mess with it!') |
|
64
|
|
|
''''Delete current database! During testing only''' |
|
|
|
|
|
|
65
|
|
|
os.remove(PathString) |
|
66
|
|
|
|
|
67
|
|
|
# Create the database |
|
68
|
|
|
assert create_localDB_CNBP(PathString) |
|
69
|
|
|
logger.info('Test SQLite database successfully created. Gonna mess with it!') |
|
70
|
|
|
|
|
71
|
|
|
tableName = 'id_table' # All CNBP database should have this table name. |
|
|
|
|
|
|
72
|
|
|
MRNColumn = "MRN" |
|
|
|
|
|
|
73
|
|
|
CNBPIDColumn = "CNBPID" |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
# Populate the table with some fake records. |
|
76
|
|
|
ConnectedDatabase = sqlite3.connect(PathString) |
|
|
|
|
|
|
77
|
|
|
c = ConnectedDatabase.cursor() |
|
|
|
|
|
|
78
|
|
|
c.execute("INSERT INTO {tn} ({mrn},{cnbpid}) VALUES (291010,'CNBP0010001')". |
|
79
|
|
|
format(tn=tableName, mrn=MRNColumn, cnbpid=CNBPIDColumn)) |
|
80
|
|
|
c.execute("INSERT INTO {tn} ({mrn},{cnbpid}) VALUES (292010,'CNBP0020001')". |
|
81
|
|
|
format(tn=tableName, mrn=MRNColumn, cnbpid=CNBPIDColumn)) |
|
82
|
|
|
c.execute("INSERT INTO {tn} ({mrn},{cnbpid}) VALUES (295010,'CNBP0010001')". |
|
83
|
|
|
format(tn=tableName, mrn=MRNColumn, cnbpid=CNBPIDColumn)) |
|
84
|
|
|
c.execute("INSERT INTO {tn} ({mrn},{cnbpid}) VALUES (297120,'CNBP0030001')". |
|
85
|
|
|
format(tn=tableName, mrn=MRNColumn, cnbpid=CNBPIDColumn)) |
|
86
|
|
|
c.execute("INSERT INTO {tn} ({mrn},{cnbpid}) VALUES (291310,'CNBP0510001')". |
|
87
|
|
|
format(tn=tableName, mrn=MRNColumn, cnbpid=CNBPIDColumn)) |
|
88
|
|
|
ConnectedDatabase.commit() |
|
89
|
|
|
ConnectedDatabase.close() |
|
90
|
|
|
|
|
91
|
|
|
logger.info('Test SQLite database successfully inserted with mock records. Gonna mess with it!') |
|
92
|
|
|
|
|
93
|
|
|
# Create on Connecting to the database file |
|
94
|
|
|
assert(check_value(PathString, tableName, "MRN", 291010)) |
|
|
|
|
|
|
95
|
|
|
assert(check_value(PathString, tableName, "CNBPID", "CNBP0010001")) |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
# Remove test data base created |
|
98
|
|
|
os.remove(PathString) |
|
99
|
|
|
|
|
100
|
|
|
return True |
|
101
|
|
|
|
|
102
|
|
|
def test_CreateSubjectCheckExist(): |
|
|
|
|
|
|
103
|
|
|
logger = logging.getLogger('UT_CreateAndCheck') |
|
104
|
|
|
PathString = "TestCNBPQuery.sqlite" |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
# if SQL already exist, quit script. |
|
107
|
|
|
SQLPath = Path(PathString) |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
# check if path is a file and exist. |
|
110
|
|
|
if SQLPath.is_file(): |
|
111
|
|
|
logger.info('Test SQLite database file already exist. Gonna mess with it!') |
|
112
|
|
|
''''Delete current database! During testing only''' |
|
|
|
|
|
|
113
|
|
|
os.remove(PathString) |
|
114
|
|
|
|
|
115
|
|
|
# Create the database |
|
116
|
|
|
assert create_localDB_CNBP(PathString) |
|
117
|
|
|
logger.info('Test SQLite database successfully created. Gonna mess with it!') |
|
118
|
|
|
|
|
119
|
|
|
tableName = 'id_table' # All CNBP database should have this table name. |
|
|
|
|
|
|
120
|
|
|
MRNColumn = "MRN" |
|
|
|
|
|
|
121
|
|
|
CNBPIDColumn = "CNBPID" |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
create_entry(PathString, tableName, MRNColumn, 2918210) |
|
124
|
|
|
create_entry(PathString, tableName, MRNColumn, 23452346) |
|
125
|
|
|
create_entry(PathString, tableName, MRNColumn, 2345234) |
|
126
|
|
|
create_entry(PathString, tableName, MRNColumn, 273411) |
|
127
|
|
|
create_entry(PathString, tableName, MRNColumn, 364573) |
|
128
|
|
|
create_entry(PathString, tableName, MRNColumn, 7424141) |
|
129
|
|
|
|
|
130
|
|
|
success, _ = check_value(PathString, tableName, MRNColumn, 7129112) |
|
131
|
|
|
assert not success |
|
132
|
|
|
|
|
133
|
|
|
success, _ = check_value(PathString, tableName, MRNColumn, 2918210) |
|
134
|
|
|
assert success |
|
135
|
|
|
|
|
136
|
|
|
success, _ = check_value(PathString, tableName, MRNColumn, 712921) |
|
137
|
|
|
assert not success |
|
138
|
|
|
|
|
139
|
|
|
success, _ = check_value(PathString, tableName, MRNColumn, 742) |
|
140
|
|
|
assert not success |
|
141
|
|
|
|
|
142
|
|
|
success, _ = check_value(PathString, tableName, MRNColumn, 364573) |
|
143
|
|
|
assert success |
|
144
|
|
|
|
|
145
|
|
|
logger.info('Tested SQLIte database entry. ') |
|
146
|
|
|
|
|
147
|
|
|
# Remove test data base created |
|
148
|
|
|
os.remove(PathString) |
|
149
|
|
|
|
|
150
|
|
|
return True |
|
151
|
|
|
|
|
152
|
|
|
def test_SubjectUpdate(): |
|
|
|
|
|
|
153
|
|
|
logger = logging.getLogger('UT_CreateAndCheck') |
|
154
|
|
|
PathString = "TestCNBPQuery.sqlite" |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
# if SQL already exist, quit script. |
|
157
|
|
|
SQLPath = Path(PathString) |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
# check if path is a file and exist. |
|
160
|
|
|
if SQLPath.is_file(): |
|
161
|
|
|
logger.info('Test SQLite database file already exist. Gonna mess with it!') |
|
162
|
|
|
''''Delete current database! During testing only''' |
|
|
|
|
|
|
163
|
|
|
os.remove(PathString) |
|
164
|
|
|
|
|
165
|
|
|
# Create the database |
|
166
|
|
|
assert create_localDB_CNBP(PathString) |
|
167
|
|
|
logger.info('Test SQLite database successfully created. Gonna mess with it!') |
|
168
|
|
|
|
|
169
|
|
|
tableName = 'id_table' # All CNBP database should have this table name. |
|
|
|
|
|
|
170
|
|
|
MRNColumn = "MRN" |
|
|
|
|
|
|
171
|
|
|
CNBPIDColumn = "CNBPID" |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
create_entry(PathString, tableName, MRNColumn, 2918210) |
|
174
|
|
|
create_entry(PathString, tableName, MRNColumn, 23452346) |
|
175
|
|
|
create_entry(PathString, tableName, MRNColumn, 2345234) |
|
176
|
|
|
create_entry(PathString, tableName, MRNColumn, 273411) |
|
177
|
|
|
create_entry(PathString, tableName, MRNColumn, 364573) |
|
178
|
|
|
create_entry(PathString, tableName, MRNColumn, 7424141) |
|
179
|
|
|
|
|
180
|
|
|
update_entry(PathString, tableName, MRNColumn, 7424141, CNBPIDColumn, "CNBPID0010001") |
|
181
|
|
|
update_entry(PathString, tableName, MRNColumn, 2345234, CNBPIDColumn, "CNBPID0010002") |
|
182
|
|
|
update_entry(PathString, tableName, MRNColumn, 2918210, CNBPIDColumn, "CNBPID0010003") |
|
183
|
|
|
update_entry(PathString, tableName, MRNColumn, 273411, CNBPIDColumn, "CNBPID0010004") |
|
184
|
|
|
|
|
185
|
|
|
success, _ = check_value(PathString, tableName, CNBPIDColumn, 'CNBPID0010006') |
|
186
|
|
|
assert not success |
|
187
|
|
|
|
|
188
|
|
|
success, _ = check_value(PathString, tableName, CNBPIDColumn, 'CNBPID0010001') |
|
189
|
|
|
assert success |
|
190
|
|
|
|
|
191
|
|
|
success, _ = check_value(PathString, tableName, CNBPIDColumn, 55555) |
|
192
|
|
|
assert not success |
|
193
|
|
|
|
|
194
|
|
|
success, _ = check_value(PathString, tableName, CNBPIDColumn, 742) |
|
195
|
|
|
assert not success |
|
196
|
|
|
|
|
197
|
|
|
success, _ = check_value(PathString, tableName, CNBPIDColumn, 'CNBPID0010003') |
|
198
|
|
|
assert success |
|
199
|
|
|
|
|
200
|
|
|
logger.info('Tested SQLIte database entry. ') |
|
201
|
|
|
|
|
202
|
|
|
# Remove test data base created |
|
203
|
|
|
os.remove(PathString) |
|
204
|
|
|
|
|
205
|
|
|
return True |
|
206
|
|
|
|
|
207
|
|
|
if __name__ == '__main__': |
|
208
|
|
|
test_SubjectUpdate() |
|
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.