|
| 1 | +from simple_ddl_parser import DDLParser |
| 2 | + |
| 3 | + |
| 4 | +def test_sets_with_dot_and_comma(): |
| 5 | + ddl = """ |
| 6 | + -- |
| 7 | + -- PostgreSQL database dump |
| 8 | + -- |
| 9 | +
|
| 10 | + -- Dumped from database version 11.6 (Debian 11.6-1.pgdg90+1) |
| 11 | + -- Dumped by pg_dump version 12.9 (Ubuntu 12.9-0ubuntu0.20.04.1) |
| 12 | +
|
| 13 | + SET statement_timeout = 0; |
| 14 | + SET lock_timeout = 0; |
| 15 | + SET idle_in_transaction_session_timeout = 0; |
| 16 | + SET client_encoding = 'UTF8'; |
| 17 | + SET standard_conforming_strings = on; |
| 18 | + SELECT pg_catalog.set_config('search_path', '', false); |
| 19 | + SET check_function_bodies = false; |
| 20 | + SET xmloption = content; |
| 21 | + SET client_min_messages = warning; |
| 22 | + SET row_security = off; |
| 23 | +
|
| 24 | + SET default_tablespace = ''; |
| 25 | +
|
| 26 | + -- |
| 27 | + -- Name: accounts; Type: TABLE; Schema: public; Owner: myapp |
| 28 | + -- |
| 29 | +
|
| 30 | +
|
| 31 | + """ |
| 32 | + result = DDLParser(ddl).run(group_by_type=True, output_mode="bigquery") |
| 33 | + expected = { |
| 34 | + "ddl_properties": [ |
| 35 | + {"name": "statement_timeout", "value": "0"}, |
| 36 | + {"name": "lock_timeout", "value": "0"}, |
| 37 | + {"name": "idle_in_transaction_session_timeout", "value": "0"}, |
| 38 | + {"name": "client_encoding", "value": "'UTF8'"}, |
| 39 | + {"name": "standard_conforming_strings", "value": "on"}, |
| 40 | + {"name": "check_function_bodies", "value": "false"}, |
| 41 | + {"name": "xmloption", "value": "content"}, |
| 42 | + {"name": "client_min_messages", "value": "warning"}, |
| 43 | + {"name": "row_security", "value": "off"}, |
| 44 | + {"name": "default_tablespace", "value": "''"}, |
| 45 | + ], |
| 46 | + "domains": [], |
| 47 | + "schemas": [], |
| 48 | + "sequences": [], |
| 49 | + "tables": [], |
| 50 | + "types": [], |
| 51 | + } |
| 52 | + assert expected == result |
| 53 | + |
| 54 | + |
| 55 | +def test_parse_validly_tables_after_set(): |
| 56 | + |
| 57 | + ddl = """ |
| 58 | + -- |
| 59 | + -- PostgreSQL database dump |
| 60 | + -- |
| 61 | +
|
| 62 | + -- Dumped from database version 11.6 (Debian 11.6-1.pgdg90+1) |
| 63 | + -- Dumped by pg_dump version 12.9 (Ubuntu 12.9-0ubuntu0.20.04.1) |
| 64 | +
|
| 65 | + SET statement_timeout = 0; |
| 66 | + SET lock_timeout = 0; |
| 67 | + SET idle_in_transaction_session_timeout = 0; |
| 68 | + SET client_encoding = 'UTF8'; |
| 69 | + SET standard_conforming_strings = on; |
| 70 | + SELECT pg_catalog.set_config('search_path', '', false); |
| 71 | + SET check_function_bodies = false; |
| 72 | + SET xmloption = content; |
| 73 | + SET client_min_messages = warning; |
| 74 | + SET row_security = off; |
| 75 | +
|
| 76 | + SET default_tablespace = ''; |
| 77 | +
|
| 78 | + -- |
| 79 | + -- Name: accounts; Type: TABLE; Schema: public; Owner: myapp |
| 80 | + -- |
| 81 | +
|
| 82 | + CREATE TABLE public.accounts ( |
| 83 | + user_id integer NOT NULL, |
| 84 | + username character varying(50) NOT NULL, |
| 85 | + password character varying(50) NOT NULL, |
| 86 | + email character varying(255) NOT NULL, |
| 87 | + ); |
| 88 | +
|
| 89 | +
|
| 90 | + """ |
| 91 | + result = DDLParser(ddl).run(group_by_type=True, output_mode="bigquery") |
| 92 | + expected = { |
| 93 | + "ddl_properties": [ |
| 94 | + {"name": "statement_timeout", "value": "0"}, |
| 95 | + {"name": "lock_timeout", "value": "0"}, |
| 96 | + {"name": "idle_in_transaction_session_timeout", "value": "0"}, |
| 97 | + {"name": "client_encoding", "value": "'UTF8'"}, |
| 98 | + {"name": "standard_conforming_strings", "value": "on"}, |
| 99 | + {"name": "check_function_bodies", "value": "false"}, |
| 100 | + {"name": "xmloption", "value": "content"}, |
| 101 | + {"name": "client_min_messages", "value": "warning"}, |
| 102 | + {"name": "row_security", "value": "off"}, |
| 103 | + {"name": "default_tablespace", "value": "''"}, |
| 104 | + ], |
| 105 | + "domains": [], |
| 106 | + "schemas": [], |
| 107 | + "sequences": [], |
| 108 | + "tables": [ |
| 109 | + { |
| 110 | + "alter": {}, |
| 111 | + "checks": [], |
| 112 | + "columns": [ |
| 113 | + { |
| 114 | + "check": None, |
| 115 | + "default": None, |
| 116 | + "name": "user_id", |
| 117 | + "nullable": False, |
| 118 | + "references": None, |
| 119 | + "size": None, |
| 120 | + "type": "integer", |
| 121 | + "unique": False, |
| 122 | + }, |
| 123 | + { |
| 124 | + "check": None, |
| 125 | + "default": None, |
| 126 | + "name": "username", |
| 127 | + "nullable": False, |
| 128 | + "references": None, |
| 129 | + "size": 50, |
| 130 | + "type": "character varying", |
| 131 | + "unique": False, |
| 132 | + }, |
| 133 | + { |
| 134 | + "check": None, |
| 135 | + "default": None, |
| 136 | + "name": "password", |
| 137 | + "nullable": False, |
| 138 | + "references": None, |
| 139 | + "size": 50, |
| 140 | + "type": "character varying", |
| 141 | + "unique": False, |
| 142 | + }, |
| 143 | + { |
| 144 | + "check": None, |
| 145 | + "default": None, |
| 146 | + "name": "email", |
| 147 | + "nullable": False, |
| 148 | + "references": None, |
| 149 | + "size": 255, |
| 150 | + "type": "character varying", |
| 151 | + "unique": False, |
| 152 | + }, |
| 153 | + ], |
| 154 | + "dataset": "public", |
| 155 | + "index": [], |
| 156 | + "partitioned_by": [], |
| 157 | + "primary_key": [], |
| 158 | + "table_name": "accounts", |
| 159 | + "tablespace": None, |
| 160 | + } |
| 161 | + ], |
| 162 | + "types": [], |
| 163 | + } |
| 164 | + assert expected == result |
0 commit comments