Esempio n. 1
0
  def test_xml(self):
    def to_bool(val):
      return val == "true"

    root = ET.fromstring(XML_SOURCE)
    events = root.find("response/events")

    event_transform = dict(
      id=T.val(T.xml_attr("id"), conv.to_int),
      name=T.val(T.xml_text("name")),
      date=T.val(T.xml_text("occurrence"), conv.to_date),
      not_held=T.val(T.xml_text("did_not_meet"), to_bool),
      head_count=T.val(T.xml_text("head_count"), conv.accept_none_wrapper(conv.to_int)),
      attendees=T.lst(T.xml_children("attendees/attendee"), T.obj(None, dict(
        id=T.val(T.xml_attr("id"), conv.to_int),
        first_name=T.val(T.xml_text("first_name")),
        last_name=T.val(T.xml_text("last_name"))
      )))
    )

    l = T.get_lst(events, T.xml_children("event"), T.obj(None, event_transform))
    self.assertEqual(len(l), 2)
    e = l[0]
    self.assertEqual(set(e.keys()), {"attendees", "id", "date", "head_count", "not_held", "name"})
    al = e["attendees"]
    self.assertEqual(len(al), 6)
    a = al[0]
    self.assertEqual(set(a.keys()), {"id", "first_name", "last_name"})
Esempio n. 2
0
 def test_accept_none_wrapper(self):
   conv = cv.accept_none_wrapper(cv.to_int)
   self.assertEqual(conv(None), None)
   self.assertEqual(conv("3"), 3)
   self.assertEqual(conv(3), 3)