def test_04_expose_data(self): do = TestDataObj( { "title": "Test Title", "name": "Test Name", "exposed": "Yes", "objy": { "one": "first", "two": "second" }, "listy": [{ "three": "third", "four": "fourth" }] }, expose_data=True) # try accessing the exposed field assert do.exposed == "Yes" # now do a more complex un-structured dataobj data = {"one": {"two": [{"three": "four"}]}} do = dataobj.DataObj(data, expose_data=True) one = do.one
def test_09_silent_prune(self): data = { "one": { "two": "twice", "pruned": "bye" }, "three": "third", "four": [{ "five": 5, "cut": -1 }], "nixed": "outta here" } do = dataobj.DataObj(data, struct={ "fields": { "three": { "coerce": "unicode" } }, "lists": { "four": { "contains": "object" } }, "objects": ["one"], "structs": { "one": { "fields": { "two": { "coerce": "unicode" } } }, "four": { "fields": { "five": { "coerce": "unicode" } } } } }, construct_silent_prune=True, expose_data=True) assert do.one.two == "twice" with self.assertRaises(AttributeError): do.one.pruned assert do.three == "third" with self.assertRaises(AttributeError): do.nixed assert do.four[0].five == "5" with self.assertRaises(AttributeError): do.four[0].cut
def _score_check(reference_issn, o, field, name): if o.get(field) is None: raise ImportException(u"{n} for ISSN {x} is not set".format( n=name, x=reference_issn)) coerce = dataobj.DataObj()._int() try: coerce(o.get(field)) except ValueError: raise ImportException( u"{n} for ISSN {x} is not an integer: {y}".format( n=name, x=reference_issn, y=o.get(field)))
def test_01_delete_with_prune(self): d = dataobj.DataObj() d._set_single("one.two", "value") d._set_single("one.three.four.five", "value") d._delete("one.three.four.five")
def test_06_setattr_no_struct_expose(self): # 2. no struct, expose_data do = dataobj.DataObj( { "one": "first", "two": {}, "three": {}, "four": [], "five": [] }, properties={ "the_name": ("name", None), "theobj": ("theobj", None), "thelist": ("thelist", None), "wrapobj": ("wrapobj", None), "wraplist": ("wraplist", None), "customobj": ("customobj", CustomDO), "customlist": ("customlist", CustomDO), "invalidobj": ("invalidobj", CustomDO), "invalidlist": ("invalidlist", CustomDO) }, expose_data=True) # Setting fields with raw values # 2. A dynamic property (where properties are set) do.the_name = "My Name" # 3. The internal data structure key name (when expose_data is on) do.one = "prime" # Setting an object # a. set a raw value # on a defined property do.theobj = {"one": "first", "two": "second"} # on the internal data do.two = {"a": "b"} # b. set a vanilla DataObj # on a defined property do.wrapobj = dataobj.DataObj({"one": "first", "two": "second"}) # on the internal data do.three = dataobj.DataObj({"a": "b"}) # c. set a custom DataObj that is valid (where properties are set) do.customobj = CustomDO({"one": "first", "two": "second"}) # d. set a custom DataObj that is invalid (where properties are set) with self.assertRaises(AttributeError): do.invalidobj = InvalidDO({"one": "first", "two": "second"}) # Setting a list # a. set a raw value # on a defined property do.thelist = [{"three": "third", "four": "fourth"}] # on the internal data do.four = [{"three": "third", "four": "fourth"}] # b. set a vanilla DataObj # on a defined property do.wraplist = [dataobj.DataObj({"three": "third", "four": "fourth"})] # on the internal data do.five = [dataobj.DataObj({"three": "third", "four": "fourth"})] # c. set a custom DataObj that is valid (where properties are set) do.customlist = [CustomDO({"three": "third", "four": "fourth"})] # d. set a custom DataObj that is invalid (where properties are set) with self.assertRaises(AttributeError): do.invalidlist = [InvalidDO({"three": "third", "four": "fourth"})] # a previously unknown property do.whatever = "hello" # now do our checks assert do.the_name == "My Name" assert do.data.get("name") == "My Name" assert do.one == "prime" assert do.data.get("one") == "prime" assert do.theobj == {"one": "first", "two": "second"} assert do.data.get("theobj") == {"one": "first", "two": "second"} assert isinstance(do.two, dataobj.DataObj) assert do.two.data == {"a": "b"} assert do.two.a == "b" assert do.wrapobj == {"one": "first", "two": "second"} assert do.data.get("wrapobj") == {"one": "first", "two": "second"} assert isinstance(do.three, dataobj.DataObj) assert do.three.data == {"a": "b"} assert do.three.a == "b" assert isinstance(do.customobj, CustomDO) assert do.customobj.data == {"one": "first", "two": "second"} assert do.data.get("customobj") == {"one": "first", "two": "second"} assert "invalidobj" not in do.data assert do.thelist == [{"three": "third", "four": "fourth"}] assert do.data.get("thelist") == [{"three": "third", "four": "fourth"}] assert isinstance(do.four[0], dataobj.DataObj) assert do.four[0].data == {"three": "third", "four": "fourth"} assert do.four[0].four == "fourth" assert do.data.get("four") == [{"three": "third", "four": "fourth"}] assert do.wraplist == [{"three": "third", "four": "fourth"}] assert do.data.get("wraplist") == [{ "three": "third", "four": "fourth" }] assert isinstance(do.five[0], dataobj.DataObj) assert do.five[0].data == {"three": "third", "four": "fourth"} assert do.five[0].three == "third" assert do.data.get("five") == [{"three": "third", "four": "fourth"}] assert isinstance(do.customlist[0], CustomDO) assert do.customlist[0].data == {"three": "third", "four": "fourth"} assert do.data.get("customlist") == [{ "three": "third", "four": "fourth" }] assert "invalidlist" not in do.data assert do.whatever == "hello" assert "whatever" not in do.data
def test_05_setattr_no_struct_no_expose(self): # 1. no struct, no expose_data do = dataobj.DataObj( properties={ "the_name": ("name", None), "theobj": ("theobj", None), "thelist": ("thelist", None), "wrapobj": ("wrapobj", None), "wraplist": ("wraplist", None), "customobj": ("customobj", CustomDO), "customlist": ("customlist", CustomDO), "invalidobj": ("invalidobj", CustomDO), "invalidlist": ("invalidlist", CustomDO) }) # Setting fields with raw values # 2. A dynamic property (where properties are set) do.the_name = "My Name" # Setting an object # a. set a raw value do.theobj = {"one": "first", "two": "second"} # b. set a vanilla DataObj do.wrapobj = dataobj.DataObj({"one": "first", "two": "second"}) # c. set a custom DataObj that is valid (where properties are set) do.customobj = CustomDO({"one": "first", "two": "second"}) # d. set a custom DataObj that is invalid (where properties are set) with self.assertRaises(AttributeError): do.invalidobj = InvalidDO({"one": "first", "two": "second"}) # Setting a list # a. set a raw value do.thelist = [{"three": "third", "four": "fourth"}] # b. set a vanilla DataObj do.wraplist = [dataobj.DataObj({"three": "third", "four": "fourth"})] # c. set a custom DataObj that is valid (where properties are set) do.customlist = [CustomDO({"three": "third", "four": "fourth"})] # d. set a custom DataObj that is invalid (where properties are set) with self.assertRaises(AttributeError): do.invalidlist = [InvalidDO({"three": "third", "four": "fourth"})] # a previously unknown property do.whatever = "hello" # now do our checks assert do.the_name == "My Name" assert do.data.get("name") == "My Name" assert do.theobj == {"one": "first", "two": "second"} assert do.data.get("theobj") == {"one": "first", "two": "second"} assert do.wrapobj == {"one": "first", "two": "second"} assert do.data.get("wrapobj") == {"one": "first", "two": "second"} assert isinstance(do.customobj, CustomDO) assert do.customobj.data == {"one": "first", "two": "second"} assert do.data.get("customobj") == {"one": "first", "two": "second"} assert "invalidobj" not in do.data assert do.thelist == [{"three": "third", "four": "fourth"}] assert do.data.get("thelist") == [{"three": "third", "four": "fourth"}] assert do.wraplist == [{"three": "third", "four": "fourth"}] assert do.data.get("wraplist") == [{ "three": "third", "four": "fourth" }] assert isinstance(do.customlist[0], CustomDO) assert do.customlist[0].data == {"three": "third", "four": "fourth"} assert do.data.get("customlist") == [{ "three": "third", "four": "fourth" }] assert "invalidlist" not in do.data assert do.whatever == "hello" assert "whatever" not in do.data