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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
#!/usr/bin/env python3
# Copyright (C) 2017 Christian Thomas Jacobs.
# This file is part of PyQSO.
# PyQSO is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyQSO is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyQSO. If not, see <http://www.gnu.org/licenses/>.
import unittest
from pyqso.log import *
class TestLog(unittest.TestCase):
""" The unit tests for the Log class. """
def setUp(self):
""" Create a connection to a temporary database and set up the objects needed for the unit tests. """
self.connection = sqlite.connect(":memory:")
self.connection.row_factory = sqlite.Row
self.field_names = ["CALL", "QSO_DATE", "TIME_ON", "FREQ", "BAND", "MODE", "RST_SENT", "RST_RCVD"]
self.fields_and_data = {"CALL": "TEST123", "QSO_DATE": "20130312", "TIME_ON": "1234", "FREQ": "145.500", "BAND": "2m", "MODE": "FM", "RST_SENT": "59", "RST_RCVD": "59"}
c = self.connection.cursor()
query = "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT"
for field_name in self.field_names:
s = ", %s TEXT" % field_name.lower()
query = query + s
query = query + ")"
c.execute(query)
self.log = Log(self.connection, "test")
def tearDown(self):
""" Destroy the connection to the temporary database. """
self.connection.close()
def test_add_missing_db_columns(self):
""" Check that any missing columns in the database are added successfully. """
c = self.connection.cursor()
# 'Before' state.
column_names_before = []
c.execute("PRAGMA table_info(test)")
result = c.fetchall()
for t in result:
column_names_before.append(t[1].upper())
# Add missing columns.
self.log.add_missing_db_columns()
# 'After' state.
column_names_after = []
c.execute("PRAGMA table_info(test)")
result = c.fetchall()
for t in result:
column_names_after.append(t[1].upper())
print("Column names before: ", column_names_before)
print("Column names after: ", column_names_after)
assert(len(column_names_before) == len(self.field_names) + 1) # Added 1 here because of the "id" column in all database tables.
assert(len(column_names_after) == len(AVAILABLE_FIELD_NAMES_ORDERED) + 1)
for field_name in AVAILABLE_FIELD_NAMES_ORDERED:
assert(field_name in column_names_after)
def test_add_record(self):
""" Check that a single record can be successfully added. """
self.log.add_record(self.fields_and_data)
c = self.connection.cursor()
c.execute("SELECT * FROM test")
records = c.fetchall()
assert len(records) == 1
# Check that all the data has been added to all the fields.
for field_name in self.field_names:
print(self.fields_and_data[field_name], records[0][field_name])
assert self.fields_and_data[field_name] == records[0][field_name]
# Check consistency of index between Gtk.ListStore and the database.
assert(records[0]["id"] == 1)
iter = self.log.get_iter_first()
row_index = self.log.get_value(iter, 0)
assert(records[0]["id"] == row_index)
def test_add_record_multiple(self):
""" Check that multiple records can be successfully added in one go. """
self.log.add_record([self.fields_and_data]*5)
c = self.connection.cursor()
c.execute("SELECT * FROM test")
records = c.fetchall()
assert len(records) == 5
def test_delete_record(self):
""" Check that a record can be successfully deleted. """
query = "INSERT INTO test VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"
c = self.connection.cursor()
c.execute(query, (self.fields_and_data["CALL"], self.fields_and_data["QSO_DATE"], self.fields_and_data["TIME_ON"], self.fields_and_data["FREQ"], self.fields_and_data["BAND"], self.fields_and_data["MODE"], self.fields_and_data["RST_SENT"], self.fields_and_data["RST_RCVD"]))
c.execute("SELECT * FROM test")
records_before = c.fetchall()
self.log.delete_record(1)
c.execute("SELECT * FROM test")
records_after = c.fetchall()
assert(len(records_before) == 1)
assert(len(records_after) == 0)
def test_edit_record(self):
""" Check that a record's fields can be successfully edited. """
query = "INSERT INTO test VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"
c = self.connection.cursor()
c.execute(query, (self.fields_and_data["CALL"], self.fields_and_data["QSO_DATE"], self.fields_and_data["TIME_ON"], self.fields_and_data["FREQ"], self.fields_and_data["BAND"], self.fields_and_data["MODE"], self.fields_and_data["RST_SENT"], self.fields_and_data["RST_RCVD"]))
c.execute("SELECT * FROM test")
record_before = c.fetchall()[0]
self.log.edit_record(1, "CALL", "TEST456")
self.log.edit_record(1, "FREQ", "145.450")
c.execute("SELECT * FROM test")
record_after = c.fetchall()[0]
assert(record_before["CALL"] == "TEST123")
assert(record_after["CALL"] == "TEST456")
assert(record_before["FREQ"] == "145.500")
assert(record_after["FREQ"] == "145.450")
def test_get_record_by_index(self):
""" Check that a record can be retrieved using its index. """
query = "INSERT INTO test VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"
c = self.connection.cursor()
c.execute(query, (self.fields_and_data["CALL"], self.fields_and_data["QSO_DATE"], self.fields_and_data["TIME_ON"], self.fields_and_data["FREQ"], self.fields_and_data["BAND"], self.fields_and_data["MODE"], self.fields_and_data["RST_SENT"], self.fields_and_data["RST_RCVD"]))
record = self.log.get_record_by_index(1)
print("Contents of retrieved record: ", record)
for field_name in list(record.keys()):
if(field_name.upper() == "ID"):
continue
else:
assert(record[field_name.upper()] == self.fields_and_data[field_name.upper()])
assert(len(record) == len(self.fields_and_data) + 1)
def test_records(self):
""" Check that all records in a log can be successfully retrieved. """
query = "INSERT INTO test VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"
c = self.connection.cursor()
# Add the same record twice
c.execute(query, (self.fields_and_data["CALL"], self.fields_and_data["QSO_DATE"], self.fields_and_data["TIME_ON"], self.fields_and_data["FREQ"], self.fields_and_data["BAND"], self.fields_and_data["MODE"], self.fields_and_data["RST_SENT"], self.fields_and_data["RST_RCVD"]))
c.execute(query, (self.fields_and_data["CALL"], self.fields_and_data["QSO_DATE"], self.fields_and_data["TIME_ON"], self.fields_and_data["FREQ"], self.fields_and_data["BAND"], self.fields_and_data["MODE"], self.fields_and_data["RST_SENT"], self.fields_and_data["RST_RCVD"]))
records = self.log.records
print("Contents of all retrieved records: ", records)
assert(len(records) == 2) # There should be 2 records
for field_name in self.field_names:
assert(records[0][field_name] == self.fields_and_data[field_name])
assert(records[1][field_name] == self.fields_and_data[field_name])
def test_record_count(self):
""" Check that the total number of records in a log is calculated correctly. """
query = "INSERT INTO test VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"
c = self.connection.cursor()
# Add the same record twice
c.execute(query, (self.fields_and_data["CALL"], self.fields_and_data["QSO_DATE"], self.fields_and_data["TIME_ON"], self.fields_and_data["FREQ"], self.fields_and_data["BAND"], self.fields_and_data["MODE"], self.fields_and_data["RST_SENT"], self.fields_and_data["RST_RCVD"]))
c.execute(query, (self.fields_and_data["CALL"], self.fields_and_data["QSO_DATE"], self.fields_and_data["TIME_ON"], self.fields_and_data["FREQ"], self.fields_and_data["BAND"], self.fields_and_data["MODE"], self.fields_and_data["RST_SENT"], self.fields_and_data["RST_RCVD"]))
record_count = self.log.record_count
print("Number of records in the log: ", record_count)
assert(record_count == 2) # There should be 2 records
def test_get_duplicates(self):
""" Insert n records, n-1 of which are duplicates, and check that the duplicates are successfully identified. """
query = "INSERT INTO test VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"
c = self.connection.cursor()
n = 5 # The total number of records to insert.
for i in range(0, n):
c.execute(query, (self.fields_and_data["CALL"], self.fields_and_data["QSO_DATE"], self.fields_and_data["TIME_ON"], self.fields_and_data["FREQ"], self.fields_and_data["BAND"], self.fields_and_data["MODE"], self.fields_and_data["RST_SENT"], self.fields_and_data["RST_RCVD"]))
assert(len(self.log.get_duplicates()) == n-1) # Expecting n-1 duplicates.
def test_remove_duplicates(self):
""" Insert n records, n-1 of which are duplicates, and check that the duplicates are successfully removed. """
n = 5 # The total number of records to insert.
for i in range(0, n):
self.log.add_record(self.fields_and_data)
(number_of_duplicates, number_of_duplicates_removed) = self.log.remove_duplicates()
print("Number of duplicates: %d" % number_of_duplicates)
print("Number of duplicates removed: %d" % number_of_duplicates_removed)
assert(number_of_duplicates == number_of_duplicates_removed)
assert(number_of_duplicates == 4)
assert(self.log.record_count == 1)
def test_rename(self):
""" Check that a log can be successfully renamed. """
old_name = "test"
new_name = "hello"
success = self.log.rename(new_name)
assert(success)
with self.connection:
c = self.connection.cursor()
c.execute("SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name=?)", [old_name])
exists = c.fetchone()
assert(exists[0] == 0) # Old log name should no longer exist.
c.execute("SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name=?)", [new_name])
exists = c.fetchone()
assert(exists[0] == 1) # New log name should now exist.
assert(self.log.name == new_name)
if(__name__ == '__main__'):
unittest.main()
|