xfstestsdb/tests/test_gtk.py

201 lines
9.0 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Tests the `xfstestsdb gtk` command."""
import errno
import io
import unittest
import unittest.mock
import xfstestsdb.gtk
from gi.repository import Gio
from gi.repository import Adw
class TestApplication(unittest.TestCase):
"""Tests the Gtk Application."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.application = xfstestsdb.gtk.Application(self.xfstestsdb.sql)
def test_init(self):
"""Check that the Gtk Application was set up properly."""
self.assertIsInstance(self.application, Adw.Application)
self.assertEqual(self.application.get_application_id(),
xfstestsdb.gtk.gsetup.APPLICATION_ID)
self.assertEqual(self.application.get_flags(),
Gio.ApplicationFlags.HANDLES_COMMAND_LINE)
self.assertEqual(self.application.get_resource_base_path(),
xfstestsdb.gtk.gsetup.RESOURCE_PATH)
self.assertEqual(self.application.runid, 0)
self.assertIsNone(self.application.properties)
self.assertIsNone(self.application.model)
self.assertIsNone(self.application.summary)
@unittest.mock.patch("gi.repository.Adw.Application.activate")
@unittest.mock.patch("gi.repository.Adw.Application.do_command_line")
def test_command_line(self, mock_command_line: unittest.mock.Mock,
mock_activate: unittest.mock.Mock):
"""Check that we handle the command-line signal."""
mock_cmd = unittest.mock.Mock()
mock_cmd.get_arguments.return_value = []
self.application.do_command_line(mock_cmd)
mock_command_line.assert_called_with(self.application, mock_cmd)
mock_cmd.get_arguments.assert_called()
mock_activate.assert_called()
self.assertEqual(self.application.runid, 0)
self.assertFalse(self.application.show_sidebar)
self.assertIsNone(self.application.environment)
self.assertIsNone(self.application.properties)
self.assertIsNone(self.application.model)
self.assertIsNone(self.application.summary)
mock_command_line.reset_mock()
mock_activate.reset_mock()
mock_cmd.reset_mock()
mock_cmd.get_arguments.return_value = ["show-sidebar"]
self.application.do_command_line(mock_cmd)
self.assertTrue(self.application.show_sidebar)
self.application.show_sidebar = False
mock_command_line.reset_mock()
mock_activate.reset_mock()
mock_cmd.reset_mock()
mock_cmd.get_arguments.return_value = ["runid=42"]
self.application.do_command_line(mock_cmd)
mock_command_line.assert_called_with(self.application, mock_cmd)
mock_cmd.get_arguments.assert_called()
mock_activate.assert_called()
self.assertEqual(self.application.runid, 42)
self.assertIsInstance(self.application.properties,
xfstestsdb.gtk.model.PropertyList)
self.assertIsInstance(self.application.model,
xfstestsdb.gtk.model.TestCaseList)
self.assertIsInstance(self.application.summary,
xfstestsdb.gtk.model.SummaryList)
self.assertEqual(self.application.environment,
self.application.properties.environment)
self.assertEqual(self.application.model.runid, 42)
self.assertFalse(self.application.show_sidebar)
@unittest.mock.patch("xfstestsdb.gtk.gsetup.add_style")
@unittest.mock.patch("gi.repository.Adw.Application.add_window")
@unittest.mock.patch("gi.repository.Adw.Application.do_startup")
def test_startup(self, mock_startup: unittest.mock.Mock,
mock_add_window: unittest.mock.Mock,
mock_add_style: unittest.mock.Mock):
"""Check that startup sets up our application instance correctly."""
self.assertIsNone(self.application.win)
self.assertIsNone(self.application.sidebar)
self.assertIsNone(self.application.view)
self.application.emit("startup")
self.assertIsInstance(self.application.sidebar,
xfstestsdb.gtk.sidebar.Sidebar)
self.assertIsInstance(self.application.view,
xfstestsdb.gtk.view.XfstestsView)
self.assertIsInstance(self.application.win,
xfstestsdb.gtk.window.Window)
self.assertEqual(self.application.win.child, self.application.view)
self.assertEqual(self.application.win.sidebar,
self.application.sidebar)
self.assertEqual(self.application.sidebar.sql,
self.application.sql)
mock_startup.assert_called_with(self.application)
mock_add_window.assert_called_with(self.application.win)
mock_add_style.assert_called()
self.application.sidebar.runid = 42
self.assertEqual(self.application.runid, 42)
self.assertEqual(self.application.win.runid, 42)
self.application.show_sidebar = True
self.assertTrue(self.application.win.show_sidebar)
properties = xfstestsdb.gtk.model.PropertyList(self.xfstestsdb.sql, 42)
self.application.properties = properties
self.assertEqual(self.application.view.properties, properties)
self.application.environment = properties.environment
self.assertEqual(self.application.view.environment,
properties.environment)
model = xfstestsdb.gtk.model.TestCaseList(self.xfstestsdb.sql, 42)
self.application.model = model
self.assertEqual(self.application.view.model, model)
summary = xfstestsdb.gtk.model.SummaryList(self.xfstestsdb.sql, 42)
self.application.summary = summary
self.assertEqual(self.application.view.summary, summary)
@unittest.mock.patch("gi.repository.Adw.Application.add_window")
@unittest.mock.patch("gi.repository.Adw.Application.do_startup")
@unittest.mock.patch("gi.repository.Adw.Application.do_activate")
def test_activate(self, mock_activate: unittest.mock.Mock,
mock_startup: unittest.mock.Mock,
mock_add_window: unittest.mock.Mock):
"""Check that activating our application works correctly."""
self.application.emit("startup")
with unittest.mock.patch.object(self.application.win,
"present") as mock_present:
self.application.emit("activate")
mock_activate.assert_called_with(self.application)
mock_present.assert_called()
@unittest.mock.patch("gi.repository.Adw.Application.do_shutdown")
def test_shutdown(self, mock_shutdown: unittest.mock.Mock):
"""Check that shutting down application cleans up global state."""
self.application.emit("shutdown")
mock_shutdown.assert_called_with(self.application)
class TestGtk(unittest.TestCase):
"""Tests the `xfstestsdb gtk` command."""
def setUp(self):
"""Set up common variables."""
self.xfstestsdb = xfstestsdb.Command()
self.gtk = self.xfstestsdb.commands["gtk"]
def test_init(self):
"""Check that the gtk command was set up properly."""
self.assertIsInstance(self.gtk, xfstestsdb.commands.Command)
self.assertIsInstance(self.gtk, xfstestsdb.gtk.Command)
self.assertEqual(self.xfstestsdb.subparser.choices["gtk"],
self.gtk.parser)
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
@unittest.mock.patch("xfstestsdb.gtk.Application")
def test_gtk_empty(self, mock_app: unittest.mock.Mock,
mock_stderr: unittest.mock.Mock):
"""Check that running `xfstestsdb gtk` without a runid."""
self.xfstestsdb.run(["gtk"])
mock_app.assert_called_with(self.xfstestsdb.sql)
mock_app.return_value.run.assert_called_with(["show-sidebar"])
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
@unittest.mock.patch("xfstestsdb.gtk.Application")
def test_gtk_runid(self, mock_app: unittest.mock.Mock,
mock_stdout: io.StringIO):
"""Check running `xfstestsdb gtk` with the --runid option."""
self.xfstestsdb.run(["new", "/dev/vda1"])
self.xfstestsdb.run(["gtk", "1"])
mock_app.assert_called_with(self.xfstestsdb.sql)
mock_app.return_value.run.assert_called_with(["runid=1"])
@unittest.mock.patch("sys.stderr", new_callable=io.StringIO)
@unittest.mock.patch("xfstestsdb.gtk.Application")
def test_gtk_error(self, mock_app: unittest.mock.Mock,
mock_stderr: io.StringIO):
"""Check running the gtk command with an invalid runid."""
with self.assertRaises(SystemExit) as sys_exit:
self.xfstestsdb.run(["gtk", "2"])
mock_app.assert_not_called()
self.assertEqual(sys_exit.exception.code, errno.ENOENT)
self.assertEqual(mock_stderr.getvalue(),
"error: run #2 does not exist\n")