def type_check(self): fields = set(self.fields.keys()) for field in self.data_type.fields: if field.field_name in fields: fields.remove(field) else: raise FixtureTypeCheckError("field %s not in fixture data %s" % (field.field_name, self.get_id)) if fields: raise FixtureTypeCheckError("fields %s from fixture data %s not in fixture data type" % (', '.join(fields), self.get_id))
def type_check(self): fields = set(self.fields.keys()) for field in self.data_type.fields: if field.field_name in fields: fields.remove(field) else: raise FixtureTypeCheckError( f"field {field.field_name} not in fixture data {self.get_id}" ) if fields: raise FixtureTypeCheckError( f"fields {', '.join(fields)} from fixture data {self.get_id} not in fixture data type" )
def to_xml(self): def _serialize(val): return unicode(val) if isinstance(val, (int, Decimal)) else val xData = ElementTree.Element(self.data_type.tag) for attribute in self.data_type.item_attributes: try: xData.attrib[attribute] = _serialize(self.item_attributes[attribute]) except KeyError as e: # This should never occur, buf if it does, the OTA restore on mobile will fail and # this error would have been raised and email-logged. raise FixtureTypeCheckError( "Table with tag %s has an item with id %s that doesn't have an attribute as defined in its types definition" % (self.data_type.tag, self.get_id) ) for field in self.data_type.fields: if not self.fields.has_key(field.field_name): xField = ElementTree.SubElement(xData, field.field_name) xField.text = "" else: for field_with_attr in self.fields[field.field_name].field_list: xField = ElementTree.SubElement(xData, field.field_name) xField.text = field_with_attr.field_value or "" for attribute in field_with_attr.properties: val = field_with_attr.properties[attribute] xField.attrib[attribute] = _serialize(val) return xData
def to_xml(self): xData = ElementTree.Element(self.data_type.tag) for attribute in self.data_type.item_attributes: try: xData.attrib[attribute] = serialize( self.item_attributes[attribute]) except KeyError as e: # This should never occur, buf if it does, the OTA restore on mobile will fail and # this error would have been raised and email-logged. raise FixtureTypeCheckError( "Table with tag %s has an item with id %s that doesn't have an attribute as defined in its types definition" % (self.data_type.tag, self.get_id)) for field in self.data_type.fields: escaped_field_name = clean_fixture_field_name(field.field_name) if field.field_name not in self.fields: xField = ElementTree.SubElement(xData, escaped_field_name) xField.text = "" else: for field_with_attr in self.fields[ field.field_name].field_list: xField = ElementTree.SubElement(xData, escaped_field_name) xField.text = serialize(field_with_attr.field_value) for attribute in field_with_attr.properties: val = field_with_attr.properties[attribute] xField.attrib[attribute] = serialize(val) return xData