Skip to content

Commit

Permalink
Add custom validator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lbiaggi committed Mar 21, 2024
1 parent 34adb54 commit 51513d4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
57 changes: 57 additions & 0 deletions doorstop/core/tests/test_item_extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# SPDX-License-Identifier: LGPL-3.0-only
# pylint: disable=C0302

"""Unit tests for the doorstop.core.item module."""

import os
import unittest
from types import ModuleType
from unittest.mock import patch

from doorstop.common import import_path_as_module
from doorstop.core.tests import TESTS_ROOT, MockItem, MockSimpleDocumentExtensions


class TestItem(unittest.TestCase):
"""Unit tests for the Item class."""

# pylint: disable=protected-access,no-value-for-parameter

def setUp(self):
path = os.path.join("path", "to", "RQ001.yml")
self.item = MockItem(MockSimpleDocumentExtensions(), path)


@patch("doorstop.settings.CACHE_PATHS", False)
def test_load_custom_validator_per_folder(self):
"""Load a valid custom validator per folder."""
path = os.path.join("path", "to", "RQ001.yml")
self.item = MockItem(
MockSimpleDocumentExtensions(
item_validator=f"{TESTS_ROOT}/validators/validator_dummy.py"
),
path,
)
document = self.item.document
validator = import_path_as_module(document.extensions["item_validator"])

self.assertEqual(isinstance(validator, ModuleType), True)

@patch("doorstop.settings.CACHE_PATHS", False)
def test_load_custom_validator_per_folder_and_fails(self):
"""Load a invalid custom validator per folder and fails with FileNotFoundError."""
path = os.path.join("path", "to", "RQ001.yml")
self.item = MockItem(
MockSimpleDocumentExtensions(
item_validator=f"{TESTS_ROOT}/files/validator_dummy2.py"
),
path,
)
document = self.item.document
try:
validator = import_path_as_module(document.extensions["item_validator"])
except FileNotFoundError:
validator = FileNotFoundError

self.assertEqual(FileNotFoundError, validator)

8 changes: 8 additions & 0 deletions doorstop/core/tests/validators/validator_dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: LGPL-3.0-only

from doorstop import DoorstopError, DoorstopInfo, DoorstopWarning


def item_validator(item):
if item:
yield DoorstopInfo("Loaded")

0 comments on commit 51513d4

Please sign in to comment.