Update tests

This commit is contained in:
Veronica Berglyd Olsen
2023-08-25 18:37:01 +02:00
parent fa7e9b7f33
commit 10c7934ae2
28 changed files with 99 additions and 50 deletions
+4 -5
View File
@@ -29,11 +29,11 @@ import logging
from time import time
from typing import TYPE_CHECKING
from pathlib import Path
from novelwriter.core.item import NWItem
from novelwriter.enum import nwItemLayout, nwItemClass
from novelwriter.error import formatException
from novelwriter.common import formatTimeStamp, isHandle
from novelwriter.core.item import NWItem
if TYPE_CHECKING: # pragma: no cover
from novelwriter.core.project import NWProject
@@ -199,9 +199,8 @@ class NWDocument:
updatedDate = currTime
# DocMeta Line
if self._item is None:
docMeta = ""
else:
docMeta = ""
if self._item:
docMeta = (
f"%%~name: {self._item.itemName}\n"
f"%%~path: {self._item.itemParent}/{self._item.itemHandle}\n"
@@ -328,7 +327,7 @@ class NWDocument:
self._docMeta["updated"] = metaBits[1].strip()
else:
logger.debug("Ignoring meta data: '%s'", metaLine.strip())
logger.debug("Unknown meta data: '%s'", metaLine.strip())
return
+1 -2
View File
@@ -226,8 +226,7 @@ def prjLipsum():
@pytest.fixture(scope="session")
def ipsumText():
"""Return five paragraphs of Lorem Ipsum text.
"""
"""Return five paragraphs of Lorem Ipsum text."""
thatIpsum = [(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc maximus justo non dictum co"
"mmodo. Curabitur lacinia tempor orci vel luctus. Phasellus porta metus eu massa luctus, e"
+2
View File
@@ -1,6 +1,8 @@
%%~name: Ancient Europe
%%~path: 60bdf227455cc/04468803b92e1
%%~kind: WORLD/NOTE
%%~hash: b4318c2de40a1fc4055d18f8d02696b9cf171ea2
%%~date: Unknown/Unknown
# Ancient Europe
@tag: Europe
+2
View File
@@ -1,6 +1,8 @@
%%~name: Main
%%~path: 6c6afb1247750/2426c6f0ca922
%%~kind: PLOT/NOTE
%%~hash: db3897d166e246acdb5e25e9bd98c5a40a699ed0
%%~date: Unknown/Unknown
# Main Plot
@tag: Main
+2
View File
@@ -1,6 +1,8 @@
%%~name: Chapter Two
%%~path: 6bd935d2490cd/441420a886d82
%%~kind: NOVEL/DOCUMENT
%%~hash: fd6d46708faa1333f8f7ba0442fc5b35bf1e3f85
%%~date: Unknown/Unknown
## Chapter Two
@pov: Bod
+2
View File
@@ -1,6 +1,8 @@
%%~name: Scene Five
%%~path: 6bd935d2490cd/47666c91c7ccf
%%~kind: NOVEL/DOCUMENT
%%~hash: d210c26966da6f9edea8726567abf4860b7bb9b7
%%~date: Unknown/Unknown
### Scene Five
@pov: Bod
+2
View File
@@ -1,6 +1,8 @@
%%~name: Mr. Nobody
%%~path: 67a8707f2f249/4c4f28287af27
%%~kind: CHARACTER/NOTE
%%~hash: 07a86c2001669de7d7a7545286c22b963e3ccf63
%%~date: Unknown/Unknown
# Nobody Owens
@tag: Bod
+2
View File
@@ -1,6 +1,8 @@
%%~name: Lorem Ipsum
%%~path: b3643d0f92e32/7a992350f3eb6
%%~kind: NOVEL/DOCUMENT
%%~hash: 8efda028000b70be0d7dbe9647b6026082ef05c9
%%~date: Unknown/Unknown
#! Lorem Ipsum
>> **By lipsum.com** <<
+2
View File
@@ -1,6 +1,8 @@
%%~name: Interlude
%%~path: b3643d0f92e32/846352075de7d
%%~kind: NOVEL/DOCUMENT
%%~hash: ac0e16c65142b9f1e0fa281bdc9b954e44026740
%%~date: Unknown/Unknown
##! Why do we use it?
% Exctracted from the lipsum.com website.
+2
View File
@@ -1,6 +1,8 @@
%%~name: Scene One
%%~path: 45e6b01ca35c1/88243afbe5ed8
%%~kind: NOVEL/DOCUMENT
%%~hash: a09245a7a772bbe02850b5db109977e336cd9cc1
%%~date: Unknown/Unknown
### Scene One
@pov: Bod
+2
View File
@@ -1,6 +1,8 @@
%%~name: Prologue
%%~path: b3643d0f92e32/88d59a277361b
%%~kind: NOVEL/DOCUMENT
%%~hash: 19a2aa95b07ce10eaea753c46569929439648c63
%%~date: Unknown/Unknown
##! Prologue
% Synopsis:Explanation from the lipsum.com website.
+2
View File
@@ -1,6 +1,8 @@
%%~name: Front Matter
%%~path: b3643d0f92e32/8c58a65414c23
%%~kind: NOVEL/DOCUMENT
%%~hash: 5c3961cb7616ef2b378010f38a89ed268ef15d92
%%~date: Unknown/Unknown
[NEW PAGE]
% Exctracted from the lipsum.com website.
+2
View File
@@ -1,6 +1,8 @@
%%~name: Act One
%%~path: b3643d0f92e32/db7e733775d4d
%%~kind: NOVEL/DOCUMENT
%%~hash: d93cd4c96d49e4afd93c82cca29d413a35012108
%%~date: Unknown/Unknown
# Act One
>> “Fusce maximus felis libero” <<
+2
View File
@@ -1,6 +1,8 @@
%%~name: Scene Three
%%~path: 6bd935d2490cd/eb103bc70c90c
%%~kind: NOVEL/DOCUMENT
%%~hash: c4eda49e4fe81dc450d547eee0bdabe77fdaaa98
%%~date: Unknown/Unknown
### Scene Three
@pov: Bod
+2
View File
@@ -1,6 +1,8 @@
%%~name: Scene Four
%%~path: 6bd935d2490cd/f8c0562e50f1b
%%~kind: NOVEL/DOCUMENT
%%~hash: 9461a279b9fb6ef005ee4d432fcda77ff5bfbd42
%%~date: Unknown/Unknown
### Scene Four
@pov: Bod
+2
View File
@@ -1,6 +1,8 @@
%%~name: Scene Two
%%~path: 45e6b01ca35c1/f96ec11c6a3da
%%~kind: NOVEL/DOCUMENT
%%~hash: ebe3fbaa16d9d81bc1a139822e3bf39bb357866d
%%~date: Unknown/Unknown
### Scene Two
@pov: Bod
+2
View File
@@ -1,6 +1,8 @@
%%~name: Chapter One
%%~path: 45e6b01ca35c1/fb609cd8319dc
%%~kind: NOVEL/DOCUMENT
%%~hash: 5dabeaa7a58238a6ad99ce73176b1bdfad1a71b7
%%~date: Unknown/Unknown
## Chapter One
@pov: Bod
+2 -2
View File
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="2.0.7" hexVersion="0x020007f0" fileVersion="1.5" fileRevision="1" timeStamp="2023-06-03 18:18:14">
<project id="5ac0df12-8b8b-476b-9905-21d33685687c" saveCount="42" autoCount="24" editTime="1913">
<novelWriterXML appVersion="2.1-beta1" hexVersion="0x020100b1" fileVersion="1.5" fileRevision="1" timeStamp="2023-08-25 18:03:37">
<project id="5ac0df12-8b8b-476b-9905-21d33685687c" saveCount="43" autoCount="24" editTime="1935">
<name>Lorem Ipsum</name>
<title>Lorem Ipsum</title>
<author>lipsum.com</author>
@@ -1,6 +1,8 @@
%%~name: Chapter 1
%%~path: 0000000000008/0000000000010
%%~kind: NOVEL/DOCUMENT
%%~hash: 25af8567c8a3b3b49a27dcda2999ed1f8811955a
%%~date: 2023-08-25 18:32:30/2023-08-25 18:32:30
## Chapter 1
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc maximus justo non dictum commodo. Curabitur lacinia tempor orci vel luctus. Phasellus porta metus eu massa luctus, eget euismod risus rhoncus. Vestibulum sed arcu nisi. Maecenas pretium facilisis velit, vel semper lacus aliquam sit amet. Vestibulum vulputate neque ligula, rhoncus blandit turpis consequat id. Mauris sagittis vehicula imperdiet. Duis sed nunc pretium, ornare purus vel, sodales augue. Maecenas a suscipit risus. Quisque volutpat justo eleifend est ullamcorper fermentum. Donec ullamcorper et tortor a laoreet. Nam id risus nisi. Vivamus non imperdiet erat, sit amet imperdiet felis. Mauris vitae neque et est aliquam scelerisque non non ipsum.
@@ -1,6 +1,8 @@
%%~name: All of Chapter 1
%%~path: 0000000000008/0000000000014
%%~kind: NOVEL/DOCUMENT
%%~hash: 9e632e7e29860572c685da8501b92bf99825ac3b
%%~date: 2023-08-25 18:29:39/2023-08-25 18:29:39
% Merge Novel Chapter: Chapter 1 [New]
## Chapter 1
@@ -1,6 +1,8 @@
%%~name: New Scene
%%~path: 000000000000d/000000000000f
%%~kind: NOVEL/DOCUMENT
%%~hash: fd5dc2f0c9767cb124b1bf2300d7a33b7780045e
%%~date: 2023-08-25 18:08:01/2023-08-25 18:08:04
# Novel
## Chapter
@@ -1,6 +1,8 @@
%%~name: New Note
%%~path: 000000000000a/0000000000010
%%~kind: CHARACTER/NOTE
%%~hash: 9fae6dfdd3d1c0822d3a3cf90c0142e65ad8e557
%%~date: 2023-08-25 18:14:24/2023-08-25 18:14:24
# Jane Doe
@tag: Jane
@@ -1,6 +1,8 @@
%%~name: New Note
%%~path: 0000000000009/0000000000011
%%~kind: PLOT/NOTE
%%~hash: 8ff26f8a18ad6390c2ce725c441ab0c792a125cf
%%~date: 2023-08-25 18:15:35/2023-08-25 18:15:35
# Main Plot
@tag: MainPlot
@@ -1,6 +1,8 @@
%%~name: New Note
%%~path: 000000000000b/0000000000012
%%~kind: WORLD/NOTE
%%~hash: 3f5c3c6c3ba1c27c30b8ac9e59c222fb9a1bd775
%%~date: 2023-08-25 18:17:45/2023-08-25 18:17:45
# Main Location
@tag: Home
+27 -27
View File
@@ -26,8 +26,8 @@ from shutil import copyfile
from pathlib import Path
from zipfile import ZipFile
from tools import C, NWD_IGNORE, buildTestProject, cmpFiles, XML_IGNORE
from mocked import causeOSError
from tools import C, buildTestProject, cmpFiles, XML_IGNORE
from novelwriter import CONFIG
from novelwriter.constants import nwItemClass
@@ -46,19 +46,19 @@ def testCoreTools_DocMerger(monkeypatch, mockGUI, fncPath, tstPaths, mockRnd, ip
# =====================
hChapter1 = theProject.newFile("Chapter 1", C.hNovelRoot)
hSceneOne11 = theProject.newFile("Scene 1.1", hChapter1)
hSceneOne12 = theProject.newFile("Scene 1.2", hChapter1)
hSceneOne13 = theProject.newFile("Scene 1.3", hChapter1)
hSceneOne11 = theProject.newFile("Scene 1.1", hChapter1) # type: ignore
hSceneOne12 = theProject.newFile("Scene 1.2", hChapter1) # type: ignore
hSceneOne13 = theProject.newFile("Scene 1.3", hChapter1) # type: ignore
docText1 = "\n\n".join(ipsumText[0:2]) + "\n\n"
docText2 = "\n\n".join(ipsumText[1:3]) + "\n\n"
docText3 = "\n\n".join(ipsumText[2:4]) + "\n\n"
docText4 = "\n\n".join(ipsumText[3:5]) + "\n\n"
theProject.writeNewFile(hChapter1, 2, True, docText1)
theProject.writeNewFile(hSceneOne11, 3, True, docText2)
theProject.writeNewFile(hSceneOne12, 3, True, docText3)
theProject.writeNewFile(hSceneOne13, 3, True, docText4)
theProject.writeNewFile(hChapter1, 2, True, docText1) # type: ignore
theProject.writeNewFile(hSceneOne11, 3, True, docText2) # type: ignore
theProject.writeNewFile(hSceneOne12, 3, True, docText3) # type: ignore
theProject.writeNewFile(hSceneOne13, 3, True, docText4) # type: ignore
# Basic Checks
# ============
@@ -81,12 +81,12 @@ def testCoreTools_DocMerger(monkeypatch, mockGUI, fncPath, tstPaths, mockRnd, ip
testFile = tstPaths.outDir / "coreDocTools_DocMerger_0000000000014.nwd"
compFile = tstPaths.refDir / "coreDocTools_DocMerger_0000000000014.nwd"
assert docMerger.newTargetDoc(hChapter1, "All of Chapter 1") == "0000000000014"
assert docMerger.newTargetDoc(hChapter1, "All of Chapter 1") == "0000000000014" # type: ignore
assert docMerger.appendText(hChapter1, True, "Merge") is True
assert docMerger.appendText(hSceneOne11, True, "Merge") is True
assert docMerger.appendText(hSceneOne12, True, "Merge") is True
assert docMerger.appendText(hSceneOne13, True, "Merge") is True
assert docMerger.appendText(hChapter1, True, "Merge") is True # type: ignore
assert docMerger.appendText(hSceneOne11, True, "Merge") is True # type: ignore
assert docMerger.appendText(hSceneOne12, True, "Merge") is True # type: ignore
assert docMerger.appendText(hSceneOne13, True, "Merge") is True # type: ignore
# Block writing and check error handling
with monkeypatch.context() as mp:
@@ -98,7 +98,7 @@ def testCoreTools_DocMerger(monkeypatch, mockGUI, fncPath, tstPaths, mockRnd, ip
# Write properly, and compare
assert docMerger.writeTargetDoc() is True
copyfile(saveFile, testFile)
assert cmpFiles(testFile, compFile)
assert cmpFiles(testFile, compFile, ignoreStart=NWD_IGNORE)
# Merge into Existing
# ===================
@@ -107,15 +107,15 @@ def testCoreTools_DocMerger(monkeypatch, mockGUI, fncPath, tstPaths, mockRnd, ip
testFile = tstPaths.outDir / "coreDocTools_DocMerger_0000000000010.nwd"
compFile = tstPaths.refDir / "coreDocTools_DocMerger_0000000000010.nwd"
docMerger.setTargetDoc(hChapter1)
docMerger.setTargetDoc(hChapter1) # type: ignore
assert docMerger.appendText(hSceneOne11, True, "Merge") is True
assert docMerger.appendText(hSceneOne12, True, "Merge") is True
assert docMerger.appendText(hSceneOne13, True, "Merge") is True
assert docMerger.appendText(hSceneOne11, True, "Merge") is True # type: ignore
assert docMerger.appendText(hSceneOne12, True, "Merge") is True # type: ignore
assert docMerger.appendText(hSceneOne13, True, "Merge") is True # type: ignore
assert docMerger.writeTargetDoc() is True
copyfile(saveFile, testFile)
assert cmpFiles(testFile, compFile)
assert cmpFiles(testFile, compFile, ignoreStart=NWD_IGNORE)
# Just for debugging
docMerger.writeTargetDoc()
@@ -163,11 +163,11 @@ def testCoreTools_DocSplitter(monkeypatch, mockGUI, fncPath, mockRnd, ipsumText)
docText = "\n\n".join(docData)
docRaw = docText.splitlines()
assert theProject.storage.getDocument(hSplitDoc).writeDocument(docText) is True
theProject.tree[hSplitDoc].setStatus(C.sFinished)
theProject.tree[hSplitDoc].setImport(C.iMain)
theProject.tree[hSplitDoc].setStatus(C.sFinished) # type: ignore
theProject.tree[hSplitDoc].setImport(C.iMain) # type: ignore
docSplitter = DocSplitter(theProject, hSplitDoc)
assert docSplitter._srcItem.isFileType()
docSplitter = DocSplitter(theProject, hSplitDoc) # type: ignore
assert docSplitter._srcItem.isFileType() # type: ignore
assert docSplitter.getError() == ""
# Run the split algorithm
@@ -247,8 +247,8 @@ def testCoreTools_DocSplitter(monkeypatch, mockGUI, fncPath, mockRnd, ipsumText)
# Check that status and importance has been preserved
for rHandle in resDocHandle:
assert theProject.tree[rHandle].itemStatus == C.sFinished
assert theProject.tree[rHandle].itemImport == C.iMain
assert theProject.tree[rHandle].itemStatus == C.sFinished # type: ignore
assert theProject.tree[rHandle].itemImport == C.iMain # type: ignore
# Check handling of improper initialisation
docSplitter = DocSplitter(theProject, C.hInvalid)
@@ -416,7 +416,7 @@ def testCoreTools_NewMinimal(monkeypatch, fncPath, tstPaths, mockGUI, mockRnd):
assert projBuild.buildProject({}) is False
# Wrong type should also fail
assert projBuild.buildProject("stuff") is False
assert projBuild.buildProject("stuff") is False # type: ignore
# Try again with a proper path
assert projBuild.buildProject({"projPath": fncPath}) is True
@@ -508,7 +508,7 @@ def testCoreTools_NewCustomB(monkeypatch, fncPath, tstPaths, mockRnd):
@pytest.mark.core
def testCoreTools_NewSample(monkeypatch, fncPath, tstPaths):
def testCoreTools_NewSample(monkeypatch, mockGUI, fncPath, tstPaths):
"""Check that we can create a new project can be created from the
provided sample project via a zip file.
"""
+14 -7
View File
@@ -21,8 +21,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import pytest
from tools import C, MOCK_TIME, buildTestProject, readFile, writeFile
from mocked import causeOSError
from tools import C, buildTestProject, readFile, writeFile
from novelwriter.enum import nwItemClass, nwItemLayout
from novelwriter.core.project import NWProject
@@ -32,6 +32,8 @@ from novelwriter.core.document import NWDocument
@pytest.mark.core
def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
"""Test loading and saving a document with the NWDocument class."""
monkeypatch.setattr("novelwriter.core.document.time", lambda: MOCK_TIME)
theProject = NWProject()
mockRnd.reset()
buildTestProject(theProject, fncPath)
@@ -48,7 +50,7 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
# Non-existent handle
theDoc = NWDocument(theProject, C.hInvalid)
assert theDoc.readDocument() is None
assert theDoc._currHash is None
assert theDoc._lastHash == ""
assert theDoc.fileExists() is False
# No content path
@@ -90,7 +92,7 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
# Set handle and save
theText = "### Test File\n\nText ...\n\n"
theDoc = NWDocument(theProject, xHandle)
assert theDoc.readDocument(xHandle) == ""
assert theDoc.readDocument(xHandle) == "" # type: ignore
assert theDoc.writeDocument(theText) is True
# Save again to ensure temp file and previous file is handled
@@ -102,6 +104,8 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
"%%~name: New File\n"
f"%%~path: {C.hNovelRoot}/{xHandle}\n"
"%%~kind: NOVEL/DOCUMENT\n"
"%%~hash: b288c3ab03181027d9a16d7fd2291262f5de9ac8\n"
"%%~date: 2019-05-10 18:52:00/2019-05-10 18:52:00\n"
"### Test File\n\n"
"Text ...\n\n"
)
@@ -170,9 +174,10 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
@pytest.mark.core
def testCoreDocument_Methods(mockGUI, fncPath, mockRnd):
"""Test other methods of the NWDocument class.
"""
def testCoreDocument_Methods(monkeypatch, mockGUI, fncPath, mockRnd):
"""Test other methods of the NWDocument class."""
monkeypatch.setattr("novelwriter.core.document.time", lambda: MOCK_TIME)
theProject = NWProject()
mockRnd.reset()
buildTestProject(theProject, fncPath)
@@ -187,7 +192,7 @@ def testCoreDocument_Methods(mockGUI, fncPath, mockRnd):
# Check the item
assert theDoc.getCurrentItem() is not None
assert theDoc.getCurrentItem().itemHandle == C.hSceneDoc
assert theDoc.getCurrentItem().itemHandle == C.hSceneDoc # type: ignore
# Check the meta
theName, theParent, theClass, theLayout = theDoc.getMeta()
@@ -202,6 +207,8 @@ def testCoreDocument_Methods(mockGUI, fncPath, mockRnd):
"%%~name: New Scene\n"
f"%%~path: {C.hChapterDir}/{C.hSceneDoc}\n"
"%%~kind: NOVEL/DOCUMENT\n"
"%%~hash: dd350c602de803554b2a7c17f191ae25dea1df63\n"
"%%~date: 2019-05-10 18:52:00/2019-05-10 18:52:00\n"
"%%~ stuff\n"
"### Test File\n\n"
"Text ...\n\n"
+5 -5
View File
@@ -24,7 +24,7 @@ import pytest
from shutil import copyfile
from tools import (
C, cmpFiles, buildTestProject, XML_IGNORE, getGuiItem, writeFile
C, NWD_IGNORE, cmpFiles, buildTestProject, XML_IGNORE, getGuiItem, writeFile
)
from PyQt5.QtCore import Qt
@@ -529,25 +529,25 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, projPath, tstPaths, mockRnd):
testFile = tstPaths.outDir / "guiEditor_Main_Final_000000000000f.nwd"
compFile = tstPaths.refDir / "guiEditor_Main_Final_000000000000f.nwd"
copyfile(projFile, testFile)
assert cmpFiles(testFile, compFile)
assert cmpFiles(testFile, compFile, ignoreStart=NWD_IGNORE)
projFile = projPath / "content" / "0000000000010.nwd"
testFile = tstPaths.outDir / "guiEditor_Main_Final_0000000000010.nwd"
compFile = tstPaths.refDir / "guiEditor_Main_Final_0000000000010.nwd"
copyfile(projFile, testFile)
assert cmpFiles(testFile, compFile)
assert cmpFiles(testFile, compFile, ignoreStart=NWD_IGNORE)
projFile = projPath / "content" / "0000000000011.nwd"
testFile = tstPaths.outDir / "guiEditor_Main_Final_0000000000011.nwd"
compFile = tstPaths.refDir / "guiEditor_Main_Final_0000000000011.nwd"
copyfile(projFile, testFile)
assert cmpFiles(testFile, compFile)
assert cmpFiles(testFile, compFile, ignoreStart=NWD_IGNORE)
projFile = projPath / "content" / "0000000000012.nwd"
testFile = tstPaths.outDir / "guiEditor_Main_Final_0000000000012.nwd"
compFile = tstPaths.refDir / "guiEditor_Main_Final_0000000000012.nwd"
copyfile(projFile, testFile)
assert cmpFiles(testFile, compFile)
assert cmpFiles(testFile, compFile, ignoreStart=NWD_IGNORE)
# qtbot.stop()
+4 -2
View File
@@ -23,11 +23,14 @@ from __future__ import annotations
import shutil
from pathlib import Path
from datetime import datetime
from PyQt5.QtWidgets import qApp
XML_IGNORE = ("<novelWriterXML", "<project")
ODT_IGNORE = ("<meta:generator", "<meta:creation-date", "<dc:date", "<meta:editing")
NWD_IGNORE = ("%%~date:",)
MOCK_TIME = datetime(2019, 5, 10, 18, 52, 0).timestamp()
class C:
@@ -62,8 +65,7 @@ def cmpFiles(
ignoreLines: list | None = None,
ignoreStart: tuple | None = None
) -> bool:
"""Compare two files, but optionally ignore lines given by a list.
"""
"""Compare two files, with optional line ignore."""
if ignoreLines is None:
ignoreLines = []