def test_first_subfield(): """Create a field with subfields and try to access first subfield ^* .""" f = MasterField(26, "^aParis^bUnesco^c-1965") f2 = MasterField( 24, "Techniques for the measurement of transpiration of individual plants") assert f.a == "Paris", "Failed to access subfield a (f.a)" assert f["a"] == "Paris", "Failed to access subfield a (f['a'])" assert f["*"] == "Paris", "Failed to access first subfield (f['*'])" assert f2["*"] == f2.data, "Failed to compute v24^* == v24.data"
def __setitem__(self, key, value): """Hook that intercepts data insertion and updates automatically accounting fields. """ encoding = safe_encoding(self.mst) try: # try to convert key into int if possible key = int(key) except ValueError: pass self.last_insert_key.append(key) if type(value) in (MasterField, MasterContainerField): # adjust encoding in fields value.encoding = encoding dict.__setitem__(self, key, value) elif type(value) in (list, tuple): # Build an appropriate MasterContainerField dict.__setitem__(self, key, MasterContainerField(key, value, config=self.config)) elif type(value) in (str, unicode): # received a simple string # delegate the operation to standard dict implementation # but create a MasterField instance wrapping the value. dict.__setitem__(self, key, MasterField(key, value, config=self.config)) else: raise ValueError("Invalid type %s for field %s"%(type(value), key))
def test_invalid_string_tag(): """Create a field with a string tag, should signal it as invalid""" try: f = MasterField("bla", "valid data though") except ValueError: pass else: assert False, "Failed to raise exception with invalid tag type"
def assemble_record(pairs): encoding = safe_encoding(self.mst) for key, values in groupby(pairs, lambda x: getslice(x, 0, 1)): key = key[0] fields = [MasterField(tag, data, config=self.config) \ for tag,data in values] if len(fields)==1: # single values are not wrapped in lists new_field = fields[0] else: new_field = MasterContainerField(key, sequence=fields, config=self.config) dict.__setitem__(self, key, new_field)
def test_valid_string_tag(): """Create a field with a integer conversible string tag""" f = MasterField("50", "Incl. bibl.") assert f.tag == 50, "Failed to convert string tag into integer"
def test_access_subfield(): """Create a field with subfields and try to access them.""" f = MasterField(26, "^aParis^bUnesco^c-1965") assert f.a == "Paris", "Failed to access subfield a" assert f.b == "Unesco", "Failed to access subfield b" assert f.c == "-1965", "Failed to access subfield c"
def test_xml(): """Create a field and render as xml.""" f = MasterField(44, "^aXML Text") assert f.to_xml( ) == '<field tag="44"><occ><subfield tag="a">XML Text</subfield></occ></field>', "Error XML."
def test_data_field(): """Create a field with data and test its size.""" f = MasterField(44, "Paper on: <plant physiology><plant transpiration>"+\ "<measurement and instruments>") assert len(f.data) == 78, "Field data does not match expected size."
def test_empty_field(): """Create an empty field just with a tag.""" f = MasterField(1) assert f.tag == 1, "Field tag is not set correctly."