コード例 #1
0
    def create_graph_test_3v_2e():
        graph = Graph()
        vertex1 = graph.add_vertex('person', {'username': '******'})
        vertex2 = graph.add_vertex('listing', {'title': 'Skyzone1'})
        vertex3 = graph.add_vertex('listing', {'title': 'Skyzone2'})
        vertex1.add_edge('personListing', vertex2)
        vertex1.add_edge('personListing', vertex3)

        return graph
コード例 #2
0
    def create_graph_test_3v_2e():
        graph = Graph()
        vertex1 = graph.add_vertex('person', {'username': '******'})
        vertex2 = graph.add_vertex('listing', {'title': 'Skyzone1'})
        vertex3 = graph.add_vertex('listing', {'title': 'Skyzone2'})
        vertex1.add_edge('personListing', vertex2)
        vertex1.add_edge('personListing', vertex3)

        return graph
コード例 #3
0
    def test_pipeline_graph_vertex_chain_to_list(self):
        graph = Graph()
        graph.add_vertex('test_label', {'test_field': 1})
        graph.add_vertex('test_label', {'test_field': 2})

        pipeline_test = pipeline.Pipeline(
            graph.get_vertices_iterator(),
            [pipes.GraphVertexPipe(),
             pipes.ElementIdPipe()])

        self.assertEqual(pipeline_test.to_list(), [1, 2])
コード例 #4
0
    def setUp(self):
        """
        setUp is invoked before each test method
        """
        self.graph_test_1 = Graph()
        self.graph_test_1.add_vertex('test_label', {'test_field': 1})
        self.graph_test_1.add_vertex('test_label', {'test_field': 2})

        self.graph_test_2 = Graph()
        self.graph_test_2.add_vertex('test_label', {'test_field': 8})
        self.graph_test_2.add_vertex('test_label', {'test_field': 10})
        self.graph_test_2.add_vertex('test_label', {'test_field': 12, 'time': 'now'})
コード例 #5
0
    def setUp(self):
        self.graph = Graph()
        self.graph.add_vertex('test_label', {'test_field': 1})
        self.graph.add_vertex('test_label', {'test_field': 2})
        self.graph.add_vertex('test_label', {'test_field': 12, 'time': 'now'})

        self.graph2 = Graph()
        self.vertex1 = self.graph2.add_vertex('person', {'username': '******'}, current_id=10)
        self.vertex2 = self.graph2.add_vertex('listing', {'title': 'Skyzone1'}, current_id=20)
        self.vertex3 = self.graph2.add_vertex('listing', {'title': 'Skyzone2'}, current_id=30)
        self.vertex1.add_edge('personListing', self.vertex2)
        self.vertex1.add_edge('personListing', self.vertex3)
コード例 #6
0
    def setUp(self):
        self.graph_test_1 = Graph()
        self.graph_test_1.add_vertex('test_label', {'test_field': 1})
        self.graph_test_1.add_vertex('test_label', {'test_field': 2})

        self.graph_test_2 = Graph()
        self.graph_test_2.add_vertex('test_label', {'test_field': 8})
        self.graph_test_2.add_vertex('test_label', {'test_field': 10})
        self.graph_test_2.add_vertex('test_label', {
            'test_field': 12,
            'time': 'now'
        })
コード例 #7
0
    def test_graph_add_two_vertex(self):
        graph = Graph()
        self.assertEqual(str(graph), 'Graph(vertices: 0, edges: 0)')

        added_vertex = graph.add_vertex('test_label', {'test_field': 1})

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label')
        self.assertEqual(added_vertex.get_property('test_field'), 1)

        added_vertex = graph.add_vertex('test_label1', {'test_field': 2})

        self.assertEqual(str(graph), 'Graph(vertices: 2, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label1')
        self.assertEqual(added_vertex.get_property('test_field'), 2)
コード例 #8
0
    def test_graph_add_edit_one_vertex(self):
        graph = Graph()
        self.assertEqual(str(graph), 'Graph(vertices: 0, edges: 0)')

        added_vertex = graph.add_vertex('test_label', {'test_field': 1})

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label')
        self.assertEqual(added_vertex.get_property('test_field'), 1)

        added_vertex.set_property('test_field', 2)
        added_vertex = graph.get_vertex(1)
        self.assertEqual(added_vertex.get_property('test_field'), 2)

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
コード例 #9
0
    def setUp(self):
        """
        setUp is invoked before each test method
        """
        self.graph_test_1 = Graph()
        self.graph_test_1.add_vertex('test_label', {'test_field': 1})
        self.graph_test_1.add_vertex('test_label', {'test_field': 2})

        self.graph_test_2 = Graph()
        self.graph_test_2.add_vertex('test_label', {'test_field': 8})
        self.graph_test_2.add_vertex('test_label', {'test_field': 10})
        self.graph_test_2.add_vertex('test_label', {
            'test_field': 12,
            'time': 'now'
        })
コード例 #10
0
ファイル: test_graph.py プロジェクト: markbetters/ozp-backend
    def test_graph_add_edit_one_vertex(self):
        graph = Graph()
        self.assertEqual(str(graph), 'Graph(vertices: 0, edges: 0)')

        added_vertex = graph.add_vertex('test_label', {'test_field': 1})

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label')
        self.assertEqual(added_vertex.get_property('test_field'), 1)

        added_vertex.set_property('test_field', 2)
        added_vertex = graph.get_vertex(1)
        self.assertEqual(added_vertex.get_property('test_field'), 2)

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
コード例 #11
0
ファイル: test_graph.py プロジェクト: markbetters/ozp-backend
    def test_graph_add_two_vertex(self):
        graph = Graph()
        self.assertEqual(str(graph), 'Graph(vertices: 0, edges: 0)')

        added_vertex = graph.add_vertex('test_label', {'test_field': 1})

        self.assertEqual(str(graph), 'Graph(vertices: 1, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label')
        self.assertEqual(added_vertex.get_property('test_field'), 1)

        added_vertex = graph.add_vertex('test_label1', {'test_field': 2})

        self.assertEqual(str(graph), 'Graph(vertices: 2, edges: 0)')
        self.assertEqual(added_vertex.label, 'test_label1')
        self.assertEqual(added_vertex.get_property('test_field'), 2)
コード例 #12
0
    def test_pipeline_graph_vertex_while(self):
        graph = Graph()
        graph.add_vertex('test_label', {'test_field': 1})
        graph.add_vertex('test_label', {'test_field': 2})

        pipeline_test = pipeline.Pipeline(graph.get_vertices_iterator(),
                                          [pipes.GraphVertexPipe()])

        try:
            list_out = []
            while pipeline_test.has_next():
                current_object = pipeline_test.next()
                list_out.append(current_object.id)
        except recommend_utils.FastNoSuchElementException:
            # Ignore FastNoSuchElementException
            pass
        self.assertEqual(list_out, [1, 2])
コード例 #13
0
    def load_sample_profile_listing_graph():
        graph = Graph()
        profile1 = graph.add_vertex('profile', {'username': '******'}, current_id='p-1')
        profile2 = graph.add_vertex('profile', {'username': '******'}, current_id='p-2')
        profile3 = graph.add_vertex('profile', {'username': '******'}, current_id='p-3')
        profile4 = graph.add_vertex('profile', {'username': '******'}, current_id='p-4')
        profile5 = graph.add_vertex('profile', {'username': '******'}, current_id='p-5')

        listing1 = graph.add_vertex('listing', {'title': 'listing1'}, current_id='l-1')
        listing2 = graph.add_vertex('listing', {'title': 'listing2'}, current_id='l-2')
        listing3 = graph.add_vertex('listing', {'title': 'listing3'}, current_id='l-3')
        listing4 = graph.add_vertex('listing', {'title': 'listing4'}, current_id='l-4')
        listing5 = graph.add_vertex('listing', {'title': 'listing5'}, current_id='l-5')
        listing6 = graph.add_vertex('listing', {'title': 'listing6'}, current_id='l-6')
        listing7 = graph.add_vertex('listing', {'title': 'listing7'}, current_id='l-7')
        listing8 = graph.add_vertex('listing', {'title': 'listing8'}, current_id='l-8')

        category1 = graph.add_vertex('category', {'title': 'category1'}, current_id='c-1')
        category2 = graph.add_vertex('category', {'title': 'category2'}, current_id='c-2')

        listing1.add_edge('listingCategory', category1)
        listing2.add_edge('listingCategory', category2)
        listing3.add_edge('listingCategory', category1)
        listing4.add_edge('listingCategory', category2)

        listing5.add_edge('listingCategory', category1)
        listing6.add_edge('listingCategory', category2)
        listing7.add_edge('listingCategory', category1)
        listing8.add_edge('listingCategory', category2)

        profile1.add_edge('bookmarked', listing1)
        profile1.add_edge('bookmarked', listing2)
        profile1.add_edge('bookmarked', listing3)

        profile2.add_edge('bookmarked', listing1)
        profile2.add_edge('bookmarked', listing4)
        profile2.add_edge('bookmarked', listing5)

        profile3.add_edge('bookmarked', listing1)
        profile3.add_edge('bookmarked', listing2)
        profile3.add_edge('bookmarked', listing5)
        profile3.add_edge('bookmarked', listing6)
        profile3.add_edge('bookmarked', listing8)

        profile4.add_edge('bookmarked', listing2)
        profile4.add_edge('bookmarked', listing7)

        profile5.add_edge('bookmarked', listing2)
        profile5.add_edge('bookmarked', listing3)

        return graph
コード例 #14
0
    def test_pipeline_graph_vertex_chain_dict_to_list(self):
        graph = Graph()
        graph.add_vertex('test_label', {'test_field': 8})
        graph.add_vertex('test_label', {'test_field': 10})
        graph.add_vertex('test_label', {'test_field': 12, 'time': 'now'})

        pipeline_test = pipeline.Pipeline(
            graph.get_vertices_iterator(),
            [pipes.GraphVertexPipe(),
             pipes.ElementPropertiesPipe()])
        output = [{
            'test_field': 8
        }, {
            'test_field': 10
        }, {
            'test_field': 12,
            'time': 'now'
        }]
        self.assertEqual(pipeline_test.to_list(), output)
コード例 #15
0
    def test_graph_three_vertices_simple(self):
        graph = Graph()
        vertex1 = graph.add_vertex('person', {'username': '******'}, current_id=10)
        vertex2 = graph.add_vertex('listing', {'title': 'Skyzone1'}, current_id=20)
        vertex3 = graph.add_vertex('listing', {'title': 'Skyzone2'}, current_id=30)
        vertex1.add_edge('personListing', vertex2)
        vertex1.add_edge('personListing', vertex3)
        vertex1.add_edge('testListing', vertex3)

        self.assertEqual(str(graph), 'Graph(vertices: 3, edges: 3)')
        # Check Vertex 1
        self.assertEqual(len(vertex1.get_in_edges('personListing')), 0)
        self.assertEqual([edge.label for edge in vertex1.get_in_edges('personListing')], [])
        self.assertEqual([edge.in_vertex.id for edge in vertex1.get_in_edges('personListing')], [])

        self.assertEqual(len(vertex1.get_out_edges('personListing')), 2)
        self.assertEqual(len(vertex1.get_out_edges()), 3)
        self.assertEqual([edge.label for edge in vertex1.get_out_edges('personListing')], ['personListing', 'personListing'])
        self.assertEqual([edge.out_vertex.id for edge in vertex1.get_out_edges('personListing')], [20, 30])

        # Check Vertex 2
        self.assertEqual(len(vertex2.get_in_edges('personListing')), 1)
        self.assertEqual([edge.label for edge in vertex2.get_in_edges('personListing')], ['personListing'])
        self.assertEqual([edge.in_vertex.id for edge in vertex2.get_in_edges('personListing')], [10])

        self.assertEqual(len(vertex2.get_out_edges('personListing')), 0)
        self.assertEqual([edge.label for edge in vertex2.get_out_edges('personListing')], [])
        self.assertEqual([edge.out_vertex.id for edge in vertex2.get_out_edges('personListing')], [])

        # Check Vertex 3
        self.assertEqual(len(vertex3.get_in_edges('personListing')), 1)
        self.assertEqual([edge.label for edge in vertex3.get_in_edges('personListing')], ['personListing'])
        self.assertEqual([edge.in_vertex.id for edge in vertex3.get_in_edges('personListing')], [10])

        self.assertEqual(len(vertex3.get_out_edges('personListing')), 0)
        self.assertEqual([edge.label for edge in vertex3.get_out_edges('personListing')], [])
        self.assertEqual([edge.out_vertex.id for edge in vertex3.get_out_edges('personListing')], [])
コード例 #16
0
    def load_db_into_graph():
        """
        Load Django Database into graph

        Agency <--stewardedAgency--
        Agency <--agency--          Profile --bookmarked--> Listing --listingCategory--> Category
                                                                    --listingAgency--> Agency

        Steps:
            Load all Category
            Load all Agency
            Load all Listings
                Link to Agency
                Link to Category
            Load all Profiles
                Link to Agency
                Link bookmarked listings
        """
        graph = Graph()

        for category in models.Category.objects.all():
            data = {'title': category.title}
            added_vertex = graph.add_vertex('category', data, current_id='c-{}'.format(category.pk))

        for agency in models.Agency.objects.all():
            data = {'title': agency.title,
                    'short_name': agency.short_name}
            added_vertex = graph.add_vertex('agency', data, current_id='a-{}'.format(agency.pk))

        for listing in models.Listing.objects.all():
            data = {'title': listing.title,
                    'description': listing.description,
                    'is_private': listing.is_private,
                    'security_marking': listing.security_marking,
                    'is_enabled': listing.is_enabled,
                    'is_deleted': listing.is_deleted,
                    'is_featured': listing.is_featured,
                    'approval_status': listing.approval_status}

            if listing.is_enabled and not listing.is_deleted and listing.approval_status == models.Listing.APPROVED:
                added_vertex = graph.add_vertex('listing', data, current_id='l-{}'.format(listing.pk))

                # One Agency per listing
                current_agency = listing.agency
                added_vertex.add_edge('listingAgency', graph.get_vertex('a-{}'.format(current_agency.pk)))

                # Many Categories per listing
                for current_category in listing.categories.all():
                    added_vertex.add_edge('listingCategory', graph.get_vertex('c-{}'.format(current_category.pk)))

        for profile in models.Profile.objects.all():
            data = {'username': profile.user.username,
                    'highest_role': profile.highest_role()}
            added_vertex = graph.add_vertex('profile', data, current_id='p-{}'.format(profile.pk))

            # Many Agencies per profile
            for current_agency in profile.organizations.all():
                added_vertex.add_edge('agency', graph.get_vertex('a-{}'.format(current_agency.pk)))

            # Many stewardedAgency Agencies per profile
            for current_agency in profile.stewarded_organizations.all():
                added_vertex.add_edge('stewardedAgency', graph.get_vertex('a-{}'.format(current_agency.pk)))

            for current_entry in models.ApplicationLibraryEntry.objects.filter(owner=profile):
                current_listing = current_entry.listing
                data = {'folder_name': current_entry.folder}
                added_vertex.add_edge('bookmarked', graph.get_vertex('l-{}'.format(current_listing.pk)), data)

        return graph
コード例 #17
0
class PipelineTest(TestCase):

    def setUp(self):
        """
        setUp is invoked before each test method
        """
        self.graph_test_1 = Graph()
        self.graph_test_1.add_vertex('test_label', {'test_field': 1})
        self.graph_test_1.add_vertex('test_label', {'test_field': 2})

        self.graph_test_2 = Graph()
        self.graph_test_2.add_vertex('test_label', {'test_field': 8})
        self.graph_test_2.add_vertex('test_label', {'test_field': 10})
        self.graph_test_2.add_vertex('test_label', {'test_field': 12, 'time': 'now'})

    @classmethod
    def setUpTestData(cls):
        """
        Set up test data for the whole TestCase (only run once for the TestCase)
        """
        pass

    def _iterate_pipeline(self, current_pipeline):
        list_out = []

        try:
            while current_pipeline.has_next():
                current_object = current_pipeline.next()
                list_out.append(current_object)
        except recommend_utils.FastNoSuchElementException:
            # Ignore FastNoSuchElementException
            pass
        return list_out

    def test_pipeline_limit(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.LimitPipe(5)])

        self.assertEqual(pipeline_test.to_list(), [1, 2, 3, 4, 5])

        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.LimitPipe(2)])

        self.assertEqual(pipeline_test.to_list(), [1, 2])

        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3]),
                                          [pipes.LimitPipe(5)])

        self.assertEqual(pipeline_test.to_list(), [1, 2, 3])

    def test_pipeline_exclude_limit(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.ExcludePipe([1]),
                                           pipes.LimitPipe(5)])

        self.assertEqual(pipeline_test.to_list(), [2, 3, 4, 5, 6])

    def test_pipeline_capitalize(self):
        caps_pipe = pipes.CapitalizePipe()

        pipeline_test = pipeline.Pipeline()
        pipeline_test.add_pipe(caps_pipe)

        pipeline_test.set_starts(recommend_utils.ListIterator(['this', 'is', 'the', 'test']))

        list_out = self._iterate_pipeline(pipeline_test)
        self.assertEqual(list_out, ['THIS', 'IS', 'THE', 'TEST'])

    def test_pipeline_capitalize_len(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator(['this', 'is', 'the', 'test']),
                                          [pipes.CapitalizePipe(),
                                           pipes.LenPipe()])
        list_out = self._iterate_pipeline(pipeline_test)
        self.assertEqual(list_out, [4, 2, 3, 4])

    def test_pipeline_capitalize_len_list(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator(['this', 'is', 'the', 'test']),
                                          [pipes.CapitalizePipe(),
                                           pipes.LenPipe()])

        self.assertEqual(pipeline_test.to_list(), [4, 2, 3, 4])

    def test_pipeline_graph_vertex_while(self):
        pipeline_test = pipeline.Pipeline(self.graph_test_1.get_vertices_iterator(),
                                          [pipes.GraphVertexPipe()])

        list_out = self._iterate_pipeline(pipeline_test)
        self.assertEquals(str(list_out), '[Vertex(test_label), Vertex(test_label)]')
        # self.assertEqual(list_out, [1, 2])

    def test_pipeline_to_list_exception(self):
        pipeline_test = pipeline.Pipeline()

        with self.assertRaisesRegex(Exception, 'No Start Iterator set') as err:
            pipeline_test.to_list()

    def test_pipeline_graph_vertex_chain_to_list(self):
        pipeline_test = pipeline.Pipeline(self.graph_test_1.get_vertices_iterator(),
                                          [pipes.GraphVertexPipe(),
                                           pipes.ElementIdPipe()])

        self.assertEqual(pipeline_test.to_list(), [1, 2])
        self.assertEqual(str(pipeline_test), '[DictKeyValueIterator(2), GraphVertexPipe(), ElementIdPipe()]')

    def test_pipeline_graph_vertex_chain_dict_to_list(self):
        pipeline_test = pipeline.Pipeline(self.graph_test_2.get_vertices_iterator(),
                                          [pipes.GraphVertexPipe(),
                                           pipes.ElementPropertiesPipe()])
        expected_output = [
            {'test_field': 8},
            {'test_field': 10},
            {'test_field': 12, 'time': 'now'}
        ]
        self.assertEqual(pipeline_test.to_list(), expected_output)
        self.assertEqual(str(pipeline_test), '[DictKeyValueIterator(3), GraphVertexPipe(), ElementPropertiesPipe(internal:False)]')

    def test_pipeline_graph_vertex_chain_dict_to_list_internal(self):
        pipeline_test = pipeline.Pipeline(self.graph_test_2 .get_vertices_iterator(),
                                          [pipes.GraphVertexPipe(),
                                           pipes.ElementPropertiesPipe(internal=True)])
        expected_output = [
            {'_id': 1, '_label': 'test_label', 'test_field': 8},
            {'_id': 2, '_label': 'test_label', 'test_field': 10},
            {'_id': 3, '_label': 'test_label', 'test_field': 12, 'time': 'now'}
        ]
        self.assertEqual(pipeline_test.to_list(), expected_output)
        self.assertEqual(str(pipeline_test), '[DictKeyValueIterator(3), GraphVertexPipe(), ElementPropertiesPipe(internal:True)]')

    def test_pipeline_get_starts(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.ExcludePipe([1]),
                                           pipes.LimitPipe(5)])
        result = pipeline_test.get_starts()

        self.assertEqual(str(result), 'ListIterator(7)')

    def test_pipeline_get_pipes(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.ExcludePipe([1]),
                                           pipes.LimitPipe(5)])
        result = ', '.join([str(pipe) for pipe in pipeline_test.get_pipes()])

        self.assertEqual(str(result), 'ListIterator(7), ExcludePipe(), LimitPipe(limit_number:5)')

    def test_pipeline_size(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.ExcludePipe([1]),
                                           pipes.LimitPipe(5)])

        self.assertEqual(pipeline_test.size(), 3)
        self.assertEqual(pipeline_test.to_list(), [2, 3, 4, 5, 6])

    def test_pipeline_count(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.ExcludePipe([1]),
                                           pipes.LimitPipe(5)])
        result = pipeline_test.count()
        self.assertEqual(result, 5)

        result = pipeline_test.count()
        self.assertEqual(result, 0)
        # TODO: A way to reset pipeline to iterate again

    def test_pipeline_count_exception(self):
        pipeline_test = pipeline.Pipeline()

        with self.assertRaisesRegex(Exception, 'No Start Iterator set') as err:
            pipeline_test.count()

    def test_pipeline_iterate(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.ExcludePipe([1]),
                                           pipes.LimitPipe(5)])
        result = pipeline_test.iterate()
        self.assertEqual(result, None)
        # TODO: Have a SideEffectPipe to prove that it is iterating

    def test_pipeline_iterate_exception(self):
        pipeline_test = pipeline.Pipeline()

        with self.assertRaisesRegex(Exception, 'No Start Iterator set') as err:
            pipeline_test.iterate()

    def test_pipeline_remove(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.ExcludePipe([1]),
                                           pipes.LimitPipe(5)])

        self.assertRaises(recommend_utils.UnsupportedOperationException, pipeline_test.remove)

    def test_pipeline_refresh_as_pipes(self):
        pipeline_test = pipeline.Pipeline(recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
                                          [pipes.ExcludePipe([1]),
                                           pipes.LimitPipe(5)])
        result = pipeline_test.refresh_as_pipes()
        self.assertEqual(result, None)
コード例 #18
0
    def load_db_into_graph():
        """
        Load Django Database into graph

        Agency <--stewardedAgency--
        Agency <--agency--          Profile --bookmarked--> Listing --listingCategory--> Category
                                                                    --listingAgency--> Agency

        Steps:
            Load all Category
            Load all Agency
            Load all Listings
                Link to Agency
                Link to Category
            Load all Profiles
                Link to Agency
                Link bookmarked listings
        """
        graph = Graph()

        for category in models.Category.objects.all():
            data = {'title': category.title}
            added_vertex = graph.add_vertex('category',
                                            data,
                                            current_id='c-{}'.format(
                                                category.pk))

        for agency in models.Agency.objects.all():
            data = {'title': agency.title, 'short_name': agency.short_name}
            added_vertex = graph.add_vertex('agency',
                                            data,
                                            current_id='a-{}'.format(
                                                agency.pk))

        for listing in models.Listing.objects.all():
            data = {
                'title': listing.title,
                'description': listing.description,
                'is_private': listing.is_private,
                'security_marking': listing.security_marking,
                'is_enabled': listing.is_enabled,
                'is_deleted': listing.is_deleted,
                'is_featured': listing.is_featured,
                'approval_status': listing.approval_status
            }

            if listing.is_enabled and not listing.is_deleted and listing.approval_status == models.Listing.APPROVED:
                added_vertex = graph.add_vertex('listing',
                                                data,
                                                current_id='l-{}'.format(
                                                    listing.pk))

                # One Agency per listing
                current_agency = listing.agency
                added_vertex.add_edge(
                    'listingAgency',
                    graph.get_vertex('a-{}'.format(current_agency.pk)))

                # Many Categories per listing
                for current_category in listing.categories.all():
                    added_vertex.add_edge(
                        'listingCategory',
                        graph.get_vertex('c-{}'.format(current_category.pk)))

        for profile in models.Profile.objects.all():
            data = {
                'username': profile.user.username,
                'highest_role': profile.highest_role()
            }
            added_vertex = graph.add_vertex('profile',
                                            data,
                                            current_id='p-{}'.format(
                                                profile.pk))

            # Many Agencies per profile
            for current_agency in profile.organizations.all():
                added_vertex.add_edge(
                    'agency',
                    graph.get_vertex('a-{}'.format(current_agency.pk)))

            # Many stewardedAgency Agencies per profile
            for current_agency in profile.stewarded_organizations.all():
                added_vertex.add_edge(
                    'stewardedAgency',
                    graph.get_vertex('a-{}'.format(current_agency.pk)))

            for current_entry in models.ApplicationLibraryEntry.objects.filter(
                    owner=profile):
                current_listing = current_entry.listing
                data = {'folder_name': current_entry.folder}
                added_vertex.add_edge(
                    'bookmarked',
                    graph.get_vertex('l-{}'.format(current_listing.pk)), data)

        return graph
コード例 #19
0
    def load_sample_profile_listing_graph():
        graph = Graph()
        profile1 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-1')
        profile2 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-2')
        profile3 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-3')
        profile4 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-4')
        profile5 = graph.add_vertex('profile', {'username': '******'},
                                    current_id='p-5')

        listing1 = graph.add_vertex('listing', {'title': 'listing1'},
                                    current_id='l-1')
        listing2 = graph.add_vertex('listing', {'title': 'listing2'},
                                    current_id='l-2')
        listing3 = graph.add_vertex('listing', {'title': 'listing3'},
                                    current_id='l-3')
        listing4 = graph.add_vertex('listing', {'title': 'listing4'},
                                    current_id='l-4')
        listing5 = graph.add_vertex('listing', {'title': 'listing5'},
                                    current_id='l-5')
        listing6 = graph.add_vertex('listing', {'title': 'listing6'},
                                    current_id='l-6')
        listing7 = graph.add_vertex('listing', {'title': 'listing7'},
                                    current_id='l-7')
        listing8 = graph.add_vertex('listing', {'title': 'listing8'},
                                    current_id='l-8')

        category1 = graph.add_vertex('category', {'title': 'category1'},
                                     current_id='c-1')
        category2 = graph.add_vertex('category', {'title': 'category2'},
                                     current_id='c-2')

        listing1.add_edge('listingCategory', category1)
        listing2.add_edge('listingCategory', category2)
        listing3.add_edge('listingCategory', category1)
        listing4.add_edge('listingCategory', category2)

        listing5.add_edge('listingCategory', category1)
        listing6.add_edge('listingCategory', category2)
        listing7.add_edge('listingCategory', category1)
        listing8.add_edge('listingCategory', category2)

        profile1.add_edge('bookmarked', listing1)
        profile1.add_edge('bookmarked', listing2)
        profile1.add_edge('bookmarked', listing3)

        profile2.add_edge('bookmarked', listing1)
        profile2.add_edge('bookmarked', listing4)
        profile2.add_edge('bookmarked', listing5)

        profile3.add_edge('bookmarked', listing1)
        profile3.add_edge('bookmarked', listing2)
        profile3.add_edge('bookmarked', listing5)
        profile3.add_edge('bookmarked', listing6)
        profile3.add_edge('bookmarked', listing8)

        profile4.add_edge('bookmarked', listing2)
        profile4.add_edge('bookmarked', listing7)

        profile5.add_edge('bookmarked', listing2)
        profile5.add_edge('bookmarked', listing3)

        return graph
コード例 #20
0
ファイル: test_graph.py プロジェクト: markbetters/ozp-backend
    def test_graph_three_vertices_simple(self):
        graph = Graph()
        vertex1 = graph.add_vertex('person', {'username': '******'},
                                   current_id=10)
        vertex2 = graph.add_vertex('listing', {'title': 'Skyzone1'},
                                   current_id=20)
        vertex3 = graph.add_vertex('listing', {'title': 'Skyzone2'},
                                   current_id=30)
        vertex1.add_edge('personListing', vertex2)
        vertex1.add_edge('personListing', vertex3)
        vertex1.add_edge('testListing', vertex3)

        self.assertEqual(str(graph), 'Graph(vertices: 3, edges: 3)')
        # Check Vertex 1
        self.assertEqual(len(vertex1.get_in_edges('personListing')), 0)
        self.assertEqual(
            [edge.label for edge in vertex1.get_in_edges('personListing')], [])
        self.assertEqual([
            edge.in_vertex.id for edge in vertex1.get_in_edges('personListing')
        ], [])

        self.assertEqual(len(vertex1.get_out_edges('personListing')), 2)
        self.assertEqual(len(vertex1.get_out_edges()), 3)
        self.assertEqual(
            [edge.label for edge in vertex1.get_out_edges('personListing')],
            ['personListing', 'personListing'])
        self.assertEqual([
            edge.out_vertex.id
            for edge in vertex1.get_out_edges('personListing')
        ], [20, 30])

        # Check Vertex 2
        self.assertEqual(len(vertex2.get_in_edges('personListing')), 1)
        self.assertEqual(
            [edge.label for edge in vertex2.get_in_edges('personListing')],
            ['personListing'])
        self.assertEqual([
            edge.in_vertex.id for edge in vertex2.get_in_edges('personListing')
        ], [10])

        self.assertEqual(len(vertex2.get_out_edges('personListing')), 0)
        self.assertEqual(
            [edge.label for edge in vertex2.get_out_edges('personListing')],
            [])
        self.assertEqual([
            edge.out_vertex.id
            for edge in vertex2.get_out_edges('personListing')
        ], [])

        # Check Vertex 3
        self.assertEqual(len(vertex3.get_in_edges('personListing')), 1)
        self.assertEqual(
            [edge.label for edge in vertex3.get_in_edges('personListing')],
            ['personListing'])
        self.assertEqual([
            edge.in_vertex.id for edge in vertex3.get_in_edges('personListing')
        ], [10])

        self.assertEqual(len(vertex3.get_out_edges('personListing')), 0)
        self.assertEqual(
            [edge.label for edge in vertex3.get_out_edges('personListing')],
            [])
        self.assertEqual([
            edge.out_vertex.id
            for edge in vertex3.get_out_edges('personListing')
        ], [])
コード例 #21
0
    def create_graph_template():
        graph = Graph()
        graph.add_vertex()

        return graph
コード例 #22
0
class PipelineTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        pass

    def setUp(self):
        self.graph_test_1 = Graph()
        self.graph_test_1.add_vertex('test_label', {'test_field': 1})
        self.graph_test_1.add_vertex('test_label', {'test_field': 2})

        self.graph_test_2 = Graph()
        self.graph_test_2.add_vertex('test_label', {'test_field': 8})
        self.graph_test_2.add_vertex('test_label', {'test_field': 10})
        self.graph_test_2.add_vertex('test_label', {
            'test_field': 12,
            'time': 'now'
        })

    def _iterate_pipeline(self, current_pipeline):
        list_out = []

        try:
            while current_pipeline.has_next():
                current_object = current_pipeline.next()
                list_out.append(current_object)
        except recommend_utils.FastNoSuchElementException:
            # Ignore FastNoSuchElementException
            pass
        return list_out

    def test_pipeline_limit(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.LimitPipe(5)])

        self.assertEqual(pipeline_test.to_list(), [1, 2, 3, 4, 5])

        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.LimitPipe(2)])

        self.assertEqual(pipeline_test.to_list(), [1, 2])

        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3]), [pipes.LimitPipe(5)])

        self.assertEqual(pipeline_test.to_list(), [1, 2, 3])

    def test_pipeline_exclude_limit(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.ExcludePipe([1]),
             pipes.LimitPipe(5)])

        self.assertEqual(pipeline_test.to_list(), [2, 3, 4, 5, 6])

    def test_pipeline_capitalize(self):
        caps_pipe = pipes.CapitalizePipe()

        pipeline_test = pipeline.Pipeline()
        pipeline_test.add_pipe(caps_pipe)

        pipeline_test.set_starts(
            recommend_utils.ListIterator(['this', 'is', 'the', 'test']))

        list_out = self._iterate_pipeline(pipeline_test)
        self.assertEqual(list_out, ['THIS', 'IS', 'THE', 'TEST'])

    def test_pipeline_capitalize_len(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator(['this', 'is', 'the', 'test']),
            [pipes.CapitalizePipe(), pipes.LenPipe()])
        list_out = self._iterate_pipeline(pipeline_test)
        self.assertEqual(list_out, [4, 2, 3, 4])

    def test_pipeline_capitalize_len_list(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator(['this', 'is', 'the', 'test']),
            [pipes.CapitalizePipe(), pipes.LenPipe()])

        self.assertEqual(pipeline_test.to_list(), [4, 2, 3, 4])

    def test_pipeline_graph_vertex_while(self):
        pipeline_test = pipeline.Pipeline(
            self.graph_test_1.get_vertices_iterator(),
            [pipes.GraphVertexPipe()])

        list_out = self._iterate_pipeline(pipeline_test)
        self.assertEquals(str(list_out),
                          '[Vertex(test_label), Vertex(test_label)]')
        # self.assertEqual(list_out, [1, 2])

    def test_pipeline_to_list_exception(self):
        pipeline_test = pipeline.Pipeline()

        with self.assertRaisesRegex(Exception, 'No Start Iterator set') as err:
            pipeline_test.to_list()

    def test_pipeline_graph_vertex_chain_to_list(self):
        pipeline_test = pipeline.Pipeline(
            self.graph_test_1.get_vertices_iterator(),
            [pipes.GraphVertexPipe(),
             pipes.ElementIdPipe()])

        self.assertEqual(pipeline_test.to_list(), [1, 2])
        self.assertEqual(
            str(pipeline_test),
            '[DictKeyValueIterator(2), GraphVertexPipe(), ElementIdPipe()]')

    def test_pipeline_graph_vertex_chain_dict_to_list(self):
        pipeline_test = pipeline.Pipeline(
            self.graph_test_2.get_vertices_iterator(),
            [pipes.GraphVertexPipe(),
             pipes.ElementPropertiesPipe()])
        expected_output = [{
            'test_field': 8
        }, {
            'test_field': 10
        }, {
            'test_field': 12,
            'time': 'now'
        }]
        self.assertEqual(pipeline_test.to_list(), expected_output)
        self.assertEqual(
            str(pipeline_test),
            '[DictKeyValueIterator(3), GraphVertexPipe(), ElementPropertiesPipe(internal:False)]'
        )

    def test_pipeline_graph_vertex_chain_dict_to_list_internal(self):
        pipeline_test = pipeline.Pipeline(
            self.graph_test_2.get_vertices_iterator(), [
                pipes.GraphVertexPipe(),
                pipes.ElementPropertiesPipe(internal=True)
            ])
        expected_output = [{
            '_id': 1,
            '_label': 'test_label',
            'test_field': 8
        }, {
            '_id': 2,
            '_label': 'test_label',
            'test_field': 10
        }, {
            '_id': 3,
            '_label': 'test_label',
            'test_field': 12,
            'time': 'now'
        }]
        self.assertEqual(pipeline_test.to_list(), expected_output)
        self.assertEqual(
            str(pipeline_test),
            '[DictKeyValueIterator(3), GraphVertexPipe(), ElementPropertiesPipe(internal:True)]'
        )

    def test_pipeline_get_starts(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.ExcludePipe([1]),
             pipes.LimitPipe(5)])
        result = pipeline_test.get_starts()

        self.assertEqual(str(result), 'ListIterator(7)')

    def test_pipeline_get_pipes(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.ExcludePipe([1]),
             pipes.LimitPipe(5)])
        result = ', '.join([str(pipe) for pipe in pipeline_test.get_pipes()])

        self.assertEqual(
            str(result),
            'ListIterator(7), ExcludePipe(), LimitPipe(limit_number:5)')

    def test_pipeline_size(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.ExcludePipe([1]),
             pipes.LimitPipe(5)])

        self.assertEqual(pipeline_test.size(), 3)
        self.assertEqual(pipeline_test.to_list(), [2, 3, 4, 5, 6])

    def test_pipeline_count(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.ExcludePipe([1]),
             pipes.LimitPipe(5)])
        result = pipeline_test.count()
        self.assertEqual(result, 5)

        result = pipeline_test.count()
        self.assertEqual(result, 0)
        # TODO: A way to reset pipeline to iterate again

    def test_pipeline_count_exception(self):
        pipeline_test = pipeline.Pipeline()

        with self.assertRaisesRegex(Exception, 'No Start Iterator set') as err:
            pipeline_test.count()

    def test_pipeline_iterate(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.ExcludePipe([1]),
             pipes.LimitPipe(5)])
        result = pipeline_test.iterate()
        self.assertEqual(result, None)
        # TODO: Have a SideEffectPipe to prove that it is iterating

    def test_pipeline_iterate_exception(self):
        pipeline_test = pipeline.Pipeline()

        with self.assertRaisesRegex(Exception, 'No Start Iterator set') as err:
            pipeline_test.iterate()

    def test_pipeline_remove(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.ExcludePipe([1]),
             pipes.LimitPipe(5)])

        self.assertRaises(recommend_utils.UnsupportedOperationException,
                          pipeline_test.remove)

    def test_pipeline_refresh_as_pipes(self):
        pipeline_test = pipeline.Pipeline(
            recommend_utils.ListIterator([1, 2, 3, 4, 5, 6, 7]),
            [pipes.ExcludePipe([1]),
             pipes.LimitPipe(5)])
        result = pipeline_test.refresh_as_pipes()
        self.assertEqual(result, None)
コード例 #23
0
class GraphQueryTest(TestCase):
    def setUp(self):
        """
        setUp is invoked before each test method
        """
        self.graph = Graph()
        self.graph.add_vertex('test_label', {'test_field': 1})
        self.graph.add_vertex('test_label', {'test_field': 2})
        self.graph.add_vertex('test_label', {'test_field': 12, 'time': 'now'})

        self.graph2 = Graph()
        self.vertex1 = self.graph2.add_vertex('person',
                                              {'username': '******'},
                                              current_id=10)
        self.vertex2 = self.graph2.add_vertex('listing', {'title': 'Skyzone1'},
                                              current_id=20)
        self.vertex3 = self.graph2.add_vertex('listing', {'title': 'Skyzone2'},
                                              current_id=30)
        self.vertex1.add_edge('personListing', self.vertex2)
        self.vertex1.add_edge('personListing', self.vertex3)

    @classmethod
    def setUpTestData(cls):
        """
        Set up test data for the whole TestCase (only run once for the TestCase)
        """
        data_gen.run()

    def test_graph_query_builder(self):
        query = self.graph.query().V()
        self.assertEqual(str(query), "[GraphVertexPipe()]")

    def test_graph_query_builder_chain(self):
        query = self.graph.query().V().to_dict()
        self.assertEqual(
            str(query),
            "[GraphVertexPipe(), ElementPropertiesPipe(internal:False)]")

    def test_graph_query_V_dict(self):
        query = self.graph.query().V().to_dict()
        all_vertices = query.to_list()

        output = [{
            'test_field': 1
        }, {
            'test_field': 2
        }, {
            'test_field': 12,
            'time': 'now'
        }]

        self.assertEqual(all_vertices, output)

    def test_graph_query_V_id(self):
        query = self.graph.query().V().id()
        all_vertices = query.to_list()

        output = [1, 2, 3]
        self.assertEqual(all_vertices, output)

    def test_graph_query_v_to_list(self):
        query = self.graph2.query().v(10).id()
        all_vertices = query.to_list()
        output = [10]
        self.assertEqual(all_vertices, output)

        query = self.graph2.query().v(10, 20).id()
        all_vertices = query.to_list()
        output = [10, 20]
        self.assertEqual(all_vertices, output)

    def test_graph_query_v_out_to_list(self):
        query = self.graph2.query().v(10).out().id()
        all_vertices = query.to_list()
        output = [20, 30]
        self.assertEqual(all_vertices, output)

        query = self.graph2.query().v(20).out().id()
        all_vertices = query.to_list()
        output = []
        self.assertEqual(all_vertices, output)

    def test_graph_sample_profile_listing_in(self):
        graph = GraphFactory.load_sample_profile_listing_graph()
        self.assertEqual(str(graph), 'Graph(vertices: 15, edges: 23)')

        query_results = graph.query().v('l-1').id().to_list()
        output = ['l-1']
        self.assertEqual(query_results, output)

        query_results = graph.query().v('l-1').in_('bookmarked').id().to_list()
        output = ['p-1', 'p-2', 'p-3']
        self.assertEqual(query_results, output)

        query_results = graph.query().v('l-1', 'l-2', 'l-3').id().to_list()
        output = ['l-1', 'l-2', 'l-3']
        self.assertEqual(query_results, output)

    def test_graph_sample_profile_listing_categories(self):
        graph = GraphFactory.load_sample_profile_listing_graph()
        self.assertEqual(str(graph), 'Graph(vertices: 15, edges: 23)')

        query_results = (
            graph.query().v('p-1')  # Profile
            .out('bookmarked')  # Listing
            .out('listingCategory').id().to_list()
        )  # Get listings of target profile ids

        expected_categories = ['c-1', 'c-2', 'c-1']

        self.assertEqual(expected_categories, query_results)

    def test_graph_sample_profile_listing_side_effect(self):
        graph = GraphFactory.load_sample_profile_listing_graph()
        self.assertEqual(str(graph), 'Graph(vertices: 15, edges: 23)')

        profile_listing_categories_ids = []
        query_results = (graph.query().v('p-1').out('bookmarked').side_effect(
            lambda current_vertex: [
                profile_listing_categories_ids.append(current) for current in
                current_vertex.query().out('listingCategory').id().to_list()
            ]).id().to_list())  # Get listings of target profile ids

        expected_categories = ['c-1', 'c-2', 'c-1']
        expected_listing = ['l-1', 'l-2', 'l-3']

        self.assertEqual(expected_categories, profile_listing_categories_ids)
        self.assertEqual(expected_listing, query_results)

    def test_graph_sample_profile_listing(self):
        graph = GraphFactory.load_sample_profile_listing_graph()
        self.assertEqual(str(graph), 'Graph(vertices: 15, edges: 23)')

        query_results = graph.query().v('p-1').id().to_list()
        output = ['p-1']
        self.assertEqual(query_results, output)

        query_results = graph.query().v('p-1').out('bookmarked').id().to_list()
        output = ['l-1', 'l-2', 'l-3']
        self.assertEqual(query_results, output)

        query_results = graph.query().v('p-1').out('bookmarked').in_(
            'bookmarked').id().to_list()
        output = [
            'p-1', 'p-2', 'p-3', 'p-3', 'p-1', 'p-4', 'p-5', 'p-1', 'p-5'
        ]
        self.assertEqual(query_results, output)

        query_results = graph.query().v('p-1').out('bookmarked').in_(
            'bookmarked').id().distinct().to_list()
        output = ['p-1', 'p-2', 'p-3', 'p-4', 'p-5']
        self.assertEqual(query_results, output)

        query_results = graph.query().v('p-1').out('bookmarked').in_(
            'bookmarked').distinct().id().to_list()
        output = ['p-1', 'p-2', 'p-3', 'p-4', 'p-5']
        self.assertEqual(query_results, output)

        query_results = graph.query().v('p-1').out('bookmarked').in_(
            'bookmarked').distinct().exclude_ids(['p-1']).id().to_list()
        output = ['p-2', 'p-3', 'p-4', 'p-5']
        self.assertEqual(query_results, output)
コード例 #24
0
    def create_graph_template():
        graph = Graph()
        graph.add_vertex()

        return graph