예제 #1
0
 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
예제 #2
0
 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
            },
            {}
           ]
          }
         }
         ''')
예제 #3
0
 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
예제 #4
0
 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')]]
예제 #5
0
 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')]]
예제 #6
0
 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'
예제 #7
0
 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
예제 #8
0
 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)
예제 #9
0
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
예제 #10
0
파일: actions.py 프로젝트: MattHJensen/luca
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
예제 #11
0
 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
           }
          }
         }
         ''')
예제 #12
0
 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
예제 #13
0
 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'