From e4e1b626a60253cdf4c9347e55316311af07be4d Mon Sep 17 00:00:00 2001 From: Taimoor Zaeem Date: Tue, 11 Nov 2025 12:35:36 +0500 Subject: [PATCH] test(io): move resource embedding tests to test_io.py - Adds fixtures to `test/io/fixtures.sql` to test resource embedding related queries. - Moves the resource embedding related tests that no longer require big schema from `test_big_schema.py` to `test_io.py`. Closes #4417. Signed-off-by: Taimoor Zaeem (cherry picked from commit 91abcd49e12b2893a8e61234a0dbdf433baa94aa) --- ...chema_cache_snapshot[dbRelationships].yaml | 40 ++++++++++- ...est_schema_cache_snapshot[dbRoutines].yaml | 17 +++++ .../test_schema_cache_snapshot[dbTables].yaml | 71 +++++++++++++++++++ test/io/fixtures.sql | 38 ++++++++++ test/io/test_big_schema.py | 52 -------------- test/io/test_io.py | 48 +++++++++++++ 6 files changed, 213 insertions(+), 53 deletions(-) diff --git a/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRelationships].yaml b/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRelationships].yaml index fe51488c7..c633fc35b 100644 --- a/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRelationships].yaml +++ b/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRelationships].yaml @@ -1 +1,39 @@ -[] +- - - qiName: directors + qiSchema: public + - public + - - relCardinality: + relColumns: + - - id + - director_id + relCons: fk_director + tag: O2M + relFTableIsView: false + relForeignTable: + qiName: films + qiSchema: public + relIsSelf: false + relTable: + qiName: directors + qiSchema: public + relTableIsView: false + tag: Relationship + +- - - qiName: films + qiSchema: public + - public + - - relCardinality: + relColumns: + - - director_id + - id + relCons: fk_director + tag: M2O + relFTableIsView: false + relForeignTable: + qiName: directors + qiSchema: public + relIsSelf: false + relTable: + qiName: films + qiSchema: public + relTableIsView: false + tag: Relationship diff --git a/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml b/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml index b3be27232..61a871c89 100644 --- a/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml +++ b/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml @@ -106,6 +106,23 @@ pdSchema: public pdVolatility: Volatile +- - qiName: notify_pgrst + qiSchema: public + - - pdDescription: null + pdFuncSettings: [] + pdHasVariadic: false + pdName: notify_pgrst + pdParams: [] + pdReturnType: + contents: + contents: + qiName: void + qiSchema: pg_catalog + tag: Scalar + tag: Single + pdSchema: public + pdVolatility: Volatile + - - qiName: migrate_function qiSchema: public - - pdDescription: null diff --git a/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbTables].yaml b/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbTables].yaml index 3734502fa..1f1ac761e 100644 --- a/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbTables].yaml +++ b/test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbTables].yaml @@ -71,6 +71,37 @@ tableSchema: public tableUpdatable: true +- - qiName: directors + qiSchema: public + - tableColumns: + id: + colDefault: null + colDescription: null + colEnum: [] + colMaxLen: null + colName: id + colNominalType: integer + colNullable: false + colType: integer + name: + colDefault: null + colDescription: null + colEnum: [] + colMaxLen: null + colName: name + colNominalType: text + colNullable: true + colType: text + tableDeletable: true + tableDescription: null + tableInsertable: true + tableIsView: false + tableName: directors + tablePKCols: + - id + tableSchema: public + tableUpdatable: true + - - qiName: projects qiSchema: public - tableColumns: {} @@ -95,6 +126,46 @@ tableSchema: public tableUpdatable: false +- - qiName: films + qiSchema: public + - tableColumns: + director_id: + colDefault: null + colDescription: null + colEnum: [] + colMaxLen: null + colName: director_id + colNominalType: integer + colNullable: true + colType: integer + id: + colDefault: null + colDescription: null + colEnum: [] + colMaxLen: null + colName: id + colNominalType: integer + colNullable: false + colType: integer + title: + colDefault: null + colDescription: null + colEnum: [] + colMaxLen: null + colName: title + colNominalType: text + colNullable: true + colType: text + tableDeletable: true + tableDescription: null + tableInsertable: true + tableIsView: false + tableName: films + tablePKCols: + - id + tableSchema: public + tableUpdatable: true + - - qiName: items qiSchema: public - tableColumns: diff --git a/test/io/fixtures.sql b/test/io/fixtures.sql index f40fd4554..cbb70f6a1 100644 --- a/test/io/fixtures.sql +++ b/test/io/fixtures.sql @@ -260,3 +260,41 @@ select * from infinite_recursion; create or replace function "true"() returns boolean as $_$ select true; $_$ language sql; + +create or replace function notify_pgrst() returns void as $$ + notify pgrst; +$$ language sql; + +-- directors and films table can be used for resource embedding tests +create table directors ( + id int primary key, + name text +); + +create table films ( + id int primary key, + title text, + director_id int, + + constraint fk_director + foreign key (director_id) references directors (id) + on update cascade + on delete cascade +); + +-- data to test resource embedding +truncate table directors cascade; +insert into directors +values (1, 'quentin tarantino'), + (2, 'christopher nolan'), + (3, 'yorgos lathinmos'); + +truncate table films cascade; +insert into films +values (1, 'pulp fiction', 1), + (2, 'intersteller',2), + (3, 'dogtooth',3), + (4, 'reservoir dogs', 1); + + +GRANT SELECT ON directors, films TO postgrest_test_anonymous, postgrest_test_w_superuser_settings; diff --git a/test/io/test_big_schema.py b/test/io/test_big_schema.py index 70516f47e..ae8e8a990 100644 --- a/test/io/test_big_schema.py +++ b/test/io/test_big_schema.py @@ -7,58 +7,6 @@ import pytest from postgrest import run -def test_requests_with_resource_embedding_wait_for_schema_cache_reload(defaultenv): - "requests that use the schema cache with resource embedding wait long for the schema cache to reload" - - env = { - **defaultenv, - "PGRST_DB_SCHEMAS": "apflora", - "PGRST_DB_POOL": "2", - "PGRST_DB_ANON_ROLE": "postgrest_test_anonymous", - "PGRST_INTERNAL_SCHEMA_CACHE_RELATIONSHIP_LOAD_SLEEP": "5100", - } - - with run(env=env, wait_max_seconds=30) as postgrest: - # reload the schema cache - response = postgrest.session.get("/rpc/notify_pgrst") - assert response.status_code == 204 - - postgrest.wait_until_scache_starts_loading() - - response = postgrest.session.get("/tpopmassn?select=*,tpop(*)") - assert response.status_code == 200 - - assert response.elapsed.total_seconds() > 5 - - -def test_requests_without_resource_embedding_wait_for_schema_cache_reload(defaultenv): - "requests that use the schema cache without resource embedding wait less for the schema cache to reload" - - env = { - **defaultenv, - "PGRST_DB_SCHEMAS": "apflora", - "PGRST_DB_POOL": "2", - "PGRST_DB_ANON_ROLE": "postgrest_test_anonymous", - "PGRST_INTERNAL_SCHEMA_CACHE_LOAD_SLEEP": "1100", - "PGRST_INTERNAL_SCHEMA_CACHE_RELATIONSHIP_LOAD_SLEEP": "5000", - } - - with run(env=env, wait_max_seconds=30) as postgrest: - # reload the schema cache - response = postgrest.session.get("/rpc/notify_pgrst") - assert response.status_code == 204 - - postgrest.wait_until_scache_starts_loading() - - response = postgrest.session.get("/tpopmassn") - assert response.status_code == 200 - - assert ( - response.elapsed.total_seconds() > 1 - and response.elapsed.total_seconds() < 5 - ) - - def test_schema_cache_load_max_duration(defaultenv): "schema cache load should not surpass a max_duration of elapsed milliseconds" diff --git a/test/io/test_io.py b/test/io/test_io.py index a02d29948..21749767c 100644 --- a/test/io/test_io.py +++ b/test/io/test_io.py @@ -2072,3 +2072,51 @@ def test_db_pre_config_with_pg_reserved_words(defaultenv): in line for line in output ) + + +def test_requests_with_resource_embedding_wait_for_schema_cache_reload(defaultenv): + "requests that use the schema cache with resource embedding wait long for the schema cache to reload" + + env = { + **defaultenv, + "PGRST_DB_POOL": "2", + "PGRST_INTERNAL_SCHEMA_CACHE_RELATIONSHIP_LOAD_SLEEP": "5100", + } + + with run(env=env, wait_max_seconds=30) as postgrest: + # reload the schema cache + response = postgrest.session.get("/rpc/notify_pgrst") + assert response.status_code == 204 + + postgrest.wait_until_scache_starts_loading() + + response = postgrest.session.get("/directors?select=id,name,films(title)") + assert response.status_code == 200 + + assert response.elapsed.total_seconds() > 5 + + +def test_requests_without_resource_embedding_wait_for_schema_cache_reload(defaultenv): + "requests that use the schema cache without resource embedding wait less for the schema cache to reload" + + env = { + **defaultenv, + "PGRST_DB_POOL": "2", + "PGRST_INTERNAL_SCHEMA_CACHE_LOAD_SLEEP": "1100", + "PGRST_INTERNAL_SCHEMA_CACHE_RELATIONSHIP_LOAD_SLEEP": "5000", + } + + with run(env=env, wait_max_seconds=30) as postgrest: + # reload the schema cache + response = postgrest.session.get("/rpc/notify_pgrst") + assert response.status_code == 204 + + postgrest.wait_until_scache_starts_loading() + + response = postgrest.session.get("/films") + assert response.status_code == 200 + + assert ( + response.elapsed.total_seconds() > 1 + and response.elapsed.total_seconds() < 5 + )