Пример #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"})
Пример #2
0
  def test_json(self):
    def to_bool(val):
      return val == "true"

    root = json.loads(JSON_SOURCE)

    fund_transform = dict(
      id=T.val(T.json_val("/@id"), conv.to_int),
      created_date=T.val(T.json_val("/createdDate"), conv.to_datetime),
      created_by_user_id=T.val(T.json_val("/createdByPerson/@id"), conv.to_int),
      fund_type=T.obj(T.json_val("/fundType"), dict(
        id=T.val(T.json_val("/@id"), conv.to_int),
        name=T.val(T.json_val("/name")))),
      is_active=T.val(T.json_val("/isActive"), to_bool)
    )

    l = T.get_lst(root, T.json_vals("/funds/fund/*"), T.obj(None, fund_transform, T.flatten({"fund_type"})))
    self.assertEqual(len(l), 2)
    f = l[0]
    self.assertEqual(set(f.keys()), {"created_by_user_id", "created_date", "fund_type_id", "fund_type_name", "id", "is_active"})