def test_moving_section(self): self.map_rule4() src = parse(""" s1[t1] - p2 mapping [T2:P1] s2[t2] mapping [T2] """) map = mapping.create_mapping(src) # now move s1 -> s2[t2] cmd = commands.MoveObject(obj=src['s1'], dst=src['s2']) cmd() # now src should look like this dst = parse(""" s2[t2] mapping [T2] - s1[t1] - p2 mapping [T2:P1] """) self.check(src, dst, map=False) # and the mapping should have been updated, so that # it is equal to executing mapping directly dst = mapping.create_mapping(dst) self.check(map, dst, map=False) cmd.undo() # here again we lose the order dst = parse(""" s2[t2] mapping [T2] s1[t1] - p2 mapping [T2:P1] """) dst = mapping.create_mapping(dst) self.check(map, dst, map=False)
def test_editing(self): self.map_rule4() src = parse(""" s1[t1] - p2 mapping [T2:P1] s2[t2] mapping [T2] """) map = mapping.create_mapping(src) src['s1'].properties['p2'].mapping = 'map#T3:P1' dst = parse(""" s1[t1] - S3[T3] - P1 s2[T2] """) self.check(map, dst, do_map=False) src['s1'].properties['p2'].mapping = 'map#T2:P1' dst = parse(""" s1[t1] s2[T2] - P1 """) self.check(map, dst, do_map=False) with self.assertRaises(mapping.MappingError): src['s2'].mapping = 'map#T3' src['s1'].properties['p2'].mapping = '' dst = parse(""" s1[t1] - p2 s2[T2] """) self.check(map, dst, do_map=False) src['s1'].mapping = 'map#T1' # this currently changes the order of the sections in the mapping # however the resulting document should still be fine dst = parse(""" s2[T2] s1[T1] - p2 """) self.check(map, dst, do_map=False) # see above if this fails
def check(self, src, dst, map=True): """ check if the mappingn of *src* is equivalent to *dst* if map is False, just compare src to dst, assuming the mapping has already been applied manually """ if map: map = mapping.create_mapping(src) else: map = src if map != dst: dumper.dumpDoc(map) print "---- vs ----" dumper.dumpDoc(dst) self.assertEqual(map, dst) self.assertEqual(dst, map) # do the vice versa test too return map
def test_moving_property(self): """ moving a mapped property should unmap it, move it and then remap it """ self.map_rule4() src = parse(""" s1[t1] - p2 mapping [T2:P1] s2[t2] mapping [T2] """) map = mapping.create_mapping(src) # now move p2 -> s2[t2] cmd = commands.MoveObject(obj=src['s1'].properties['p2'], dst=src['s2']) cmd() dst = parse(""" s1[t1] s2[T2] - P1 """) self.check(map, dst, map=False)
def odML_mapped_document_be_valid(doc): """ try to create a mapping of the document and if that succeeds validate the mapped document according to the validation rules """ if mapping.proxy is not None and isinstance(doc, mapping.proxy.Proxy): return # don't try to map already mapped documents # first check if any object has a mapping attribute for sec in doc.itersections(recursive=True): if sec.mapping is not None: break for prop in sec.properties: if prop.mapping is not None: break else: # no break in the loop, continue with next section continue break # found a mapping can stop searching else: return # no mapping found mdoc = doc._active_mapping if mdoc is not None: mapping.unmap_document(doc) mdoc = None # TODO: if mdoc is set there is already a mapping present. However, this # TODO may have been corrupted by user interaction, thus we should actually # TODO unmap the document, create a new one and then remap the original one try: if mdoc is None: mdoc = mapping.create_mapping(doc) except mapping.MappingError as e: yield ValidationError(doc, 'mapping: %s' % str(e), 'error') return v = Validation(mdoc) for err in v.errors: err.mobj = err.obj err.obj = mapping.get_object_from_mapped_equivalent(err.obj) err.msg = "mapping: " + err.msg yield err