def test_save_new_project(self): """ Does saving a new project to file work correctly ? """ context = DataContext(name='dummy_context', _bindings={'a': 1, 'b': 2}) self.app.block_unit = BlockUnit(code=self.code, data_context=context) project_name = create_unique_project_name(self.dir_name, 'test_save_project') project_file = os.path.join(self.dir_name, project_name + '.prj') self.app.save_project_to_file(project_file) # Check if the new files exist project_dir = os.path.join(self.dir_name, project_name + '_files') context_file = os.path.join(project_dir, project_name + '.pickle') script_file = os.path.join(project_dir, project_name + '.py') layout_file = os.path.join(project_dir, 'layout.pickle') # print project_file, project_dir, context_file, script_file self.assertTrue([ os.path.exists(project_file), os.path.exists(project_dir), os.path.exists(context_file), os.path.exists(script_file) ]) # Check if the code was saved out correctly file_object = open(script_file, 'r') check_code = file_object.read() file_object.close() a, b = 1, 2 exec check_code self.assertTrue(c, 3) # Check if the project file was written out correctly file_object = open(project_file, 'rb') check_code = file_object.read() file_object.close() actual_result = [ line[line.find('=') + 1:].strip() for line in check_code.split('\n') ] self.assertTrue( actual_result == [script_file, context_file, layout_file]) # Check if the context is correct retrieved_context = DataContext.load_context_from_file(context_file) expected_keys = set(['a', 'b', 'c']) self.assertEqual(retrieved_context.name, 'dummy_context') self.assertEqual(set(retrieved_context.keys()), expected_keys) self.assertEqual(retrieved_context['c'], 3) # Cleanup os.remove(project_file) os.remove(script_file) os.remove(context_file) os.remove(layout_file) os.rmdir(project_dir)
def test_save_new_project(self): """ Does saving a new project to file work correctly ? """ context = DataContext(name = 'dummy_context', _bindings = {'a':1, 'b':2}) self.app.block_unit = BlockUnit(code=self.code, data_context = context) project_name = create_unique_project_name(self.dir_name, 'test_save_project') project_file = os.path.join(self.dir_name,project_name+'.prj') self.app.save_project_to_file(project_file) # Check if the new files exist project_dir = os.path.join(self.dir_name,project_name+'_files') context_file = os.path.join(project_dir, project_name+'.pickle') script_file = os.path.join(project_dir, project_name+'.py') layout_file = os.path.join(project_dir, 'layout.pickle') # print project_file, project_dir, context_file, script_file self.assertTrue([os.path.exists(project_file), os.path.exists(project_dir), os.path.exists(context_file), os.path.exists(script_file)]) # Check if the code was saved out correctly file_object = open(script_file, 'r') check_code = file_object.read() file_object.close() a, b = 1,2 exec check_code self.assertTrue(c, 3) # Check if the project file was written out correctly file_object = open(project_file, 'rb') check_code = file_object.read() file_object.close() actual_result = [line[line.find('=')+1:].strip() for line in check_code.split('\n')] self.assertTrue(actual_result==[script_file, context_file, layout_file]) # Check if the context is correct retrieved_context = DataContext.load_context_from_file(context_file) expected_keys = set(['a', 'b', 'c']) self.assertEqual(retrieved_context.name, 'dummy_context') self.assertEqual(set(retrieved_context.keys()), expected_keys) self.assertEqual(retrieved_context['c'], 3) # Cleanup os.remove(project_file) os.remove(script_file) os.remove(context_file) os.remove(layout_file) os.rmdir(project_dir)