Beispiel #1
0
 def test_raises_error_if_trying_to_register_a_factory__that_is_already_registered_as_a_definition(
         self):
     library = MockLibrary()
     library.definitions = {'ciao': {'producer': 'int'}}
     factory = lambda random_funcs, config: 10
     with self.assertRaises(ValueError):
         library.register_factory('ciao', factory)
Beispiel #2
0
 def test_can_register_a_variable(self):
     library = MockLibrary()
     library.register_variable('ciao', 1.0, SimpleType('float'))
     expected_env = {
         'ciao': 1.0,
         '::types::': {
             'ciao': SimpleType('float')
         }
     }
     self.assertEqual(expected_env, library.env())
Beispiel #3
0
 def test_can_register_a_function(self):
     library = MockLibrary()
     library.register_function('ciao', float, [SimpleType('int')],
                               SimpleType('float'))
     expected_func_env = {
         'ciao': float,
         '::types::': {
             'ciao': ([SimpleType('int')], SimpleType('float'))
         }
     }
     self.assertEqual(expected_func_env, library.func_env())
Beispiel #4
0
 def test_can_register_multiple_definitions(self):
     library = MockLibrary()
     factory_int = lambda random_funcs, config: 1
     factory_float = lambda random_funcs, config: 2.0
     library.register_factory('int', factory_int)
     library.register_factory('float', factory_float)
     library.register_definitions({
         'perc': {
             'producer': 'int'
         },
         'other': {
             'producer': 'float'
         }
     })
     self.assertEqual(1, library.make_producer('perc', {}))
     self.assertEqual(2.0, library.make_producer('other', {}))
Beispiel #5
0
 def test_can_make_schema_with_transformers(self):
     schema = make_schema_cmdline([('A', '@bob'), ('B', '%int')], [('bob', '%int')], show_header=True,
                                  library=MockLibrary())
     self.assertTrue(schema.show_header)
     self.assertEqual(('A', 'B'), schema.columns)
     producers = sorted(schema.producers, key=lambda x: x.name)
     self.assertEqual(2, len(producers))
     self.assertEqual(SimpleNamespace(name='producer#0', type='int', config={}), producers[0])
     self.assertEqual(SimpleNamespace(name='producer#1', type='int', config={}), producers[1])
     self.assertEqual(5, len(schema.transformers))
     expected_transformer_bob = SimpleNamespace(name='transformer#0', inputs=['producer#0'], outputs=['bob'],
                                                transformer=IdentityTransformer(1))
     expected_transformer_A = SimpleNamespace(name='transformer#1', inputs=['producer#0'], outputs=['A'],
                                              transformer=IdentityTransformer(1))
     expected_transformer_B = SimpleNamespace(name='transformer#2', inputs=['producer#1'], outputs=['B'],
                                              transformer=IdentityTransformer(1))
     expected_transformer_A_copy = SimpleNamespace(name='transformer#3', inputs=['producer#0'], outputs=['A'],
                                                   transformer=IdentityTransformer(1))
     expected_transformer_B_copy = SimpleNamespace(name='transformer#4', inputs=['producer#1'], outputs=['B'],
                                                   transformer=IdentityTransformer(1))
     self.assertEqual(expected_transformer_bob, schema.transformers[0])
     self.assertEqual(expected_transformer_A, schema.transformers[1])
     self.assertEqual(expected_transformer_B, schema.transformers[2])
     self.assertEqual(expected_transformer_A_copy, schema.transformers[3])
     self.assertEqual(expected_transformer_B_copy, schema.transformers[4])
Beispiel #6
0
 def test_can_make_schema_with_a_single_column(self):
     schema = make_schema_expr('%int', ['A'], show_header=True, library=MockLibrary())
     self.assertTrue(schema.show_header)
     self.assertEqual(('A',), schema.columns)
     self.assertEqual(1, len(schema.producers))
     self.assertEqual(SimpleNamespace(name='producer#0', type='int', config={}), schema.producers[0])
     self.assertEqual(1, len(schema.transformers))
     expected_transformer = SimpleNamespace(name='transformer#0', inputs=['producer#0'], outputs=['A'],
                                            transformer=IdentityTransformer(1))
     self.assertEqual(expected_transformer, schema.transformers[0])
Beispiel #7
0
 def test_raises_error_when_creating_library_if_name_has_multiple_definitions(
         self):
     with self.assertRaises(ValueError):
         MockLibrary(factories={'a': (lambda x: 1)},
                     definitions={'a': None})
Beispiel #8
0
 def test_can_lookup_definitions(self):
     library = MockLibrary()
     factory = lambda random_funcs, config: 1
     library.register_factory('int', factory)
     library.register_definition('perc', {'producer': 'int'})
     self.assertEqual(1, library.make_producer('perc', {}))
Beispiel #9
0
 def test_raises_error_if_trying_to_unknown_producer(self):
     library = MockLibrary()
     with self.assertRaises(LookupError):
         library.make_producer('ciao', {})
Beispiel #10
0
 def test_compatibility(self):
     compat = MockLibrary().compatibility()
     self.assertEqual(
         SimpleType('a'),
         compat.get_upperbound(SimpleType('a'), SimpleType('b')))
Beispiel #11
0
 def test_raises_error_if_trying_to_register_a_factory_multiple_times(self):
     library = MockLibrary()
     factory = lambda random_funcs, config: 10
     library.register_factory('ciao', factory)
     with self.assertRaises(ValueError):
         library.register_factory('ciao', factory)
Beispiel #12
0
 def test_can_register_a_definition(self):
     library = MockLibrary()
     factory = lambda random_funcs, config: 10
     library.register_factory('int', factory)
     library.register_definition('ciao', {'producer': 'int', 'config': {}})
     self.assertEqual(10, library.make_producer('ciao', {}))
Beispiel #13
0
 def test_can_register_a_factory(self):
     library = MockLibrary()
     factory = lambda random_funcs, config: 10
     library.register_factory('ciao', factory)
     self.assertEqual(factory, library.get_producer_factory('ciao'))
Beispiel #14
0
 def test_func_env(self):
     self.assertEqual({}, MockLibrary().func_env())
Beispiel #15
0
 def test_can_make_schema_with_multiple_columns(self):
     schema = make_schema_expr('%int . %int', ['A', 'B'], show_header=True, library=MockLibrary())
     self.assertTrue(schema.show_header)
     self.assertEqual(('A', 'B'), schema.columns)
     producers = sorted(schema.producers, key=lambda x: x.name)
     self.assertEqual(2, len(producers))
     self.assertEqual(SimpleNamespace(name='producer#0', type='int', config={}), producers[0])
     self.assertEqual(SimpleNamespace(name='producer#1', type='int', config={}), producers[1])
     self.assertEqual(2, len(schema.transformers))
     expected_transformer_A = SimpleNamespace(name='transformer#0', inputs=['producer#0'], outputs=['A'],
                                              transformer=IdentityTransformer(1))
     expected_transformer_B = SimpleNamespace(name='transformer#1', inputs=['producer#1'], outputs=['B'],
                                              transformer=IdentityTransformer(1))
     self.assertEqual(expected_transformer_A, schema.transformers[0])
     self.assertEqual(expected_transformer_B, schema.transformers[1])