import test from "ava"; import {random_user} from "../../lib/testing.js"; import { User, Payment } from "../../lib/models.js"; test('test user can have many payments', async (t) => { let buyer_id = undefined; let buyer_pays = undefined; try { let fake = random_user(); t.not(fake, undefined); let buyer = await User.insert(fake); t.not(buyer, undefined); t.is(buyer.full_name, fake.full_name); // use this to delete in the finally buyer_id = buyer.id; // confirm that we get unpaid when there's no payment const [unpaid, paytest] = await Payment.paid(buyer); t.false(unpaid); t.is(paytest, undefined); let pay1 = await Payment.fake_payment(); t.not(pay1, undefined); await Payment.update({id: pay1.id}, {user_id: buyer.id}); pay1.user_id = buyer.id; // quick dirty update // test payment has user // TODO: test payment with multiple users let pay1_user = await pay1.user; t.not(pay1_user, undefined); t.is(pay1_user.id, buyer.id); // now test the reverse users has payments buyer_pays = await buyer.payments(); t.not(buyer_pays, undefined); t.not(buyer_pays.length > 0, undefined); t.is(buyer_pays[0].id, pay1.id); const [paid, payment] = await Payment.paid(buyer); t.true(paid); t.is(payment.status, "complete"); } catch(error) { console.log(error); t.fail(error.message); } finally { if(buyer_id) await User.delete({id: buyer_id}); if(buyer_pays) { for(let pay of buyer_pays) { await Payment.delete({id: pay.id}); } } } });