Add draft for article & constitution, auto set last_version column

This commit is contained in:
2019-09-06 00:36:41 +02:00
parent d7d88a3295
commit 56ea1507f7
5 changed files with 341 additions and 166 deletions

View File

@@ -6,6 +6,9 @@ declare
_citizen_id uuid;
created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01"}';
created_article json := '{"version_id":"933b6a1b-50c9-42b6-989f-c02a57814ef9", "title": "Love the world", "anonymous": false, "content": "bla bal bla", "tags": ["love", "test"]}';
created_article_v2 json;
first_article_id uuid;
second_article_id uuid;
selected_article json;
selected_total int;
begin
@@ -25,9 +28,14 @@ begin
select upsert_article(created_article) into created_article;
assert created_article->>'version_id' is not null, 'version_id should not be null';
assert (created_article->>'version_number')::int = 1, format('version_number must be equal to 1, %s instead', created_article->>'version_number');
assert (created_article->>'is_last_version')::bool = true, 'The first insert must be set to the last version';
first_article_id = (created_article->>'id')::uuid;
-- try to create new version
select upsert_article(created_article) into created_article;
assert (created_article->>'version_number')::int = 2, format('version_number must be equal to 2, %s instead', created_article->>'version_number');
select upsert_article(created_article) into created_article_v2;
assert (created_article_v2->>'version_number')::int = 2, format('version_number must be equal to 2, %s instead', created_article_v2->>'version_number');
assert (created_article_v2->>'is_last_version')::bool = true, 'The second insert must be set to the last version';
second_article_id = (created_article_v2->>'id')::uuid;
-- get articles versions by version_id
select resource, total into selected_article, selected_total from find_articles_versions_by_version_id((created_article->>'version_id')::uuid);
@@ -42,13 +50,29 @@ begin
assert selected_total = 2, format('the total must be 2, %s instead', selected_total);
-- get article by id and check the title
select find_article_by_id((created_article->>'id')::uuid) into selected_article;
select find_article_by_id((created_article_v2->>'id')::uuid) into selected_article;
assert selected_article->>'title' = 'Love the world', format('title must be "Love the world", %s', selected_article->>'title');
-- get article by version_id and check the title
select find_last_article_by_version_id((created_article->>'version_id')::uuid) into selected_article;
select find_last_article_by_version_id((created_article_v2->>'version_id')::uuid) into selected_article;
assert selected_article->>'title' = 'Love the world', format('title must be "Love the world", %s', selected_article->>'title');
assert (selected_article->>'version_number')::int = 2, format('version_id must be 2, %s instead', selected_article->>'version_number');
-- update to draft, then the last_version column must be change
update article
set is_draft = true
where id = second_article_id;
select find_last_article_by_version_id((created_article_v2->>'version_id')::uuid) into selected_article;
assert (selected_article->>'version_number')::int = 1, format('version_id must be 1, %s instead', selected_article->>'version_number');
update article
set is_draft = false
where id = second_article_id;
select find_last_article_by_version_id((created_article_v2->>'version_id')::uuid) into selected_article;
assert (selected_article->>'version_number')::int = 2, format('version_id must be 2, %s instead', selected_article->>'version_number');
-- -- check if user id is returned
-- assert (selected_article#>>'{created_by, user, id}')::uuid = _user_id, format('user_id must be %s instead of %s', _user_id, (selected_article#>>'{created_by, user, id}')::uuid);