def test_dependent_output_reduction(self): code = 'from blockcanvas.debug.my_operator import add, mul\n' \ 'c = add(a,b)\n' \ 'd = mul(c,16)\n' block = Block(code) context = DataContext() context['a'] = 1 context['b'] = 2 sub_block = block.restrict(outputs=('d')) self.assertEqual(sub_block.inputs, set(['a', 'b'])) sub_block.execute(context) self.assertTrue(context.has_key('c')) self.assertEqual(context['c'], 3) self.assertTrue(context.has_key('d')) self.assertEqual(context['d'], 48)
def test_unbound_inputs(self): code = 'from blockcanvas.debug.my_operator import add, mul\n' \ "c = add(a,b)\n" \ "d = mul(c,a)\n" \ "z = add(b, 2)" block = Block(code) context = DataContext() context['a'] = 2 context['b'] = 1 sub_block = block.restrict(inputs=('a')) self.assertEqual(sub_block.inputs, set(['a', 'b'])) self.assertEqual(sub_block.outputs, set(['c', 'd', 'add', 'mul'])) sub_block.execute(context) self.assertTrue(context.has_key('c')) self.assertEqual(context['c'], 3) self.assertTrue(context.has_key('d')) self.assertEqual(context['d'], 6)
def test_load_block_from_file(self): """ Does loading script from file work correctly ? """ # Preparing the test case file_path = os.path.join(self.dir_name, 'test_load_block.py') file_object = open(file_path, 'w') file_object.write(self.code) file_object.close() # Load the script from the file. self.app.block_unit = BlockUnit() self.app.load_block_from_file(file_path) # Cleanup after loading the file os.remove(file_path) # Test the block unit context = DataContext(name='dummy_context', _bindings={'a': 1, 'b': 2}) self.app.block_unit.codeblock.execute(context) self.assertTrue(context.has_key('c')) self.assertEqual(context['c'], 3)
def test_load_block_from_file(self): """ Does loading script from file work correctly ? """ # Preparing the test case file_path = os.path.join(self.dir_name, 'test_load_block.py') file_object = open(file_path, 'w') file_object.write(self.code) file_object.close() # Load the script from the file. self.app.block_unit = BlockUnit() self.app.load_block_from_file(file_path) # Cleanup after loading the file os.remove(file_path) # Test the block unit context = DataContext(name = 'dummy_context', _bindings = {'a':1, 'b':2}) self.app.block_unit.codeblock.execute(context) self.assertTrue(context.has_key('c')) self.assertEqual(context['c'], 3)