Exemplo n.º 1
0
    def test_should_be_able_to_construct_object_from_recipe_on_type_in_node_id_with_id_suffix(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType1-ModelTypeComplexConstructor60'

        self.agraph_compiler.register_node_builder(
            ModelTypeComplexConstructor,
            lambda id: ModelTypeComplexConstructor({
                'id': int(id),
                'property': 'value'
            }))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]),
                      [ModelType1, ModelTypeComplexConstructor])
        self.assertIn(type(self.graph[0][1]),
                      [ModelType1, ModelTypeComplexConstructor])

        complex_object = next(
            filter(lambda node: isinstance(node, ModelTypeComplexConstructor),
                   self.graph[0]))
        self.assertEqual(complex_object.id, 60)
        self.assertEqual(complex_object.property, 'value')
Exemplo n.º 2
0
    def test_should_use_type_correct_recipe_for_relation_build(self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        node_1 = ModelType1(1)
        node_2 = ModelType2(1)
        list_node = ['this_node_is_']
        self.agraph_model.register_node('N1', node_1)
        self.agraph_model.register_node('N2', node_2)
        self.agraph_model.register_node('N3', list_node)

        self.agraph_representation = r'N3-N1'

        self.agraph_compiler.register_relation_builder(
            ModelType1, list, lambda model_type_1_instance, list_node_instance:
            list_node_instance.append('beautiful'))
        self.agraph_compiler.register_relation_builder(
            ModelType2, list, lambda model_type_2_instance, list_node_instance:
            list_node_instance.append('hideous'))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(list_node), 2)
        self.assertEqual(list_node[0] + list_node[1], 'this_node_is_beautiful')
Exemplo n.º 3
0
    def test_should_construct_object_with_recipe_over_auto_detected_class(
            self):  #TODO Consider the desired behavior!
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType1-ModelTypeComplexConstructorAuto'

        self.agraph_compiler.register_node_builder(
            ModelTypeComplexConstructor,
            lambda id: ModelTypeComplexConstructor({
                'id': id,
                'property': 'value'
            }))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]),
                      [ModelType1, ModelTypeComplexConstructor])
        self.assertIn(type(self.graph[0][1]),
                      [ModelType1, ModelTypeComplexConstructor])
        complex_object = next(
            filter(lambda node: isinstance(node, ModelTypeComplexConstructor),
                   self.graph[0]))
        self.assertEqual(complex_object.id, 'Auto')
        self.assertEqual(complex_object.property, 'value')
Exemplo n.º 4
0
class TestBrokenGraphs():
    def setUp(self):
        self.agraph_model = AGraphModel()
        self.agraph_model.register_node('N0', 'node0')
        self.agraph_model.register_node('N1', 'node1')
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

    def tearDown(self):
        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

    def test_unattached_edge(self):
        self.agraph_representation = r'''
        N0-N1-
        '''
        # should raise exception or ignore bad edge
        pass

    def test_crossing_edge(self):
        self.agraph_representation = r'''
           N2
           |
        N0---N1
           |
           N3
        '''
        # should raise exception or ignore bad edge
        pass
Exemplo n.º 5
0
 def tearDown(self):
     self.agraph_model = AGraphModel()
     self.agraph_compiler = AGraphCompiler(self.agraph_model)
     self.agraph_compiler.set_representation(self.agraph_representation)
     self.__register_nodes_from_representation()
     self.graph = self.agraph_compiler.compile()
     self.assertEqual(len(self.graph), self.tested_edges)
     self.__assert_nodes_on_graph()
     self.__assert_explicit_edges()
Exemplo n.º 6
0
    def test_should_associate_node_id_with_registered_instance(self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        node_instance = ModelType1(1)
        self.agraph_model.register_node('N1', node_instance)

        self.agraph_representation = r'N1-N1'

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertTrue(node_instance in self.graph[0])
Exemplo n.º 7
0
    def test_should_be_able_to_construct_object_on_type_in_node_id_if_id_not_registered(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType2-ModelType1'

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]), [ModelType1, ModelType2])
        self.assertIn(type(self.graph[0][1]), [ModelType1, ModelType2])
Exemplo n.º 8
0
    def test_should_be_able_to_construct_object_on_type_in_node_id_with_id_suffix(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType260-ModelType111'

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]), [ModelType1, ModelType2])
        self.assertIn(type(self.graph[0][1]), [ModelType1, ModelType2])
        self.assertIn(self.graph[0][0].id, [60, 11])
        self.assertIn(self.graph[0][1].id, [60, 11])
Exemplo n.º 9
0
    def test_should_identify_multiedge_node_constructed_from_type_in_node(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'''
            ModelType-ModelType
                \       /
                ModelType
        '''

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        ids = set()
        for edge in self.graph:
            ids.update([edge[0].id, edge[1].id])
        self.assertEqual(len(ids), 3)
Exemplo n.º 10
0
    def test_should_use_recipe_for_relation_build_on_notdirectional_edge(self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        node_1 = ModelType1(1)
        node_2 = ModelType2(1)
        self.agraph_model.register_node('N1', node_1)
        self.agraph_model.register_node('N2', node_2)

        self.agraph_representation = r'N1-N2'

        self.agraph_compiler.register_relation_builder(
            ModelType1, ModelType2,
            lambda model_type_1_instance, model_type_2_instance:
            model_type_2_instance.add_mode_1_instance(model_type_1_instance))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertTrue(node_1 in self.graph[0] and node_2 in self.graph[0])
        self.assertTrue(node_1 in node_2.model_type_1_instances)
Exemplo n.º 11
0
 def __init__(self):
     self.model: AGraphModel = AGraphModel()
     self.compiler: AGraphCompiler = AGraphCompiler(self.model)
Exemplo n.º 12
0
class TestAGraphModel(unittest.TestCase):
    def test_should_associate_node_id_with_registered_instance(self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        node_instance = ModelType1(1)
        self.agraph_model.register_node('N1', node_instance)

        self.agraph_representation = r'N1-N1'

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertTrue(node_instance in self.graph[0])

    def test_should_use_recipe_for_relation_build_on_notdirectional_edge(self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        node_1 = ModelType1(1)
        node_2 = ModelType2(1)
        self.agraph_model.register_node('N1', node_1)
        self.agraph_model.register_node('N2', node_2)

        self.agraph_representation = r'N1-N2'

        self.agraph_compiler.register_relation_builder(
            ModelType1, ModelType2,
            lambda model_type_1_instance, model_type_2_instance:
            model_type_2_instance.add_mode_1_instance(model_type_1_instance))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertTrue(node_1 in self.graph[0] and node_2 in self.graph[0])
        self.assertTrue(node_1 in node_2.model_type_1_instances)

    def test_should_use_type_correct_recipe_for_relation_build(self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        node_1 = ModelType1(1)
        node_2 = ModelType2(1)
        list_node = ['this_node_is_']
        self.agraph_model.register_node('N1', node_1)
        self.agraph_model.register_node('N2', node_2)
        self.agraph_model.register_node('N3', list_node)

        self.agraph_representation = r'N3-N1'

        self.agraph_compiler.register_relation_builder(
            ModelType1, list, lambda model_type_1_instance, list_node_instance:
            list_node_instance.append('beautiful'))
        self.agraph_compiler.register_relation_builder(
            ModelType2, list, lambda model_type_2_instance, list_node_instance:
            list_node_instance.append('hideous'))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(list_node), 2)
        self.assertEqual(list_node[0] + list_node[1], 'this_node_is_beautiful')

    def test_should_use_reversed_recipe_for_relation_build_if_singledirection_recipe_provided_on_notdirectional_edge(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        node_1 = ModelType1(1)
        node_2 = ModelType2(1)
        self.agraph_model.register_node('N1', node_1)
        self.agraph_model.register_node('N2', node_2)
        self.agraph_compiler.register_relation_builder(
            ModelType1, ModelType2,
            lambda model_type_1_instance, model_type_2_instance:
            model_type_2_instance.add_mode_1_instance(model_type_1_instance))

        self.agraph_representation = r'N1-N2'
        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()
        self.assertTrue(node_1 in self.graph[0] and node_2 in self.graph[0])
        self.assertTrue(node_1 in node_2.model_type_1_instances)

        self.reversed_agraph_model = AGraphModel()
        self.reversed_agraph_compiler = AGraphCompiler(
            self.reversed_agraph_model)

        self.reversed_agraph_model.register_node('N1', node_1)
        self.reversed_agraph_model.register_node('N2', node_2)
        self.reversed_agraph_compiler.register_relation_builder(
            ModelType1, ModelType2,
            lambda model_type_1_instance, model_type_2_instance:
            model_type_2_instance.add_mode_1_instance(model_type_1_instance))

        self.reversed_agraph_representation = r'N2-N1'
        self.reversed_agraph_compiler.set_representation(
            self.reversed_agraph_representation)
        self.graph = self.reversed_agraph_compiler.compile()
        self.assertTrue(node_1 in self.graph[0] and node_2 in self.graph[0])
        self.assertTrue(node_1 in node_2.model_type_1_instances)

    def test_should_be_able_to_construct_object_on_type_in_node_id_if_id_not_registered(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType2-ModelType1'

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]), [ModelType1, ModelType2])
        self.assertIn(type(self.graph[0][1]), [ModelType1, ModelType2])

    def test_should_be_able_to_construct_object_from_recipe_on_type_in_node_id_if_recipe_provided(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType1-ModelTypeComplexConstructor'

        self.agraph_compiler.register_node_builder(
            ModelTypeComplexConstructor,
            lambda: ModelTypeComplexConstructor({
                'id': 1,
                'property': 'value'
            }))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]),
                      [ModelType1, ModelTypeComplexConstructor])
        self.assertIn(type(self.graph[0][1]),
                      [ModelType1, ModelTypeComplexConstructor])

        complex_object = next(
            filter(lambda node: isinstance(node, ModelTypeComplexConstructor),
                   self.graph[0]))
        self.assertEqual(complex_object.property, 'value')

    def test_should_be_able_to_construct_object_from_recipe_on_type_in_node_id_with_id_suffix(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType1-ModelTypeComplexConstructor60'

        self.agraph_compiler.register_node_builder(
            ModelTypeComplexConstructor,
            lambda id: ModelTypeComplexConstructor({
                'id': int(id),
                'property': 'value'
            }))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]),
                      [ModelType1, ModelTypeComplexConstructor])
        self.assertIn(type(self.graph[0][1]),
                      [ModelType1, ModelTypeComplexConstructor])

        complex_object = next(
            filter(lambda node: isinstance(node, ModelTypeComplexConstructor),
                   self.graph[0]))
        self.assertEqual(complex_object.id, 60)
        self.assertEqual(complex_object.property, 'value')

    def test_should_construct_object_with_recipe_over_auto_detected_class(
            self):  #TODO Consider the desired behavior!
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType1-ModelTypeComplexConstructorAuto'

        self.agraph_compiler.register_node_builder(
            ModelTypeComplexConstructor,
            lambda id: ModelTypeComplexConstructor({
                'id': id,
                'property': 'value'
            }))

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]),
                      [ModelType1, ModelTypeComplexConstructor])
        self.assertIn(type(self.graph[0][1]),
                      [ModelType1, ModelTypeComplexConstructor])
        complex_object = next(
            filter(lambda node: isinstance(node, ModelTypeComplexConstructor),
                   self.graph[0]))
        self.assertEqual(complex_object.id, 'Auto')
        self.assertEqual(complex_object.property, 'value')

    def test_should_be_able_to_construct_object_on_type_in_node_id_with_id_suffix(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'ModelType260-ModelType111'

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertIn(type(self.graph[0][0]), [ModelType1, ModelType2])
        self.assertIn(type(self.graph[0][1]), [ModelType1, ModelType2])
        self.assertIn(self.graph[0][0].id, [60, 11])
        self.assertIn(self.graph[0][1].id, [60, 11])

    def test_should_identify_multiedge_node_constructed_from_type_in_node(
            self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

        self.agraph_representation = r'''
            ModelType-ModelType
                \       /
                ModelType
        '''

        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        ids = set()
        for edge in self.graph:
            ids.update([edge[0].id, edge[1].id])
        self.assertEqual(len(ids), 3)

    def test_should_be_able_to_register_model_type(self):
        pass

    def test_should_be_able_to_construct_many_to_many_relation(self):
        pass
Exemplo n.º 13
0
 def __init__(self, model: AGraphModel = None):
     self.model = model or AGraphModel()
     self.__build_class_registry()
Exemplo n.º 14
0
 def setUp(self):
     self.agraph_model = AGraphModel()
     self.agraph_model.register_node('N0', 'node0')
     self.agraph_model.register_node('N1', 'node1')
     self.agraph_compiler = AGraphCompiler(self.agraph_model)
Exemplo n.º 15
0
class TestBasicRepresentations(unittest.TestCase):
    def setUp(self):
        self.agraph_model = AGraphModel()
        self.agraph_model.register_node('N0', 'node0')
        self.agraph_model.register_node('N1', 'node1')
        self.agraph_compiler = AGraphCompiler(self.agraph_model)

    def tearDown(self):
        self.agraph_compiler.set_representation(self.agraph_representation)
        self.graph = self.agraph_compiler.compile()

        self.assertEqual(len(self.graph), 1)
        self.assertTrue('node0' in self.graph[0])
        self.assertTrue('node1' in self.graph[0])
        self.graph = None

    def test_vertical(self):
        self.agraph_representation = r'''
            N0
            |
            N1
        '''

    def test_horizontal(self):
        self.agraph_representation = r'N0-N1'

    def test_backslash(self):
        self.agraph_representation = r'''
            N0
              \
               N1
        '''

    def test_forwardslash(self):
        self.agraph_representation = r'''
               N0
              /
            N1
        '''

    def test_multisegment_vertical(self):
        self.agraph_representation = r'''
            N0
            |
            |
            N1
        '''

    def test_multisegment_horizontal(self):
        self.agraph_representation = r'N0--N1'

    def test_multisegment_backslash(self):
        self.agraph_representation = r'''
            N0
              \
               \
                N1
        '''

    def test_multisegment_forwardslash(self):
        self.agraph_representation = r'''
Exemplo n.º 16
0
class TestComplexRepresentations(unittest.TestCase):
    def setUp(self):
        self.tested_nodes = 1
        self.tested_edges = 1
        self.assert_pairs = []

    def tearDown(self):
        self.agraph_model = AGraphModel()
        self.agraph_compiler = AGraphCompiler(self.agraph_model)
        self.agraph_compiler.set_representation(self.agraph_representation)
        self.__register_nodes_from_representation()
        self.graph = self.agraph_compiler.compile()
        self.assertEqual(len(self.graph), self.tested_edges)
        self.__assert_nodes_on_graph()
        self.__assert_explicit_edges()

    def __assert_nodes_on_graph(self) -> None:
        connected_nodes = [node for edge in self.graph for node in edge]
        for index in range(0, self.tested_nodes):
            self.assertTrue(
                f'node{index}' in connected_nodes,
                msg=f'node{index} should be in any pair of {self.graph}')

    def __assert_explicit_edges(self) -> None:
        for nodes_pair in list(
                map(
                    lambda ids_pair:
                    (ids_pair[0].replace('N', 'node'), ids_pair[1].replace(
                        'N', 'node')), self.assert_pairs)):
            self.assertTrue(reduce(
                lambda result, edge: result or
                (nodes_pair[0] in edge and nodes_pair[1] in edge), self.graph,
                False),
                            msg=f'pair {nodes_pair} should be in {self.graph}')

    def __register_nodes_from_representation(self) -> None:
        for index in range(0, self.tested_nodes):
            self.agraph_model.register_node(f'N{index}', f'node{index}')

    def register_nodes_in_representation(self, count: int) -> None:
        self.tested_nodes = count

    def register_edges_in_representation(self, count: int,
                                         *pairs: tuple) -> None:
        self.tested_edges = count
        self.assert_pairs = pairs

    def test_circular_should_connect_itself(self):
        self.agraph_representation = r'''
              N0
             /  \
            *----* 
        '''

    def test_multiple_occurances_of_node(self):
        self.agraph_representation = r'''
            N0-N1-N2-N0
        '''
        self.register_nodes_in_representation(3)
        self.register_edges_in_representation(3)

    def test_tight_edges(self):
        self.agraph_representation = r'''
             N0
            /N1\
           N2| N3
          N4-*
        '''
        self.register_nodes_in_representation(5)
        self.register_edges_in_representation(3)

    def test_asterisk_should_detect_only_edges_connected_to_it_1(self):
        self.agraph_representation = r'''
         N2-*N1
            ||
           N0*-N3 
        '''
        self.register_nodes_in_representation(4)
        self.register_edges_in_representation(2, ('N0', 'N2'), ('N1', 'N3'))

    def test_asterisk_should_detect_only_edges_connected_to_it_2(self):
        self.agraph_representation = r'''
          N3
            \
         N2-*N1
             \
             N0
        '''
        self.register_nodes_in_representation(4)
        self.register_edges_in_representation(2, ('N0', 'N2'), ('N1', 'N3'))

    def test_multiple_graphs(self):
        self.agraph_representation = r'''
          N0
            \ N1--N2    N9
             N3--N4 \  /
            /  \ N5--N6--N10
          N7    N8  /  \
                   N11 N12
        '''
        self.register_nodes_in_representation(13)
        self.register_edges_in_representation(11, ('N0', 'N3'), ('N4', 'N3'),
                                              ('N7', 'N3'), ('N8', 'N3'),
                                              ('N1', 'N2'), ('N2', 'N6'),
                                              ('N5', 'N6'), ('N9', 'N6'),
                                              ('N10', 'N6'), ('N11', 'N6'),
                                              ('N12', 'N6'))

    def test_hexagram(self):
        self.agraph_representation = r'''
          N0----N1
         /|    /||\
        N7**--* |*N2
        |//     | \\
        N6      |  N3
         \      | /
          N5----N4
        '''
        self.register_nodes_in_representation(8)
        self.register_edges_in_representation(12, ('N0', 'N6'), ('N1', 'N3'),
                                              ('N1', 'N4'), ('N1', 'N6'))