Example #1
0
 def test_make_module(self):
   """
   Test that the generated module has the classes and nested classes we expect.
   """
   gcode = gencode.GenCode()
   gcode.make_module(self.schema)
   module = gcode.usercode
   self.assertTrue(isinstance(module.Students, table.UserTable))
Example #2
0
    def test_make_module_text(self):
        """
    Test that make_module_text produces the exact sample output that we have stored
    in the docstring of usercode.py.
    """
        import usercode
        usercode_sample_re = re.compile(r'^==========*\n', re.M)
        saved_sample = usercode_sample_re.split(usercode.__doc__)[1]

        gcode = gencode.GenCode()
        gcode.make_module(self.schema)
        generated = gcode.get_user_text()
        self.assertEqual(
            generated, saved_sample,
            "Generated code doesn't match sample:\n" + "".join(
                difflib.unified_diff(generated.splitlines(True),
                                     saved_sample.splitlines(True),
                                     fromfile="generated",
                                     tofile="usercode.py")))
Example #3
0
    def test_grist_names(self):
        # Verifies that we can correctly extract the names of Grist objects that occur in formulas.
        # This is used by automatic formula adjustments when columns or tables get renamed.
        gcode = gencode.GenCode()
        gcode.make_module(self.schema)
        # The output of grist_names is described in codebuilder.py, and copied here:
        # col_info:   (table_id, col_id) for the formula the name is found in. It is the value passed
        #             in by gencode.py to codebuilder.make_formula_body().
        # start_pos:  Index of the start character of the name in the text of the formula.
        # table_id:   Parsed name when the tuple is for a table name; the name of the column's table
        #             when the tuple is for a column name.
        # col_id:     None when tuple is for a table name; col_id when the tuple is for a column name.
        expected_names = [
            (('Students', 'fullName'), 4, 'Students', 'firstName'),
            (('Students', 'fullName'), 26, 'Students', 'lastName'),
            (('Students', 'fullNameLen'), 8, 'Students', 'fullName'),
            (('Students', 'schoolShort'), 11, 'Schools', 'name'),
            (('Students', 'schoolShort'), 4, 'Students', 'school'),
            (('Students', 'schoolRegion'), 15, 'Schools', 'address'),
            (('Students', 'schoolRegion'), 8, 'Students', 'school'),
            (('Students', 'schoolRegion'), 42, 'Address', 'country'),
            (('Students', 'schoolRegion'), 28, 'Address', 'state'),
            (('Students', 'schoolRegion'), 68, 'Address', 'region'),
            (('Students', 'school2'), 0, 'Schools', None),
            (('Students', 'school2'), 36, 'Schools', 'name'),
            (('Students', 'school2'), 29, 'Students', 'school'),
            (('Address', 'region'), 48, 'Address', 'country'),
        ]
        self.assertEqual(gcode.grist_names(), expected_names)

        # Test the case of a bare-word function with a keyword argument appearing in a formula. This
        # case had a bug with code parsing.
        self.schema['Address'].columns['testcol'] = schema.SchemaColumn(
            'testcol', 'Any', True,
            'foo(bar=$region) or max(Students.all, key=lambda n: -n)')
        gcode.make_module(self.schema)
        self.assertEqual(
            gcode.grist_names(), expected_names + [
                (('Address', 'testcol'), 9, 'Address', 'region'),
                (('Address', 'testcol'), 24, 'Students', None),
            ])