Esempio n. 1
0
    def test_relationship__select_by_fact_id(self):
        """Verify select_by_fact_id method finds expected Relationships
        """
        rel_type_ids = [uuid.uuid4(), uuid.uuid4()]
        fact_id = uuid.uuid4()

        # Add two Relationships with fact_id
        for rel_type_id in rel_type_ids:
            db.session.add(
                Relationship(relationship_type_id=rel_type_id,
                             subject_id=uuid.uuid4(),
                             object_id=uuid.uuid4(),
                             fact_id=fact_id))

        # Add another Relationship with a different fact_id
        db.session.add(
            Relationship(relationship_type_id=uuid.uuid4(),
                         subject_id=uuid.uuid4(),
                         object_id=uuid.uuid4(),
                         fact_id=uuid.uuid4()))
        self.reset_session()

        retrieved_rels = Relationship.select_by_fact_id(fact_id)
        self.assertEqual(len(rel_type_ids), len(retrieved_rels))
        for rel in retrieved_rels:
            self.assertEqual(fact_id, rel.fact_id)
Esempio n. 2
0
    def test_concept_types_relationship__multiple(self):
        """Test concept_types on one concept that is two different things.
        """
        high_heel = self._get_concept('high_heel')
        shoe = self._get_concept('shoe')
        safety_hazard = self._get_concept('safety_hazard')
        rel_type = self._get_relationship_type('is')
        relationship = Relationship(subject=high_heel,
                                    object=shoe,
                                    relationship_types=[rel_type])
        db.session.add(relationship)
        relationship = Relationship(subject=high_heel,
                                    object=safety_hazard,
                                    relationship_types=[rel_type])
        db.session.add(relationship)
        self.reset_session()

        # select concept
        high_heel = db.session.query(Concept).filter_by(
            concept_id=high_heel.concept_id).first()
        self.assertIsNotNone(high_heel)

        # check concept_types relationships
        self.assertEqual(set(['shoe', 'safety_hazard']),
                         set(high_heel.concept_types))
        self.assertEqual(
            set([shoe.concept_id, safety_hazard.concept_id]),
            set([
                r.object.concept_id
                for r in high_heel.concept_type_relationships
            ]))
Esempio n. 3
0
    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))
Esempio n. 4
0
    def test_relationship__object_relation(self):
        """Verify object relation on Relationship.
        """
        rel_id = uuid.uuid4()
        object_concept = self._get_concept('flat')
        relationship = Relationship(relationship_id=rel_id,
                                    subject_id=uuid.uuid4(),
                                    object=object_concept,
                                    relationship_type_id=uuid.uuid4())
        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(object_concept.concept_id, retrieved_rel.object_id)
        self.assertEqual(object_concept.concept_id,
                         retrieved_rel.object.concept_id)
        self.assertEqual(object_concept.concept_name,
                         retrieved_rel.object.concept_name)
        self.reset_session()

        # Verify Concept was persisted as well
        retrieved_concept = db.session.query(Concept).filter_by(
            concept_id=object_concept.concept_id).first()
        self.assertIsNotNone(retrieved_concept,
                             "Expected to find persisted Concept")
Esempio n. 5
0
    def test_relationship__select_by_foreign_keys(self):
        """Verify select_by_foreign_keys method finds expected Relationship.
        """
        rel_type_id = uuid.uuid4()
        subject_id = uuid.uuid4()
        object_id = uuid.uuid4()

        fk_sets = [(rel_type_id, subject_id, object_id),
                   (uuid.uuid4(), subject_id, object_id),
                   (rel_type_id, uuid.uuid4(), object_id),
                   (rel_type_id, subject_id, uuid.uuid4())]

        for (rt_id, s_id, o_id) in fk_sets:
            db.session.add(
                Relationship(relationship_type_id=rt_id,
                             subject_id=s_id,
                             object_id=o_id))
        self.reset_session()

        retrieved_rel = Relationship.select_by_foreign_keys(
            subject_id=subject_id,
            object_id=object_id,
            relationship_type_id=rel_type_id)
        self.assertIsNotNone(retrieved_rel,
                             "Expected to find persisted Relationship")
        self.assertEqual(rel_type_id, retrieved_rel.relationship_type_id)
        self.assertEqual(subject_id, retrieved_rel.subject_id)
        self.assertEqual(object_id, retrieved_rel.object_id)
Esempio n. 6
0
    def _setup_relationships(self):
        # Concepts and relationships
        high_heel = self._get_concept('high_heel')
        trainer = self._get_concept('trainer')
        shoe = self._get_concept('shoe')
        safety_hazard = self._get_concept('safety_hazard')
        is_relationship_type = self._get_relationship_type('is')
        kicks_relationship_type = self._get_relationship_type('kicks')

        # 'high_heel is shoe'
        high_heel_is_shoe_id = uuid.uuid4()
        relationship = Relationship(relationship_id=high_heel_is_shoe_id,
                                    subject=high_heel,
                                    object=shoe,
                                    relationship_types=[is_relationship_type])
        db.session.add(relationship)

        # 'high_heel is safety_hazard'
        high_heel_is_safety_hazard_id = uuid.uuid4()
        relationship = Relationship(
            relationship_id=high_heel_is_safety_hazard_id,
            subject=high_heel,
            object=safety_hazard,
            relationship_types=[is_relationship_type])
        db.session.add(relationship)

        # 'trainer is shoe'
        trainer_is_shoe_id = uuid.uuid4()
        relationship = Relationship(relationship_id=trainer_is_shoe_id,
                                    subject=trainer,
                                    object=shoe,
                                    relationship_types=[is_relationship_type])
        db.session.add(relationship)

        # 'high_heel kicks trainer'
        high_heel_kicks_trainer_id = uuid.uuid4()
        relationship = Relationship(
            relationship_id=high_heel_kicks_trainer_id,
            subject=high_heel,
            object=trainer,
            relationship_types=[kicks_relationship_type],
            count=3)
        db.session.add(relationship)
        self.reset_session()
Esempio n. 7
0
    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())
Esempio n. 8
0
    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))
Esempio n. 9
0
    def test_relationship__default_id(self):
        """Verify that relationship_id is assigned when Relationship is persisted.
        """
        rel_type_id = uuid.uuid4()
        subject_id = uuid.uuid4()
        object_id = uuid.uuid4()
        relationship = Relationship(relationship_type_id=rel_type_id,
                                    subject_id=subject_id,
                                    object_id=object_id)
        db.session.add(relationship)
        self.reset_session()

        retrieved_rel = db.session.query(Relationship).filter_by(
            relationship_type_id=rel_type_id).first()

        self.assertIsNotNone(retrieved_rel,
                             "Expected to find persisted Relationship")
        self.assertEqual(rel_type_id, retrieved_rel.relationship_type_id)
        self.assertEqual(subject_id, retrieved_rel.subject_id)
        self.assertEqual(object_id, retrieved_rel.object_id)
        self.assertTrue(isinstance(retrieved_rel.relationship_id, uuid.UUID))
Esempio n. 10
0
    def test_concept_types_relationship(self):
        """Test simple concept_types relationship: two concepts and one relationship.
        """
        rel_id = uuid.uuid4()
        subject_concept = self._get_concept('flat')
        object_concept = self._get_concept('shoe')
        rel_type = self._get_relationship_type('is')
        relationship = Relationship(relationship_id=rel_id,
                                    subject=subject_concept,
                                    object=object_concept,
                                    relationship_types=[rel_type])
        db.session.add(relationship)
        self.reset_session()

        # select concept
        subject_concept = db.session.query(Concept).filter_by(
            concept_id=subject_concept.concept_id).first()
        self.assertIsNotNone(subject_concept)
        self.assertEqual(['shoe'], subject_concept.concept_types)
        self.assertEqual(
            object_concept.concept_id,
            subject_concept.concept_type_relationships[0].object.concept_id)
Esempio n. 11
0
    def test_relationship__all_attributes(self):
        """Verify creation with all data.
        """
        rel_id = uuid.uuid4()
        count = 10
        fact_id = uuid.uuid4()
        relationship = Relationship(relationship_id=rel_id,
                                    relationship_type_id=uuid.uuid4(),
                                    subject_id=uuid.uuid4(),
                                    object_id=uuid.uuid4(),
                                    count=count,
                                    fact_id=fact_id)
        db.session.add(relationship)
        self.reset_session()

        retrieved_rel = db.session.query(Relationship).filter_by(
            relationship_id=rel_id).first()

        self.assertIsNotNone(retrieved_rel,
                             "Expected to find persisted Relationship")
        self.assertEqual(count, retrieved_rel.count)
        self.assertEqual(fact_id, retrieved_rel.fact_id)
Esempio n. 12
0
    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")
Esempio n. 13
0
    def test_relationship(self):
        """Verify creation with all non-nullable data.
        """
        rel_id = uuid.uuid4()
        rel_type_id = uuid.uuid4()
        subject_id = uuid.uuid4()
        object_id = uuid.uuid4()
        relationship = Relationship(relationship_id=rel_id,
                                    relationship_type_id=rel_type_id,
                                    subject_id=subject_id,
                                    object_id=object_id)
        db.session.add(relationship)
        self.reset_session()

        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_id, retrieved_rel.relationship_type_id)
        self.assertEqual(subject_id, retrieved_rel.subject_id)
        self.assertEqual(object_id, retrieved_rel.object_id)
        self.assertIsNone(retrieved_rel.count)
        self.assertIsNone(retrieved_rel.fact_id)