-- 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;
