def test_person_address_dict():
    """
    Verify dict operations.
    """
    person1 = Person()
    person1.first = "John"

    person2 = Person()
    person2.first = "Jane"

    address = Address()
    address.city = "Springfield"

    # empty collection
    collection = PersonAddressDict()

    eq_(len(collection), 0)
    ok_("address" not in collection)
    ok_("person" not in collection)
    with assert_raises(KeyError):
        collection["address"]
    with assert_raises(KeyError):
        collection["person"]

    # set item
    collection["person"] = person1

    eq_(len(collection), 1)
    ok_("person" in collection)
    eq_(collection["person"], person1)
    eq_(set(collection.keys()), {"person"})
    eq_(set(collection.values()), {person1})
    eq_(set(collection.items()), {("person", person1)})

    # add item
    collection.add(address)

    eq_(len(collection), 2)
    ok_("address" in collection)
    eq_(collection["address"], address)
    eq_(set(collection.keys()), {"address", "person"})
    eq_(set(collection.values()), {address, person1})
    eq_(set(collection.items()), {("address", address), ("person", person1)})

    # delete item
    del collection["address"]
    eq_(len(collection), 1)
    ok_("address" not in collection)
    with assert_raises(KeyError):
        collection["address"]
    eq_(set(collection.keys()), {"person"})
    eq_(set(collection.values()), {person1})
    eq_(set(collection.items()), {("person", person1)})

    # adding duplicate key
    collection.add(person2)

    eq_(len(collection), 1)
    eq_(collection["person"], person2)
Beispiel #2
0
def test_person_list():
    """
    Verify list operations.
    """
    person1 = Person()
    person1.first = "John"

    person2 = Person()
    person2.first = "Jane"

    # test append and __len__
    person_list = PersonList()
    eq_(len(person_list), 0)
    person_list.append(person1)
    eq_(len(person_list), 1)
    eq_(person1._parent, person_list)

    person_list.append(person2)
    eq_(len(person_list), 2)
    eq_(person2._parent, person_list)

    eq_(person_list.to_xml(),
        """<person-list><person type="object"><first>John</first></person><person type="object"><first>Jane</first></person></person-list>""")  # noqa

    # test __getitem__
    eq_(person_list[0].first, "John")
    eq_(person_list[0]._parent, person_list)
    eq_(person_list[1].first, "Jane")
    eq_(person_list[0]._parent, person_list)

    # test __iter__
    eq_([person.first for person in person_list], ["John", "Jane"])
    ok_(all([person._parent == person_list for person in person_list]))

    # test __delitem__
    with assert_raises(IndexError):
        del person_list[2]
    del person_list[1]
    eq_(len(person_list), 1)
    eq_(person_list.to_xml(),
        """<person-list><person type="object"><first>John</first></person></person-list>""")

    # test __setitem__
    person_list[0] = person2
    eq_(person_list.to_xml(),
        """<person-list><person type="object"><first>Jane</first></person></person-list>""")