def test_bool(self): td = make_TD(bool) elt = td.xml_element(True, 'foo') assert XU.str_from_xml_elt(elt) == '<foo>true</foo>' assert td.extract_from(elt, 'foo') is True elt = td.xml_element(False, 'foo') assert XU.str_from_xml_elt(elt) == '<foo>false</foo>' assert td.extract_from(elt, 'foo') is False
def test_type_descriptor(self, td): rect = Rectangle(42, 100) elt = td.xml_element(rect, 'rect') assert XU.str_from_xml_elt(elt) == expected_rect_xml(42, 100) rect_round_trip = td.extract_from(elt, 'rect') assert rect_round_trip == rect
def test_type_descriptor(self, td): val = [42, 100, 99, 123] elt = td.xml_element(val, 'widths') assert (XU.str_from_xml_elt(elt) == '<widths>%s</widths>' % ''.join('<wd>%d</wd>' % x for x in val)) val_round_trip = td.extract_from(elt, 'widths') assert val_round_trip == val
def test_round_trip(self, colour, paint_pot_cls, tag, tag_for_expected): pp = paint_pot_cls(100) pp_xml = pp.as_xml(tag) assert str_from_xml_elt(pp_xml) == self.expected_xml(tag_for_expected) pp1 = paint_pot_cls.from_xml(pp_xml, tag_for_expected) assert type(pp1) is type(pp) assert pp1 == pp
def test_1(self): layout = Layout('dark-blue', [['rounded', 'red'], ['chamfered', 'matt'], ['pointy', 'anodized', 'black'], ['bevelled', 'twisted', 'plastic-coated']], ['red', 'burnt-ochre', 'orange'], np.array([99, 42, 123], dtype=np.uint32), Rectangle(210, 297), np.array([(20, 30), (40, 50)], dtype=RectangleDType)) xml = X.serialize(layout, 'layout') xml_str = XU.str_from_xml_elt(xml) expected_str = remove_whitespace( """<layout> <colour>dark-blue</colour> <corner-properties> <corner> <prop>rounded</prop> <prop>red</prop> </corner> <corner> <prop>chamfered</prop> <prop>matt</prop> </corner> <corner> <prop>pointy</prop> <prop>anodized</prop> <prop>black</prop> </corner> <corner> <prop>bevelled</prop> <prop>twisted</prop> <prop>plastic-coated</prop> </corner> </corner-properties> <stripes> <stripe-colour>red</stripe-colour> <stripe-colour>burnt-ochre</stripe-colour> <stripe-colour>orange</stripe-colour> </stripes> <product-id-codes>99,42,123</product-id-codes> <shape> <width>210</width> <height>297</height> </shape> <components> <rect> <width>20</width> <height>30</height> </rect> <rect> <width>40</width> <height>50</height> </rect> </components> </layout>""") assert xml_str == expected_str X.deserialize(Layout, xml, 'layout')
def test_terse_string_descriptors(self, dtype_str): td = make_TD(dtype_str) tp = np.dtype(dtype_str).type elt = td.xml_element(19, 'foo') assert '<foo>19</foo>' == XU.str_from_xml_elt(elt) val_round_trip = td.extract_from(elt, 'foo') assert tp is type(val_round_trip) assert val_round_trip == 19
def test_tuple_construction(self, descriptor_tup, exp_tag, exp_vslot): elt_descriptor = X.ElementDescriptor.new_from_tuple(descriptor_tup) val = 42 assert elt_descriptor.tag == exp_tag assert elt_descriptor.value_from(self.rect) == val assert elt_descriptor.value_slot == exp_vslot xml_elt = elt_descriptor.xml_element(self.rect) assert XU.str_from_xml_elt(xml_elt) == '<%s>%d</%s>' % (exp_tag, val, exp_tag) round_trip_val = elt_descriptor.extract_from(xml_elt) assert round_trip_val == val
def test_round_trip(self, type_descr, val, exp_xml, tag): assert val.shape == () xml_elt = type_descr.xml_element(val, tag) assert XU.str_from_xml_elt(xml_elt) == exp_xml val_rt = type_descr.extract_from(xml_elt, tag) assert type(val_rt) == np.ndarray assert val_rt.dtype == val.dtype assert val_rt.shape == val.shape assert val_rt == val
def test_type_descriptor(self, td_func): for tp, val, exp_txt in [(int, 42, '42'), (float, 3.5, '3.5'), (str, '3 < 5', '3 < 5'), (np.uint8, np.uint8(42), '42')]: td = td_func(tp) elt = td.xml_element(val, 'foo') assert XU.str_from_xml_elt(elt) == '<foo>%s</foo>' % exp_txt val_round_trip = td.extract_from(elt, 'foo') assert type(val_round_trip) is tp assert val_round_trip == val
def test_type_descriptor(self, td): xml_elt = td.xml_element(self.vals, 'rects') expected_xml = remove_whitespace( """<rects> <rect><width>42</width><height>100</height></rect> <rect><width>99</width><height>12</height></rect> </rects>""") assert XU.str_from_xml_elt(xml_elt) == expected_xml vals_rt = td.extract_from(xml_elt, 'rects') assert vals_rt.dtype == self.vals.dtype assert vals_rt.shape == self.vals.shape assert np.all(vals_rt == self.vals)
def test_nested_elements(self): elt = lxml.etree.Element('foo') child_0 = lxml.etree.Element('bar') child_0.text = '123' elt.append(child_0) child_1 = lxml.etree.Element('baz') child_1.text = '456' elt.append(child_1) assert XmlUtils.str_from_xml_elt(elt) == '<foo><bar>123</bar><baz>456</baz></foo>'
def test_pretty(self): elt = lxml.etree.Element('foo') child_0 = lxml.etree.Element('bar') child_0.text = '123' elt.append(child_0) child_1 = lxml.etree.Element('baz') child_1.text = '456' elt.append(child_1) assert (XmlUtils.str_from_xml_elt(elt, pretty_print=True) == '<foo>\n <bar>123</bar>\n <baz>456</baz>\n</foo>\n')
def test_round_trip(self): from enum import Enum Animal = Enum('Animal', 'Cat Dog Rabbit') class PetDetails(XMLSerializableNamedTuple): xml_descriptor = [('type', Animal), ('weight', float)] pd = PetDetails(Animal.Dog, 42.5) pd_xml = pd.as_xml('pet-details') assert str_from_xml_elt(pd_xml) == ('<pet-details><type>Dog</type>' '<weight>42.5</weight></pet-details>') pd1 = PetDetails.from_xml(pd_xml, 'pet-details') assert pd1 == pd
def test_type_descriptor(self, td): groups = [[1, 2], [3, 4, 5]] elt = td.xml_element(groups, 'stripe-groups') exp_txt = ('<stripe-groups>%s</stripe-groups>' % ''.join(('<stripe-group>%s</stripe-group>' % ''.join('<wd>%d</wd>' % x for x in grp)) for grp in groups)) assert exp_txt == XU.str_from_xml_elt(elt) groups_round_trip = td.extract_from(elt, 'stripe-groups') assert groups == groups_round_trip
def test_round_trip(self): xml_elt = self.type_descr.xml_element(self.vals, 'rect-pairs') expected_xml = remove_whitespace( """<rect-pairs> <rect-pair> <big><width>420</width><height>100</height></big> <small><width>42</width><height>10</height></small> </rect-pair> <rect-pair> <big><width>430</width><height>110</height></big> <small><width>43</width><height>11</height></small> </rect-pair> </rect-pairs>""") assert expected_xml == XU.str_from_xml_elt(xml_elt) vals_rt = self.type_descr.extract_from(xml_elt, 'rect-pairs') assert vals_rt.dtype == self.vals.dtype assert vals_rt.shape == self.vals.shape assert np.all(vals_rt == self.vals)
def test_round_trip(self, tag, tag_for_expected): r = Rectangle(42, 100) r_xml = r.as_xml(tag) assert str_from_xml_elt(r_xml) == self.expected_xml(tag_for_expected) r1 = Rectangle.from_xml(r_xml, tag_for_expected) assert r1 == r
def test_nonempty_element(self): elt = lxml.etree.Element('foo') elt.text = 'hello' assert XmlUtils.str_from_xml_elt(elt) == '<foo>hello</foo>'
def test_1(self): serialized_xml = X.serialize(self.rect, 'rect') assert XU.str_from_xml_elt(serialized_xml) == expected_rect_xml(42, 123) rect_round_trip = X.deserialize(Rectangle, serialized_xml, 'rect') assert rect_round_trip == self.rect
def test_round_trip(self, tag, tag_for_expected): e = Ellipse(42, 99, 'red') e_xml = e.as_xml(tag) assert str_from_xml_elt(e_xml) == self.expected_xml(tag_for_expected) e1 = Ellipse.from_xml(e_xml, tag_for_expected) assert e1 == e
def test_as_xml_str(self, tag, kwargs): if 'pretty_print' not in kwargs: kwargs['pretty_print'] = True p = Pattern(33, [Circle(10, 'blue'), Circle(12, 'red')]) assert str_from_xml_elt(p.as_xml(tag), **kwargs) == p.as_xml_str(tag, **kwargs)
def test_round_trip(self, tag, tag_for_expected): p = Pattern(33, [Circle(10, 'blue'), Circle(12, 'red')]) p_xml = p.as_xml(tag) assert str_from_xml_elt(p_xml) == self.expected_xml(tag_for_expected) p1 = Pattern.from_xml(p_xml, tag_for_expected) assert p1 == p
def test_round_trip(self, tag, tag_for_expected): c = Circle(42, 'orange') c_xml = c.as_xml(tag) assert str_from_xml_elt(c_xml) == self.expected_xml(tag_for_expected) c1 = Circle.from_xml(c_xml, tag_for_expected) assert c1 == c
def test_empty_element(self): elt = lxml.etree.Element('foo') assert XmlUtils.str_from_xml_elt(elt) == '<foo/>'
def test_content(self): xs = np.array([32, 42, 100, 99, -100], dtype=np.int32) elt = self.td.xml_element(xs, 'values') assert XU.str_from_xml_elt(elt) == '<values>32,42,100,99,-100</values>'