We're now getting to 88% coverage on python. I don't think I can get it much higher between the statements that pytest doesn't count, the ones which aren't easy to reproduce in test env (eg no internet connection), and the ones essential to the app's startup workflow.
This commit is contained in:
parent
5bdf81b2ed
commit
84a65cd1fc
6 changed files with 201 additions and 42 deletions
|
@ -17,18 +17,166 @@
|
|||
"""
|
||||
|
||||
import pytest
|
||||
from os import getcwd
|
||||
from os import getcwd, remove
|
||||
from os.path import join
|
||||
from tempfile import TemporaryDirectory
|
||||
from LogarithmPlotter.util import config
|
||||
from json import loads
|
||||
from shutil import copy2
|
||||
|
||||
from PySide6.QtCore import QObject, Signal, QThreadPool
|
||||
from PySide6.QtGui import QImage
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from LogarithmPlotter import __VERSION__ as version
|
||||
from LogarithmPlotter.util import config, helper
|
||||
from LogarithmPlotter.util.helper import ChangelogFetcher, Helper, InvalidFileException
|
||||
|
||||
pwd = getcwd()
|
||||
|
||||
helper.SHOW_GUI_MESSAGES = False
|
||||
|
||||
@pytest.fixture()
|
||||
def temporary():
|
||||
directory = TemporaryDirectory()
|
||||
config.CONFIG_PATH = join(directory.name, "config.json")
|
||||
tmpfile = join(directory.name, "graph.png")
|
||||
yield tmpfile
|
||||
yield tmpfile, directory
|
||||
directory.cleanup()
|
||||
|
||||
|
||||
class MockHelperSignals(QObject):
|
||||
changelogFetched = Signal(str)
|
||||
|
||||
def __init__(self, expect_404):
|
||||
QObject.__init__(self)
|
||||
self.expect_404 = expect_404
|
||||
self.changelogFetched.connect(self.changelog_fetched)
|
||||
self.changelog = None
|
||||
|
||||
def changelog_fetched(self, changelog):
|
||||
self.changelog = changelog
|
||||
|
||||
|
||||
class TestChangelog:
|
||||
|
||||
def test_exists(self, qtbot):
|
||||
helper.CHANGELOG_VERSION = '0.5.0'
|
||||
mock_helper = MockHelperSignals(False)
|
||||
fetcher = ChangelogFetcher(mock_helper)
|
||||
fetcher.run() # Does not raise an exception
|
||||
qtbot.waitSignal(mock_helper.changelogFetched, timeout=10000)
|
||||
assert type(mock_helper.changelog) == str
|
||||
assert '404' not in mock_helper.changelog
|
||||
|
||||
def tests_no_exist(self, qtbot):
|
||||
mock_helper = MockHelperSignals(True)
|
||||
helper.CHANGELOG_VERSION = '1.0.0'
|
||||
fetcher = ChangelogFetcher(mock_helper)
|
||||
fetcher.run()
|
||||
qtbot.waitSignal(mock_helper.changelogFetched, timeout=10000)
|
||||
assert type(mock_helper.changelog) == str
|
||||
assert '404' in mock_helper.changelog
|
||||
|
||||
|
||||
class TestHelper:
|
||||
def test_read(self, temporary):
|
||||
# Test file reading and information loading.
|
||||
tmpfile, directory = temporary
|
||||
obj = Helper(pwd, tmpfile)
|
||||
data = obj.load("ci/test1.lpf")
|
||||
assert type(data) == str
|
||||
data = loads(data)
|
||||
assert data['type'] == "logplotv1"
|
||||
# Checking data['types'] of valid file.
|
||||
# See https://git.ad5001.eu/Ad5001/LogarithmPlotter/wiki/LogarithmPlotter-file-format-v1.0
|
||||
assert type(data['width']) == int
|
||||
assert type(data['height']) == int
|
||||
assert type(data['xzoom']) in (int, float)
|
||||
assert type(data['yzoom']) in (int, float)
|
||||
assert type(data['xmin']) in (int, float)
|
||||
assert type(data['ymax']) in (int, float)
|
||||
assert type(data['xaxisstep']) == str
|
||||
assert type(data['yaxisstep']) == str
|
||||
assert type(data['xaxislabel']) == str
|
||||
assert type(data['yaxislabel']) == str
|
||||
assert type(data['logscalex']) == bool
|
||||
assert type(data['linewidth']) in (int, float)
|
||||
assert type(data['showxgrad']) == bool
|
||||
assert type(data['showygrad']) == bool
|
||||
assert type(data['textsize']) in (int, float)
|
||||
assert type(data['history']) == list and len(data['history']) == 2
|
||||
assert type(data['history'][0]) == list
|
||||
assert type(data['history'][1]) == list
|
||||
for action_list in data['history']:
|
||||
for action in action_list:
|
||||
assert type(action[0]) == str
|
||||
assert type(action[1]) == list
|
||||
assert type(data['objects']) == dict
|
||||
for obj_type, objects in data['objects'].items():
|
||||
assert type(obj_type) == str
|
||||
assert type(objects) == list
|
||||
for obj in objects:
|
||||
assert type(obj) == list
|
||||
|
||||
def test_read_newer(self, temporary):
|
||||
tmpfile, directory = temporary
|
||||
obj = Helper(pwd, tmpfile)
|
||||
newer_file_path = join(directory.name, "newer.lpf")
|
||||
with open(newer_file_path, "w") as f:
|
||||
f.write("LPFv2[other invalid data]")
|
||||
with pytest.raises(InvalidFileException):
|
||||
obj.load(newer_file_path)
|
||||
|
||||
def test_read_invalid_file(self, temporary):
|
||||
tmpfile, directory = temporary
|
||||
obj = Helper(pwd, tmpfile)
|
||||
with pytest.raises(InvalidFileException):
|
||||
obj.load("./inexistant.lpf")
|
||||
with pytest.raises(InvalidFileException):
|
||||
obj.load("./pyproject.toml")
|
||||
|
||||
def test_write(self, temporary):
|
||||
tmpfile, directory = temporary
|
||||
obj = Helper(pwd, tmpfile)
|
||||
target = join(directory.name, "target.lpf")
|
||||
data = "example_data"
|
||||
obj.write(target, data)
|
||||
with open(target, "r") as f:
|
||||
read_data = f.read()
|
||||
# Ensure data has been written.
|
||||
assert read_data == "LPFv1" + data
|
||||
|
||||
def test_tmp_graphic(self, temporary):
|
||||
tmpfile, directory = temporary
|
||||
obj = Helper(pwd, tmpfile)
|
||||
assert obj.gettmpfile() == tmpfile
|
||||
obj.copyImageToClipboard()
|
||||
clipboard = QApplication.clipboard()
|
||||
assert type(clipboard.image()) == QImage
|
||||
|
||||
def test_strings(self, temporary):
|
||||
tmpfile, directory = temporary
|
||||
obj = Helper(pwd, tmpfile)
|
||||
assert obj.getVersion() == version
|
||||
assert type(obj.getDebugInfos()) == str
|
||||
assert type(obj.getSetting("check_for_updates")) == str
|
||||
assert type(obj.getSettingInt("check_for_updates")) == float
|
||||
assert type(obj.getSettingBool("check_for_updates")) == bool
|
||||
|
||||
def test_set_config(self, temporary):
|
||||
tmpfile, directory = temporary
|
||||
obj = Helper(pwd, tmpfile)
|
||||
obj.setSetting("last_install_greet", obj.getSetting("last_install_greet"))
|
||||
obj.setSettingBool("check_for_updates", obj.getSettingBool("check_for_updates"))
|
||||
obj.setSettingInt("default_graph.xzoom", obj.getSettingInt("default_graph.xzoom"))
|
||||
|
||||
def test_fetch_changelog(self, temporary, qtbot):
|
||||
tmpfile, directory = temporary
|
||||
obj = Helper(pwd, tmpfile)
|
||||
copy2("../../CHANGELOG.md", "../../LogarithmPlotter/util/CHANGELOG.md")
|
||||
obj.fetchChangelog()
|
||||
assert QThreadPool.globalInstance().activeThreadCount() == 0
|
||||
qtbot.waitSignal(obj.changelogFetched, timeout=10000)
|
||||
remove("../../LogarithmPlotter/util/CHANGELOG.md")
|
||||
obj.fetchChangelog()
|
||||
assert QThreadPool.globalInstance().activeThreadCount() > 0
|
||||
qtbot.waitSignal(obj.changelogFetched, timeout=10000)
|
|
@ -36,7 +36,7 @@ class MockIO:
|
|||
|
||||
class MockFileOpenEvent(QEvent):
|
||||
def __init__(self, file):
|
||||
super(QEvent.FileOpen)
|
||||
QEvent.__init__(self, QEvent.FileOpen)
|
||||
self._file = file
|
||||
|
||||
def file(self):
|
||||
|
|
|
@ -33,7 +33,7 @@ def check_update_callback_type(show_alert, msg_text, update_available):
|
|||
assert type(msg_text) == str
|
||||
assert type(update_available) == bool
|
||||
|
||||
def test_update():
|
||||
def test_update(qtbot):
|
||||
def check_older(show_alert, msg_text, update_available):
|
||||
check_update_callback_type(show_alert, msg_text, update_available)
|
||||
assert update_available
|
||||
|
@ -50,14 +50,18 @@ def test_update():
|
|||
update_info_newer.got_update_info.connect(check_newer)
|
||||
runnable = UpdateCheckerRunnable('1.0.0', update_info_newer)
|
||||
runnable.run()
|
||||
qtbot.waitSignal(update_info_newer.got_update_info, timeout=10000)
|
||||
runnable = UpdateCheckerRunnable('0.1.0', update_info_older)
|
||||
runnable.run()
|
||||
qtbot.waitSignal(update_info_older.got_update_info, timeout=10000)
|
||||
runnable = UpdateCheckerRunnable('0.5.0+dev0+git20240101', update_info_older)
|
||||
runnable.run()
|
||||
qtbot.waitSignal(update_info_older.got_update_info, timeout=10000)
|
||||
|
||||
def test_update_checker():
|
||||
check_for_updates('0.6.0', MockWindow())
|
||||
def test_update_checker(qtbot):
|
||||
update_info = check_for_updates('0.6.0', MockWindow())
|
||||
assert QThreadPool.globalInstance().activeThreadCount() == 1
|
||||
qtbot.waitSignal(update_info.got_update_info, timeout=10000)
|
||||
argv.append("--no-check-for-updates")
|
||||
check_for_updates('0.6.0', MockWindow())
|
||||
update_info = check_for_updates('0.6.0', MockWindow())
|
||||
assert QThreadPool.globalInstance().activeThreadCount() < 2 # No new update checks where added
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue