From 8603c598155b146239060a3d8293f85d0305b117 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Sep 2020 22:28:52 +0200 Subject: [PATCH 1/4] Added more tests for common functions --- codecov.yml | 2 -- nw/common.py | 4 ++-- tests/test_common.py | 57 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/codecov.yml b/codecov.yml index 805f5187..0a7e08aa 100644 --- a/codecov.yml +++ b/codecov.yml @@ -22,5 +22,3 @@ parsers: loop: yes method: no macro: no - -# comment: false diff --git a/nw/common.py b/nw/common.py index 71aa4873..2d82290b 100644 --- a/nw/common.py +++ b/nw/common.py @@ -224,9 +224,9 @@ def fuzzyTime(secDiff): return "%d weeks ago" % int(round(secDiff/604800)) elif secDiff < 3888000: # 45 days return "a month ago" - elif secDiff < 31104000: # 360 days + elif secDiff < 29808000: # 345 days return "%d months ago" % int(round(secDiff/2592000)) - elif secDiff < 47304000: # 1.5 years + elif secDiff < 47336400: # 1.5 years return "a year ago" else: return "%d years ago" % int(round(secDiff/31557600)) diff --git a/tests/test_common.py b/tests/test_common.py index f2b895e2..94e384db 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -3,7 +3,10 @@ """ import pytest -from nw.common import checkString, checkBool, checkInt, colRange +from nw.common import ( + checkString, checkBool, checkInt, colRange, formatInt, transferCase, + fuzzyTime +) from nwtools import cmpList @pytest.mark.core @@ -62,3 +65,55 @@ def testColRange(): colRange([200, 50, 0], [50, 200, 0], 5), [[200, 50, 0], [162, 87, 0], [124, 124, 0], [86, 161, 0], [50, 200, 0]] ) + +@pytest.mark.core +def testFormatInt(): + assert formatInt(1000) == "1000" + assert formatInt(1234) == "1.23k" + assert formatInt(12345) == "12.3k" + assert formatInt(123456) == "123k" + assert formatInt(1234567) == "1.23M" + assert formatInt(12345678) == "12.3M" + assert formatInt(123456789) == "123M" + assert formatInt(1234567890) == "1.23G" + +@pytest.mark.core +def testTransferCase(): + assert transferCase(1, "TaRgEt") == "TaRgEt" + assert transferCase("source", 1) == 1 + assert transferCase("", "TaRgEt") == "TaRgEt" + assert transferCase("source", "") == "" + assert transferCase("Source", "target") == "Target" + assert transferCase("SOURCE", "target") == "TARGET" + assert transferCase("source", "TARGET") == "target" + +@pytest.mark.core +def testFuzzyTime(): + assert fuzzyTime(-1) == "in the future" + assert fuzzyTime(0) == "just now" + assert fuzzyTime(29) == "just now" + assert fuzzyTime(30) == "a minute ago" + assert fuzzyTime(89) == "a minute ago" + assert fuzzyTime(90) == "2 minutes ago" + assert fuzzyTime(149) == "2 minutes ago" + assert fuzzyTime(151) == "3 minutes ago" + assert fuzzyTime(3299) == "55 minutes ago" + assert fuzzyTime(3300) == "an hour ago" + assert fuzzyTime(5399) == "an hour ago" + assert fuzzyTime(5400) == "2 hours ago" + assert fuzzyTime(84599) == "23 hours ago" + assert fuzzyTime(84600) == "a day ago" + assert fuzzyTime(129599) == "a day ago" + assert fuzzyTime(129600) == "2 days ago" + assert fuzzyTime(561599) == "6 days ago" + assert fuzzyTime(561600) == "a week ago" + assert fuzzyTime(907199) == "a week ago" + assert fuzzyTime(907200) == "2 weeks ago" + assert fuzzyTime(2419199) == "4 weeks ago" + assert fuzzyTime(2419200) == "a month ago" + assert fuzzyTime(3887999) == "a month ago" + assert fuzzyTime(3888000) == "2 months ago" + assert fuzzyTime(29807999) == "11 months ago" + assert fuzzyTime(29808000) == "a year ago" + assert fuzzyTime(47336399) == "a year ago" + assert fuzzyTime(47336400) == "2 years ago" From 61b9567e3a8a5d53e0241f39aea4bf463151565c Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Sep 2020 22:40:45 +0200 Subject: [PATCH 2/4] Added test for document meta --- nw/core/document.py | 4 ++-- tests/test_project.py | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/nw/core/document.py b/nw/core/document.py index 9aec16f6..6c859168 100644 --- a/nw/core/document.py +++ b/nw/core/document.py @@ -230,13 +230,13 @@ class NWDoc(): for aClass in nwItemClass: if theMeta.startswith(aClass.name): theClass = aClass - theMeta = theMeta.lstrip(aClass.name+":") + theMeta = theMeta[len(aClass.name)+1:] theLayout = nwItemLayout.NO_LAYOUT for aLayout in nwItemLayout: if theMeta.startswith(aLayout.name): theLayout = aLayout - theMeta = theMeta.lstrip(aLayout.name+":") + theMeta = theMeta[len(aLayout.name)+1:] return theMeta, thePath, theClass, theLayout diff --git a/tests/test_project.py b/tests/test_project.py index 4e90a8c4..2354fa08 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -9,8 +9,9 @@ from shutil import copyfile from nwtools import cmpFiles from nw.core.project import NWProject +from nw.core.document import NWDoc from nw.core.index import NWIndex -from nw.constants import nwItemClass +from nw.constants import nwItemClass, nwItemLayout @pytest.mark.project def testProjectNewOpenSave(nwFuncTemp, nwTempProj, nwRef, nwTemp, nwDummy): @@ -295,3 +296,21 @@ def testProjectNewSample(nwFuncTemp, nwRef, nwConf, nwDummy): assert theProject.projName == "Sample Project" assert theProject.saveProject() assert theProject.closeProject() + +@pytest.mark.project +def testDocMeta(nwDummy, nwLipsum): + theProject = NWProject(nwDummy) + theProject.projTree.setSeed(42) + assert theProject.openProject(nwLipsum) + + aDoc = NWDoc(theProject, nwDummy) + assert aDoc.openDocument("47666c91c7ccf") + theMeta, thePath, theClass, theLayout = aDoc.getMeta() + + assert theMeta == "Scene Five" + assert len(thePath) == 3 + assert thePath[0] == "47666c91c7ccf" + assert thePath[1] == "6bd935d2490cd" + assert thePath[2] == "b3643d0f92e32" + assert theClass == nwItemClass.NOVEL + assert theLayout == nwItemLayout.SCENE From b79e63c9d3545191bd4ddfa804b25c074435c991 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Sep 2020 22:43:43 +0200 Subject: [PATCH 3/4] Added a last check on doc meta --- tests/test_project.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_project.py b/tests/test_project.py index 2354fa08..834af196 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -314,3 +314,10 @@ def testDocMeta(nwDummy, nwLipsum): assert thePath[2] == "b3643d0f92e32" assert theClass == nwItemClass.NOVEL assert theLayout == nwItemLayout.SCENE + + aDoc.docMeta = "too_short" + theMeta, thePath, theClass, theLayout = aDoc.getMeta() + assert theMeta == "" + assert thePath == [] + assert theClass is None + assert theLayout is None From 00c4404f9deb7d99f2a6a1b5fda4f154ef56a5df Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Sep 2020 23:02:12 +0200 Subject: [PATCH 4/4] Added spell check test --- tests/test_project.py | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/test_project.py b/tests/test_project.py index 834af196..db24da92 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -11,6 +11,7 @@ from nwtools import cmpFiles 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 @pytest.mark.project @@ -321,3 +322,53 @@ def testDocMeta(nwDummy, nwLipsum): assert thePath == [] assert theClass is None assert theLayout is None + +@pytest.mark.project +def testSpellEnchant(nwTemp, nwConf): + wList = path.join(nwTemp, "wordlist.txt") + with open(wList, mode="w") as wFile: + wFile.write("a_word\nb_word\nc_word\n") + + spChk = NWSpellEnchant() + spChk.mainConf = nwConf + spChk.setLanguage("en", wList) + + assert spChk.checkWord("a_word") + assert spChk.checkWord("b_word") + assert spChk.checkWord("c_word") + assert not spChk.checkWord("d_word") + + spChk.addWord("d_word") + assert spChk.checkWord("d_word") + + wSuggest = spChk.suggestWords("wrod") + assert len(wSuggest) > 0 + assert "word" in wSuggest + + dList = spChk.listDictionaries() + assert len(dList) > 0 + +@pytest.mark.project +def testSpellSimple(nwTemp, nwConf): + wList = path.join(nwTemp, "wordlist.txt") + with open(wList, mode="w") as wFile: + wFile.write("a_word\nb_word\nc_word\n") + + spChk = NWSpellSimple() + spChk.mainConf = nwConf + spChk.setLanguage("en", wList) + + assert spChk.checkWord("a_word") + assert spChk.checkWord("b_word") + assert spChk.checkWord("c_word") + assert not spChk.checkWord("d_word") + + spChk.addWord("d_word") + assert spChk.checkWord("d_word") + + wSuggest = spChk.suggestWords("wrod") + assert len(wSuggest) > 0 + assert "word" in wSuggest + + dList = spChk.listDictionaries() + assert len(dList) > 0