def test_itdbfilter(): """Test InfileToDbFilter""" m = MockCodeGenMachine() f = InfileToDbFilter(m) f.given_classname = 'MyFactory' args = f.methodargs() exp_args = "cyclus::InfileTree* tree, cyclus::DbInit di" yield assert_equal, exp_args, args impl = f.impl() exp_impl = (' int rawcycpp_shape_y[1] = {42};\n' ' cycpp_shape_y = std::vector<int>(rawcycpp_shape_y, ' 'rawcycpp_shape_y + 1);\n' ' tree = tree->SubTree("config/*");\n' ' cyclus::InfileTree* sub;\n' ' int i;\n' ' int n;\n' ' x = cyclus::Query<int>(tree, "x");\n' 'THINGFISH\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' 'ABSOLUTELY FREE\n' ' ->Record();\n') yield assert_equal, exp_impl, impl
def test_internal_infiletodb(): # the expected output (i.e. 'want':...) is set as 'throw' if the # configuration should cause an exception. cases = [ { 'params': { 'internal': True }, 'want': 'throw' }, { 'params': { 'internal': True, 'default': 7 }, 'want': (' cyclus::InfileTree* sub = tree->SubTree("config/*");\n' ' int i;\n' ' int n;\n' ' int x_tmp = 7;\n' ' x = x_tmp;\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' ' ->Record();\n') }, { 'params': { 'derived_init': 'x *= 42;' }, 'want': (' cyclus::InfileTree* sub = tree->SubTree("config/*");\n' ' int i;\n' ' int n;\n' ' {\n' ' int x_val = cyclus::Query<int>(sub, "x");\n' ' x = x_val;\n' ' }\n' ' x *= 42;\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' ' ->Record();\n') }, { 'params': { 'derived_init': 'x *= 42;', 'default': 7 }, 'want': (' cyclus::InfileTree* sub = tree->SubTree("config/*");\n' ' int i;\n' ' int n;\n' ' if (sub->NMatches("x") > 0) {\n' ' {\n' ' int x_val = cyclus::Query<int>(sub, "x");\n' ' x = x_val;\n' ' }\n' ' } else {\n' ' int x_tmp = 7;\n' ' x = x_tmp;\n' ' }\n' ' x *= 42;\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' ' ->Record();\n') }, { 'params': { 'internal': True, 'derived_init': 'x *= 42;', 'default': 7 }, 'want': (' cyclus::InfileTree* sub = tree->SubTree("config/*");\n' ' int i;\n' ' int n;\n' ' int x_tmp = 7;\n' ' x = x_tmp;\n' ' x *= 42;\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' ' ->Record();\n') }, ] for i, case in enumerate(cases): keys = case['params'].copy() m = MockCodeGenMachine() params = {'type': 'int'} params.update(keys) m.context = { "MyFactory": OrderedDict([('vars', OrderedDict([ ('x', params), ]))]) } f = InfileToDbFilter(m) f.given_classname = 'MyFactory' want = case['want'] impl = '' if want == 'throw': haderr = False try: impl = f.impl() except: haderr = True msg = 'case {0} failed: expected raised exception, got none.' assert_true(haderr, msg) continue else: impl = f.impl() msg = 'case {0} failed\n ---- got ----\n {1}\n ---- want ----\n {2}'.format( i + 1, impl.replace('\n', '\n '), want.replace('\n', '\n ')) if want != impl: pprint.pprint(impl) assert_true(False, msg)
def test_internal_infiletodb(): # the expected output (i.e. 'want':...) is set as 'throw' if the # configuration should cause an exception. cases = [ {'params': {'internal': True}, 'want': 'throw'}, {'params': {'internal': True, 'default': 7}, 'want': (' cyclus::InfileTree* sub = tree->SubTree("config/*");\n' ' int i;\n' ' int n;\n' ' int x_tmp = 7;\n' ' x = x_tmp;\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' ' ->Record();\n') }, {'params': {'derived_init': 'x *= 42;'}, 'want': (' cyclus::InfileTree* sub = tree->SubTree("config/*");\n' ' int i;\n' ' int n;\n' ' {\n' ' int x_val = cyclus::Query<int>(sub, "x");\n' ' x = x_val;\n' ' }\n' ' x *= 42;\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' ' ->Record();\n') }, {'params': {'derived_init': 'x *= 42;', 'default': 7}, 'want': (' cyclus::InfileTree* sub = tree->SubTree("config/*");\n' ' int i;\n' ' int n;\n' ' if (sub->NMatches("x") > 0) {\n' ' {\n' ' int x_val = cyclus::Query<int>(sub, "x");\n' ' x = x_val;\n' ' }\n' ' } else {\n' ' int x_tmp = 7;\n' ' x = x_tmp;\n' ' }\n' ' x *= 42;\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' ' ->Record();\n') }, {'params': {'internal': True, 'derived_init': 'x *= 42;', 'default': 7}, 'want': (' cyclus::InfileTree* sub = tree->SubTree("config/*");\n' ' int i;\n' ' int n;\n' ' int x_tmp = 7;\n' ' x = x_tmp;\n' ' x *= 42;\n' ' di.NewDatum("Info")\n' ' ->AddVal("x", x)\n' ' ->Record();\n') }, ] for i, case in enumerate(cases): keys = case['params'].copy() m = MockCodeGenMachine() params = {'type': 'int'} params.update(keys) m.context = {"MyFactory": OrderedDict([('vars', OrderedDict([ ('x', params), ]))])} f = InfileToDbFilter(m) f.given_classname = 'MyFactory' want = case['want'] impl = '' if want == 'throw': haderr = False try: impl = f.impl() except: haderr = True msg = 'case {0} failed: expected raised exception, got none.' assert_true(haderr, msg) continue else: impl = f.impl() msg = 'case {0} failed\n ---- got ----\n {1}\n ---- want ----\n {2}'.format(i + 1, impl.replace('\n', '\n '), want.replace('\n', '\n ')) if want != impl: pprint.pprint(impl) assert_true(False, msg)