Beispiel #1
0
    def test_delete(self):

        class MyObj(Record):
            foo = Property()
            bar = Property()

        class OtherObj(Record):
            objs = ListProperty(of=MyObj)

        mo = MyObj(foo="bar")
        fs1 = FieldSelector(["foo"])
        self.assertEqual(fs1.get(mo), "bar")
        fs1.delete(mo)
        self.assertRaises(AttributeError, fs1.get, mo)

        oo = OtherObj(objs=[{"foo": "bar"}, {"foo": "frop"}])

        fs2 = FieldSelector(["objs", 0, "foo"])
        self.assertEqual(fs2.get(oo), "bar")
        fs2.delete(oo)
        self.assertRaises(AttributeError, fs2.get, mo)
        fs2.put(oo, "hey")

        fs3 = FieldSelector(["objs", None, "foo"])
        self.assertEqual(fs3.get(oo), ["hey", "frop"])
        fs3.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{}, {}]))

        oo = OtherObj(objs=[{"foo": "baz"}, {"bar": "quux"}])
        self.assertEqual(fs3.get(oo), ["baz", None])
        fs3.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{}, {"bar": "quux"}]))

        fs4 = FieldSelector(["objs", None, "bar", "foo"])
        self.assertRaises(FieldSelectorException, fs4.get, oo)

        oo = OtherObj(objs=[{"foo": "baz"}, {"bar": MyObj(foo="bob")}])
        self.assertEqual(fs4.get(oo), [None, "bob"])
        fs4.delete(oo)
        self.assertRaises(FieldSelectorException, fs4.get, oo)

        fs5 = FieldSelector(["objs", 1])
        fs5.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{"foo": "baz"}]))

        fs6 = FieldSelector(["objs", None])
        fs6.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[]))
    def test_dict(self):
        from normalize.coll import dict_of
        from testclasses import Person
        Rolodeck = dict_of(Person)

        deck = Rolodeck({
            "bob": Person(id=123, name="Bob"),
            "peter": Person(id=124, name="Peter"),
            "eve": Person(id=125, name="Steve"),
        })

        fs1 = FieldSelector(["bob", "id"])
        self.assertEqual(fs1.get(deck), 123)
        self.assertRaisesRegexp(ValueError, r'id is required', fs1.delete,
                                deck)

        fs2 = FieldSelector(["cuthbert", "id"])
        fs2.post(deck, 923)
        self.assertEqual(deck['cuthbert'].id, 923)
        FieldSelector(["ruth"]).post(deck, {"id": 523, "name": "Ruth"})
        self.assertEqual(deck['ruth'].name, "Ruth")

        FieldSelector(["bob"]).delete(deck)
        self.assertNotIn("bob", deck)

        FieldSelector([None]).delete(deck)
        self.assertEqual(deck, {})
Beispiel #3
0
    def test_dict(self):
        from normalize.coll import dict_of
        from testclasses import Person
        Rolodeck = dict_of(Person)

        deck = Rolodeck({
            "bob": Person(id=123, name="Bob"),
            "peter": Person(id=124, name="Peter"),
            "eve": Person(id=125, name="Steve"),
        })

        fs1 = FieldSelector(["bob", "id"])
        self.assertEqual(fs1.get(deck), 123)
        self.assertRaisesRegexp(
            ValueError, r'id is required', fs1.delete, deck,
        )

        fs2 = FieldSelector(["cuthbert", "id"])
        fs2.post(deck, 923)
        self.assertEqual(deck['cuthbert'].id, 923)
        FieldSelector(["ruth"]).post(deck, {"id": 523, "name": "Ruth"})
        self.assertEqual(deck['ruth'].name, "Ruth")

        FieldSelector(["bob"]).delete(deck)
        self.assertNotIn("bob", deck)

        FieldSelector([None]).delete(deck)
        self.assertEqual(deck, {})
    def test_delete(self):
        class MyObj(Record):
            foo = Property()
            bar = Property()

        class OtherObj(Record):
            objs = ListProperty(of=MyObj)

        mo = MyObj(foo="bar")
        fs1 = FieldSelector(["foo"])
        self.assertEqual(fs1.get(mo), "bar")
        fs1.delete(mo)
        self.assertRaises(AttributeError, fs1.get, mo)

        oo = OtherObj(objs=[{"foo": "bar"}, {"foo": "frop"}])

        fs2 = FieldSelector(["objs", 0, "foo"])
        self.assertEqual(fs2.get(oo), "bar")
        fs2.delete(oo)
        self.assertRaises(AttributeError, fs2.get, mo)
        fs2.put(oo, "hey")

        fs3 = FieldSelector(["objs", None, "foo"])
        self.assertEqual(fs3.get(oo), ["hey", "frop"])
        fs3.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{}, {}]))

        oo = OtherObj(objs=[{"foo": "baz"}, {"bar": "quux"}])
        self.assertEqual(fs3.get(oo), ["baz", None])
        fs3.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{}, {"bar": "quux"}]))

        fs4 = FieldSelector(["objs", None, "bar", "foo"])
        self.assertRaises(FieldSelectorException, fs4.get, oo)

        oo = OtherObj(objs=[{"foo": "baz"}, {"bar": MyObj(foo="bob")}])
        self.assertEqual(fs4.get(oo), [None, "bob"])
        fs4.delete(oo)
        self.assertRaises(FieldSelectorException, fs4.get, oo)

        fs5 = FieldSelector(["objs", 1])
        fs5.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[{"foo": "baz"}]))

        fs6 = FieldSelector(["objs", None])
        fs6.delete(oo)
        self.assertEqual(oo, OtherObj(objs=[]))
    def test_get(self):
        record = MockJsonRecord({
            "age": 5,
            "children": [{
                "name": "foo"
            }, {
                "name": "bar"
            }],
            "name": "case1",
        })
        # test valid field selectors
        fs = FieldSelector(["name"])
        self.assertEqual(fs.get(record), "case1")
        fs = FieldSelector(["age"])
        self.assertEqual(fs.get(record), 5)
        fs = FieldSelector(["children"])
        self.assertEqual(len(fs.get(record)), 2)
        fs = FieldSelector(["children", 1])
        self.assertEqual(fs.get(record).name, "bar")
        self.assertEqual(fs.get(record), MockChildRecord({"name": "bar"}))
        fs = FieldSelector(["children", 1, "name"])
        self.assertEqual(fs.get(record), "bar")
        fs = FieldSelector(["children", None, "name"])
        self.assertEqual(fs.get(record), ["foo", "bar"])

        # test invalid selectors
        fs = FieldSelector(["bad_name"])  # bad property name
        with self.assertRaisesRegexp(
                FieldSelectorException, "Could not find property specified "
                "by name: bad_name"):
            fs.get(record)
        fs = FieldSelector(["children", 10])  # bad index
        with self.assertRaisesRegexp(
                FieldSelectorException, "Could not find Record specified by "
                "index: 10"):
            fs.get(record)
        # bad nested property name
        fs = FieldSelector(["children", 1, "bad_name"])
        with self.assertRaisesRegexp(
                FieldSelectorException, "Could not find property specified "
                "by name: bad_name"):
            fs.get(record)

        # bad property name
        fs = FieldSelector(["bad_name"])
        with self.assertRaises(AttributeError):
            fs.get(record)
        # bad index
        fs = FieldSelector(["children", 10])
        with self.assertRaises(KeyError):
            fs.get(record)
        # bad nested property name
        fs = FieldSelector(["children", 1, "bad_name"], )
        with self.assertRaises(AttributeError):
            fs.get(record)
Beispiel #6
0
    def test_get(self):
        record = MockJsonRecord(
            {
                "age": 5,
                "children": [{"name": "foo"}, {"name": "bar"}],
                "name": "case1",
            }
        )
        # test valid field selectors
        fs = FieldSelector(["name"])
        self.assertEqual(fs.get(record), "case1")
        fs = FieldSelector(["age"])
        self.assertEqual(fs.get(record), 5)
        fs = FieldSelector(["children"])
        self.assertEqual(len(fs.get(record)), 2)
        fs = FieldSelector(["children", 1])
        self.assertEqual(fs.get(record).name, "bar")
        self.assertEqual(fs.get(record), MockChildRecord({"name": "bar"}))
        fs = FieldSelector(["children", 1, "name"])
        self.assertEqual(fs.get(record), "bar")
        fs = FieldSelector(["children", None, "name"])
        self.assertEqual(fs.get(record), ["foo", "bar"])

        # test invalid selectors
        fs = FieldSelector(["bad_name"])  # bad property name
        with self.assertRaisesRegexp(
            FieldSelectorException, "Could not find property specified "
            "by name: bad_name"
        ):
            fs.get(record)
        fs = FieldSelector(["children", 10])  # bad index
        with self.assertRaisesRegexp(
            FieldSelectorException, "Could not find Record specified by "
            "index: 10"
        ):
            fs.get(record)
        # bad nested property name
        fs = FieldSelector(["children", 1, "bad_name"])
        with self.assertRaisesRegexp(
            FieldSelectorException, "Could not find property specified "
            "by name: bad_name"
        ):
            fs.get(record)

        # bad property name
        fs = FieldSelector(["bad_name"])
        with self.assertRaises(AttributeError):
            fs.get(record)
        # bad index
        fs = FieldSelector(["children", 10])
        with self.assertRaises(KeyError):
            fs.get(record)
        # bad nested property name
        fs = FieldSelector(
            ["children", 1, "bad_name"],
        )
        with self.assertRaises(AttributeError):
            fs.get(record)