def test_load_and_resave_project(self): """ Does load and resaving a project work correctly ? """ project_name = create_unique_project_name(self.dir_name, 'test_load_save_project') project_dir = os.path.join(self.dir_name, project_name+'_files') project_file = os.path.join(self.dir_name, project_name+'.prj') script_path = os.path.join(project_dir, project_name+'.py') context_path = os.path.join(project_dir, project_name+'.pickle') layout_path = os.path.join(project_dir, 'layout.pickle') script = '\n'.join(('SCRIPT_PATH = '+script_path, 'CONTEXT_PATH = '+context_path, 'LAYOUT_PATH = '+layout_path)) os.makedirs(project_dir) file_object = open(project_file, 'wb') file_object.write(script) file_object.close() file_object = open(script_path, 'w') file_object.write(self.code) file_object.close() d = DataContext(name = 'dummy_context', _bindings = {'a': 1, 'b':2, 'e': 0.01}) d.save_context_to_file(context_path) # Check if the setup is correct self.assertTrue([os.path.exists(project_file), os.path.exists(project_dir), os.path.exists(script_path), os.path.exists(context_path)]) # Load the project from the file. self.app.block_unit = BlockUnit() self.app.load_project_from_file(project_file) self.app.block_unit.codeblock.code = self.app.block_unit.codeblock.code + '\nd = add(a,c)' self.app.save_loaded_project() # The key 'context' should get removed while saving the project. expected_keys = set(['a', 'b', 'c', 'd', 'e']) self.assertEqual(set(self.app.block_unit.data_context.keys()), expected_keys) self.assertEqual(self.app.block_unit.data_context['d'], 4) # Check if the new code contains the new line file_object = open(script_path, 'r') code = file_object.read() a,b = 1,2 exec code file_object.close() self.assertEqual(d, 4) # Clean up after loading os.remove(project_file) os.remove(script_path) os.remove(context_path) os.remove(layout_path) os.rmdir(project_dir)
def test_load_project_from_file(self): """ Does loading a project from file work correctly ? """ # Preparing the test case # Make project.py, project.pickle, project.prj project_name = create_unique_project_name(self.dir_name, 'test_load_project') project_dir = os.path.join(self.dir_name, project_name + '_files') project_file = os.path.join(self.dir_name, project_name + '.prj') script_path = os.path.join(project_dir, project_name + '.py') context_path = os.path.join(project_dir, project_name + '.pickle') script = '\n'.join( ('SCRIPT_PATH = ' + script_path, 'CONTEXT_PATH = ' + context_path)) os.makedirs(project_dir) file_object = open(project_file, 'wb') file_object.write(script) file_object.close() file_object = open(script_path, 'w') file_object.write(self.code) file_object.close() d = DataContext(name='dummy_context', _bindings={ 'a': 1, 'b': 2, 'd': 0.01 }) d.save_context_to_file(context_path) # Check if the setup is correct self.assertTrue([ os.path.exists(project_file), os.path.exists(project_dir), os.path.exists(script_path), os.path.exists(context_path) ]) # Load the project from the file. self.app.block_unit = BlockUnit() self.app.load_project_from_file(project_file) # Clean up after loading os.remove(project_file) os.remove(script_path) os.remove(context_path) os.rmdir(project_dir) expected_keys = set(['a', 'b', 'c', 'd', 'context']) self.assertEqual(set(self.app.block_unit.data_context.keys()), expected_keys) self.assertTrue(self.app.block_unit.data_context.has_key('c')) self.assertEqual(self.app.block_unit.data_context['c'], 3)
def test_load_project_from_file(self): """ Does loading a project from file work correctly ? """ # Preparing the test case # Make project.py, project.pickle, project.prj project_name = create_unique_project_name(self.dir_name, 'test_load_project') project_dir = os.path.join(self.dir_name, project_name+'_files') project_file = os.path.join(self.dir_name, project_name+'.prj') script_path = os.path.join(project_dir, project_name+'.py') context_path = os.path.join(project_dir, project_name+'.pickle') script = '\n'.join(('SCRIPT_PATH = '+script_path, 'CONTEXT_PATH = '+context_path)) os.makedirs(project_dir) file_object = open(project_file, 'wb') file_object.write(script) file_object.close() file_object = open(script_path, 'w') file_object.write(self.code) file_object.close() d = DataContext(name = 'dummy_context', _bindings = {'a': 1, 'b':2, 'd': 0.01}) d.save_context_to_file(context_path) # Check if the setup is correct self.assertTrue([os.path.exists(project_file), os.path.exists(project_dir), os.path.exists(script_path), os.path.exists(context_path)]) # Load the project from the file. self.app.block_unit = BlockUnit() self.app.load_project_from_file(project_file) # Clean up after loading os.remove(project_file) os.remove(script_path) os.remove(context_path) os.rmdir(project_dir) expected_keys = set(['a', 'b', 'c', 'd', 'context']) self.assertEqual(set(self.app.block_unit.data_context.keys()), expected_keys) self.assertTrue(self.app.block_unit.data_context.has_key('c')) self.assertEqual(self.app.block_unit.data_context['c'], 3)
def test_load_and_resave_project(self): """ Does load and resaving a project work correctly ? """ project_name = create_unique_project_name(self.dir_name, 'test_load_save_project') project_dir = os.path.join(self.dir_name, project_name + '_files') project_file = os.path.join(self.dir_name, project_name + '.prj') script_path = os.path.join(project_dir, project_name + '.py') context_path = os.path.join(project_dir, project_name + '.pickle') layout_path = os.path.join(project_dir, 'layout.pickle') script = '\n'.join( ('SCRIPT_PATH = ' + script_path, 'CONTEXT_PATH = ' + context_path, 'LAYOUT_PATH = ' + layout_path)) os.makedirs(project_dir) file_object = open(project_file, 'wb') file_object.write(script) file_object.close() file_object = open(script_path, 'w') file_object.write(self.code) file_object.close() d = DataContext(name='dummy_context', _bindings={ 'a': 1, 'b': 2, 'e': 0.01 }) d.save_context_to_file(context_path) # Check if the setup is correct self.assertTrue([ os.path.exists(project_file), os.path.exists(project_dir), os.path.exists(script_path), os.path.exists(context_path) ]) # Load the project from the file. self.app.block_unit = BlockUnit() self.app.load_project_from_file(project_file) self.app.block_unit.codeblock.code = self.app.block_unit.codeblock.code + '\nd = add(a,c)' self.app.save_loaded_project() # The key 'context' should get removed while saving the project. expected_keys = set(['a', 'b', 'c', 'd', 'e']) self.assertEqual(set(self.app.block_unit.data_context.keys()), expected_keys) self.assertEqual(self.app.block_unit.data_context['d'], 4) # Check if the new code contains the new line file_object = open(script_path, 'r') code = file_object.read() a, b = 1, 2 exec code file_object.close() self.assertEqual(d, 4) # Clean up after loading os.remove(project_file) os.remove(script_path) os.remove(context_path) os.remove(layout_path) os.rmdir(project_dir)