Update metadata tests, Refactor metadata table, Add metadata_ext tests

This commit is contained in:
xbgmsharp
2025-05-17 17:22:24 +02:00
parent 02130a9e4f
commit ad2e95bfa8
4 changed files with 136 additions and 45 deletions

View File

@@ -343,6 +343,18 @@ let configtime = new Date().toISOString();
obj_name: 'settings'
}
},
],
meta_ext_fn: [
{ url: '/metadata_ext?',
res: {
obj_name: 'configuration'
}
},
{ url: `/metadata_ext?`,
res: {
obj_name: 'image'
}
},
]
}
].forEach( function(test){
@@ -621,7 +633,7 @@ request.set('User-Agent', 'PostgSail unit tests');
.set('Authorization', `Bearer ${vessel_jwt}`)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('Prefer', 'return=headers-only,resolution=merge-duplicates')
.set('Prefer', 'missing=default,return=headers-only,resolution=merge-duplicates')
.end(function(err,res){
res.status.should.equal(201);
//console.log(res.header);

View File

@@ -413,19 +413,19 @@ request.set('User-Agent', 'PostgSail unit tests');
describe("Vessel POST metadata, JWT vessel_role", function(){
it('/metadata', function(done) {
it('/metadata?on_conflict=vessel_id', function(done) {
request = supertest.agent(test.cname);
request
.post('/metadata')
.post('/metadata?on_conflict=vessel_id')
.send(test.vessel_metadata)
.set('Authorization', `Bearer ${vessel_jwt}`)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('Prefer', 'return=headers-only')
.set('Prefer', 'missing=default,return=headers-only,resolution=merge-duplicates')
.end(function(err,res){
//console.log(res.body);
//console.log(res.header);
res.status.should.equal(201);
res.status.should.equal(200);
should.exist(res.header['server']);
res.header['server'].should.match(new RegExp('postgrest','g'));
done(err);

View File

@@ -11,22 +11,74 @@ select current_database();
-- output display format
\x on
SELECT count(*) as count_eq_2 FROM api.metadata m;
SELECT v.vessel_id as "vessel_id" FROM auth.vessels v WHERE v.owner_email = 'demo+kapla@openplotter.cloud' \gset
--\echo :"vessel_id"
SELECT set_config('vessel.id', :'vessel_id', false) IS NOT NULL as vessel_id;
--SELECT * FROM api.metadata m;
-- user_role
SET ROLE user_role;
\echo 'api.metadata details'
SELECT m.id, m.name, m.mmsi, m.length, m.beam, m.height, m.ship_type, m.plugin_version, m.signalk_version, m.time IS NOT NULL AS time, m.active, configuration, available_keys FROM api.metadata AS m ORDER BY m.name ASC;
SELECT vessel_id IS NOT NULL AS vessel_id_not_null, m.name, m.mmsi, m.length, m.beam, m.height, m.ship_type, m.plugin_version, m.signalk_version, m.time IS NOT NULL AS time, m.active, configuration, available_keys FROM api.metadata AS m ORDER BY m.name ASC;
\echo 'api.metadata get configuration'
select configuration from api.metadata WHERE vessel_id = current_setting('vessel.id', false);
select configuration from api.metadata; --WHERE vessel_id = current_setting('vessel.id', false);
\echo 'api.metadata update configuration'
UPDATE api.metadata SET configuration = '{ "depthKey": "environment.depth.belowTransducer" }' WHERE vessel_id = current_setting('vessel.id', false);
UPDATE api.metadata SET configuration = '{ "depthKey": "environment.depth.belowTransducer" }'; --WHERE vessel_id = current_setting('vessel.id', false);
\echo 'api.metadata get configuration with new value'
select configuration->'depthKey' AS depthKey, configuration->'update_at' IS NOT NULL AS update_at from api.metadata WHERE vessel_id = current_setting('vessel.id', false);
select configuration->'depthKey' AS depthKey, configuration->'update_at' IS NOT NULL AS update_at_not_null from api.metadata; --WHERE vessel_id = current_setting('vessel.id', false);
\echo 'api.metadata get configuration base on update_at value'
select configuration->'depthKey' AS depthKey, configuration->'update_at' IS NOT NULL AS update_at from api.metadata WHERE vessel_id = current_setting('vessel.id', false) AND configuration->>'update_at' <= to_char(NOW(), 'YYYY-MM-DD"T"HH24:MI:SS"Z"');
select configuration->'depthKey' AS depthKey, configuration->'update_at' IS NOT NULL AS update_at_not_null from api.metadata WHERE vessel_id = current_setting('vessel.id', false) AND configuration->>'update_at' <= to_char(NOW(), 'YYYY-MM-DD"T"HH24:MI:SS"Z"');
-- Upsert make_model on metadata_ext table
\echo 'api.metadata_ext set make_model'
INSERT INTO api.metadata_ext (vessel_id, make_model)
VALUES (current_setting('vessel.id', false), 'my super yacht')
ON CONFLICT (vessel_id) DO UPDATE
SET make_model = EXCLUDED.make_model;
-- Upsert polar on metadata_ext table
\echo 'api.metadata_ext set polar'
INSERT INTO api.metadata_ext (vessel_id, polar)
VALUES (current_setting('vessel.id', false), 'twa/tws;4;6;8;10;12;14;16;20;24\n0;0;0;0;0;0;0;0;0;0')
ON CONFLICT (vessel_id) DO UPDATE
SET polar = EXCLUDED.polar;
-- Upsert image on metadata_ext table
\echo 'api.metadata_ext set image/image_b64'
INSERT INTO api.metadata_ext (vessel_id, image_b64)
VALUES (current_setting('vessel.id', false), 'iVBORw0KGgoAAAANSUhEUgAAAMgAAAAyCAIAAACWMwO2AAABNklEQVR4nO3bwY6CMBiF0XYy7//KzIKk6VBjiMMNk59zVljRIH6WsrBv29bgal93HwA1CYsIYREhLCKERYSwiBAWEcIiQlhECIsIYREhLCKERYSwiBAWEcIiQlhECIsIYREhLCK+7z6A/6j33lq75G8m')
ON CONFLICT (vessel_id) DO UPDATE
SET image_b64 = EXCLUDED.image_b64;
-- Ensure make_model on metadata_ext table is updated
\echo 'api.metadata_ext get make_model'
SELECT make_model FROM api.metadata_ext; --WHERE vessel_id = current_setting('vessel.id', false);
-- Ensure polar_updated_at on metadata_ext table is updated by trigger
\echo 'api.metadata_ext get polar_updated_at'
SELECT polar,polar_updated_at IS NOT NULL AS polar_updated_at_not_null FROM api.metadata_ext; --WHERE vessel_id = current_setting('vessel.id', false);
-- Ensure image_updated_at on metadata_ext table is updated by trigger
\echo 'api.metadata_ext get image_updated_at'
SELECT image_b64 IS NULL AS image_b64_is_null,image IS NOT NULL AS image_not_null,image_updated_at IS NOT NULL AS image_updated_at_not_null FROM api.metadata_ext; --WHERE vessel_id = current_setting('vessel.id', false);
-- vessel_role
SET ROLE vessel_role;
\echo 'api.metadata get configuration with new value as vessel'
select configuration->'depthKey' AS depthKey, configuration->'update_at' IS NOT NULL AS update_at_not_null from api.metadata; -- WHERE vessel_id = current_setting('vessel.id', false);
\echo 'api.metadata get configuration base on update_at value as vessel'
select configuration->'depthKey' AS depthKey, configuration->'update_at' IS NOT NULL AS update_at_not_null from api.metadata WHERE vessel_id = current_setting('vessel.id', false) AND configuration->>'update_at' <= to_char(NOW(), 'YYYY-MM-DD"T"HH24:MI:SS"Z"');
-- api_anonymous
SET ROLE api_anonymous;
\echo 'api_anonymous get vessel image'
SELECT api.vessel_image(current_setting('vessel.id', false)) IS NOT NULL AS vessel_image_not_null;

View File

@@ -5,26 +5,16 @@
You are now connected to database "signalk" as user "username".
Expanded display is on.
-[ RECORD 1 ]-
count_eq_2 | 2
-[ RECORD 1 ]
vessel_id | t
SET
api.metadata details
-[ RECORD 1 ]---+----------------
id | 2
name | aava
mmsi | 787654321
length | 12
beam | 10
height | 24
ship_type | 37
plugin_version | 1.0.2
signalk_version | 1.20.0
time | t
active | t
configuration |
available_keys | []
-[ RECORD 2 ]---+----------------
id | 1
-[ RECORD 1 ]------+----------------
vessel_id_not_null | t
name | kapla
mmsi | 123456789
length | 12
@@ -45,12 +35,49 @@ configuration |
api.metadata update configuration
UPDATE 1
api.metadata get configuration with new value
-[ RECORD 1 ]----------------------------------
-[ RECORD 1 ]------+------------------------------------
depthkey | "environment.depth.belowTransducer"
update_at | t
update_at_not_null | t
api.metadata get configuration base on update_at value
-[ RECORD 1 ]----------------------------------
-[ RECORD 1 ]------+------------------------------------
depthkey | "environment.depth.belowTransducer"
update_at | t
update_at_not_null | t
api.metadata_ext set make_model
INSERT 0 1
api.metadata_ext set polar
INSERT 0 1
api.metadata_ext set image/image_b64
INSERT 0 1
api.metadata_ext get make_model
-[ RECORD 1 ]--------------
make_model | my super yacht
api.metadata_ext get polar_updated_at
-[ RECORD 1 ]-------------+-----------------------------------------------------
polar | twa/tws;4;6;8;10;12;14;16;20;24\n0;0;0;0;0;0;0;0;0;0
polar_updated_at_not_null | t
api.metadata_ext get image_updated_at
-[ RECORD 1 ]-------------+--
image_b64_is_null | f
image_not_null | t
image_updated_at_not_null | t
SET
api.metadata get configuration with new value as vessel
-[ RECORD 1 ]------+------------------------------------
depthkey | "environment.depth.belowTransducer"
update_at_not_null | t
api.metadata get configuration base on update_at value as vessel
-[ RECORD 1 ]------+------------------------------------
depthkey | "environment.depth.belowTransducer"
update_at_not_null | t
SET
api_anonymous get vessel image
-[ RECORD 1 ]---------+--
vessel_image_not_null | t