Skip to content

Commit

Permalink
minor formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpue committed Oct 16, 2023
1 parent 03b952d commit 8a84b04
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 76 deletions.
4 changes: 1 addition & 3 deletions docs/conf.py
Expand Up @@ -21,9 +21,7 @@
import os
import sys

sys.path.insert(
0, os.path.abspath("..")
) # necessary to import graphtransilterator here
sys.path.insert(0, os.path.abspath("..")) # necessary to import graphtransilterator here

package_path = os.path.abspath("..") # this may work for jupyter-sphinx?
os.environ["PYTHONPATH"] = ":".join((package_path, os.environ.get("PYTHONPATH", "")))
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Expand Up @@ -99,4 +99,3 @@ skip_empty = true
[tool.coverage.run]
branch = true
source = ["graphtransliterator"]

17 changes: 4 additions & 13 deletions tests/test_ambiguity.py
Expand Up @@ -59,24 +59,15 @@ def test_GraphParser_check_ambiguity():

def test_GraphTransliterator_easy_reading():
assert (
_easyreading_rule(
TransliterationRule("", ["class_a"], [], ["a"], [], ["class_a"], 0)
)
_easyreading_rule(TransliterationRule("", ["class_a"], [], ["a"], [], ["class_a"], 0))
== "<class_a> a <class_a>"
)
assert (
_easyreading_rule(
TransliterationRule("", ["class_a"], [], ["a"], [], ["class_a"], 0)
)
_easyreading_rule(TransliterationRule("", ["class_a"], [], ["a"], [], ["class_a"], 0))
== "<class_a> a <class_a>"
)
assert _easyreading_rule(TransliterationRule("", [], ["b"], ["a"], ["b"], [], 0)) == "(b) a (b)"
assert (
_easyreading_rule(TransliterationRule("", [], ["b"], ["a"], ["b"], [], 0))
== "(b) a (b)"
)
assert (
_easyreading_rule(
TransliterationRule("", ["class_a"], ["b"], ["a"], ["b"], ["class_a"], 0)
)
_easyreading_rule(TransliterationRule("", ["class_a"], ["b"], ["a"], ["b"], ["class_a"], 0))
== "(<class_a> b) a (b <class_a>)"
)
32 changes: 8 additions & 24 deletions tests/test_cli.py
Expand Up @@ -49,16 +49,12 @@ def test_cli_transliterate_tests(tmpdir):
runner = CliRunner()

# test bundled
bundled_result = runner.invoke(
cli.main, ["transliterate", "--from", "bundled", "Example", "a"]
)
bundled_result = runner.invoke(cli.main, ["transliterate", "--from", "bundled", "Example", "a"])
assert bundled_result.exit_code == 0
assert bundled_result.output.strip() == "A"

# test multiple inputs with python output
bundled_multiple_result = runner.invoke(
cli.main, ["transliterate", "--from", "bundled", "Example", "a", "a"]
)
bundled_multiple_result = runner.invoke(cli.main, ["transliterate", "--from", "bundled", "Example", "a", "a"])
assert bundled_multiple_result.exit_code == 0
assert bundled_multiple_result.output.strip() == str(["A", "A"])

Expand All @@ -72,37 +68,29 @@ def test_cli_transliterate_tests(tmpdir):

# test transliterate from JSON
json_ = transliterator.dumps()
json_result = runner.invoke(
cli.main, ["transliterate", "--from", "json", json_, "a"]
)
json_result = runner.invoke(cli.main, ["transliterate", "--from", "json", json_, "a"])
assert json_result.exit_code == 0
assert json_result.output.strip() == "A"

# test transliterate from json file
json_file = tmpdir.mkdir("sub").join("test.json")
json_file.write(json_)
json_file_result = runner.invoke(
cli.main, ["transliterate", "--from", "json_file", json_file.strpath, "a"]
)
json_file_result = runner.invoke(cli.main, ["transliterate", "--from", "json_file", json_file.strpath, "a"])
assert json_file_result.exit_code == 0
assert json_file_result.output.strip() == "A"

# test transliterate from yaml file
yaml_file = tmpdir.join("test.yaml")
yaml_file.write(test_yaml)
yaml_file_result = runner.invoke(
cli.main, ["transliterate", "--from", "yaml_file", yaml_file.strpath, "a"]
)
yaml_file_result = runner.invoke(cli.main, ["transliterate", "--from", "yaml_file", yaml_file.strpath, "a"])
assert yaml_file_result.exit_code == 0
assert yaml_file_result.output.strip() == "A"


def test_cli_generate_tests():
"""Test tests command."""
runner = CliRunner()
gen_tests_result = runner.invoke(
cli.main, ["generate-tests", "--from", "bundled", "Example"]
)
gen_tests_result = runner.invoke(cli.main, ["generate-tests", "--from", "bundled", "Example"])
assert gen_tests_result.exit_code == 0
yaml_ = yaml.safe_load(StringIO(gen_tests_result.output))
assert len(yaml_) == 5 and type(yaml_) == dict
Expand Down Expand Up @@ -163,13 +151,9 @@ def test_dump_tests():
runner = CliRunner()
test_result = runner.invoke(cli.main, ["dump-tests", "Example"]) # yaml
assert "a: A" in test_result.output
test_result = runner.invoke(
cli.main, ["dump-tests", "--to", "json", "Example"]
) # json
test_result = runner.invoke(cli.main, ["dump-tests", "--to", "json", "Example"]) # json
assert '"a": "A"' in test_result.output, test_result.output
test_result = runner.invoke(
cli.main, ["dump-tests", "--to", "yaml", "Example"]
) # json
test_result = runner.invoke(cli.main, ["dump-tests", "--to", "yaml", "Example"]) # json
assert "a: A" in test_result.output


Expand Down
8 changes: 2 additions & 6 deletions tests/test_compression.py
Expand Up @@ -51,16 +51,12 @@ def test_compression():
decompressed_config = compression.decompress_config(compressed_config)
gt_from_decompressed = GraphTransliterator.load(decompressed_config)
# Compare JSON dumps with sorted keys.
assert json.dumps(gt.dump(), sort_keys=True) == json.dumps(
gt_from_decompressed.dump(), sort_keys=True
)
assert json.dumps(gt.dump(), sort_keys=True) == json.dumps(gt_from_decompressed.dump(), sort_keys=True)
# Test bad compression level
with pytest.raises(ValueError):
gt.dump(compression_level=graphtransliterator.HIGHEST_COMPRESSION_LEVEL + 1)
# Test compression at level 0 (should likely not be called)
assert "compressed_settings" not in compression.compress_config(
gt.dump(), compression_level=0
)
assert "compressed_settings" not in compression.compress_config(gt.dump(), compression_level=0)
# Test compression levels
assert '"tokens": ' in gt.dumps(compression_level=0)
assert '"compressed_settings"' in gt.dumps(compression_level=1)
Expand Down
27 changes: 6 additions & 21 deletions tests/test_graphtransliterator.py
Expand Up @@ -200,14 +200,8 @@ def test_graphtransliterator_process():

assert process._process_rules({"a": "A"})[0]["tokens"] == ["a"]
assert process._process_rules({"a": "A"})[0]["production"] == "A"
assert (
process._process_onmatch_rules(data["onmatch_rules"])[0]["prev_classes"][0]
== "class1"
)
assert (
process._process_onmatch_rules(data["onmatch_rules"])[0]["next_classes"][0]
== "class2"
)
assert process._process_onmatch_rules(data["onmatch_rules"])[0]["prev_classes"][0] == "class1"
assert process._process_onmatch_rules(data["onmatch_rules"])[0]["next_classes"][0] == "class2"


def test_graphtransliterator_models():
Expand Down Expand Up @@ -403,9 +397,7 @@ def test_serialization():
settings = gt.dump()
for _ in to_drop:
settings.pop(_)
if settings.get("onmatch_rules_lookup") and not settings.get(
"onmatch_rules"
):
if settings.get("onmatch_rules_lookup") and not settings.get("onmatch_rules"):
with pytest.raises(ValidationError):
assert GraphTransliterator.load(settings)
else:
Expand Down Expand Up @@ -493,9 +485,7 @@ def test_GraphTransliterator(tmpdir):
assert len(set(GraphTransliterator.from_easyreading_dict(input_dict).tokens)) == 4

assert GraphTransliterator.from_yaml(yaml_str).transliterate("ab") == "A,B"
assert (
GraphTransliterator.from_yaml_file(yaml_filename).transliterate("ab") == "A,B"
)
assert GraphTransliterator.from_yaml_file(yaml_filename).transliterate("ab") == "A,B"
assert (
GraphTransliterator.from_easyreading_dict(
{
Expand Down Expand Up @@ -530,10 +520,7 @@ def test_GraphTransliterator_ignore_errors():
token_class: wb
"""
# check that ignore_errors works
assert (
GraphTransliterator.from_yaml(yaml_str, ignore_errors=True).transliterate("a")
== ""
)
assert GraphTransliterator.from_yaml(yaml_str, ignore_errors=True).transliterate("a") == ""

with pytest.raises(NoMatchingTransliterationRuleException):
gt = GraphTransliterator.from_yaml(yaml_str, ignore_errors=False)
Expand Down Expand Up @@ -597,9 +584,7 @@ def test_GraphTransliterator_productions():
whitespace = {"default": " ", "token_class": "wb", "consolidate": True}
rules = {"ab": "AB", " ": "_"}
settings = {"tokens": tokens, "rules": rules, "whitespace": whitespace}
assert set(GraphTransliterator.from_easyreading_dict(settings).productions) == set(
["AB", "_"]
)
assert set(GraphTransliterator.from_easyreading_dict(settings).productions) == set(["AB", "_"])


def test_GraphTransliterator_pruned_of():
Expand Down
10 changes: 2 additions & 8 deletions tests/test_transliterators.py
Expand Up @@ -15,9 +15,7 @@ def init_test_class(self, method=method, coverage=True):
self._module_name = transliterator._module_name
self._init_from(method, check_ambiguity=True, coverage=True)

class_name = "Test{}From{}".format(
type(transliterator).__name__, method.upper()
)
class_name = "Test{}From{}".format(type(transliterator).__name__, method.upper())
SuperClass = transliterator.__class__
TestClass = type(class_name, (SuperClass,), {"__init__": init_test_class})
# Confirm it is a subclass of super class
Expand Down Expand Up @@ -57,11 +55,7 @@ def test_iter_names():

def test_iter_transliterators():
"""Test transliterators.iter_transliterators()."""
example = [
_
for _ in transliterators.iter_transliterators()
if type(_).__name__ == "Example"
].pop()
example = [_ for _ in transliterators.iter_transliterators() if type(_).__name__ == "Example"].pop()
assert example.transliterate("a") == "A"


Expand Down

0 comments on commit 8a84b04

Please sign in to comment.