More unit testing.

pull/61/head
Christian T. Jacobs 2017-06-24 20:56:04 +01:00
rodzic 459322689a
commit a09d5ab1ce
3 zmienionych plików z 63 dodań i 36 usunięć

Wyświetl plik

@ -89,11 +89,10 @@ class Logbook:
opened = self.open(path=path) opened = self.open(path=path)
return opened return opened
def open(self, widget=None, path=None, render=True): def open(self, widget=None, path=None):
""" Open a logbook, and render all the logs within it. """ Open a logbook, and render all the logs within it.
:arg str path: An optional argument containing the database file location, if already known. If this is None, a file selection dialog will appear. :arg str path: An optional argument containing the database file location, if already known. If this is None, a file selection dialog will appear.
:arg bool render: An optional argument to specify whether or not the logs should be rendered in the logbook. By default this is True, but is sometimes set to False for unit testing purposes.
:returns: True if the logbook is successfully opened, and False otherwise. :returns: True if the logbook is successfully opened, and False otherwise.
:rtype: bool :rtype: bool
""" """
@ -129,35 +128,34 @@ class Logbook:
else: else:
logging.debug("All logs retrieved successfully.") logging.debug("All logs retrieved successfully.")
if(render): logging.debug("Rendering logs...")
logging.debug("Rendering logs...") # For rendering the logs. One treeview and one treeselection per Log.
# For rendering the logs. One treeview and one treeselection per Log. self.treeview = []
self.treeview = [] self.treeselection = []
self.treeselection = [] self.sorter = []
self.sorter = [] self.filter = []
self.filter = [] self.summary = Summary(self.application)
self.summary = Summary(self.application) self.blank = Blank(self.application)
self.blank = Blank(self.application)
# FIXME: This is an unfortunate work-around. If the area around the "+/New Log" button # FIXME: This is an unfortunate work-around. If the area around the "+/New Log" button
# is clicked, PyQSO will change to an empty page. This signal is used to stop this from happening. # is clicked, PyQSO will change to an empty page. This signal is used to stop this from happening.
self.notebook.connect("switch-page", self.on_switch_page) self.notebook.connect("switch-page", self.on_switch_page)
for i in range(len(self.logs)): for i in range(len(self.logs)):
self.render_log(i) self.render_log(i)
logging.debug("All logs rendered successfully.") logging.debug("All logs rendered successfully.")
self.summary.update() self.summary.update()
self.application.toolbox.awards.count(self) self.application.toolbox.awards.count(self)
context_id = self.application.statusbar.get_context_id("Status") context_id = self.application.statusbar.get_context_id("Status")
self.application.statusbar.push(context_id, "Logbook: %s" % self.path) self.application.statusbar.push(context_id, "Logbook: %s" % self.path)
self.application.toolbar.set_logbook_button_sensitive(False) self.application.toolbar.set_logbook_button_sensitive(False)
self.application.menu.set_logbook_item_sensitive(False) self.application.menu.set_logbook_item_sensitive(False)
self.application.menu.set_log_items_sensitive(True) self.application.menu.set_log_items_sensitive(True)
self.application.toolbar.filter_source.set_sensitive(True) self.application.toolbar.filter_source.set_sensitive(True)
self.notebook.show_all() self.notebook.show_all()
else: else:
logging.debug("Not connected to a logbook. No logs were opened.") logging.debug("Not connected to a logbook. No logs were opened.")
@ -377,7 +375,7 @@ class Logbook:
self.sorter.append(Gtk.TreeModelSort(model=self.filter[index])) self.sorter.append(Gtk.TreeModelSort(model=self.filter[index]))
self.sorter[index].set_sort_column_id(0, Gtk.SortType.ASCENDING) self.sorter[index].set_sort_column_id(0, Gtk.SortType.ASCENDING)
self.treeview.append(Gtk.TreeView(self.sorter[index])) self.treeview.append(Gtk.TreeView(model=self.sorter[index]))
self.treeview[index].set_grid_lines(Gtk.TreeViewGridLines.BOTH) self.treeview[index].set_grid_lines(Gtk.TreeViewGridLines.BOTH)
self.treeview[index].connect("row-activated", self.edit_record_callback) self.treeview[index].connect("row-activated", self.edit_record_callback)
self.treeselection.append(self.treeview[index].get_selection()) self.treeselection.append(self.treeview[index].get_selection())
@ -392,8 +390,8 @@ class Logbook:
vbox.pack_start(sw, True, True, 0) vbox.pack_start(sw, True, True, 0)
# Add a close button to the tab # Add a close button to the tab
hbox = Gtk.HBox(False, 0) hbox = Gtk.HBox(homogeneous=False, spacing=0)
label = Gtk.Label(self.logs[index].name) label = Gtk.Label(label=self.logs[index].name)
hbox.pack_start(label, False, False, 0) hbox.pack_start(label, False, False, 0)
hbox.show_all() hbox.show_all()
@ -516,8 +514,8 @@ class Logbook:
page.set_name(new_log_name) page.set_name(new_log_name)
# ... and update the tab's label. # ... and update the tab's label.
hbox = Gtk.HBox(False, 0) hbox = Gtk.HBox(homogeneous=False, spacing=0)
label = Gtk.Label(new_log_name) label = Gtk.Label(label=new_log_name)
hbox.pack_start(label, False, False, 0) hbox.pack_start(label, False, False, 0)
hbox.show_all() hbox.show_all()
self.notebook.set_tab_label(page, hbox) self.notebook.set_tab_label(page, hbox)

Wyświetl plik

@ -70,7 +70,7 @@ class Summary(object):
if(have_config and config.has_option(section, option)): if(have_config and config.has_option(section, option)):
if(config.get("general", "show_yearly_statistics") == "True" and have_matplotlib): if(config.get("general", "show_yearly_statistics") == "True" and have_matplotlib):
hbox = Gtk.HBox() hbox = Gtk.HBox()
label = Gtk.Label("Display statistics for year: ", halign=Gtk.Align.START) label = Gtk.Label(label="Display statistics for year: ", halign=Gtk.Align.START)
hbox.pack_start(label, False, False, 6) hbox.pack_start(label, False, False, 6)
year_select = Gtk.ComboBoxText() year_select = Gtk.ComboBoxText()
min_year, max_year = self.get_year_bounds() min_year, max_year = self.get_year_bounds()

Wyświetl plik

@ -25,19 +25,19 @@ except ImportError:
import os import os
from pyqso.logbook import * from pyqso.logbook import *
class TestLogbook(unittest.TestCase): class TestLogbook(unittest.TestCase):
""" The unit tests for the Logbook class. """ """ The unit tests for the Logbook class. """
def setUp(self): @mock.patch('pyqso.logbook.Logbook.filter_by_callsign')
def setUp(self, mock_filter_by_callsign):
""" Set up the Logbook object and connection to the test database needed for the unit tests. """ """ Set up the Logbook object and connection to the test database needed for the unit tests. """
self.logbook = Logbook(application=mock.MagicMock()) self.logbook = Logbook(application=mock.MagicMock())
# Open the test database file. # Open the test database file.
path_to_test_database = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir, "res/test.db") path_to_test_database = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir, "res/test.db")
opened = self.logbook.open(path=path_to_test_database, render=False) opened = self.logbook.open(path=path_to_test_database)
assert(opened) assert(opened)
assert(self.logbook.connection is not None) assert(self.logbook.connection is not None)
@ -52,11 +52,25 @@ class TestLogbook(unittest.TestCase):
closed = self.logbook.close() closed = self.logbook.close()
assert(closed) assert(closed)
def test_db_disconnect(self):
""" Check that the logbook can disconnect from the database. """
disconnected = self.logbook.db_disconnect()
assert(disconnected)
# Attempt to disconnect again. This shouldn't do anything.
disconnected = self.logbook.db_disconnect()
assert(disconnected)
def test_new(self):
""" """
#path_to_new_database = "Logbook.test_new.db"
#opened = self.logbook.open(path=path_to_invalid_database)
pass
@mock.patch('pyqso.auxiliary_dialogs.handle_gtk_dialog') @mock.patch('pyqso.auxiliary_dialogs.handle_gtk_dialog')
def test_open_invalid_logbook(self, mock_handle_gtk_dialog): def test_open_invalid_logbook(self, mock_handle_gtk_dialog):
""" Open an invalid database file (comprising only one line of plain text) and check that an error occurs. """ """ Open an invalid database file (comprising only one line of plain text) and check that an error occurs. """
path_to_invalid_database = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir, "res/invalid.db") path_to_invalid_database = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir, "res/invalid.db")
opened = self.logbook.open(path=path_to_invalid_database, render=False) opened = self.logbook.open(path=path_to_invalid_database)
assert(not opened) assert(not opened)
assert(self.logbook.logs is None) assert(self.logbook.logs is None)
@ -103,5 +117,20 @@ class TestLogbook(unittest.TestCase):
assert(self.logbook.get_log_index(name="test2") == 1) assert(self.logbook.get_log_index(name="test2") == 1)
assert(self.logbook.get_log_index(name="helloworld") is None) assert(self.logbook.get_log_index(name="helloworld") is None)
@mock.patch('pyqso.logbook.Logbook.render_log')
@mock.patch('pyqso.auxiliary_dialogs.handle_gtk_dialog')
@mock.patch('pyqso.logbook.LogNameDialog')
def test_new_log(self, mock_LogNameDialog, mock_handle_gtk_dialog, mock_render_log):
""" Start off with an empty logbook and check that a new log can successfully be added. """
f = open("Logbook.test_new_log.db", "w").close()
opened = self.logbook.open(path="Logbook.test_new_log.db")
self.logbook.summary = mock.MagicMock()
assert(opened)
mock_LogNameDialog().dialog.run.return_value = Gtk.ResponseType.OK
mock_LogNameDialog().name = "my_new_log"
self.logbook.new_log()
assert(len(self.logbook.logs) == 1)
assert(self.logbook.logs[0].name == "my_new_log")
if(__name__ == '__main__'): if(__name__ == '__main__'):
unittest.main() unittest.main()