diff --git a/src/pg/sql/40_observatory_utility.sql b/src/pg/sql/40_observatory_utility.sql index cb5611d..03164e5 100644 --- a/src/pg/sql/40_observatory_utility.sql +++ b/src/pg/sql/40_observatory_utility.sql @@ -5,7 +5,8 @@ -- geometries. CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GeomTable( geom geometry, - geometry_id text + geometry_id text, + time_span text DEFAULT NULL ) RETURNS TEXT AS $$ @@ -23,9 +24,11 @@ BEGIN AND coltable.column_id = col.id AND coltable.table_id = tab.id AND col.id = $1 + AND CASE WHEN $3::TEXT IS NOT NULL THEN timespan ILIKE $3::TEXT ELSE TRUE END + ORDER BY timespan DESC LIMIT 1 ) ' - USING geometry_id, geom + USING geometry_id, geom, time_span INTO result; return result; diff --git a/src/pg/test/expected/40_observatory_utility_test.out b/src/pg/test/expected/40_observatory_utility_test.out index 2895652..4a8e6c8 100644 --- a/src/pg/test/expected/40_observatory_utility_test.out +++ b/src/pg/test/expected/40_observatory_utility_test.out @@ -1,166 +1,29 @@ -\i test/sql/load_fixtures.sql +\pset format unaligned +\set ECHO all +\i test/fixtures/load_fixtures.sql SET client_min_messages TO WARNING; \set ECHO none -Loading obs_table.sql fixture file... -Done. -Loading obs_column.sql fixture file... -Done. -Loading obs_column_table.sql fixture file... -Done. -Loading obs_column_to_column.sql fixture file... -Done. -Loading obs_column_tag.sql fixture file... -Done. -Loading obs_tag.sql fixture file... -Done. -Loading obs_85328201013baa14e8e8a4a57a01e6f6fbc5f9b1.sql fixture file... -Done. -Loading obs_3e7cc9cfd403b912c57b42d5f9195af9ce2f3cdb.sql fixture file... -Done. -Loading obs_ab038198aaab3f3cb055758638ee4de28ad70146.sql fixture file... -Done. -Loading obs_a92e1111ad3177676471d66bb8036e6d057f271b.sql fixture file... -Done. -Loading obs_11ee8b82c877c073438bc935a91d3dfccef875d1.sql fixture file... -Done. -Loading obs_d34555209878e8c4b37cf0b2b3d072ff129ec470.sql fixture file... -Done. -Loading obs_b0ef6dd68d5faddbf231fd7f02916b3d00ec43c4.sql fixture file... -Done. --- OBS_GeomTable --- get table with known geometry_id --- should give back a table like obs_{hex hash} -SELECT - cdb_observatory._OBS_GeomTable( - ST_SetSRID(ST_Point(-74.0059, 40.7128), 4326), - 'us.census.tiger.census_tract' - ); - _obs_geomtable ----------------------------------------------- - obs_a92e1111ad3177676471d66bb8036e6d057f271b +_obs_geomtable +obs_fc050f0b8673cfe3c6aa1040f749eb40975691b7 (1 row) +_obs_geomtable --- get null for unknown geometry_id --- should give back null -SELECT - cdb_observatory._OBS_GeomTable( - ST_SetSRID(ST_Point(-74.0059, 40.7128), 4326), - 'us.census.tiger.nonexistant_id' -- not in catalog - ); - _obs_geomtable ----------------- - (1 row) - --- future test: give back nulls when geometry doesn't intersect --- SELECT --- cdb_observatory._OBS_GeomTable( --- ST_SetSRID(ST_Point(0,0)), -- should give back null since it's in the ocean? --- 'us.census.tiger.census_tract' --- ); --- OBS_GetColumnData --- should give back: --- colname | tablename | aggregate --- -----------|-----------------|----------- --- geoid | obs_{hex table} | null --- total_pop | obs_{hex table} | sum -WITH result as ( -SELECT - array_agg(a) expected from cdb_observatory._OBS_GetColumnData( - 'us.census.tiger.census_tract', - Array['us.census.tiger.census_tract_geoid', 'us.census.acs.B01001001'], - '2009 - 2013') a -) -select (expected)[1]::text = '{"colname":"geoid","tablename":"obs_d34555209878e8c4b37cf0b2b3d072ff129ec470","aggregate":null,"name":"US Census Tract Geoids","type":"Text","description":""}' as test_get_obs_column_with_geoid_and_census_1, - (expected)[2]::text = '{"colname":"geoid","tablename":"obs_ab038198aaab3f3cb055758638ee4de28ad70146","aggregate":null,"name":"US Census Tract Geoids","type":"Text","description":""}' as test_get_obs_column_with_geoid_and_census_2 -from result; - test_get_obs_column_with_geoid_and_census_1 | test_get_obs_column_with_geoid_and_census_2 ----------------------------------------------+--------------------------------------------- - t | t +test_get_obs_column_with_geoid_and_census_1|test_get_obs_column_with_geoid_and_census_2 +t|t (1 row) - --- should be null-valued -WITH result as ( -SELECT - array_agg(a) expected from cdb_observatory._OBS_GetColumnData( - 'us.census.tiger.census_tract', - Array['us.census.tiger.baloney'], - '2009 - 2013') a -) -select expected is null as OBS_GetColumnData_missing_measure -from result; - obs_getcolumndata_missing_measure ------------------------------------ - t +obs_getcolumndata_missing_measure +t (1 row) - --- OBS_BuildSnapshotQuery --- Should give back: SELECT vals[1] As total_pop, vals[2] As male_pop, vals[3] As female_pop, vals[4] As median_age -SELECT - cdb_observatory._OBS_BuildSnapshotQuery( - Array['total_pop','male_pop','female_pop','median_age'] - ); - _obs_buildsnapshotquery -------------------------------------------------------------------------------------------------- - SELECT vals[1] As total_pop, vals[2] As male_pop, vals[3] As female_pop, vals[4] As median_age +_obs_buildsnapshotquery +SELECT vals[1] As total_pop, vals[2] As male_pop, vals[3] As female_pop, vals[4] As median_age (1 row) - --- should give back: SELECT vals[1] As mandarin_orange -SELECT - cdb_observatory._OBS_BuildSnapshotQuery( - Array['mandarin_orange'] - ); - _obs_buildsnapshotquery ------------------------------------- - SELECT vals[1] As mandarin_orange +_obs_buildsnapshotquery +SELECT vals[1] As mandarin_orange (1 row) - -SELECT cdb_observatory._OBS_GetRelatedColumn( - Array[ - 'es.ine.pop_0_4', - 'us.census.acs.B01001001', - 'us.census.acs.B01001002' - ], - 'denominator' - ); - _obs_getrelatedcolumn -------------------------------------------------- - {es.ine.total_pop,NULL,us.census.acs.B01001001} +_obs_getrelatedcolumn +{es.ine.total_pop,NULL,us.census.acs.B01001001} (1 row) - --- should give back a standardized measure name -SELECT cdb_observatory._OBS_StandardizeMeasureName('test 343 %% 2 qqq }}{{}}'); - _obs_standardizemeasurename ------------------------------ - test_343_2_qqq +_obs_standardizemeasurename +test_343_2_qqq (1 row) - -\i test/sql/drop_fixtures.sql -SET client_min_messages TO NOTICE; -\set ECHO none -Dropping obs_table.sql fixture table... -Done. -Dropping obs_column.sql fixture table... -Done. -Dropping obs_column_table.sql fixture table... -Done. -Dropping obs_column_to_column.sql fixture table... -Done. -Dropping obs_column_tag.sql fixture table... -Done. -Dropping obs_tag.sql fixture table... -Done. -Dropping obs_85328201013baa14e8e8a4a57a01e6f6fbc5f9b1 fixture table... -Done. -Dropping obs_3e7cc9cfd403b912c57b42d5f9195af9ce2f3cdb fixture table... -Done. -Dropping obs_ab038198aaab3f3cb055758638ee4de28ad70146 fixture table... -Done. -Dropping obs_a92e1111ad3177676471d66bb8036e6d057f271b fixture table... -Done. -Dropping obs_11ee8b82c877c073438bc935a91d3dfccef875d1 fixture table... -Done. -Dropping obs_d34555209878e8c4b37cf0b2b3d072ff129ec470 fixture table... -Done. -Dropping obs_b0ef6dd68d5faddbf231fd7f02916b3d00ec43c4 fixture table... -Done. diff --git a/src/pg/test/sql/40_observatory_utility_test.sql b/src/pg/test/sql/40_observatory_utility_test.sql index 2fe41bd..d0fbf93 100644 --- a/src/pg/test/sql/40_observatory_utility_test.sql +++ b/src/pg/test/sql/40_observatory_utility_test.sql @@ -1,6 +1,6 @@ -\i test/sql/load_fixtures.sql - +\pset format unaligned \set ECHO all +\i test/fixtures/load_fixtures.sql -- OBS_GeomTable -- get table with known geometry_id @@ -8,7 +8,8 @@ SELECT cdb_observatory._OBS_GeomTable( ST_SetSRID(ST_Point(-74.0059, 40.7128), 4326), - 'us.census.tiger.census_tract' + 'us.census.tiger.census_tract', + '2014' ); -- get null for unknown geometry_id @@ -39,11 +40,10 @@ SELECT Array['us.census.tiger.census_tract_geoid', 'us.census.acs.B01001001'], '2009 - 2013') a ) -select (expected)[1]::text = '{"colname":"geoid","tablename":"obs_d34555209878e8c4b37cf0b2b3d072ff129ec470","aggregate":null,"name":"US Census Tract Geoids","type":"Text","description":""}' as test_get_obs_column_with_geoid_and_census_1, - (expected)[2]::text = '{"colname":"geoid","tablename":"obs_ab038198aaab3f3cb055758638ee4de28ad70146","aggregate":null,"name":"US Census Tract Geoids","type":"Text","description":""}' as test_get_obs_column_with_geoid_and_census_2 +select (expected)[1]::text = '{"colname":"geoid","tablename":"obs_d34555209878e8c4b37cf0b2b3d072ff129ec470","aggregate":null,"name":"US Census Tracts Geoids","type":"Text","description":null}' as test_get_obs_column_with_geoid_and_census_1, + (expected)[2]::text = '{"colname":"geoid","tablename":"obs_ab038198aaab3f3cb055758638ee4de28ad70146","aggregate":null,"name":"US Census Tracts Geoids","type":"Text","description":null}' as test_get_obs_column_with_geoid_and_census_2 from result; - -- should be null-valued WITH result as ( SELECT @@ -80,4 +80,4 @@ SELECT cdb_observatory._OBS_GetRelatedColumn( -- should give back a standardized measure name SELECT cdb_observatory._OBS_StandardizeMeasureName('test 343 %% 2 qqq }}{{}}'); -\i test/sql/drop_fixtures.sql +\i test/fixtures/drop_fixtures.sql