def thread_1(): with MapGetter(a) as math: from math import b, c nonlocal_check[0] = True time.sleep(0.01) nonlocal_check[2] = b nonlocal_check[3] = c
def test_mapgetter_works_with_defaultdict(): from collections import defaultdict with MapGetter(defaultdict(lambda: "baz")) as m: from m import foo, bar assert foo == "baz" assert bar == "baz"
def test_mapgetter_works_with_default_non_function_callable(): class Defaulter(object): def __call__(self, name): return name with MapGetter(default=Defaulter()) as m: from m import foo, bar assert foo == "foo" assert bar == "bar" class Defaulter(object): def __call__(self): return "baz" with MapGetter(default=Defaulter()) as m: from m import foo, bar assert foo == "baz" assert bar == "baz"
def test_mapgetter_works_with_enums(): class A(enum.Enum): foo = 0 bar = 1 baz = 2 with MapGetter(A) as A: from A import foo, bar, baz assert foo is A.foo assert bar is A.bar assert baz is A.baz assert foo.value == 0
def test_mapgetter_accepts_import_object_attributes(): try: from types import SimpleNameSpace except ImportError: class SimpleNameSpace(object): pass test_obj = SimpleNameSpace() test_obj.a = 1 test_obj.b = 2 with MapGetter(test_obj): from test_obj import a, b assert a == test_obj.a assert b == test_obj.b
def test_modify_record_by_name(self, table_info: CommaTableTestingExtrasType): """ Checks to see if modifications to a `CommaTable` object are correctly propagated where expected, when fields are edited by key (dict access). """ with MapGetter(table_info) as info: from info import table, record_index, field_index, field_name from info import some_record, some_record_copy some_record[field_name] = self.SOME_STRING # has the original record been modified? self.assert_record_has_changed( table_info=table_info, modified_string=self.SOME_STRING, )
def test_modify_record_field_slicing( self, table_info: CommaTableTestingExtrasType): """ Checks to see if modifications to a `CommaTable` object are correctly propagated where expected, when fields are edited by key (dict access). """ with MapGetter(table_info) as info: from info import table, record_index, field_index, field_name from info import some_record, some_record_copy # TEST: CAN EDITING BY FIELD SLICING PROPAGATE MODIFICATIONS table[field_name][record_index] = self.SOME_STRING # has the original record been modified? self.assert_record_has_changed( table_info=table_info, modified_string=self.SOME_STRING, )
def assert_record_unmodified(table_info: CommaTableTestingExtrasType, ): with MapGetter(table_info) as info: from info import table, record_index, field_index, original_value from info import some_record, some_record_copy # integrity: is the isolated record consistent with main record? assert table[record_index][field_index] == some_record[field_index] # check whether all accesses to this record produce the same value if string is not None: assert table[record_index][field_index] == original_value assert record[field_index] == string assert some_record_copy[field_index] == original_value # is the same as the copy? (the above may succeed and below fail, # but not the opposite; i.e. below is strictly stronger than above, # but test is intended to be granular) assert dict(some_record) == dict(some_record_copy) assert dict(table[record_index]) == dict(some_record) assert dict(table[record_index]) == dict(some_record_copy)
def assert_record_has_changed( table_info: CommaTableTestingExtrasType, modified_string: typing.Optional[str] = None, ): with MapGetter(table_info) as info: from info import table, record_index, field_index, original_value from info import some_record, some_record_copy # integrity: is the isolated record consistent with main record? assert table[record_index][field_index] == some_record[field_index] # correctness: has the isolated record been modified? if modified_string is not None: assert table[record_index][field_index] == modified_string # does it differ from the copy? assert dict(some_record) != dict(some_record_copy) assert dict(table[record_index]) != dict(some_record_copy) # check if copy still has original value if original_value is not None: assert some_record_copy[field_index] == original_value
def test_mapgetter_works_with_default_function_without_parameters(): with MapGetter(default=lambda: "baz") as m: from m import foo, bar assert foo == "baz" assert bar == "baz"
def test_mapgetter_works_with_default_value(): with MapGetter(default=None) as m: from m import n, o, p assert n is None assert o is None assert p is None
def test_mapgetter_creates_local_variables(): a = dict(b=1, c=2) with MapGetter(a) as a: from a import b, c assert b == 1 and c == 2
def test_regular_import_works_from_within_(): a = dict(b=1, c=2) with MapGetter(a) as a: from math import cos assert cos
def test_mapgetter_can_use_existing_module_name(): a = dict(b=1, c=2) with MapGetter(a) as math: from math import b, c assert b == 1 and c == 2
def test_mapgetter_can_use_any_name(): a = dict(b=1, c=2) with MapGetter(a) as anyname: from anyname import b, c assert b == 1 and c == 2
def test_mapgetter_can_be_used_with_key_renaming(): a = dict(b=1, c=2) with MapGetter(a) as a: from a import b as d, c as e assert d == 1 and e == 2
def test_mapgetter_works_with_mapping_and_default_parameter(): a = dict(b=1, c=2) with MapGetter(a, default=lambda name: name) as a: from a import b, c, d assert b == 1 and c == 2 and d == 'd'