test_LocalDBQuery.test_SubjectUpdate()   B
last analyzed

Complexity

Conditions 8

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 54
rs 7.1733
c 0
b 0
f 0
cc 8
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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