def test_custom(self): self.data["DC_SCT"] = ''' test_function("pandas.DataFrame") test_function("print") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct']) self.data["DC_SCT"] = 'test_function("pandas.date_range", not_called_msg = "stupid")' sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertEqual(sct_payload['message'], "stupid") self.data["DC_SCT"] = 'test_function("type", not_called_msg = "stupid")' sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertEqual(sct_payload['message'], "stupid") self.data["DC_SCT"] = 'test_function("pandas.Series", incorrect_msg = "stupid")' sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertIn("stupid", sct_payload['message']) self.data["DC_SCT"] = 'test_function("len", incorrect_msg = "stupid")' sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertIn("stupid", sct_payload['message'])
def do_easy_install(): easy_install = os.path.join(LBFORUM_ENV, "Scripts/easy_install.exe") print '== do_easy_install ==' print run('%s %s' % (easy_install, 'http://code.djangoproject.com/svn/django/trunk/')) print run('%s %s' % (easy_install, 'PIL')) print run('%s %s' % (easy_install, 'django-pagination')) print run('%s %s' % (easy_install, 'South'))
def test_fail_else_body_if_exp(self): self.data["DC_SOLUTION"] = self.IF_EXP_SOLUTION self.data["DC_CODE"] = "5 if offset > 8 else 7 if offset > 5 else round(10)" self.data["DC_SCT"] = helper.replace_test_if(self.data["DC_SCT"]) sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertIn("Did you call <code>round()</code> with the correct arguments?", sct_payload['message'])
def test_fail_elif_cond_if_exp(self): self.data["DC_SOLUTION"] = self.IF_EXP_SOLUTION self.data["DC_CODE"] = "x = 5 if offset > 8 else 7 if offset > 6 else round(9)" self.data["DC_SCT"] = helper.replace_test_if(self.data["DC_SCT"]) sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertIn("Unexpected expression", sct_payload['message'])
def test_fail_if_body_if_exp(self): self.data["DC_SOLUTION"] = self.IF_EXP_SOLUTION self.data["DC_CODE"] = "x = 6 if offset > 8 else 7 if offset > 5 else round(9)" self.data["DC_SCT"] = helper.replace_test_if(self.data["DC_SCT"]) sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertEqual(sct_payload['message'], "incorrect_if")
def test_F(self): self.data["DC_SCT"] = ''' list_comp = F().check_list_comp(0).check_body().set_context(ii=2).has_equal_value('unequal') Ex().check_list_comp(0).check_body().set_context(aa=2).multi(list_comp) ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_multi_generator(self): self.data["DC_SCT"] = """ Ex().check_list_comp(0).check_body()\ .multi(set_context(aa=i).has_equal_value('wrong') for i in range(2)) """ sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass(self): self.data["DC_SCT"] = ''' msg = "Don't change the predefined code!" pre = "import numpy as np; np.random.seed(123)" test_for_loop(1, body = lambda msg=msg, pre=pre: test_object_after_expression("random_walk", pre_code=pre, undefined_msg=msg, incorrect_msg = msg), expand_message = False) test_import("numpy", not_imported_msg = msg, incorrect_as_msg = msg) test_function("numpy.random.seed", not_called_msg = msg, incorrect_msg = msg) msg = "Loop over `i` in `range(10)` to loop 10 times" test_for_loop(1, for_iter=lambda msg=msg: test_function("range", not_called_msg = msg, incorrect_msg = msg)) msg = "Append `all_walks` with `random_walk` using `.append()`" pre = "import numpy as np; np.random.seed(123)" test_for_loop(1, body = lambda msg=msg, pre=pre: test_object_after_expression("all_walks", {"all_walks": []}, pre_code=pre, undefined_msg=msg, incorrect_msg = msg)) msg = "Have you correctly initialized `all_walks` to `[]` and expanded it correctly each loop?" test_expression_result(expr_code="len(all_walks)", incorrect_msg=msg) msg = "Don't forget to print out `all_walks`." test_function("print", args=[], not_called_msg=msg, incorrect_msg=msg) success_msg("Well done!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct']) self.assertEqual(sct_payload['message'], "Well done!")
def test_Pass(self): self.data["DC_SCT"] = ''' def test_first(): msg = "You don't have to change or remove anything" # this will automatically be completed with extra info # about the if statement test_expression_result({"room": "kit"}, incorrect_msg = msg) test_expression_result({"room": "not_kit"}, incorrect_msg = msg) def body_first(): msg = "You don't have to change or remove anything" test_function("print", incorrect_msg = msg) test_if_else(index = 1, test = test_first, body = body_first, orelse = body_first) def test_second(): msg = "The `area` should be greater than `15`" # ... test_expression_result({"area": 14}, incorrect_msg = msg) test_expression_result({"area": 15}, incorrect_msg = msg) test_expression_result({"area": 16}, incorrect_msg = msg) def body_second(): msg = 'Print out `\"big place!\"`' test_function("print", incorrect_msg = msg) def else_second(): msg = 'Print out `\"pretty small.\"`' test_function("print", incorrect_msg = msg) test_if_else(index = 2, test = test_second, body = body_second, orelse = else_second) success_msg("Nice! Again, feel free to play around with different values of `room` and `area` some more. Then, head over to the next exercise, where you'll take this customization one step further!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_do_eval_false_pass(self): self.data = {"DC_PEC": '', "DC_SOLUTION": "x = 2.12309123; round(x, ndigits = 4)", "DC_CODE": "x = 2.123450; round(x, ndigits = 4)", "DC_SCT": "test_function('round', do_eval = False)"} sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_nested_keyw1(self): self.data = {"DC_PEC": '', "DC_SOLUTION": "round(1.1234, ndigits = max([1, 2, 3]))", "DC_CODE": "round(1.1234, ndigits = max([1, 2, 3]))", "DC_SCT": "test_function('max', highlight=True)"} sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_nested_arg1(self): self.data = {"DC_PEC": '', "DC_SOLUTION": "print(type([1, 2, 3]))", "DC_CODE": "print(type([1, 2, 3]))", "DC_SCT": "test_function('type')"} sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_do_eval_true_pass(self): self.data = {"DC_PEC": '', "DC_SOLUTION": "round(2.1234, ndigits = 4)", "DC_CODE": "round(2.1234, ndigits = 4)", "DC_SCT": "test_function_v2('round', params=['number', 'ndigits'], do_eval = True)"} sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_step4(self): self.data["DC_CODE"] = "x.center(50, 'c')" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertIn('Did you call <code>x.center()</code> with the correct arguments?', sct_payload['message']) self.assertFalse('The argument you specified for <code>fillchar</code> seems to be incorrect.' in sct_payload['message']) helper.test_lines(self, sct_payload, 1, 1, 14, 16)
def test_step3(self): self.data["DC_CODE"] = "x.center(50)" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertIn('Have you specified all required arguments inside <code>x.center()</code>?', sct_payload['message']) self.assertFalse('You didn\'t specify <code>fillchar</code>.' in sct_payload['message']) helper.test_lines(self, sct_payload, 1, 1, 1, 12)
def test_pass(self): self.data = { "DC_PEC": "", "DC_SOLUTION": ''' # Define shout_echo def shout_echo(word1, echo=1): echo_word = '' shout_words = '' try: echo_word = word1 * echo shout_words = echo_word + '!!!' except: print("word1 must be a string and echo must be an integer.") return shout_words shout_echo("particle", echo="accelerator") ''', "DC_SCT": ''' def inner_test(): def inner_inner_test(): test_object_after_expression("echo_word", extra_env={'word1' : 'hithere', 'echo': 2}) import collections handlers = collections.OrderedDict() handlers['all'] = lambda: test_function('print') test_try_except(index = 1,body = inner_inner_test,handlers = handlers) test_function_definition("shout_echo", body=inner_test) test_function_v2("shout_echo",index=1,params=["word1", "echo"]) test_function("shout_echo",index=1) success_msg("Great work!") ''' } self.data["DC_CODE"] = self.data["DC_SOLUTION"] sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_pass(self): self.data = { "DC_PEC": "", "DC_SOLUTION": ''' def report_status(**kwargs): print("\\nBEGIN: REPORT\\n") for key, value in kwargs.items(): print(key + ": " + value) print("\\nEND REPORT") report_status(name="luke", affiliation="jedi", status="missing") report_status(name="anakin", affiliation="sith lord", status="deceased") ''', "DC_SCT": ''' def inner_test(): test_function("print", index=1) def iter_test(): context=[{'name':"luke", 'affiliation':"jedi", 'status':"missing"}] test_expression_result(context_vals = context) def body_test(): context=['name', 'luke'] test_expression_output(context_vals = context) test_for_loop(for_iter = iter_test, body = body_test) test_function("print", index=2) # Test: report_status() definition test_function_definition( "report_status", body=inner_test, outputs = [{'args': [], 'kwargs': {'name':"hugo", 'affiliation':"datacamp"}}]) ''' } self.data["DC_CODE"] = self.data["DC_SOLUTION"] sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_do_eval_none_pass(self): self.data = {"DC_PEC": '', "DC_SOLUTION": "round(123.123, ndigits = 2)", "DC_CODE": "round(123.123, ndigits = 2)", "DC_SCT": "test_function('round', do_eval = None)"} sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass_no_lam(self): self.data["DC_SCT"] = ''' msg = "In the condition of your `if` part, make sure you use `dice <= 2`." test_if_else(1, test = [test_expression_result({"dice": i}, incorrect_msg = msg) for i in range(1,7)], expand_message = False) # check elif statements? ------ msg = "Have another look at your `elif` part. If the dice is smaller than or equal to 5, and bigger than 2, `step` should be increased by 1." if2 = test_object_after_expression("step", {"step": 1}, incorrect_msg = msg) msg = "Have another look at your final `else` part. If the dice is 6, `step` should be increased by a random integer between 1 and 6, inclusive. Use `np.random.randint(1,7)`." pre = "import numpy as np; np.random.seed(123)" else2 = test_object_after_expression("step", {"step": 1}, pre_code = pre, incorrect_msg = msg) msg = "The condition of the `elif` part should be `dice <= 5`." else1 = [ test_if_else(1, test = [test_expression_result({"dice": i}, incorrect_msg = msg) for i in range(1,7)], expand_message = False), test_if_else(1, body = if2, expand_message = False), test_if_else(1, orelse = else2, expand_message = False)] test_if_else(1, orelse = else1, expand_message = False) msg = "Make sure you output `dice` and `step`, in this order, with two `print()` calls." test_expression_output(incorrect_msg = msg) success_msg("Great!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct']) self.assertEqual(sct_payload['message'], "Great!")
def test_multi_splits_node_and_check(self): self.data["DC_SCT"] = ''' test_body = F().check_list_comp(0).check_body().set_context(aa=2).has_equal_value('wrong') Ex().check_list_comp(0).multi(F().check_body().set_context(aa=2).has_equal_value('wrong')) ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass1_spec2(self): self.data["DC_SCT"] = ''' for_test = test_for_loop(1, body = test_if_else(1, body = test_function('print', highlight=True))) Ex().check_with(1).check_body().with_context(for_test) ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass(self): self.data["DC_SCT"] = ''' test_function_definition("shout", arg_names=False, arg_defaults=False, body = lambda: test_expression_output(context_vals = ['help'], incorrect_msg = 'make sure to output the correct string.')) success_msg("Nice work man!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass1(self): self.data["DC_SCT"] = ''' test_with(1, context_tests=lambda: test_function('open')) success_msg("Nice work!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass1_no_lam(self): self.data["DC_SCT"] = ''' test_with(2, body = test_for_loop(1, body = test_if_else(1, body = test_function('print', highlight=True)))) success_msg("Nice work!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Fail1(self): self.data["DC_SCT"] = ''' test_function_definition('shout') ''' sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) helper.test_lines(self, sct_payload, 2, 2, 13, 16)
def test_Pass1(self): self.data["DC_SCT"] = ''' test_function_definition("shout") success_msg("Nice work man!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass(self): self.data["DC_CODE"] = ''' def shout(): shout_word = 'congratulations' + '!!!' ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_pass(self): self.data = { "DC_PEC": "", "DC_SOLUTION": ''' def echo_shout(word): echo_word = word*2 print(echo_word) def shout(): nonlocal echo_word echo_word = echo_word + '!!!' shout() print(echo_word) echo_shout('hello') ''', "DC_SCT": ''' def inner_test(): test_object_after_expression("echo_word", context_vals=["hello"]) test_function_definition("shout") test_function("shout") test_function("print", args=[], index=1) test_function("print", args=[], index=2) test_function_definition("echo_shout", body=inner_test) test_function("echo_shout") ''' } self.data["DC_CODE"] = self.data["DC_SOLUTION"] sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_method2(self): self.data = { "DC_PEC": ''' from urllib.request import urlretrieve from sqlalchemy import create_engine, MetaData, Table from urllib.request import urlretrieve; urlretrieve('https://s3.amazonaws.com/assets.datacamp.com/production/course_1115/datasets/census.sqlite', 'census.sqlite') engine = create_engine('sqlite:///census.sqlite') metadata = MetaData() connection = engine.connect() from sqlalchemy import select census = Table('census', metadata, autoload=True, autoload_with=engine) stmt = select([census]) ''', "DC_SOLUTION": ''' x = connection.execute(stmt).fetchall() ''', "DC_CODE": ''' x = connection.execute(object = stmt).fetchall() ''', "DC_SCT": ''' test_function_v2('connection.execute', params = ['object'], do_eval = False) test_function_v2('connection.execute.fetchall', params = []) ''' } sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass2(self): self.data["DC_SCT"] = ''' test_function_definition("shout", results=[('help', 'fire')]) success_msg("Nice work!") ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_do_eval_3(self): self.data["DC_CODE"] = "x = 2; pow(3, x, 4)" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct'])
def test_step2(self): self.data["DC_CODE"] = "df = pd.DataFrame(x=[1, 2, 3])" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertEqual('paramsnotmatchedmsg', sct_payload['message']) helper.test_lines(self, sct_payload, 1, 1, 6, 30)
def when_replace(self, orig, new): self.data['DC_CODE'] = self.data['DC_SOLUTION'].replace(orig, new) self.sct_payload = helper.run(self.data) return self.sct_payload['correct']
def test_pass(self): self.data["DC_CODE"] = self.data["DC_SOLUTION"] sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass2(self): self.data["DC_SCT"] = ''' test_function_definition('shout', arg_defaults = False, results = [('help')]) ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def start(args, logfile, errfile): helper.set_database_host(args) command = Command( "rvm ruby-2.0.0-p0 do bundle exec thin start -C config/thin.yml", False) return helper.run([command], logfile, errfile, args.troot)
def test_Pass(self): sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertEqual(sct_payload['message'], 'test') helper.test_lines(self, sct_payload, 3, 3, 5, 41)
def test_step5(self): self.data["DC_CODE"] = "x.center(50, 't')" sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_step4(self): self.data["DC_CODE"] = "x.center(50, 'c')" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) helper.test_lines(self, sct_payload, 1, 1, 14, 16)
def test_step1(self): self.data["DC_CODE"] = "" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) helper.test_absent_lines(self, sct_payload)
def test_step5(self): self.data[ "DC_CODE"] = "df = pd.DataFrame(data=[1, 2, 3], columns=['a'])" sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_step4(self): self.data["DC_CODE"] = "df = pd.DataFrame(data=[1, 2, 3])" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertIn('columnsnotspecified', sct_payload['message']) helper.test_lines(self, sct_payload, 1, 1, 6, 33)
def test_do_eval_4(self): self.data["DC_CODE"] = "pow(3, 2, 3)" sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_Pass1(self): self.data["DC_CODE"] = 'test = 3' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
# (143,98) + (76,66) # ### Exercise 3 # # Extend `ECCTest` to test for the additions from the previous exercise. Call this `test_add`. # # #### Make [this test](/edit/code-ch03/ecc.py) pass: `ecc.py:ECCTest:test_add` # In[6]: # Exercise 3 reload(ecc) run(ecc.ECCTest("test_add")) # ### Exercise 4 # # For the curve \\(y^{2}\\)=\\(x^{3}\\)+7 over \\(F_{223}\\), find: # # * 2⋅(192,105) # * 2⋅(143,98) # * 2⋅(47,71) # * 4⋅(47,71) # * 8⋅(47,71) # * 21⋅(47,71) # In[33]:
def test_Pass4(self): self.data["DC_CODE"] = "test = 3\nprint('not test')" sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def stop(logfile, errfile): helper.run([Command('rm -rf tmp/*', True)], logfile, errfile, '.') return helper.stop('thin', logfile, errfile)
import re from helper import run hlp = run('--help') stdout = hlp.stdout def test_usage(): assert stdout.startswith('Usage: filabel.py [OPTIONS] [REPOSLUGS]...') def test_description(): assert ('CLI tool for filename-pattern-based labeling of GitHub PRs' in stdout) def test_state(): assert re.search( r'-s,\s+--state\s+\[open\|closed\|all\]\s+' r'Filter pulls by state\.\s+\[default:\s+open\]', stdout) def test_delete_old(): assert re.search( r'-d, --delete-old / -D, --no-delete-old\s+' r'Delete labels that do not\s+match\s+anymore\.' r'\s+\[default:\s+True\]', stdout) def test_branch():
def tearDown(self): sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def start(args, logfile, errfile): db_host = "DB_HOST={0}".format(args.database_host or 'localhost') start_server = db_host + " rvm ruby-2.0.0-p0 do bundle exec puma -C config/puma.rb -w 8 --preload" return helper.run([Command(start_server, False)], logfile, errfile, args.troot)
def test_Pass3(self): self.data["DC_SCT"] = ''' test_function_definition('shout', arg_defaults = False, body = lambda: test_function('print', args=[])) ''' sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_fail_star_args_name(self): self.data[ 'DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), *wrongargsname, **kwargs): pass""" self.data['DC_SCT'] = self.SCT_CHECK_ARGS + '.has_equal_name()' sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct'])
def when_code_is_sol(self): self.data['DC_CODE'] = self.data['DC_SOLUTION'] self.sct_payload = helper.run(self.data) return self.sct_payload['correct']
def test_step3(self): self.data["DC_CODE"] = "df = pd.DataFrame(data=[1, 2, 3])" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct'])
def test_fail_star_args_undef(self): self.data[ 'DC_CODE'] = """def my_fun(x, y = 4, z = ('a', 'b'), args=2, **kwargs): pass""" self.data['DC_SCT'] = self.SCT_CHECK_ARGS sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct'])
def test_step1(self): self.data["DC_CODE"] = "" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertEqual('notcalledmsg', sct_payload['message']) helper.test_absent_lines(self, sct_payload)
def test_fail_1(self): self.data["DC_CODE"] = "" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) self.assertEqual(sct_payload['message'], 'cough')
def test_no_copy_bad_sct_passes(self): self.data["DC_SOLUTION"] = "a = [2]" self.data["DC_CODE"] = "a = [1]" self.data["DC_SCT"] = "Ex().has_equal_value(expr_code = 'a[0] = 3', name = 'a', copy = False).has_equal_value(expr_code = 'a', name = 'a')" sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])
def test_step2(self): self.data["DC_CODE"] = "x.center(width = 50)" sct_payload = helper.run(self.data) self.assertFalse(sct_payload['correct']) helper.test_lines(self, sct_payload, 1, 1, 1, 20)
def test_fun_step2(self): self.data["DC_CODE"] = "for i in range(10):\n print(i)" sct_payload = helper.run(self.data) self.assertTrue(sct_payload['correct'])