import related
from .models import Person, RoleModels
def test_set_construction():
scientists = [Person(first_name="Grace", last_name="Hopper"),
Person(first_name="Katherine", last_name="Johnson"),
Person(first_name="Katherine", last_name="Johnson")]
assert len(scientists) == 3
role_models = RoleModels(scientists=scientists)
assert len(role_models.scientists) == 2
assert related.to_yaml(role_models).strip() in (SET_ORDER_1, SET_ORDER_2)
SET_ORDER_1 = """
scientists:
- first_name: Grace
last_name: Hopper
- first_name: Katherine
last_name: Johnson
""".strip()
SET_ORDER_2 = """
scientists:
- first_name: Katherine
last_name: Johnson
- first_name: Grace
last_name: Hopper
""".strip()
def test_strict_mode():
p = related.to_model(Person, dict(first_name="Grace", last_name="Hopper"))
assert p.first_name == "Grace"
try:
d = dict(first_name="Grace", last_name="Hopper", extra="Failure!")
related.to_model(Person, d)
assert False, "Did not fail."
except ValueError as e:
assert e, "Error as expected."
from tests.utilities.utilities import compare_lists
class TestNormalizeAndCompareLists(object):
def test_lists_empty_string(self):
assert compare_lists(["", "", ""], ["", "", ""])
def test_lists_not_same_size(self):
assert not compare_lists(["", ""], ["", "", ""])
def test_lists_empty(self):
assert compare_lists([], [])
def test_list_empty_and_other_not(self):
assert not compare_lists([], ["foo"])
assert not compare_lists(["foo"], [])
def test_list_tabs(self):
assert compare_lists(["\t", ""], ["\t", ""])
def test_list_mixed_order(self):
assert compare_lists(["", "\t"], ["\t", ""])
assert compare_lists(["", "foo"], ["foo", ""])
assert compare_lists(["foo", ""], ["foo", ""])
assert not compare_lists(["bar", ""], ["foo", ""])
assert not compare_lists(["", "bar"], ["foo", ""])
def test_list_mix_tabs_spaces_letters(self):
assert not compare_lists(["foo \t", "bar"], ["\t", ""])
assert compare_lists(["foo dssdsd\t", "bar"], ["\tfoo dssdsd", "\tbar"])
def test_strict_mode(self):
assert not compare_lists(["foo \t", "bar"], ["foo", "bar"], True)
assert compare_lists(["foo", "bar"], ["foo", "bar"], True)
import re
import unittest
import warnings
from importlib import import_module
from import_guard import ForbiddenImportError, guard
class TestForbiddenImportError(unittest.TestCase):
def test_strict_mode(self):
guard.set_deny_rules({"csv": "re"})
guard.enable(strict=True)
with self.assertRaises(ForbiddenImportError):
import_module("csv")
class TestProject(unittest.TestCase):
def test_test_proj(self):
# rules are defined inside test_proj
expected = {
# <module> forbidden in <caller>
("csv", "test_proj.api"),
("bisect", "test_proj.api"),
("json", "test_proj.logging"),
("test_proj.business_logic", "test_proj.core"),
("test_proj.logging", "test_proj.api"),
}
# capture all warnings, extract affected modules
# and compare to expected
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
import_module("test_proj").run()
pattern = re.compile(r"Importing `([\w\.]+)` from `([\w\.]+)`")
actual = {pattern.findall(str(x.message))[0] for x in w}
self.assertEqual(actual, expected)