def testCardinalities(self): schema = Schema() schema.addEntity('A') schema.addEntity('B') TestUtil.assertRaisesMessage( self, Exception, "Bad cardinality for entity1 or entity2: xxxx one. Should be either Schema.ONE or Schema.MANY", schema.addRelationship, 'AB', ('A', 'xxxx'), ('B', Schema.ONE)) TestUtil.assertRaisesMessage( self, Exception, "Bad cardinality for entity1 or entity2: many xxxx. Should be either Schema.ONE or Schema.MANY", schema.addRelationship, 'AB', ('A', Schema.MANY), ('B', 'xxxx')) TestUtil.assertRaisesMessage( self, Exception, "Bad cardinality for entity1 or entity2: xxxx many. Should be either Schema.ONE or Schema.MANY", schema.addRelationship, 'AB', ('A', 'xxxx'), ('B', Schema.MANY)) TestUtil.assertRaisesMessage( self, Exception, "Bad cardinality for entity1 or entity2: one xxxx. Should be either Schema.ONE or Schema.MANY", schema.addRelationship, 'AB', ('A', Schema.ONE), ('B', 'xxxx')) schema.addRelationship('AB', ('A', Schema.ONE), ('B', Schema.ONE)) ab = schema.getRelationship('AB') self.assertEqual(Schema.ONE, ab.getCardinality('A')) self.assertEqual(Schema.ONE, ab.getCardinality('B')) schema.addEntity('C') schema.addRelationship('BC', ('B', Schema.ONE), ('C', Schema.MANY)) bc = schema.getRelationship('BC') self.assertEqual(Schema.ONE, bc.getCardinality('B')) self.assertEqual(Schema.MANY, bc.getCardinality('C')) TestUtil.assertRaisesMessage( self, Exception, "Entity 'A' does not exist for relationship 'BC'", bc.getCardinality, 'A')
def testGetRelationshipsForEntity(self): schema = Schema() schema.addEntity('A') TestUtil.assertUnorderedListEqual( self, [], schema.getRelationshipsForEntity('A')) schema.addEntity('B') TestUtil.assertUnorderedListEqual( self, [], schema.getRelationshipsForEntity('B')) schema.addRelationship('AB', ('A', Schema.ONE), ('B', Schema.ONE)) ab = schema.getRelationship('AB') TestUtil.assertUnorderedListEqual( self, [ab], schema.getRelationshipsForEntity('A')) TestUtil.assertUnorderedListEqual( self, [ab], schema.getRelationshipsForEntity('B')) schema.addEntity('C') schema.addRelationship('BC', ('B', Schema.ONE), ('C', Schema.ONE)) bc = schema.getRelationship('BC') TestUtil.assertUnorderedListEqual( self, [ab], schema.getRelationshipsForEntity('A')) TestUtil.assertUnorderedListEqual( self, [ab, bc], schema.getRelationshipsForEntity('B')) TestUtil.assertUnorderedListEqual( self, [bc], schema.getRelationshipsForEntity('C')) # test for bad entity names TestUtil.assertRaisesMessage(self, Exception, "Entity 'XX' does not exist", schema.getRelationshipsForEntity, 'XX')
def testDuplicateAttrNames(self): schemaWithEntityA = Schema() schemaWithEntityA.addEntity('A') schemaWithEntityA.addAttribute('A', 'X', Attribute.INTEGER) TestUtil.assertRaisesMessage( self, Exception, "Schema already has attribute named 'X' for item 'A'", schemaWithEntityA.addAttribute, 'A', 'X', Attribute.INTEGER) schemaWithEntityA.addEntity('B') schemaWithEntityA.addRelationship('AB', ('A', Schema.ONE), ('B', Schema.ONE)) schemaWithEntityA.addAttribute('AB', 'XY', Attribute.INTEGER) TestUtil.assertRaisesMessage( self, Exception, "Schema already has attribute named 'XY' for item 'AB'", schemaWithEntityA.addAttribute, 'AB', 'XY', Attribute.INTEGER) a = schemaWithEntityA.getEntity('A') TestUtil.assertRaisesMessage( self, Exception, "Schema already has attribute named 'X' for item 'A'", a.addAttribute, 'X', Attribute.INTEGER) ab = schemaWithEntityA.getRelationship('AB') TestUtil.assertRaisesMessage( self, Exception, "Schema already has attribute named 'XY' for item 'AB'", ab.addAttribute, 'XY', Attribute.INTEGER)
def testGetRelationship(self): schema = Schema() schema.addEntity('A') schema.addEntity('B') schema.addRelationship('AB', ('A', Schema.ONE), ('B', Schema.ONE)) actualRelationship = schema.getRelationship('AB') self.assertEqual('AB', actualRelationship.name) self.assertTrue(schema.hasRelationship('AB')) schema.addEntity('C') schema.addRelationship('BC', ('B', Schema.ONE), ('C', Schema.ONE)) actualRelationship = schema.getRelationship('BC') self.assertEqual('BC', actualRelationship.name) self.assertTrue(schema.hasRelationship('BC')) TestUtil.assertRaisesMessage(self, Exception, "Relationship 'XX' does not exist", schema.getRelationship, 'XX') self.assertFalse(schema.hasRelationship('XX'))
def testOneRelationshipPerPairFlag(self): schema = SchemaGenerator.generateSchema( 2, 1, relationshipAttrDistribution=ConstantDistribution(0), cardinalityDistribution=ConstantDistribution(Schema.ONE), oneRelationshipPerPair=True) expectedSchema = Schema() expectedSchema.addEntity('A') expectedSchema.addEntity('B') expectedSchema.addRelationship('AB', ('A', Schema.ONE), ('B', Schema.ONE)) self.assertEqual([expectedSchema.getRelationship('AB')], schema.getRelationshipsBetweenEntities('A', 'B')) schema = SchemaGenerator.generateSchema( 3, 2, relationshipAttrDistribution=ConstantDistribution(0), entityPairPicker=makePairPickerWithList([('A', 'B'), ('A', 'B'), ('A', 'C')]), cardinalityDistribution=ConstantDistribution(Schema.ONE), oneRelationshipPerPair=True) expectedSchema.addEntity('C') expectedSchema.addRelationship('AC', ('A', Schema.ONE), ('C', Schema.ONE)) self.assertEqual([expectedSchema.getRelationship('AB')], schema.getRelationshipsBetweenEntities('A', 'B')) self.assertEqual([expectedSchema.getRelationship('AC')], schema.getRelationshipsBetweenEntities('A', 'C')) schema = SchemaGenerator.generateSchema( 4, 4, relationshipAttrDistribution=ConstantDistribution(0), entityPairPicker=makePairPickerWithList([('A', 'B'), ('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'D'), ('B', 'C')]), cardinalityDistribution=ConstantDistribution(Schema.ONE), oneRelationshipPerPair=True) expectedSchema.addEntity('D') expectedSchema.addRelationship('AD', ('A', Schema.ONE), ('D', Schema.ONE)) expectedSchema.addRelationship('BC', ('B', Schema.ONE), ('C', Schema.ONE)) self.assertEqual([expectedSchema.getRelationship('AB')], schema.getRelationshipsBetweenEntities('A', 'B')) self.assertEqual([expectedSchema.getRelationship('AC')], schema.getRelationshipsBetweenEntities('A', 'C')) self.assertEqual([expectedSchema.getRelationship('AD')], schema.getRelationshipsBetweenEntities('A', 'D')) self.assertEqual([expectedSchema.getRelationship('BC')], schema.getRelationshipsBetweenEntities('B', 'C'))
def testAddRelationshipAttribute(self): ab = Relationship('AB', ('A', Schema.ONE), ('B', Schema.ONE)) ab.addAttribute('XY1', Attribute.INTEGER) TestUtil.assertUnorderedListEqual( self, ['XY1'], [attr.name for attr in ab.attributes]) ab.addAttribute('XY2', Attribute.INTEGER) TestUtil.assertUnorderedListEqual( self, ['XY1', 'XY2'], [attr.name for attr in ab.attributes]) schema = Schema() schema.addEntity('A') schema.addEntity('B') schema.addRelationship('AB', ('A', Schema.ONE), ('B', Schema.ONE)) schema.addAttribute('AB', 'XY1', Attribute.INTEGER) TestUtil.assertUnorderedListEqual( self, ['XY1'], [attr.name for attr in schema.getRelationship('AB').attributes]) schema.addAttribute('AB', 'XY2', Attribute.INTEGER) TestUtil.assertUnorderedListEqual( self, ['XY1', 'XY2'], [attr.name for attr in schema.getRelationship('AB').attributes])
def testSchemaToJsonDict(self): schema = Schema() jsonDict = schema.toJsonDict() self.assertEqual({'entities': [], 'relationships': []}, jsonDict) schema.addEntity('A') schema.addEntity('B') schema.addEntity('C') schema.addRelationship('AB', ('A', Schema.ONE), ('B', Schema.MANY)) schema.addRelationship('BC', ('B', Schema.MANY), ('C', Schema.MANY)) schema.addAttribute('A', 'X', Attribute.INTEGER) schema.addAttribute('B', 'Y', Attribute.INTEGER) schema.addAttribute('C', 'Z', Attribute.INTEGER) schema.addAttribute('AB', 'XY1', Attribute.INTEGER) schema.addAttribute('AB', 'XY2', Attribute.INTEGER) schema.addAttribute('BC', 'YZ', Attribute.INTEGER) jsonDict = schema.toJsonDict() TestUtil.assertUnorderedListEqual(self, ['entities', 'relationships'], list(jsonDict.keys())) for entJsonDict in jsonDict['entities']: TestUtil.assertUnorderedListEqual(self, ['name', 'attributes'], list(entJsonDict.keys())) if entJsonDict['name'] == 'A': entJsonAttrList = entJsonDict['attributes'] self.assertEqual(1, len(entJsonAttrList)) self.assertEqual({ 'name': 'X', 'dataType': Attribute.INTEGER }, entJsonAttrList[0]) elif entJsonDict['name'] == 'B': entJsonAttrList = entJsonDict['attributes'] self.assertEqual(1, len(entJsonAttrList)) self.assertEqual({ 'name': 'Y', 'dataType': Attribute.INTEGER }, entJsonAttrList[0]) elif entJsonDict['name'] == 'C': entJsonAttrList = entJsonDict['attributes'] self.assertEqual(1, len(entJsonAttrList)) self.assertEqual({ 'name': 'Z', 'dataType': Attribute.INTEGER }, entJsonAttrList[0]) else: self.fail("Unknown entity name") for relJsonDict in jsonDict['relationships']: TestUtil.assertUnorderedListEqual( self, ['name', 'attributes', 'cardinalities'], list(relJsonDict.keys())) if relJsonDict['name'] == 'AB': relJsonAttrList = relJsonDict['attributes'] self.assertEqual(2, len(relJsonAttrList)) self.assertEqual({ 'name': 'XY1', 'dataType': Attribute.INTEGER }, relJsonAttrList[0]) self.assertEqual({ 'name': 'XY2', 'dataType': Attribute.INTEGER }, relJsonAttrList[1]) self.assertEqual( schema.getRelationship('AB').toJsonDict()['cardinalities'], relJsonDict['cardinalities']) elif relJsonDict['name'] == 'BC': relJsonAttrList = relJsonDict['attributes'] self.assertEqual(1, len(relJsonAttrList)) self.assertEqual({ 'name': 'YZ', 'dataType': Attribute.INTEGER }, relJsonAttrList[0]) self.assertEqual( schema.getRelationship('BC').toJsonDict()['cardinalities'], relJsonDict['cardinalities']) else: self.fail("Unknown relationship name")