def test_stop_step_no_halt(self): """ Should stop the step early but continue running future steps """ support.create_project(self, 'homer2') support.add_step(self, contents='\n'.join([ 'import cauldron as cd', 'cd.shared.test = 0', 'cd.shared.other = 0', 'cd.step.breathe()', 'cd.shared.test = 1', 'cd.step.stop()', 'cd.shared.test = 2' ])) support.add_step(self, contents='\n'.join([ 'import cauldron as cd', 'cd.shared.other = 1' ])) support.run_command('run') project = cd.project.get_internal_project() step = project.steps[0] self.assertEqual(project.shared.fetch('test'), 1) self.assertEqual(project.shared.fetch('other'), 1) self.assertNotEqual(-1, step.dom.find('cd-StepStop'))
def test_jinja(self): """ should add jinja template to display """ support.create_project(self, 'starbuck') project = cauldron.project.internal_project jinja_path = os.path.join( project.source_directory, 'template.html' ) with open(jinja_path, 'w') as fp: fp.write('<div>Hello {{ name }}</div>') step_contents = '\n'.join([ 'import cauldron as cd', 'cd.display.jinja("{}", name="starbuck")'.format( jinja_path.replace('\\', '\\\\') ) ]) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_exception(self): """ """ support.create_project(self, 'brad') support.add_step( self, 'brad_one.py', cli.reformat( """ import cauldron as cd a = dict( one=1, two=['1', '2'], three={'a': True, 'b': False, 'c': True} ) cd.display.inspect(a) cd.shared.a = a cd.display.workspace() """ ) ) support.add_step(self, 'brad_two.py', "1 + 's'") r = support.run_command('run .') self.assertFalse(r.failed, 'should not have failed') r = support.run_command('run .') self.assertTrue(r.failed, 'should have failed')
def test_change_title(self): """ should change title """ support.create_project(self, 'blaine') support.add_step(self) project = cauldron.project.get_internal_project() r = Response() step_actions.modify_step(response=r, project=project, name=project.steps[0].filename, title='a') self.assertFalse(r.failed) self.assertEqual(project.steps[0].definition.title, 'a') r = Response() step_actions.modify_step(response=r, project=project, name=project.steps[0].filename, title='b') self.assertFalse(r.failed) self.assertEqual(project.steps[0].definition.title, 'b') r = Response() step_actions.modify_step(response=r, project=project, name=project.steps[0].filename, new_name='first') self.assertFalse(r.failed) self.assertEqual(project.steps[0].definition.title, 'b')
def test_solo_error(self): """ should display an error """ response = support.create_project(self, 'bismark') self.assertFalse( response.failed, Message('should have created project', response=response)) code = 'x = 1 + "abcdefg"' support.add_step(self, contents=code) response = support.run_command('run -f') self.assertTrue(response.failed, Message('should have failed', response=response)) project = cauldron.project.internal_project self.assertTrue(project.steps[0].dom.find('cd-CodeError') > 0, 'should have included error dom') self.assertTrue(project.steps[0].dom.find('abcdefg') > 0, 'should have included error line of code') support.run_command('close')
def test_modify_move(self): """...""" support.create_project(self, 'harvey') support.add_step(self, contents='#S1') support.add_step(self, contents='#S2') r = support.run_command('steps list') step = r.data['steps'][-1] r = support.run_command( 'steps modify {} --position=0'.format(step['name']) ) self.assertFalse(r.failed, Message( 'Failed to move step 2 to the beginning', response=r )) r = support.run_command('steps list') step = r.data['steps'][0] with open(step['source_path'], 'r') as f: contents = f.read() self.assertEqual(contents.strip(), '#S2', Message( 'Step 2 should now be Step 1', response=r ))
def test_standard_error(self): """ should include standard error output """ response = support.create_project(self, 'bozeman') self.assertFalse( response.failed, Message('should have created project', response=response)) error_string = 'This is a standard error test' code = '\n'.join( ['import sys', 'sys.stderr.write("{}")'.format(error_string)]) support.add_step(self, contents=code) response = support.run_command('run -f') self.assertFalse(response.failed, Message('should have run step', response=response)) project = cauldron.project.get_internal_project() self.assertTrue(project.steps[0].dom.find(error_string) > 0, 'should have included error string') self.assertTrue(project.steps[0].dom.find('cd-StdErr') > 0, 'should have included StdErr dom')
def test_print_start(self): """ should properly print at the beginning of a step """ response = support.create_project(self, 'chicago') self.assertFalse( response.failed, Message('should have created project', response=response) ) print_string = string.ascii_lowercase code = '\n'.join([ 'import cauldron as cd', 'print("{}")'.format(print_string), 'cd.display.text("Hello World")' ]) support.add_step(self, contents=code) response = support.run_command('run -f') self.assertFalse( response.failed, Message('should have run step', response=response) ) project = cauldron.project.get_internal_project() dom = project.steps[0].dom # type: str self.assertEqual( dom.count(print_string), 2, 'should have printed ascii lowercase' )
def test_autocomplete(self): """ :return: """ support.create_project(self, 'gina') support.add_step(self, 'a.py') support.add_step(self, 'b.py') result = support.autocomplete('steps a') self.assertIn('add', result) result = support.autocomplete('steps modify ') self.assertEqual( len(result), 2, 'there are two steps in {}'.format(result) ) result = support.autocomplete('steps modify a.py --') self.assertIn('name=', result) result = support.autocomplete('steps modify fake.py --position=') self.assertEqual( len(result), 2, 'there are two steps in {}'.format(result) ) support.run_command('close')
def test_jinja(self): """ should add jinja template to display """ support.create_project(self, 'starbuck') project = cauldron.project.get_internal_project() jinja_path = os.path.join( project.source_directory, 'template.html' ) with open(jinja_path, 'w') as fp: fp.write('<div>Hello {{ name }}</div>') step_contents = '\n'.join([ 'import cauldron as cd', 'cd.display.jinja("{}", name="starbuck")'.format( jinja_path.replace('\\', '\\\\') ) ]) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_watch_not_needed(self): """ :return: """ support.create_project(self, 'betty') support.add_step(self) project = cd.project.internal_project project.current_step = project.steps[0] self.assertFalse( reloading.refresh(mime_text), Message('Should not reload if the step has not been run before') ) support.run_command('run') project.current_step = project.steps[0] self.assertFalse( reloading.refresh(mime_text), Message('Should not reload if module has not changed recently') ) project.current_step = None support.run_command('close')
def test_modify_move(self): """ :return: """ support.create_project(self, 'harvey') support.add_step(self, contents='#S1') support.add_step(self, contents='#S2') r = support.run_command('steps list') step = r.data['steps'][-1] r = support.run_command( 'steps modify {} --position=0'.format(step['name']) ) self.assertFalse(r.failed, Message( 'Failed to move step 2 to the beginning', response=r )) r = support.run_command('steps list') step = r.data['steps'][0] with open(step['source_path'], 'r') as f: contents = f.read() self.assertEqual(contents.strip(), '#S2', Message( 'Step 2 should now be Step 1', response=r )) support.run_command('close')
def test_print_solo(self): """ should properly print in a step that does nothing but print """ response = support.create_project(self, 'minneapolis') self.assertFalse( response.failed, Message('should have created project', response=response) ) print_string = string.ascii_lowercase code = '\n'.join([ 'values = [x ** 2 for x in range(100)]', 'print("{}")'.format(print_string) ]) support.add_step(self, contents=code) response = support.run_command('run -f') self.assertFalse( response.failed, Message('should have run step', response=response) ) project = cauldron.project.get_internal_project() dom = project.steps[0].dom # type: str self.assertEqual( dom.count(print_string), 2, 'should have printed ascii lowercase' )
def test_slow_printing(self): """Should not repeat print statements during slow running steps""" response = support.create_project(self, 'duluth') self.assertFalse( response.failed, Message('should have created project', response=response) ) code = '\n'.join([ 'import time', 'for letter in "BCFHMNOPRSTUVWX":', ' print("{}AT".format(letter))', ' time.sleep(0.5)' ]) support.add_step(self, contents=code) response = commander.execute('run', '-f') response.thread.join(2) step = cauldron.project.get_internal_project().steps[0] dom = step.dumps() self.assertEqual(dom.count('BAT'), 1, 'first check failed') response.thread.join(1) dom = step.dumps() self.assertEqual(dom.count('BAT'), 1, 'second check failed') response.thread.join() dom = step.dumps() self.assertEqual(dom.count('BAT'), 1, 'third check failed') self.assertLess(dom.count('SAT'), 2, 'fourth check failed')
def test_print_multiple(self): """ should properly print multiple times within a step """ response = support.create_project(self, 'omaha') self.assertFalse( response.failed, Message('should have created project', response=response)) code = '\n'.join([ 'import cauldron as cd', 'import string', 'print(string.ascii_lowercase)', 'cd.display.text("Hello World")', 'print(string.ascii_uppercase)', 'print(string.hexdigits)' ]) support.add_step(self, contents=code) response = support.run_command('run -f') self.assertFalse(response.failed, Message('should have run step', response=response)) project = cauldron.project.internal_project dom = project.steps[0].dom # type: str self.assertEqual(dom.count(string.ascii_lowercase), 1, 'should have printed ascii lowercase') self.assertEqual(dom.count(string.ascii_uppercase), 1, 'should have printed ascii uppercase') self.assertEqual(dom.count(string.hexdigits), 1, 'should have printed hex digits') support.run_command('close')
def test_no_existing_source_file(self): """ should succeed even if the step has no source file """ support.create_project(self, 'richfield') support.add_step(self, 'first') project = cauldron.project.get_internal_project() step = project.steps[0] r = Response() self.assertTrue( systems.remove(step.source_path), 'should have removed source file' ) result = step_actions.modify_step( response=r, project=project, name=step.filename, new_name='solo', title='Only Step' ) new_step = project.steps[0] self.assertTrue(result) self.assertTrue(os.path.exists(new_step.source_path))
def test_nameless_step(self): """ should rename properly a nameless step """ support.create_project(self, 'columbia-heights') project = cauldron.project.get_internal_project() project.naming_scheme = None support.add_step(self) support.add_step(self) r = Response() with patch('cauldron.session.naming.explode_filename') as func: func.return_value = dict( extension='py', index=1, name='' ) step_actions.modify_step( response=r, project=project, name=project.steps[0].filename, new_name='', position=2 ) self.assertFalse(r.failed)
def test_elapsed(self): """Should render elapsed time""" support.create_project(self, 'test_elapsed') step_contents = '\n'.join( ['import cauldron as cd', 'cd.display.elapsed()']) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_get_missing_step(self): """ should get None for a fictional step name """ support.create_project(self, 'george') support.add_step(self) project = cd.project.get_internal_project() self.assertIsNone(source.get_step(project, 'FICTIONAL-STEP'))
def test_invalid_markdown(self): """ should fail with a jinja error """ support.create_project(self, 'des-moines') support.add_step(self, 'test.md', contents='# Hello {{ missing_variable + "12" }}') response = support.run_command('run -f') self.assertTrue(response.failed)
def test_run_skip_status(self, *args): """ should succeed without running a with a skip status """ support.create_project(self, 'adams') support.add_step(self) project = cd.project.get_internal_project() step = project.steps[0] result = source.run_step(Response(), project, step) self.assertTrue(result)
def test_run_error_status(self, *args): """ should fail to run a with an error status """ support.create_project(self, 'john') support.add_step(self) project = cd.project.get_internal_project() step = project.steps[0] result = source.run_step(Response(), project, step) self.assertFalse(result)
def test_index_from_location_step_name(self): """ should return index from step name if supplied """ support.create_project(self, 'bradbury') support.add_step(self) project = cauldron.project.get_internal_project() step = project.steps[0] result = step_actions.index_from_location(None, project, step.filename) self.assertEqual(result, 1)
def test_elapsed(self): """Should render elapsed time""" support.create_project(self, 'test_elapsed') step_contents = '\n'.join([ 'import cauldron as cd', 'cd.display.elapsed()' ]) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_run_markdown(self): """ should render markdown """ support.create_project(self, 'salem') support.add_step(self, 'test.md', contents='This is markdown') response = support.run_command('run -f') self.assertFalse(response.failed, 'should have failed') step = cd.project.get_internal_project().steps[0] self.assertFalse(step.is_dirty())
def test_has_error(self): """Should have error if step raises an error when run""" support.create_project(self, 'petunia') project = cd.project.internal_project support.add_step(self, contents='raise Exception("test")') support.run_command('run') self.assertTrue(project.has_error, 'step should have errored')
def test_run_markdown(self): """ should render markdown """ support.create_project(self, 'salem') support.add_step(self, 'test.md', contents='This is markdown') response = support.run_command('run -f') self.assertFalse(response.failed, 'should have failed') step = cd.project.internal_project.steps[0] self.assertFalse(step.is_dirty())
def test_image(self): """Should render an assets image to the dom.""" support.create_project(self, 'test_image') step_contents = '\n'.join([ 'import cauldron as cd', 'cd.display.image("foo.jpg", 12, 13, "center")' ]) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertTrue(r.success, 'should not have failed')
def test_autocomplete(self): """ should autocomplete step name """ support.create_project(self, 'crystal') support.add_step(self) step = cauldron.project.get_internal_project().steps[0] result = support.autocomplete('run {}'.format(step.filename[:2])) self.assertEqual(len(result), 1) self.assertEqual(result[0], step.filename)
def test_status_of_muted_step(self): """ should have a skip status if the step is muted """ support.create_project(self, 'madison') support.add_step(self) project = cd.project.get_internal_project() step = project.steps[0] step.is_muted = True status = source.check_status(Response(), project, step) self.assertEqual(status, source.SKIP_STATUS)
def test_status_of_missing_step_file(self): """ should have an error status if the step has no file """ support.create_project(self, 'james') support.add_step(self) project = cd.project.get_internal_project() step = project.steps[0] with patch('os.path.exists', return_value=False): status = source.check_status(Response(), project, step) self.assertEqual(status, source.ERROR_STATUS)
def test_get_step_by_name(self): """ should retrieve the step from the step name string """ support.create_project(self, 'washington') support.add_step(self) project = cd.project.get_internal_project() step = project.steps[0] result = source.get_step(project, step.filename) self.assertEqual(step, result)
def test_status_of_clean_step(self): """ should have an skip status if the step is not dirty """ support.create_project(self, 'monroe') support.add_step(self) project = cd.project.get_internal_project() step = project.steps[0] step.is_dirty = lambda: False status = source.check_status(Response(), project, step) self.assertEqual(status, source.SKIP_STATUS)
def test_invalid_step_extension(self): """ should fail to execute step of unknown extension """ support.create_project(self, 'thomas') support.add_step(self, 'TEST.fake') project = cd.project.get_internal_project() step = project.steps[0] result = source._execute_step(project, step) self.assertFalse(result['success'], False)
def test_invalid_markdown(self): """ should fail with a jinja error """ support.create_project(self, 'des-moines') support.add_step( self, 'test.md', contents='# Hello {{ missing_variable + "12" }}' ) response = support.run_command('run -f') self.assertTrue(response.failed)
def test_html(self): """ should add an html div tag to the display """ support.create_project(self, 'helo') step_contents = '\n'.join( ['import cauldron as cd', 'cd.display.html("<div></div>")']) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_whitespace(self): """ should add list to display """ support.create_project(self, 'hera') step_contents = '\n'.join( ['import cauldron as cd', 'cd.display.whitespace(4)']) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_list_grid(self): """ should add list grid to display """ support.create_project(self, 'apollo-grid') step_contents = '\n'.join( ['import cauldron as cd', 'cd.display.list_grid([1, 2, 3, 4])']) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_before(self): """ should properly rename default filenames """ support.create_project(self, 'candice') support.add_step(self) support.add_step(self, position='0') project = cauldron.project.get_internal_project() steps = project.steps self.assertTrue(steps[0].filename.startswith('S01')) self.assertTrue(steps[1].filename.startswith('S02'))
def test_get_no_step(self): """Should not find a step that doesn't exist""" support.create_project(self, 'luna') project = cd.project.internal_project support.add_step(self) support.add_step(self) self.assertIsNone(project.get_step('NoSuchStep')) self.assertIsNone(project.get_step_by_reference_id('NoSuchStep')) self.assertIsNone(project.index_of_step('NoSuchStep'))
def test_run_step_execution_error(self): """ should fail when running a step that fails to execute """ support.create_project(self, 'quincy') support.add_step(self) project = cd.project.get_internal_project() step = project.steps[0] package = 'cauldron.runner.source._execute_step' with patch(package, side_effect=RuntimeError('Not Good!')): result = source.run_step(Response(), project, step) self.assertFalse(result)
def test_syntax_error(self): """ should render a syntax error """ support.create_project(self, 'seattle') support.add_step(self, contents='// Not Python') response = support.run_command('run -f') self.assertTrue(response.failed, 'should have failed') project = cd.project.get_internal_project() step = project.steps[0] self.assertTrue(step.dom.find('cd-CodeError') > 0) self.assertTrue(step.dom.find('SyntaxError') > 0)
def test_list_grid(self): """ should add list grid to display """ support.create_project(self, 'apollo-grid') step_contents = '\n'.join([ 'import cauldron as cd', 'cd.display.list_grid([1, 2, 3, 4])' ]) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_whitespace(self): """ should add list to display """ support.create_project(self, 'hera') step_contents = '\n'.join([ 'import cauldron as cd', 'cd.display.whitespace(4)' ]) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_html(self): """ should add an html div tag to the display """ support.create_project(self, 'helo') step_contents = '\n'.join([ 'import cauldron as cd', 'cd.display.html("<div></div>")' ]) support.add_step(self, contents=step_contents) r = support.run_command('run') self.assertFalse(r.failed, 'should not have failed')
def test_clean_step(self): """ should succeed in cleaning step """ support.create_project(self, 'luigi') support.add_step(self) step = cauldron.project.get_internal_project().steps[0] cleaned = self.get('/clean-step/{}'.format(step.filename)) self.assertEqual(cleaned.flask.status_code, 200) response = cleaned.response self.assertTrue(response.success) self.assertIsNotNone(response.data['project'])