-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for transforming strings
- Loading branch information
1 parent
19e91ff
commit fffd49d
Showing
6 changed files
with
129 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
# Tests / Handlers / Example handlers | ||
Simple Transdoc handlers used for testing. | ||
""" | ||
|
||
from typing import IO | ||
import transdoc | ||
from transdoc.handlers.api import TransdocHandler | ||
|
||
|
||
class SimpleHandler(TransdocHandler): | ||
"""Simple, valid handler plugin""" | ||
|
||
group = "transdoc.handlers" | ||
|
||
def matches_file(self, file_path: str) -> bool: | ||
return True | ||
|
||
def transform_file( | ||
self, | ||
transformer: transdoc.TransdocTransformer, | ||
in_path: str, | ||
in_file: IO, | ||
out_file: IO | None, | ||
) -> None: | ||
# Do nothing, we don't test outputs using this handler | ||
pass | ||
|
||
|
||
class UnsupportedHandler(TransdocHandler): | ||
"""handler plugin which supports no file types""" | ||
|
||
group = "transdoc.handlers" | ||
|
||
def matches_file(self, file_path: str) -> bool: | ||
return False | ||
|
||
def transform_file( | ||
self, | ||
transformer: transdoc.TransdocTransformer, | ||
in_path: str, | ||
in_file: IO, | ||
out_file: IO | None, | ||
) -> None: | ||
# Do nothing, we don't test outputs using this handler | ||
pass | ||
|
||
|
||
class FailToLoadHandler(TransdocHandler): | ||
"""Handler plugin that fails to load due to an exception during creation""" | ||
|
||
group = "transdoc.handlers" | ||
|
||
def __init__(self) -> None: | ||
raise RuntimeError("Intentional failure to load plugin") | ||
|
||
def matches_file(self, file_path: str) -> bool: | ||
raise NotImplementedError() | ||
|
||
def transform_file( | ||
self, | ||
transformer: transdoc.TransdocTransformer, | ||
in_path: str, | ||
in_file: IO, | ||
out_file: IO | None, | ||
) -> None: | ||
raise NotImplementedError() | ||
|
||
|
||
class ProtocolMismatchHandler: | ||
"""Handler plugin that doesn't match the protocol""" | ||
|
||
group = "transdoc.handlers" | ||
|
||
# Intentionally empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
# Tests / string test | ||
Test cases for transforming strings | ||
""" | ||
|
||
from tests.handlers.example_handlers import SimpleHandler, UnsupportedHandler | ||
from pytest_mock import MockerFixture | ||
|
||
import transdoc | ||
from transdoc import TransdocTransformer | ||
|
||
|
||
def test_transforms_strings(transformer: TransdocTransformer): | ||
assert transdoc.transform(transformer, "Input") == "Input" | ||
|
||
|
||
def test_transforms_using_given_handler( | ||
mocker: MockerFixture, transformer: TransdocTransformer | ||
): | ||
handler = SimpleHandler() | ||
matches_file = mocker.spy(handler, "matches_file") | ||
transform_file = mocker.spy(handler, "transform_file") | ||
|
||
transdoc.transform(transformer, "Input", path="<test>", handler=handler) | ||
|
||
matches_file.assert_called_once_with("<test>") | ||
transform_file.assert_called_once() | ||
|
||
|
||
def test_unsupported_handler_warning(transformer: TransdocTransformer): | ||
handler = UnsupportedHandler() | ||
transdoc.transform(transformer, "Input", path="<test>", handler=handler) | ||
# Not testing for logging issues, because that's sorta annoying. This is | ||
# just here for coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters