Files
myeasycms-v2/apps/web/supabase/tests/database/billing-orders.test.sql
giancarlo c6bb9f0758 Update tests for order and subscription updates
Added comprehensive tests for the functionality of creating, updating, and verifying billing orders and subscriptions in the Supabase database. Tests now ensure appropriate behavior of upsert operations, validation of inserted or updated data, and proper calculation of derived fields. This contributes to better coverage and confidence in the production-ready status of these features.
2024-04-20 23:48:03 +08:00

51 lines
1.8 KiB
PL/PgSQL

begin;
create extension "basejump-supabase_test_helpers" version '0.0.6';
select no_plan();
select makerkit.set_identifier('primary_owner', 'test@makerkit.dev');
select makerkit.set_identifier('owner', 'owner@makerkit.dev');
select makerkit.set_identifier('member', 'member@makerkit.dev');
select makerkit.set_identifier('custom', 'custom@makerkit.dev');
INSERT INTO public.billing_customers(account_id, provider, customer_id)
VALUES (tests.get_supabase_uid('primary_owner'), 'stripe', 'cus_test');
-- Call the upsert_order function
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'pending', 'stripe', 100, 'usd', '[{"product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 1}]');
-- Verify that the order was created correctly
SELECT is(
(SELECT status FROM public.orders WHERE id = 'order_test'),
'pending',
'The order status should be pending'
);
-- Verify that the subscription items were created correctly
SELECT row_eq(
$$ select count(*) from order_items where order_id = 'order_test' $$,
row(1::bigint),
'The order items should be created'
);
-- Call the upsert_order function again to update the order
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'succeeded', 'stripe', 100, 'usd', '[{"product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 10}]');
-- Verify that the order was updated correctly
SELECT is(
(SELECT status FROM public.orders WHERE id = 'order_test'),
'succeeded',
'The order status should be succeeded'
);
SELECT row_eq(
$$ select quantity from order_items where variant_id = 'var_test' $$,
row(10::int),
'The subscription items should be updated'
);
-- Finish the tests and clean up
SELECT * FROM finish();
ROLLBACK;