本文整理汇总了Python中normalize.FieldSelector类的典型用法代码示例。如果您正苦于以下问题:Python FieldSelector类的具体用法?Python FieldSelector怎么用?Python FieldSelector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FieldSelector类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dict
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, {})
示例2: test_add_full_collection
def test_add_full_collection(self):
fs = FieldSelector(["foo", "bar"])
self.assertEqual(fs.selectors, ["foo", "bar"])
fs.add_full_collection()
self.assertEqual(fs.selectors, ["foo", "bar", None])
self.assertEqual(str(fs), "<FieldSelector: .foo.bar[*]>")
示例3: test_subscripting
def test_subscripting(self):
fs = FieldSelector(("somewhere", "over", "the", "rainbow"))
self.assertEqual(len(fs), 4)
self.assertEqual(fs[-1], "rainbow")
shorter = fs[:-1]
self.assertIsInstance(shorter, FieldSelector)
self.assertEqual(len(shorter), 3)
self.assertTrue(fs.startswith(shorter))
示例4: test_add_index
def test_add_index(self):
fs = FieldSelector(["foo", "bar"])
self.assertEqual(fs.selectors, ["foo", "bar"])
fs.add_index(0)
self.assertEqual(fs.selectors, ["foo", "bar", 0])
with self.assertRaisesRegexp(
ValueError, "index must be an int or a long"
):
fs.add_index(0.0)
self.assertEqual(fs.selectors, ["foo", "bar", 0])
示例5: test_add_property
def test_add_property(self):
fs = FieldSelector(["foo", "bar"])
self.assertEqual(fs.selectors, ["foo", "bar"])
fs.add_property("prop_name")
self.assertEqual(fs.selectors, ["foo", "bar", "prop_name"])
with self.assertRaisesRegexp(
ValueError, "properties must be specified by their string name",
):
fs.add_property({})
self.assertEqual(fs.selectors, ["foo", "bar", "prop_name"])
示例6: test_extend
def test_extend(self):
fs1 = FieldSelector(["foo", "bar"])
fs2 = FieldSelector(["hello", "world"])
# functional extension
self.assertEqual(
(fs1 + fs2).selectors, ["foo", "bar", "hello", "world"],
)
self.assertEqual(str(fs1 + "baz"), "<FieldSelector: .foo.bar.baz>")
self.assertEqual(str(fs1 + ["baz"]), "<FieldSelector: .foo.bar.baz>")
self.assertEqual(str(fs1 + 7), "<FieldSelector: .foo.bar[7]>")
fs3 = fs1.extend(fs2)
# Verify that extend works
self.assertEqual(fs1.selectors, ["foo", "bar", "hello", "world"])
# Verify that extend chaining works
self.assertEqual(fs1, fs3)
示例7: test_put
def test_put(self):
record = MockJsonRecord()
# Test invalid FieldSelector
fs = FieldSelector(["name"])
fs.put(record, "pass")
# self.assertEqual(record.raw_data, {"children": [], 'name': 'pass'})
record = MockComplexJsonRecord()
# Test creation of attribute
fs = FieldSelector(["name"])
fs.put(record, "Bobby")
self.assertEqual(record.name, "Bobby")
# Test creation of collection and sub-Record
fs = FieldSelector(["children", 0, "name"])
with self.assertRaises(FieldSelectorException):
fs.put(record, "Johnny")
示例8: test_path_marshal
def test_path_marshal(self):
for path in (
("foo", "bar"),
(None, ),
(None, "bob", 6, "frop"),
(),
("Bob", "one flew over the cuckoo's nest"),
("Leaning", "toothpick", "syndrome", "\\/\"/"),
(u"\u2620",)
):
fs = FieldSelector(path)
fs_path = fs.path
fs_2 = FieldSelector.from_path(fs_path)
self.assertEqual(fs.path, fs_2.path)
self.assertEqual(fs.selectors, fs_2.selectors)
示例9: test_post_required
def test_post_required(self):
class FussyRecord(Record):
id = Property(required=True)
class FussPot(Record):
fuss_list = ListProperty(of=FussyRecord)
fuss_map = DictProperty(of=FussyRecord)
top_fuss = Property(isa=FussyRecord)
fp = FussPot()
fs1 = FieldSelector(("top_fuss", "id"))
fs2 = FieldSelector(("fuss_map", "phew", "id"))
fs3 = FieldSelector(("fuss_list", 0, "id"))
fs1.post(fp, 1)
fs2.post(fp, 2)
fs3.post(fp, 3)
expected = FussPot(
top_fuss={"id": 1},
fuss_map={"phew": {"id": 2}},
fuss_list=[{"id":3}],
)
示例10: test_post
def test_post(self):
record = MockComplexJsonRecord()
fs = FieldSelector(["name"])
fs.post(record, "Bobby")
self.assertEqual(record.name, "Bobby")
fs = FieldSelector(["children", 0, "name"])
fs.post(record, "Johnny")
self.assertEqual(record.children[0].name, "Johnny")
# Test addition to collection
fs = FieldSelector(["children", 1, "name"])
fs.post(record, "Susan")
self.assertEqual(record.children[1].name, "Susan")
# Test invalid addition to collection
fs = FieldSelector(["children", 9999, "name"])
with self.assertRaisesRegexp(
ValueError, "FieldSelector set out of order"
):
fs.post(record, "Joker")
# Test create of sub-Record
fs = FieldSelector(["nested", "name"])
fs.put(record, "Nested")
self.assertEqual(record.nested.name, "Nested")
示例11: test_get
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)
示例12: test_delete
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=[]))