def test_select_by_name__no_match(self): """Verify None return value if no match on relationship_type_name. """ rel_type_name = 'eats_{0}'.format(uuid.uuid4()) rel_type = RelationshipType(relationship_type_name=rel_type_name) db.session.add(rel_type) self.reset_session() retrieved_rel_type = RelationshipType.select_by_name('eats_123') self.assertIsNone(retrieved_rel_type, "Expected to find no persisted RelationshipType")
def test_select_by_name(self): """Verify select_by_name method. """ rel_type_name = 'eats_{0}'.format(uuid.uuid4()) rel_type = RelationshipType(relationship_type_name=rel_type_name) db.session.add(rel_type) self.reset_session() retrieved_rel_type = RelationshipType.select_by_name(rel_type_name) self.assertIsNotNone(retrieved_rel_type, "Expected to find persisted RelationshipType") self.assertEqual(rel_type_name, retrieved_rel_type.relationship_type_name)
def test_relationship__relationship_types__multiple(self): """Verify relationship_types and relationship_type_names with multiple matches. """ rel_id = uuid.uuid4() rel_type_id = uuid.uuid4() rel_type_names = [ '{0}_{1}'.format(n, uuid.uuid4()) for n in ['has', 'has a', 'hasa', 'have'] ] for name in rel_type_names: rel_type = RelationshipType(relationship_type_id=rel_type_id, relationship_type_name=name) db.session.add(rel_type) relationship = Relationship(relationship_id=rel_id, subject_id=uuid.uuid4(), object_id=uuid.uuid4(), relationship_type_id=rel_type_id) db.session.add(relationship) self.reset_session() # Verify RelationshipTypes were persisted retrieved_rel_types = db.session.query(RelationshipType).filter_by( relationship_type_id=rel_type_id).all() self.assertEqual(len(rel_type_names), len(retrieved_rel_types), "Expected to find persisted RelationshipTypes") # Verify Relationship retrieved_rel = db.session.query(Relationship).filter_by( relationship_id=rel_id).first() self.assertEqual(len(retrieved_rel.relationship_types), len(rel_type_names)) self.assertEqual(set(rel_type_names), set(retrieved_rel.relationship_type_names))
def _get_relationship_type(self, relationship_type_name): """Return RelationshipType with specified name and, if new, arbitrary id. """ rel_type = db.session.query(RelationshipType).filter_by( relationship_type_name=relationship_type_name).first() if not rel_type: rel_type = RelationshipType( relationship_type_id=uuid.uuid4(), relationship_type_name=relationship_type_name) return rel_type
def test_delete_relationship(self): """Verify that deleting Relationship leaves Concepts and RelationshipType intact. """ rel_id = uuid.uuid4() subject_concept = self._get_concept('flat') object_concept = self._get_concept('shoe') rel_type = RelationshipType(relationship_type_id=uuid.uuid4(), relationship_type_name='is_{0}'.format( uuid.uuid4())) relationship = Relationship(relationship_id=rel_id, subject=subject_concept, object=object_concept, relationship_types=[rel_type]) db.session.add(relationship) self.reset_session() # Verify Relationship, Concepts and RelationshipType retrieved_rel = db.session.query(Relationship).filter_by( relationship_id=rel_id).one() self.assertIsNotNone(retrieved_rel) self.assertIsNotNone( db.session.query(RelationshipType).filter_by( relationship_type_name=rel_type.relationship_type_name).one()) self.assertIsNotNone( db.session.query(Concept).filter_by( concept_id=subject_concept.concept_id).one()) self.assertIsNotNone( db.session.query(Concept).filter_by( concept_id=object_concept.concept_id).one()) # Delete Relationship db.session.delete(retrieved_rel) self.reset_session() # Verify Relationship is gone self.assertIsNone( db.session.query(Relationship).filter_by( relationship_id=rel_id).first()) # Verify Concepts and RelationshipType remain self.assertIsNotNone( db.session.query(RelationshipType).filter_by( relationship_type_name=rel_type.relationship_type_name).one()) self.assertIsNotNone( db.session.query(Concept).filter_by( concept_id=subject_concept.concept_id).one()) self.assertIsNotNone( db.session.query(Concept).filter_by( concept_id=object_concept.concept_id).one())
def test_relationship_type__default_id(self): """Verify that relationship_type_id is assigned when RelationshipType is persisted. """ rel_type_name = 'eats_{0}'.format(uuid.uuid4()) rel_type = RelationshipType(relationship_type_name=rel_type_name) db.session.add(rel_type) self.reset_session() retrieved_rel_type = db.session.query(RelationshipType).filter_by( relationship_type_name=rel_type_name).first() self.assertIsNotNone(retrieved_rel_type, "Expected to find persisted RelationshipType") self.assertEqual(rel_type_name, retrieved_rel_type.relationship_type_name) self.assertTrue( isinstance(retrieved_rel_type.relationship_type_id, uuid.UUID))
def test_relationship_type(self): """Verify creation with relationship_type_name and relationship_type_id. """ rel_type_name = 'eats_{0}'.format(uuid.uuid4()) rel_type_id = uuid.uuid4() rel_type = RelationshipType(relationship_type_name=rel_type_name, relationship_type_id=rel_type_id) db.session.add(rel_type) self.reset_session() retrieved_rel_type = db.session.query(RelationshipType).filter_by( relationship_type_id=rel_type_id).first() self.assertIsNotNone(retrieved_rel_type, "Expected to find persisted RelationshipType") self.assertEqual(rel_type_name, retrieved_rel_type.relationship_type_name) self.assertEqual(rel_type_id, retrieved_rel_type.relationship_type_id)
def test_relationship__relationship_types__new_relationship_and_type(self): """Verify that new Relationship and RelationshipType does not delete existing match. """ rel_id = uuid.uuid4() rel_type_id = uuid.uuid4() rel_type_names = [ '{0}_{1}'.format(n, uuid.uuid4()) for n in ['is', 'isa'] ] relationship_types = [ RelationshipType(relationship_type_id=rel_type_id, relationship_type_name=name) for name in rel_type_names ] # Save one RelationshipType and verify db.session.add(relationship_types[0]) self.reset_session() rel_types = db.session.query(RelationshipType).filter_by( relationship_type_id=rel_type_id).all() self.assertEqual(1, len(rel_types)) self.reset_session() # New Relationship with new RelationshipType with same relationship_type_id. relationship = Relationship(relationship_id=rel_id, subject_id=uuid.uuid4(), object_id=uuid.uuid4(), relationship_types=[relationship_types[1]]) db.session.add(relationship) self.reset_session() # Verify both RelationshipTypes exist rel_types = db.session.query(RelationshipType).filter_by( relationship_type_id=rel_type_id).all() self.assertEqual(2, len(rel_types)) self.reset_session() # Verify Relationship has both names retrieved_rel = db.session.query(Relationship).filter_by( relationship_id=rel_id).first() self.assertEqual(len(retrieved_rel.relationship_types), len(rel_type_names)) self.assertEqual(set(rel_type_names), set(retrieved_rel.relationship_type_names))
def test_relationship__relationship_types(self): """Verify relationship_types relation and relationship_type_names association proxy. """ rel_id = uuid.uuid4() rel_type = RelationshipType(relationship_type_id=uuid.uuid4(), relationship_type_name='is_{0}'.format( uuid.uuid4())) relationship = Relationship(relationship_id=rel_id, subject_id=uuid.uuid4(), object_id=uuid.uuid4(), relationship_types=[rel_type]) db.session.add(relationship) self.reset_session() # Verify Relationship was persisted retrieved_rel = db.session.query(Relationship).filter_by( relationship_id=rel_id).first() self.assertIsNotNone(retrieved_rel, "Expected to find persisted Relationship") self.assertEqual(rel_type.relationship_type_id, retrieved_rel.relationship_type_id) self.assertEqual( rel_type.relationship_type_id, retrieved_rel.relationship_types[0].relationship_type_id) self.assertEqual( rel_type.relationship_type_name, retrieved_rel.relationship_types[0].relationship_type_name) self.assertEqual(rel_type.relationship_type_name, retrieved_rel.relationship_type_names[0]) self.reset_session() # Verify RelationshipType was persisted as well retrieved_rel_type = db.session.query(RelationshipType).filter_by( relationship_type_id=rel_type.relationship_type_id).first() self.assertIsNotNone(retrieved_rel_type, "Expected to find persisted RelationshipType")