From bb59891310e8463a791f2f061f409cfe4e1379cc Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Fri, 18 Sep 2020 17:57:31 +0200 Subject: [PATCH] Cleanup in index and options and improved options test coverage --- nw/core/index.py | 3 ++- nw/core/options.py | 3 --- tests/test_project.py | 61 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/nw/core/index.py b/nw/core/index.py index d50533a1..29126bed 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -27,6 +27,7 @@ import logging import json +import nw from os import path from time import time @@ -66,9 +67,9 @@ class NWIndex(): def __init__(self, theProject, theParent): # Internal + self.mainConf = nw.CONFIG self.theProject = theProject self.theParent = theParent - self.mainConf = self.theParent.mainConf self.indexBroken = False # Indices diff --git a/nw/core/options.py b/nw/core/options.py index 9a58bf7c..314a75c6 100644 --- a/nw/core/options.py +++ b/nw/core/options.py @@ -28,7 +28,6 @@ import logging import json -import nw from os import path @@ -40,9 +39,7 @@ class OptionState(): def __init__(self, theProject): - self.mainConf = nw.CONFIG self.theProject = theProject - self.theState = {} self.validMap = { "GuiWritingStats": { diff --git a/tests/test_project.py b/tests/test_project.py index 784e9f8d..54d44d6a 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -12,7 +12,7 @@ from nw.core.project import NWProject from nw.core.document import NWDoc from nw.core.index import NWIndex from nw.core.spellcheck import NWSpellEnchant, NWSpellSimple -from nw.constants import nwItemClass, nwItemLayout +from nw.constants import nwItemClass, nwItemLayout, nwFiles @pytest.mark.project def testProjectNewOpenSave(nwFuncTemp, nwTempProj, nwRef, nwTemp, nwDummy): @@ -372,3 +372,62 @@ def testSpellSimple(nwTemp, nwConf): dList = spChk.listDictionaries() assert len(dList) > 0 + +@pytest.mark.project +def testProjectOptions(nwDummy, nwLipsum): + theProject = NWProject(nwDummy) + assert theProject.projMeta is None + + theOpts = theProject.optState + assert not theOpts.loadSettings() + assert not theOpts.saveSettings() + + # No Settings + assert theProject.openProject(nwLipsum) + assert theOpts.loadSettings() + assert theOpts.saveSettings() + assert str(theOpts.theState) == r"{}" + + # Read Invalid Settings and Filter + stateFile = path.join(theProject.projMeta, nwFiles.OPTS_FILE) + with open(stateFile, mode="w", encoding="utf8") as outFile: + outFile.write( + r'{"GuiProjectSettings": {"winWidth": 100, "winHeight": 50}, "NoGroup": {"NoName": 0}}' + ) + assert theOpts.loadSettings() + assert str(theOpts.theState) == r"{'GuiProjectSettings': {'winWidth': 100, 'winHeight': 50}}" + + # Set New Settings + assert not theOpts.setValue("NoGroup", "NoName", None) + assert not theOpts.setValue("GuiProjectSettings", "NoName", None) + assert theOpts.setValue("GuiProjectSettings", "winWidth", 200) + assert theOpts.setValue("GuiProjectSettings", "winHeight", 80) + assert str(theOpts.theState) == r"{'GuiProjectSettings': {'winWidth': 200, 'winHeight': 80}}" + + # Check Read/Write Types + + ## String + assert theOpts.setValue("GuiWritingStats", "winWidth", "123") + assert isinstance(theOpts.getString("GuiWritingStats", "winWidth", "456"), str) + assert theOpts.getString("GuiWritingStats", "NoName", "456") == "456" + + ## Int + assert theOpts.setValue("GuiWritingStats", "winWidth", "123") + assert isinstance(theOpts.getInt("GuiWritingStats", "winWidth", 456), int) + assert theOpts.getInt("GuiWritingStats", "NoName", 456) == 456 + assert theOpts.setValue("GuiWritingStats", "winWidth", "True") + assert theOpts.getInt("GuiWritingStats", "NoName", 456) == 456 + + ## Float + assert theOpts.setValue("GuiWritingStats", "winWidth", "123") + assert isinstance(theOpts.getFloat("GuiWritingStats", "winWidth", 456.0), float) + assert theOpts.getFloat("GuiWritingStats", "NoName", 456.0) == 456.0 + assert theOpts.setValue("GuiWritingStats", "winWidth", "True") + assert theOpts.getFloat("GuiWritingStats", "winWidth", 456.0) == 456.0 + + ## Bool + assert theOpts.setValue("GuiWritingStats", "winWidth", True) + assert isinstance(theOpts.getBool("GuiWritingStats", "winWidth", False), bool) + assert theOpts.getFloat("GuiWritingStats", "NoName", False) is False + assert theOpts.setValue("GuiWritingStats", "winWidth", "True") + assert theOpts.getFloat("GuiWritingStats", "winWidth", False) is False