|
2 | 2 | from sqlite_minutils.utils import OperationalError |
3 | 3 | from sqlite3 import IntegrityError |
4 | 4 | import pytest |
| 5 | +import apsw |
5 | 6 |
|
6 | 7 |
|
7 | 8 | @pytest.mark.parametrize( |
@@ -119,12 +120,12 @@ def tracer(sql, params): |
119 | 120 | dogs.transform(**params) |
120 | 121 | # If use_pragma_foreign_keys, check that we did the right thing |
121 | 122 | if use_pragma_foreign_keys: |
122 | | - assert ("PRAGMA foreign_keys=0;", None) in captured |
| 123 | + assert ("PRAGMA foreign_keys=off;", None) in captured |
123 | 124 | assert captured[-2] == ("PRAGMA foreign_key_check;", None) |
124 | | - assert captured[-1] == ("PRAGMA foreign_keys=1;", None) |
| 125 | + assert captured[-1] == ("PRAGMA foreign_keys=on;", None) |
125 | 126 | else: |
126 | | - assert ("PRAGMA foreign_keys=0;", None) not in captured |
127 | | - assert ("PRAGMA foreign_keys=1;", None) not in captured |
| 127 | + assert ("PRAGMA foreign_keys=off;", None) not in captured |
| 128 | + assert ("PRAGMA foreign_keys=on;", None) not in captured |
128 | 129 |
|
129 | 130 |
|
130 | 131 | @pytest.mark.parametrize( |
@@ -172,32 +173,25 @@ def tracer(sql, params): |
172 | 173 | ), |
173 | 174 | ], |
174 | 175 | ) |
175 | | -@pytest.mark.parametrize("use_pragma_foreign_keys", [False, True]) |
176 | 176 | def test_transform_sql_table_with_no_primary_key( |
177 | | - fresh_db, params, expected_sql, use_pragma_foreign_keys |
| 177 | + fresh_db, params, expected_sql |
178 | 178 | ): |
179 | 179 | captured = [] |
180 | 180 |
|
181 | 181 | def tracer(sql, params): |
182 | 182 | return captured.append((sql, params)) |
183 | 183 |
|
184 | 184 | dogs = fresh_db["dogs"] |
185 | | - if use_pragma_foreign_keys: |
186 | | - fresh_db.conn.execute("PRAGMA foreign_keys=ON") |
187 | 185 | dogs.insert({"id": 1, "name": "Cleo", "age": "5"}) |
188 | 186 | sql = dogs.transform_sql(**{**params, **{"tmp_suffix": "suffix"}}) |
189 | 187 | assert sql == expected_sql |
190 | 188 | # Check that .transform() runs without exceptions: |
191 | 189 | with fresh_db.tracer(tracer): |
192 | 190 | dogs.transform(**params) |
193 | | - # If use_pragma_foreign_keys, check that we did the right thing |
194 | | - if use_pragma_foreign_keys: |
195 | | - assert ("PRAGMA foreign_keys=0;", None) in captured |
196 | | - assert captured[-2] == ("PRAGMA foreign_key_check;", None) |
197 | | - assert captured[-1] == ("PRAGMA foreign_keys=1;", None) |
198 | | - else: |
199 | | - assert ("PRAGMA foreign_keys=0;", None) not in captured |
200 | | - assert ("PRAGMA foreign_keys=1;", None) not in captured |
| 191 | + # We always use foreign keys |
| 192 | + assert ("PRAGMA foreign_keys=off;", None) in captured |
| 193 | + assert captured[-2] == ("PRAGMA foreign_key_check;", None) |
| 194 | + assert captured[-1] == ("PRAGMA foreign_keys=on;", None) |
201 | 195 |
|
202 | 196 |
|
203 | 197 | def test_transform_sql_with_no_primary_key_to_primary_key_of_id(fresh_db): |
@@ -392,7 +386,7 @@ def test_transform_verify_foreign_keys(fresh_db): |
392 | 386 | try: |
393 | 387 | fresh_db["authors"].transform(rename={"id": "id2"}) |
394 | 388 | fresh_db.commit() |
395 | | - except IntegrityError: |
| 389 | + except apsw.ConstraintError: |
396 | 390 | fresh_db.rollback() |
397 | 391 |
|
398 | 392 | # This should have rolled us back |
|
0 commit comments