def test_init(self): f = Field() self.assertIsInstance(f, Field) f = Field(name="LIST", instanciator=list) self.assertEqual(f.name, "LIST") self.assertEqual(f.instance_name, "_LIST") self.assertEqual(f.instanciator, list)
class Repository(WatcherTyping2): name = Field() permission = Field() def __eq__(self, other): if not isinstance(other, Repository): return False return self.name == other.name
class RepositoryPipelinesConfiguration(Typing2): enabled = Field(default=False) def __eq__(self, other): if not isinstance(other, RepositoryPipelinesConfiguration): return False return other.enabled == self.enabled
def test_match(self): f = Field(name="NAME") f.mapping("PRETTY_NAME") self.assertEqual(f.match("NAME"), True) self.assertEqual(f.match("PRETTY_NAME"), True) self.assertEqual(f.match("OTHER"), False)
class Links(Typing2): clone = Field().list_of(inside_instanciator=LinksClone) def find_clone_by_name(self, name): for i in self.clone: if i.name == name: return i return None
class Repository(Typing2): scm = Field(default=Scm.GIT) \ .converter(loads=Scm, dumps=str) project = Field(instanciator=Project) is_private = Field(default=True) name = Field() description = Field() fork_policy = Field(default=ForkPolicy.NO_PUBLIC_FORKS) \ .converter(loads=ForkPolicy, dumps=str) language = Field(default="") links = Field(instanciator=Links)
class PullRequest(Typing2): title = Field() id = Field() close_source_branch = Field().converter(loads=bool) source = Field(instanciator=SrcDst) destination = Field(instanciator=SrcDst) state = Field().converter(loads=State, dumps=str) links = Field(instanciator=Links)
def test_get_inside_instance(self): f = Field(name="NAME") f.list_of(inside_instanciator=dict) value = f.get_inside_instance({"data": "DATA"}, None) self.assertIsInstance(value, dict) self.assertEqual(value, {"data": "DATA"}) f.list_of() value = f.get_inside_instance("data", None) self.assertIsInstance(value, str) self.assertEqual(value, "data")
class GroupPrivilege(Typing2): """ Privilege/group view """ def __get_group_slug_only(self, value): if isinstance(value, dict): return value.get("slug") return value privilege = Field() \ .converter(loads=Privilege, dumps=str) group = Field() \ .getters(__get_group_slug_only) def __eq__(self, other): if not isinstance(other, GroupPrivilege): return False return self.privilege == other.privilege and self.group == other.group
def test_list_of(self): f = Field(name="NAME") f.list_of() self.assertIsNone(f.inside_instanciator) self.assertEqual(f.instanciator, list) self.assertEqual(f.is_list, True) f.list_of(inside_instanciator=dict) self.assertEqual(f.inside_instanciator, dict)
def test_hide(self): f = Field(name="NAME") f.hide() self.assertEqual(f.hidden, True) f.unhide() self.assertEqual(f.hidden, False)
def test_setters(self): f = Field(name="NAME") def func1(instance, value): return value def func2(instance, value): return value f.setters(func1) self.assertEqual(f.setters_funcs[0], func1) f.setters(func2) self.assertEqual(f.setters_funcs[0], func1) self.assertEqual(f.setters_funcs[1], func2) f.setters(clear=True) self.assertEqual(len(f.setters_funcs), 0)
def test_delete(self): f = Field(name="NAME") instance = Typing2() f.direct_set(instance, 10) self.assertEqual(instance._NAME, 10) f.__delete__(instance) self.assertEqual(getattr(instance, "_NAME", "DONOTEXIST"), "DONOTEXIST")
def test_dumps_convert(self): f = Field(name="NAME") f.converter(dumps=int) self.assertIsNone(f.dumps_convert(None)) value = f.dumps_convert("10") self.assertIsInstance(value, int) self.assertEqual(value, 10) value = f.dumps_convert(10) self.assertIsInstance(value, int) self.assertEqual(value, 10) self.assertRaises(ValueError, f.dumps_convert, "not a number")
class BranchRestriction(Typing2): id = Field() kind = Field() \ .converter(loads=Kind, dumps=str) branch_match_kind = Field() \ .converter(loads=BranchMatchKind, dumps=str) branch_type = Field() \ .converter(loads=BranchType, dumps=str) pattern = Field() groups = Field().list_of() users = Field().list_of() value = Field() def post_dumps(self, raw, dump): if self.branch_match_kind != BranchMatchKind.BRANCHING_MODEL: # Do not use it when branch_match_kind is not branching_model. dump.pop("branch_type") def __eq__(self, other): if not isinstance(other, BranchRestriction): return False if len(self.users) != len(other.users) or len(self.groups) != len( other.groups): return False # test users for user in self.users: if user not in other.users: return False # test groups for group in self.groups: if group not in other.groups: return False return \ self.kind == other.kind and \ self.branch_match_kind == other.branch_match_kind and \ self.branch_type == other.branch_type and \ self.pattern == other.pattern and \ self.value == other.value
def test_set(self): f = Field(name="NAME") def func1(instance, value): return value + 10 f.setters(func1) f.setters(func1) instance = Typing2() f.direct_set(instance, 10) self.assertEqual(instance._NAME, 10) f.__set__(instance, 40) self.assertEqual(instance._NAME, 60) f.setters(clear=True) f.__set__(instance, 40) self.assertEqual(instance._NAME, 40)
def test_get(self): f = Field(name="NAME") def func1(instance, value): return "OTHER" f.getters(func1) instance = Typing2() instance._NAME = 10 self.assertEqual(f.direct_get(instance), 10) self.assertEqual(f.__get__(instance, None), "OTHER") f.default(33) self.assertEqual(instance._NAME, 10) del instance._NAME self.assertEqual(f.__get__(instance, None), "OTHER") self.assertEqual(instance._NAME, 33) f.getters(clear=True) self.assertEqual(f.__get__(instance, None), 33)
class DeployKey(Typing2): label = Field(default="") key = Field() id = Field()
class Event(Typing2): key = Field() type_ = Field(instanciator=EventType).converter(dumps=str).mapping("type") value = Field()
class PipelineUUID(Typing2): state = Field(instanciator=State) target = Field(instanciator=Target) build_number = Field()
class StateResult(Typing2): name = Field()
def test_converter(self): f = Field(name="NAME") f.converter(loads=int, dumps=str) self.assertEqual(f.loads_converter, int) self.assertEqual(f.dumps_converter, str)
def test_default(self): f = Field(name="NAME", default="name_value") self.assertEqual(f.default_value, "name_value") f.default(None) self.assertEqual(f.default_value, None)
class State(Typing2): name = Field() result = Field(instanciator=StateResult)
class Commit(Typing2): hash = Field()
class Target(Typing2): ref_type = Field() ref_name = Field() commit = Field(instanciator=Commit)
def test_get_name(self): f = Field(name="NAME") f.mapping("PRETTY_NAME") self.assertEqual(f.get_name(), "PRETTY_NAME") self.assertEqual(f.get_name(no_mapping=True), "NAME")
def test_get_instance(self): f = Field(name="NAME") self.assertIsNone(f.get_instance(None)) f.default("coucou") self.assertEqual(f.get_instance(None), "coucou")
def test_mapping(self): f = Field(name="NAME") f.mapping("PRETTY_NAME") self.assertEqual(f.mapping_name, "PRETTY_NAME")
class LinksClone(Typing2): href = Field() name = Field()