diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-04-25 08:14:27 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-04-25 08:14:27 -0500 |
commit | 813ba3bed433a96e02d82cad2e2940a6850d96a5 (patch) | |
tree | 893e3d8ea4d088d5915b9c25f79d3fcfa3f1404f /examples/bigquery_view_parser.py | |
parent | 486b1fd2ef7e98966665f915bc59856996ffb5b0 (diff) | |
download | pyparsing-git-813ba3bed433a96e02d82cad2e2940a6850d96a5.tar.gz |
Code cleanup in examples; move test code into main for bigquery_view_parser.py; change some lambdas to explicit methods for clarity (some discussion in #207); deleted duplicated examples
Diffstat (limited to 'examples/bigquery_view_parser.py')
-rw-r--r-- | examples/bigquery_view_parser.py | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/examples/bigquery_view_parser.py b/examples/bigquery_view_parser.py index cf23818..433a127 100644 --- a/examples/bigquery_view_parser.py +++ b/examples/bigquery_view_parser.py @@ -687,7 +687,7 @@ class BigQueryViewParser: + Suppress(".") ) + (quoted_table_part("table") | standard_table_part("table")) - ).setParseAction(lambda t: record_table_identifier(t)) + ).setParseAction(record_table_identifier) def record_quoted_table_identifier(t): identifier_list = t.asList()[0].split(".") @@ -702,7 +702,7 @@ class BigQueryViewParser: Suppress('"') + CharsNotIn('"') + Suppress('"') | Suppress("'") + CharsNotIn("'") + Suppress("'") | Suppress("`") + CharsNotIn("`") + Suppress("`") - ).setParseAction(lambda t: record_quoted_table_identifier(t)) + ).setParseAction(record_quoted_table_identifier) table_identifier = ( quoted_table_parts_identifier | quotable_table_parts_identifier @@ -809,7 +809,7 @@ class BigQueryViewParser: cls._with_aliases.add(tuple(padded_list)) with_clause = Group( - identifier.setParseAction(lambda t: record_with_alias(t)) + identifier.setParseAction(record_with_alias) + AS + LPAR + select_stmt @@ -821,6 +821,24 @@ class BigQueryViewParser: cls._parser = select_stmt return cls._parser + def test(self, sql_stmt, expected_tables, verbose=False): + def print_(*args): + if verbose: + print(*args) + + print_(sql_stmt.strip()) + found_tables = self.get_table_names(sql_stmt) + print_(found_tables) + expected_tables_set = set(expected_tables) + + if expected_tables_set != found_tables: + raise Exception( + f"Test {test_index} failed- expected {expected_tables_set} but got {found_tables}" + ) + print_() + + +if __name__ == "__main__": TEST_CASES = [ [ """ @@ -1629,18 +1647,7 @@ class BigQueryViewParser: ], ] - def test(self): - for test_index, test_case in enumerate(BigQueryViewParser.TEST_CASES): - sql_stmt, expected_tables = test_case - - found_tables = self.get_table_names(sql_stmt) - expected_tables_set = set(expected_tables) - - if expected_tables_set != found_tables: - raise Exception( - f"Test {test_index} failed- expected {expected_tables_set} but got {found_tables}" - ) - - -if __name__ == "__main__": - BigQueryViewParser().test() + parser = BigQueryViewParser() + for test_index, test_case in enumerate(TEST_CASES): + sql, expected = test_case + parser.test(sql_stmt=sql, expected_tables=expected, verbose=True) |