예제 #1
0
 def test_to_dict_when_no_entities_exist(self):
     graph = Graph()
     json_dict = graph.to_dict()
     self.assert_graph_dict(json_dict, {
         'entities': [],
         'links': [],
     })
예제 #2
0
class TestGraphLinkEntities(TestCase, AssertEntityMixin):
    def setUp(self):
        self.graph = Graph()
        self.entity_1 = Entity(1, 'E1')
        self.entity_2 = Entity(2, 'E2')
        self.graph.add_entity(self.entity_1)
        self.graph.add_entity(self.entity_2)

    def test_link_entities_when_arguments_are_none(self):

        self.graph.link_entities(self.entity_1, None)
        self.graph.link_entities(None, self.entity_2)
        self.graph.link_entities(None, None)

        self.assert_links(self.entity_1)
        self.assert_links(self.entity_2)

    def test_link_entities(self):
        self.graph.link_entities(self.entity_1, self.entity_2)

        self.assert_links(self.entity_1, successors=set([self.entity_2]))
        self.assert_links(self.entity_2, predecessors=set([self.entity_1]))

    def test_link_entities_by_id(self):
        self.graph.link_entities_by_id(1, 2)

        self.assert_links(self.entity_1, successors=set([self.entity_2]))
        self.assert_links(self.entity_2, predecessors=set([self.entity_1]))
예제 #3
0
    def test_to_dict_when_entity_has_multiple_links(self):
        graph = Graph()
        e1 = Entity(1, 'E1')
        e2 = Entity(2, 'E2')
        e3 = Entity(3, 'E3')
        graph.add_entity(e1)
        graph.add_entity(e2)
        graph.add_entity(e3)
        graph.link_entities(e1, e3)
        graph.link_entities(e2, e3)
        self.assertDictEqual(graph.entities, {1: e1, 2: e2, 3: e3})

        json_dict = graph.to_dict()

        self.assert_graph_dict(
            json_dict, {
                'entities': [{
                    'entity_id': 1,
                    'name': 'E1'
                }, {
                    'entity_id': 2,
                    'name': 'E2'
                }, {
                    'entity_id': 3,
                    'name': 'E3'
                }],
                'links': [{
                    'from': 1,
                    'to': 3
                }, {
                    'from': 2,
                    'to': 3
                }],
            })
예제 #4
0
def test_from_dict_when_entity_has_multiple_links(self):
    graph = Graph.from_dict({
        'entities': [{
            'entity_id': 1,
            'name': 'E1'
        }, {
            'entity_id': 2,
            'name': 'E2'
        }, {
            'entity_id': 3,
            'name': 'E3'
        }],
        'links': [
            {
                'from': 1,
                'to': 3
            },
            {
                'from': 2,
                'to': 3
            },
        ]
    })
    e1 = graph.entities[1]
    e2 = graph.entities[2]
    e3 = graph.entities[3]

    self.assert_links(e1, successors=set([e3]))
    self.assert_links(e2, successors=set([e3]))
    self.assert_links(e3, predecessors=set([e1, e2]))
예제 #5
0
class TestGraphCopyAndAddEntity(TestCase):
    def setUp(self):
        self.graph = Graph()
        self.entity_1 = Entity(1, 'E1')
        self.graph.add_entity(self.entity_1)

    def test_copy_and_add_entity(self):
        self.assertDictEqual(self.graph.entities, {1: self.entity_1})
        self.assertEqual(self.graph.next_entity_id, 2)

        self.graph.copy_and_add_entity(self.entity_1)

        self.assertEqual(len(self.graph.entities), 2)
        self.assertEqual(self.graph.entities[2].id, 2)
        self.assertEqual(self.graph.entities[2].name, 'E1')
        self.assertEqual(self.graph.next_entity_id, 3)
예제 #6
0
    def test_from_dict_when_entities_are_linked_in_loop(self):
        graph = Graph.from_dict({
            'entities': [{
                'entity_id': 1,
                'name': 'E1'
            }, {
                'entity_id': 2,
                'name': 'E2'
            }, {
                'entity_id': 3,
                'name': 'E3'
            }],
            'links': [{
                'from': 1,
                'to': 2
            }, {
                'from': 2,
                'to': 3
            }, {
                'from': 3,
                'to': 1
            }]
        })
        e1 = graph.entities[1]
        e2 = graph.entities[2]
        e3 = graph.entities[3]

        self.assert_links(e1, successors=set([e2]), predecessors=set([e3]))
        self.assert_links(e2, successors=set([e3]), predecessors=set([e1]))
        self.assert_links(e3, successors=set([e1]), predecessors=set([e2]))
예제 #7
0
    def test_to_dict_when_entities_are_linked_in_loop(self):
        graph = Graph()
        e1 = Entity(1, 'E1')
        e2 = Entity(2, 'E2')
        graph.add_entity(e1)
        graph.add_entity(e2)
        graph.link_entities(e1, e2)
        graph.link_entities(e2, e1)
        self.assertDictEqual(graph.entities, {1: e1, 2: e2})

        json_dict = graph.to_dict()

        self.assert_graph_dict(
            json_dict, {
                'entities': [{
                    'entity_id': 1,
                    'name': 'E1'
                }, {
                    'entity_id': 2,
                    'name': 'E2'
                }],
                'links': [{
                    'from': 1,
                    'to': 2
                }, {
                    'from': 2,
                    'to': 1
                }],
            })
예제 #8
0
    def test_to_dict_when_no_links_exist(self):
        graph = Graph()
        e1 = Entity(1, 'E1', 'D1')
        e2 = Entity(2, 'E2')
        graph.add_entity(e1)
        graph.add_entity(e2)
        self.assertDictEqual(graph.entities, {1: e1, 2: e2})

        json_dict = graph.to_dict()

        self.assert_graph_dict(
            json_dict, {
                'entities': [{
                    'entity_id': 1,
                    'name': 'E1',
                    'description': 'D1'
                }, {
                    'entity_id': 2,
                    'name': 'E2'
                }],
                'links': [],
            })
예제 #9
0
    def test_graph_clone_when_provided_entity_is_isolated(self):
        graph = Graph.from_dict({
            'entities': [
                {
                    'entity_id': 1,
                    'name': 'E1'
                },
                {
                    'entity_id': 2,
                    'name': 'E2'
                },
                {
                    'entity_id': 3,
                    'name': 'E3'
                },
            ],
            'links': [{
                'from': 1,
                'to': 2
            }]
        })
        graph.clone(3)

        self.assert_graph_dict(
            graph.to_dict(), {
                'entities': [
                    {
                        'entity_id': 1,
                        'name': 'E1'
                    },
                    {
                        'entity_id': 2,
                        'name': 'E2'
                    },
                    {
                        'entity_id': 3,
                        'name': 'E3'
                    },
                    {
                        'entity_id': 4,
                        'name': 'E3'
                    },
                ],
                'links': [
                    {
                        'from': 1,
                        'to': 2
                    },
                ]
            })
예제 #10
0
    def test_from_dict_when_links_does_not_exist_in_dict(self):
        graph = Graph.from_dict({
            'entities': [{
                'entity_id': 1,
                'name': 'E1'
            }, {
                'entity_id': 2,
                'name': 'E2'
            }]
        })

        self.assertEqual(len(graph.entities), 2)
        self.assertListEqual(list(graph.entities.keys()), [1, 2])
        self.assert_links(graph.entities[1])
        self.assert_links(graph.entities[2])
예제 #11
0
def process(input_file, entity_id, sort_keys_and_objects=False):
  """
  Function that processes input and returns string to be written 
  in stdout.
  
  :param input_file: 
    input file
  :param entity_id: 
    root entity id to start cloning from
  :param sort_keys_and_objects: 
    if set to True, enables sorting of keys and objects in output json
  """
  graph = Graph.from_dict(from_json_file(input_file), sort_links=sort_keys_and_objects)
  graph.clone(entity_id)
  return json.dumps(graph.to_dict(), indent=4, sort_keys=sort_keys_and_objects)
예제 #12
0
    def test_graph_clone_when_provided_id_exists_in_graph(self):
        graph = Graph.from_dict({
            'entities': [{
                'entity_id': 1,
                'name': 'E1'
            }, {
                'entity_id': 2,
                'name': 'E2',
                'description': 'test'
            }],
            'links': [{
                'from': 1,
                'to': 2
            }]
        })
        graph.clone(2)

        self.assert_graph_dict(
            graph.to_dict(), {
                'entities': [
                    {
                        'entity_id': 1,
                        'name': 'E1'
                    },
                    {
                        'entity_id': 2,
                        'name': 'E2',
                        'description': 'test'
                    },
                    {
                        'entity_id': 3,
                        'name': 'E2',
                        'description': 'test'
                    },
                ],
                'links': [
                    {
                        'from': 1,
                        'to': 3
                    },
                    {
                        'from': 1,
                        'to': 2
                    },
                ]
            })
예제 #13
0
 def test_graph_clone_when_provided_id_doesnt_exist_in_graph(self):
     input_dict = {
         'entities': [{
             'entity_id': 1,
             'name': 'E1'
         }, {
             'entity_id': 2,
             'name': 'E2'
         }],
         'links': [{
             'from': 1,
             'to': 2
         }]
     }
     graph = Graph.from_dict(input_dict)
     graph.clone(3)
     self.assert_graph_dict(graph.to_dict(), input_dict)
예제 #14
0
    def test_from_dict_when_two_groups_exist(self):
        graph = Graph.from_dict({
            'entities': [
                # Group 1
                {
                    'entity_id': 1,
                    'name': 'E1'
                },
                {
                    'entity_id': 2,
                    'name': 'E2'
                },
                # Group 2
                {
                    'entity_id': 3,
                    'name': 'E3'
                },
                {
                    'entity_id': 4,
                    'name': 'E4'
                },
            ],
            'links': [
                # Group 1
                {
                    'from': 1,
                    'to': 2
                },
                # Group 2
                {
                    'from': 3,
                    'to': 4
                },
            ]
        })
        e1 = graph.entities[1]
        e2 = graph.entities[2]
        e3 = graph.entities[3]
        e4 = graph.entities[4]

        self.assert_links(e1, successors=set([e2]))
        self.assert_links(e2, predecessors=set([e1]))
        self.assert_links(e3, successors=set([e4]))
        self.assert_links(e4, predecessors=set([e3]))
예제 #15
0
 def setUp(self):
     self.graph = Graph()
예제 #16
0
class TestGraphAddEntity(TestCase):
    def setUp(self):
        self.graph = Graph()

    def test_add_entity_when_none_is_passed(self):
        self.assertDictEqual(self.graph.entities, {})

        self.graph.add_entity(None)

        self.assertDictEqual(self.graph.entities, {})

    def test_add_entity_when_graph_contains_no_entities(self):
        self.assertDictEqual(self.graph.entities, {})

        new_entity = Entity(1, 'Entity_1')
        self.graph.add_entity(new_entity)

        self.assertDictEqual(self.graph.entities, {1: new_entity})

    def test_add_entity_when_graph_contains_another_entity(self):
        entity_1 = Entity(1, 'Entity_1')
        self.graph.entities = {1: entity_1}
        self.assertDictEqual(self.graph.entities, {1: entity_1})

        new_entity = Entity(2, 'Entity_2')
        self.graph.add_entity(new_entity)

        self.assertDictEqual(self.graph.entities, {1: entity_1, 2: new_entity})

    def test_add_entity_when_graph_contains_another_entity_with_same_id(self):
        old_entity = Entity(1, 'old')
        self.graph.entities = {1: old_entity}
        self.assertDictEqual(self.graph.entities, {1: old_entity})

        new_entity = Entity(1, 'new')
        self.graph.add_entity(new_entity)

        self.assertDictEqual(self.graph.entities, {1: new_entity})

    def test_add_entity_sets_next_entity_id(self):
        self.assertEqual(self.graph.next_entity_id, 1)
        new_entity = Entity(1, 'Entity_1')
        self.graph.add_entity(new_entity)

        self.assertEqual(self.graph.next_entity_id, 2)

    def test_add_entity_doesnt_set_next_entity_id_when_added_entity_has_smaller_value(
            self):
        self.graph.next_entity_id = 3
        self.assertEqual(self.graph.next_entity_id, 3)
        new_entity = Entity(1, 'Entity_1')
        self.graph.add_entity(new_entity)

        self.assertEqual(self.graph.next_entity_id, 3)
예제 #17
0
    def test_graph_clone_when_graph_has_multiple_loops(self):
        graph = Graph.from_dict({
            'entities': [
                {
                    'entity_id': 1,
                    'name': 'E1'
                },
                {
                    'entity_id': 2,
                    'name': 'E2'
                },
                {
                    'entity_id': 3,
                    'name': 'E3'
                },
                {
                    'entity_id': 4,
                    'name': 'E4'
                },
                {
                    'entity_id': 5,
                    'name': 'E5'
                },
                {
                    'entity_id': 6,
                    'name': 'E6'
                },
            ],
            'links': [
                {
                    'from': 1,
                    'to': 2
                },
                {
                    'from': 2,
                    'to': 3
                },
                {
                    'from': 3,
                    'to': 4
                },
                {
                    'from': 4,
                    'to': 2
                },
                {
                    'from': 4,
                    'to': 5
                },
                {
                    'from': 5,
                    'to': 6
                },
                {
                    'from': 6,
                    'to': 4
                },
            ]
        })
        graph.clone(3)

        self.assert_graph_dict(
            graph.to_dict(),
            {
                'entities': [
                    {
                        'entity_id': 1,
                        'name': 'E1'
                    },
                    {
                        'entity_id': 2,
                        'name': 'E2'
                    },
                    {
                        'entity_id': 3,
                        'name': 'E3'
                    },
                    {
                        'entity_id': 4,
                        'name': 'E4'
                    },
                    {
                        'entity_id': 5,
                        'name': 'E5'
                    },
                    {
                        'entity_id': 6,
                        'name': 'E6'
                    },
                    # Cloned
                    {
                        'entity_id': 7,
                        'name': 'E3'
                    },
                    # Loop 1
                    {
                        'entity_id': 8,
                        'name': 'E4'
                    },
                    {
                        'entity_id': 9,
                        'name': 'E2'
                    },
                    # Loop 2
                    {
                        'entity_id': 10,
                        'name': 'E5'
                    },
                    {
                        'entity_id': 11,
                        'name': 'E6'
                    },
                ],
                'links': [
                    {
                        'from': 1,
                        'to': 2
                    },
                    {
                        'from': 2,
                        'to': 3
                    },
                    {
                        'from': 3,
                        'to': 4
                    },
                    {
                        'from': 4,
                        'to': 2
                    },
                    {
                        'from': 4,
                        'to': 5
                    },
                    {
                        'from': 5,
                        'to': 6
                    },
                    {
                        'from': 6,
                        'to': 4
                    },
                    # Cloned
                    {
                        'from': 2,
                        'to': 7
                    },  # connects the new subgraph
                    # Loop 1
                    {
                        'from': 7,
                        'to': 8
                    },
                    {
                        'from': 8,
                        'to': 9
                    },
                    {
                        'from': 9,
                        'to': 7
                    },
                    # Loop 2
                    {
                        'from': 8,
                        'to': 10
                    },
                    {
                        'from': 10,
                        'to': 11
                    },
                    {
                        'from': 11,
                        'to': 8
                    },
                ]
            })
예제 #18
0
    def test_graph_clone_when_provided_entity_is_in_a_loop(self):
        graph = Graph.from_dict({
            'entities': [
                {
                    'entity_id': 1,
                    'name': 'E1'
                },
                {
                    'entity_id': 2,
                    'name': 'E2'
                },
                {
                    'entity_id': 3,
                    'name': 'E3'
                },
                {
                    'entity_id': 4,
                    'name': 'E4'
                },
            ],
            'links': [{
                'from': 1,
                'to': 2
            }, {
                'from': 2,
                'to': 3
            }, {
                'from': 3,
                'to': 4
            }, {
                'from': 4,
                'to': 2
            }]
        })
        graph.clone(3)

        self.assert_graph_dict(
            graph.to_dict(),
            {
                'entities': [
                    {
                        'entity_id': 1,
                        'name': 'E1'
                    },
                    {
                        'entity_id': 2,
                        'name': 'E2'
                    },
                    {
                        'entity_id': 3,
                        'name': 'E3'
                    },
                    {
                        'entity_id': 4,
                        'name': 'E4'
                    },
                    # Cloned
                    {
                        'entity_id': 5,
                        'name': 'E3'
                    },
                    {
                        'entity_id': 6,
                        'name': 'E4'
                    },
                    {
                        'entity_id': 7,
                        'name': 'E2'
                    },
                ],
                'links': [
                    {
                        'from': 1,
                        'to': 2
                    },
                    {
                        'from': 2,
                        'to': 3
                    },
                    {
                        'from': 3,
                        'to': 4
                    },
                    {
                        'from': 4,
                        'to': 2
                    },
                    # Cloned
                    {
                        'from': 2,
                        'to': 5
                    },  # connects the new subgraph
                    {
                        'from': 5,
                        'to': 6
                    },
                    {
                        'from': 6,
                        'to': 7
                    },
                    {
                        'from': 7,
                        'to': 5
                    },
                ]
            })
예제 #19
0
    def test_graph_clone_when_provided_entity_has_multiple_links(self):
        graph = Graph.from_dict(
            {
                'entities': [
                    {
                        'entity_id': 1,
                        'name': 'E1'
                    },
                    {
                        'entity_id': 2,
                        'name': 'E2'
                    },
                    {
                        'entity_id': 3,
                        'name': 'E3'
                    },
                    {
                        'entity_id': 4,
                        'name': 'E4'
                    },
                    {
                        'entity_id': 5,
                        'name': 'E5'
                    },
                ],
                'links': [{
                    'from': 1,
                    'to': 3
                }, {
                    'from': 2,
                    'to': 3
                }, {
                    'from': 3,
                    'to': 4
                }, {
                    'from': 3,
                    'to': 5
                }]
            },
            sort_links=True)
        graph.clone(3)

        self.assert_graph_dict(
            graph.to_dict(),
            {
                'entities': [
                    {
                        'entity_id': 1,
                        'name': 'E1'
                    },
                    {
                        'entity_id': 2,
                        'name': 'E2'
                    },
                    {
                        'entity_id': 3,
                        'name': 'E3'
                    },
                    {
                        'entity_id': 4,
                        'name': 'E4'
                    },
                    {
                        'entity_id': 5,
                        'name': 'E5'
                    },
                    # Cloned
                    {
                        'entity_id': 6,
                        'name': 'E3'
                    },
                    {
                        'entity_id': 7,
                        'name': 'E4'
                    },
                    {
                        'entity_id': 8,
                        'name': 'E5'
                    },
                ],
                'links': [
                    {
                        'from': 1,
                        'to': 3
                    },
                    {
                        'from': 2,
                        'to': 3
                    },
                    {
                        'from': 3,
                        'to': 4
                    },
                    {
                        'from': 3,
                        'to': 5
                    },
                    # Cloned
                    {
                        'from': 1,
                        'to': 6
                    },
                    {
                        'from': 2,
                        'to': 6
                    },
                    {
                        'from': 6,
                        'to': 7
                    },
                    {
                        'from': 6,
                        'to': 8
                    },
                ]
            })
예제 #20
0
 def setUp(self):
     self.graph = Graph()
     self.entity_1 = Entity(1, 'E1')
     self.entity_2 = Entity(2, 'E2')
     self.graph.add_entity(self.entity_1)
     self.graph.add_entity(self.entity_2)
예제 #21
0
 def test_graph_clone_when_graph_is_empty(self):
     graph = Graph.from_dict({})
     graph.clone(1)
     self.assert_graph_dict(graph.to_dict(), {'entities': [], 'links': []})
예제 #22
0
 def test_from_dict_when_entities_does_not_exist_in_dict(self):
     graph = Graph.from_dict({'links': [{'from': 1, 'to': 2}]})
     self.assertDictEqual(graph.entities, {})