def test_loading_list_of_subforms(self): f = load_json('{"inputs": {"items": [{"n": 10}, {"m": 11}]}}') assert len(f.items) == 2 assert isinstance(f.items[0], Form) assert isinstance(f.items[1], Form) assert f.items[0].n == 10 assert f.items[1].m == 11
def test_dumping_list_of_subforms(self): f = load_json('{"inputs": {"items": [{"n": 10}, {"m": 11}]}}') f._enter_output_mode() f.items[0].x = 100 f.items[0].y = 101 j = dump_json(f) assert j == dedent(u'''\ { "inputs": { "items": [ { "n": 10 }, { "m": 11 } ] }, "outputs": { "items": [ { "x": 100, "y": 101 }, {} ] } } ''')
def test_dumping_form_preserves_inputs_and_includes_new_outputs(self): for json in json_in, json_empty_output, json_filled_output: f = load_json(json) f._enter_output_mode() process_form(f) j = dump_json(f) assert j == json_filled_output
def test_building_from_json_detects_many_digit_decimals(self): f = load_json(u'{"inputs": {"line1": [["First Percent", "1.2"],' ' ["Second Percent", "4.56001"]]}}') assert isinstance(f.line1[0][1], Decimal) assert isinstance(f.line1[1][1], Decimal) assert f.line1 == [["First Percent", Decimal('1.2')], ["Second Percent", Decimal('4.56001')]]
def test_building_from_json_detects_decimals_in_lists(self): f = load_json(u'{"inputs": {"line1": [["First Bank", "1.23"],' ' ["Second Bank", "4.56"]]}}') assert isinstance(f.line1[0][1], Decimal) assert isinstance(f.line1[1][1], Decimal) assert f.line1 == [["First Bank", Decimal('1.23')], ["Second Bank", Decimal('4.56')]]
def test_building_from_json_detects_decimals(self): for json in json_in, json_empty_output, json_filled_output: f = load_json(json) self.assertIsInstance(f.Part_I.A.rents, Decimal) self.assertIsInstance(f.Part_I.B.expenses, Decimal) assert str(f.Part_I.A.rents) == '900.00' assert str(f.Part_I.B.expenses) == '230.00'
def test_building_from_json_discards_old_outputs(self): for json in json_in, json_empty_output, json_filled_output: f = load_json(json) assert f._outputs == [] with self.assertRaises(AttributeError): f.A.profit with self.assertRaises(AttributeError): f.total_rents
def test_building_from_json_reads_inputs(self): for json in json_in, json_empty_output, json_filled_output: f = load_json(json) assert f._inputs == ['form', 'ssn', 'name', 'Part_I'] assert f.form == 'rental_income' assert f.ssn == '123-45-6789' assert f.name == 'Lynn Smith' self.assertIsInstance(f.Part_I, Form) assert f.Part_I.A.address == '123 Main St, Ohio' self.assertIsInstance(f.Part_I.B, Form)
def load(json_data): form = formlib.load_json(json_data.decode('utf-8')) if not hasattr(form, 'form_name'): raise ValueError('your JSON "input" object' ' needs to specify a "form_name"') form_module_name = 'luca.forms.' + form.form_name try: form_module = importlib.import_module(form_module_name) except ImportError: raise ValueError( 'cannot find a Luca form named {!r}'.format(form_module_name)) if hasattr(form_module, 'defaults'): form._enter_default_mode() form_module.defaults(form) return form, form_module
def load(json_data): form = formlib.load_json(json_data.decode('utf-8')) if not hasattr(form, 'form_name'): raise ValueError('your JSON "input" object' ' needs to specify a "form_name"') form_module_name = 'luca.forms.' + form.form_name try: form_module = importlib.import_module(form_module_name) except ImportError: raise ValueError('cannot find a Luca form named {!r}'.format( form_module_name)) if hasattr(form_module, 'defaults'): form._enter_default_mode() form_module.defaults(form) return form, form_module
def test_dumping_form_includes_output_subforms(self): f = load_json('{"inputs": {"A": {"a": 1}}}') f._enter_output_mode() f.B = Form() f.B._enter_output_mode() f.B.b = 2 j = dump_json(f) assert j == dedent(u'''\ { "inputs": { "A": { "a": 1 } }, "outputs": { "B": { "b": 2 } } } ''')
def test_dumping_form_preserves_inputs(self): for json in json_in, json_empty_output, json_filled_output: f = load_json(json) j = dump_json(f) assert j == json_empty_output
def test_building_from_json_detects_negative_decimals(self): f = load_json(u'{"inputs": {"value": "-100.23"}}') self.assertIsInstance(f.value, Decimal) assert str(f.value) == '-100.23'