Skip to content

Commit

Permalink
Merge pull request #446 from vkbo/test_coverage
Browse files Browse the repository at this point in the history
Improved Test Coverage
  • Loading branch information
vkbo committed Sep 10, 2020
2 parents 68bf363 + 00c4404 commit 890a13a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 8 deletions.
2 changes: 0 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ parsers:
loop: yes
method: no
macro: no

# comment: false
4 changes: 2 additions & 2 deletions nw/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions nw/core/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ def getMeta(self):
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

Expand Down
57 changes: 56 additions & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
79 changes: 78 additions & 1 deletion tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
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.core.spellcheck import NWSpellEnchant, NWSpellSimple
from nw.constants import nwItemClass, nwItemLayout

@pytest.mark.project
def testProjectNewOpenSave(nwFuncTemp, nwTempProj, nwRef, nwTemp, nwDummy):
Expand Down Expand Up @@ -295,3 +297,78 @@ 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

aDoc.docMeta = "too_short"
theMeta, thePath, theClass, theLayout = aDoc.getMeta()
assert theMeta == ""
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

0 comments on commit 890a13a

Please sign in to comment.