33from datasette .utils import error_body , escape_sqlite , sqlite3
44from .utils import last_event
55import pytest
6- import re
76import time
87
98
10- def schema_variants (schema ):
11- # sqlite-utils < 4 quotes identifiers [like_this] and uses FLOAT;
12- # sqlite-utils >= 4 quotes them "like_this" and uses REAL. Given a
13- # schema fragment in the old format, return both variants so tests
14- # can pass against either version.
15- converted = re .sub (r"\[([^\]]+)\]" , r'"\1"' , schema ).replace ("FLOAT" , "REAL" )
16- return (schema , converted )
17-
18-
199def assert_schema_contains (fragment , schema ):
20- assert any (
21- variant in schema for variant in schema_variants ( fragment )
22- ), "Expected schema to contain {!r}, got {!r}" . format ( fragment , schema )
10+ assert fragment in schema , "Expected schema to contain {!r}, got {!r}" . format (
11+ fragment , schema
12+ )
2313
2414
2515def assert_schema_not_contains (fragment , schema ):
26- assert not any (
27- variant in schema for variant in schema_variants ( fragment )
16+ assert (
17+ fragment not in schema
2818 ), "Expected schema not to contain {!r}, got {!r}" .format (fragment , schema )
2919
3020
@@ -94,8 +84,8 @@ async def test_base64_write_api_create_table_infers_blob_and_raw_escapes(ds_writ
9484 headers = _headers (token ),
9585 )
9686 assert response .status_code == 201
97- assert_schema_contains ("[ data] BLOB" , response .json ()["schema" ])
98- assert_schema_contains ("[ literal] TEXT" , response .json ()["schema" ])
87+ assert_schema_contains ('" data" BLOB' , response .json ()["schema" ])
88+ assert_schema_contains ('" literal" TEXT' , response .json ()["schema" ])
9989
10090 rows = (await ds_write .get_database ("data" ).execute ("""
10191 select
@@ -1217,7 +1207,7 @@ async def test_alter_table_foreign_key_operations(ds_write):
12171207 data = response .json ()
12181208 assert data ["operations_applied" ] == 2
12191209 assert_schema_contains (
1220- "[ owner_id] INTEGER REFERENCES [ owners]([id])" , data ["schema" ]
1210+ '" owner_id" INTEGER REFERENCES " owners"("id")' , data ["schema" ]
12211211 )
12221212
12231213 response = await ds_write .client .post (
@@ -1229,7 +1219,7 @@ async def test_alter_table_foreign_key_operations(ds_write):
12291219 )
12301220 assert response .status_code == 200 , response .text
12311221 data = response .json ()
1232- assert_schema_not_contains ("[ owner_id] INTEGER REFERENCES" , data ["schema" ])
1222+ assert_schema_not_contains ('" owner_id" INTEGER REFERENCES' , data ["schema" ])
12331223
12341224 response = await ds_write .client .post (
12351225 "/data/docs/-/alter" ,
@@ -1254,7 +1244,7 @@ async def test_alter_table_foreign_key_operations(ds_write):
12541244 assert response .status_code == 200 , response .text
12551245 data = response .json ()
12561246 assert_schema_contains (
1257- "[ owner_id] INTEGER REFERENCES [ categories]([id])" , data ["schema" ]
1247+ '" owner_id" INTEGER REFERENCES " categories"("id")' , data ["schema" ]
12581248 )
12591249
12601250 response = await ds_write .client .post (
@@ -1264,7 +1254,7 @@ async def test_alter_table_foreign_key_operations(ds_write):
12641254 )
12651255 assert response .status_code == 200 , response .text
12661256 data = response .json ()
1267- assert_schema_not_contains ("[ owner_id] INTEGER REFERENCES" , data ["schema" ])
1257+ assert_schema_not_contains ('" owner_id" INTEGER REFERENCES' , data ["schema" ])
12681258
12691259
12701260@pytest .mark .asyncio
@@ -1791,12 +1781,12 @@ async def test_drop_table(ds_write, scenario):
17911781 "table_url" : "http://localhost/data/one" ,
17921782 "table_api_url" : "http://localhost/data/one.json" ,
17931783 "schema" : (
1794- " CREATE TABLE [ one] (\n "
1795- " [id] INTEGER PRIMARY KEY,\n "
1796- " [ title] TEXT,\n "
1797- " [ score] INTEGER,\n "
1798- " [ weight] FLOAT ,\n "
1799- " [ thumbnail] BLOB\n "
1784+ ' CREATE TABLE " one" (\n '
1785+ ' "id" INTEGER PRIMARY KEY,\n '
1786+ ' " title" TEXT,\n '
1787+ ' " score" INTEGER,\n '
1788+ ' " weight" REAL ,\n '
1789+ ' " thumbnail" BLOB\n '
18001790 ")"
18011791 ),
18021792 },
@@ -1828,10 +1818,10 @@ async def test_drop_table(ds_write, scenario):
18281818 "table_url" : "http://localhost/data/two" ,
18291819 "table_api_url" : "http://localhost/data/two.json" ,
18301820 "schema" : (
1831- " CREATE TABLE [ two] (\n "
1832- " [id] INTEGER PRIMARY KEY,\n "
1833- " [ title] TEXT,\n "
1834- " [ score] FLOAT \n "
1821+ ' CREATE TABLE " two" (\n '
1822+ ' "id" INTEGER PRIMARY KEY,\n '
1823+ ' " title" TEXT,\n '
1824+ ' " score" REAL \n '
18351825 ")"
18361826 ),
18371827 "row_count" : 2 ,
@@ -1857,10 +1847,10 @@ async def test_drop_table(ds_write, scenario):
18571847 "table_url" : "http://localhost/data/three" ,
18581848 "table_api_url" : "http://localhost/data/three.json" ,
18591849 "schema" : (
1860- " CREATE TABLE [ three] (\n "
1861- " [id] INTEGER PRIMARY KEY,\n "
1862- " [ title] TEXT,\n "
1863- " [ score] FLOAT \n "
1850+ ' CREATE TABLE " three" (\n '
1851+ ' "id" INTEGER PRIMARY KEY,\n '
1852+ ' " title" TEXT,\n '
1853+ ' " score" REAL \n '
18641854 ")"
18651855 ),
18661856 "row_count" : 1 ,
@@ -1882,7 +1872,7 @@ async def test_drop_table(ds_write, scenario):
18821872 "table" : "four" ,
18831873 "table_url" : "http://localhost/data/four" ,
18841874 "table_api_url" : "http://localhost/data/four.json" ,
1885- "schema" : (" CREATE TABLE [ four] (\n " " [ name] TEXT\n " ")" ),
1875+ "schema" : (' CREATE TABLE " four" (\n ' ' " name" TEXT\n ' ")" ),
18861876 "row_count" : 1 ,
18871877 },
18881878 ["create-table" , "insert-rows" ],
@@ -1902,8 +1892,8 @@ async def test_drop_table(ds_write, scenario):
19021892 "table_url" : "http://localhost/data/five" ,
19031893 "table_api_url" : "http://localhost/data/five.json" ,
19041894 "schema" : (
1905- " CREATE TABLE [ five] (\n [ type] TEXT,\n [ key] INTEGER,\n "
1906- " [ title] TEXT,\n PRIMARY KEY ([ type], [ key] )\n )"
1895+ ' CREATE TABLE " five" (\n " type" TEXT,\n " key" INTEGER,\n '
1896+ ' " title" TEXT,\n PRIMARY KEY (" type", " key" )\n )'
19071897 ),
19081898 "row_count" : 1 ,
19091899 },
@@ -2193,7 +2183,7 @@ async def test_create_table(
21932183 # Error expectations list their messages; derive the canonical envelope
21942184 expected_response = error_body (expected_response ["errors" ], expected_status )
21952185 if isinstance (expected_response , dict ) and "schema" in expected_response :
2196- assert data .get ("schema" ) in schema_variants ( expected_response ["schema" ])
2186+ assert data .get ("schema" ) == expected_response ["schema" ]
21972187 expected_response = dict (expected_response , schema = data .get ("schema" ))
21982188 assert data == expected_response
21992189 # Should have tracked the expected events
@@ -2238,7 +2228,7 @@ async def test_create_table_with_foreign_key(ds_write):
22382228 assert response .status_code == 201
22392229 data = response .json ()
22402230 assert_schema_contains (
2241- "[ owner_id] INTEGER REFERENCES [ owners]([id])" , data ["schema" ]
2231+ '" owner_id" INTEGER REFERENCES " owners"("id")' , data ["schema" ]
22422232 )
22432233
22442234
0 commit comments