From 5329d23de236e5bb89757dfde532cd7810748d5e Mon Sep 17 00:00:00 2001
From: Masaya Tojo <masaya@tojo.tokyo>
Date: Fri, 10 Mar 2023 09:08:49 +0900
Subject: Add function to assign an article to an transaction.

---
 verify/assign_article_to_transaction.sql | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 verify/assign_article_to_transaction.sql

(limited to 'verify/assign_article_to_transaction.sql')

diff --git a/verify/assign_article_to_transaction.sql b/verify/assign_article_to_transaction.sql
new file mode 100644
index 0000000..7fa8ee5
--- /dev/null
+++ b/verify/assign_article_to_transaction.sql
@@ -0,0 +1,27 @@
+-- Verify kakeibo:assign_article_to_transaction on pg
+
+BEGIN;
+
+SELECT has_function_privilege('kakeibo.assign_article_to_transaction(INTEGER, TEXT)', 'execute');
+
+DO $$
+DECLARE
+  v_transaction_id INTEGER;
+  v_article_id INTEGER;
+  v_location TEXT;
+BEGIN
+  SELECT md5(random()::TEXT) INTO v_location;
+  SELECT kakeibo.insert_transaction(0, '2023-03-09', 'テスト') INTO v_transaction_id;
+  INSERT INTO kakeibo.articles (location, title)
+  VALUES (v_location, 'テストタイトル') RETURNING id INTO v_article_id;
+  PERFORM kakeibo.assign_article_to_transaction(v_transaction_id, v_location);
+
+  PERFORM 1 from kakeibo.transactions
+   WHERE id = v_transaction_id
+     AND article_id = v_article_id;
+  IF NOT FOUND THEN
+    RAISE EXCEPTION 'Fail assign_artilce_to_transaction';
+  END IF;
+END $$;
+
+ROLLBACK;
-- 
cgit v1.2.3

