From 191680ce49a2228710f0bfc6f6236e4f5bd0628b Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 24 Jun 2018 02:33:57 -0400 Subject: add some tests --- t/account.t | 37 ++++ t/basic.t | 48 ++++ t/budget.t | 115 ++++++++++ t/category.t | 36 +++ t/lib/WWW/YNAB/MockUA.pm | 565 +++++++++++++++++++++++++++++++++++++++++++++++ t/month.t | 43 ++++ t/payee.t | 31 +++ t/transaction.t | 51 +++++ 8 files changed, 926 insertions(+) create mode 100644 t/account.t create mode 100644 t/basic.t create mode 100644 t/budget.t create mode 100644 t/category.t create mode 100644 t/lib/WWW/YNAB/MockUA.pm create mode 100644 t/month.t create mode 100644 t/payee.t create mode 100644 t/transaction.t diff --git a/t/account.t b/t/account.t new file mode 100644 index 0000000..61cf7c7 --- /dev/null +++ b/t/account.t @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use lib 't/lib'; + +use WWW::YNAB; +use WWW::YNAB::MockUA; + +my $ua = WWW::YNAB::MockUA->new; +my $ynab = WWW::YNAB->new( + access_token => 'abcdef', + ua => $ua, +); + +is(scalar $ua->test_requests, 0); + +my $budget = $ynab->budget('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); +is(scalar $ua->test_requests, 1); +isa_ok($budget, 'WWW::YNAB::Budget'); + +my $account = $budget->account('00000000-0000-0000-0000-222222222222'); +is(scalar $ua->test_requests, 2); +isa_ok($account, 'WWW::YNAB::Account'); +is($account->id, "00000000-0000-0000-0000-222222222222"); +is($account->name, "Credit Card"); +is($account->type, "creditCard"); +ok($account->on_budget); +ok(!$account->closed); +is($account->note, undef); +is($account->balance, -6543210); +is($account->cleared_balance, -5432100); +is($account->uncleared_balance, -1111110); +ok(!$account->deleted); + +done_testing; diff --git a/t/basic.t b/t/basic.t new file mode 100644 index 0000000..e01dadd --- /dev/null +++ b/t/basic.t @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use lib 't/lib'; + +use WWW::YNAB; +use WWW::YNAB::MockUA; + +my $ua = WWW::YNAB::MockUA->new; +my $ynab = WWW::YNAB->new( + access_token => 'abcdef', + ua => $ua, +); + +is(scalar $ua->test_requests, 0); +ok(!$ynab->knows_rate_limit); +ok(!$ynab->knows_total_rate_limit); + +my @budgets = $ynab->budgets; +is(scalar @budgets, 1); +isa_ok($budgets[0], 'WWW::YNAB::Budget'); +is($budgets[0]->id, 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); +is($budgets[0]->name, 'My Budget'); +is($budgets[0]->last_modified_on, "2018-06-23T17:04:12+00:00"); +is($budgets[0]->first_month, "2016-06-01"); +is($budgets[0]->last_month, "2018-07-01"); + +is(scalar $ua->test_requests, 1); +is(($ua->test_requests)[0][0], 'https://api.youneedabudget.com/v1/budgets'); +ok($ynab->knows_rate_limit); +ok($ynab->knows_total_rate_limit); +is($ynab->rate_limit, 1); +is($ynab->total_rate_limit, 200); + +my $user = $ynab->user; +isa_ok($user, 'WWW::YNAB::User'); +is($user->id, 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb'); + +is(scalar $ua->test_requests, 2); +is(($ua->test_requests)[1][0], 'https://api.youneedabudget.com/v1/user'); +ok($ynab->knows_rate_limit); +ok($ynab->knows_total_rate_limit); +is($ynab->rate_limit, 2); +is($ynab->total_rate_limit, 200); + +done_testing; diff --git a/t/budget.t b/t/budget.t new file mode 100644 index 0000000..f769cfc --- /dev/null +++ b/t/budget.t @@ -0,0 +1,115 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use lib 't/lib'; + +use WWW::YNAB; +use WWW::YNAB::MockUA; + +my $ua = WWW::YNAB::MockUA->new; +my $ynab = WWW::YNAB->new( + access_token => 'abcdef', + ua => $ua, +); + +is(scalar $ua->test_requests, 0); + +my $budget = $ynab->budget('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); +isa_ok($budget, 'WWW::YNAB::Budget'); +is($budget->id, 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); +is($budget->name, 'My Budget'); +is($budget->last_modified_on, "2018-06-23T17:04:12+00:00"); +is($budget->first_month, "2018-06-01"); +is($budget->last_month, "2018-07-01"); + +is(scalar $ua->test_requests, 1); +is(($ua->test_requests)[0][0], 'https://api.youneedabudget.com/v1/budgets/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); + +my @accounts = $budget->accounts; +is(scalar @accounts, 3); +isa_ok($accounts[0], 'WWW::YNAB::Account'); +is($accounts[0]->id, "00000000-0000-0000-0000-000000000000"); +is($accounts[0]->name, "Savings Account"); +is($accounts[0]->type, "savings"); +ok($accounts[0]->on_budget); +ok(!$accounts[0]->closed); +is($accounts[0]->note, undef); +is($accounts[0]->balance, 12345670); +is($accounts[0]->cleared_balance, 12345670); +is($accounts[0]->uncleared_balance, 0); +ok(!$accounts[0]->deleted); + +my @payees = $budget->payees; +is(scalar @payees, 3); +isa_ok($payees[0], 'WWW::YNAB::Payee'); +is($payees[0]->id, "11111111-1111-1111-1111-111111111111"); +is($payees[0]->name, "a restaurant"); +is($payees[0]->transfer_account_id, undef); +ok(!$payees[0]->deleted); + +my @category_groups = $budget->category_groups; +is(scalar @category_groups, 2); +isa_ok($category_groups[0], 'WWW::YNAB::CategoryGroup'); +is($category_groups[0]->id, "22222222-2222-2222-2222-222222222222"); +is($category_groups[0]->name, "Home"); +ok(!$category_groups[0]->hidden); +ok(!$category_groups[0]->deleted); + +my @home_categories = $category_groups[0]->categories; +is(scalar @home_categories, 1); +isa_ok($home_categories[0], 'WWW::YNAB::Category'); +is($home_categories[0]->id, "33333333-3333-3333-3333-444444444444"); +is($home_categories[0]->category_group_id, "22222222-2222-2222-2222-222222222222"); +is($home_categories[0]->name, "Utilities"); +ok(!$home_categories[0]->hidden); +is($home_categories[0]->note, undef); +is($home_categories[0]->budgeted, 123450); +is($home_categories[0]->activity, -123450); +is($home_categories[0]->balance, 0); +ok(!$home_categories[0]->deleted); + +is(scalar $category_groups[1]->categories, 2); + +my @months = $budget->months; +is(scalar @months, 3); +isa_ok($months[0], 'WWW::YNAB::Month'); +is($months[0]->month, "2018-08-01"); +is($months[0]->note, undef); +is($months[0]->to_be_budgeted, 0); +is($months[0]->age_of_money, 88); +is(scalar $months[0]->categories, 3); + +my @transactions = $budget->transactions; +is(scalar @transactions, 3); +isa_ok($transactions[0], 'WWW::YNAB::Transaction'); +is($transactions[0]->id, "44444444-4444-4444-4444-444444444444"); +is($transactions[0]->date, "2018-06-18"); +is($transactions[0]->amount, -98760); +is($transactions[0]->memo, undef); +is($transactions[0]->cleared, "cleared"); +ok($transactions[0]->approved); +is($transactions[0]->flag_color, undef); +is($transactions[0]->account_id, "00000000-0000-0000-0000-111111111111"); +is($transactions[0]->payee_id, "11111111-1111-1111-1111-222222222222"); +is($transactions[0]->category_id, "33333333-3333-3333-3333-444444444444"); +is($transactions[0]->transfer_account_id, undef); +is($transactions[0]->import_id, "YNAB:-98760:2018-06-18:1"); +ok(!$transactions[0]->deleted); +is(scalar $transactions[0]->subtransactions, 0); + +my @subtransactions = $transactions[2]->subtransactions; +is(scalar @subtransactions, 3); +is($subtransactions[0]->id, "55555555-5555-5555-5555-555555555555"); +is($subtransactions[0]->transaction_id, "44444444-4444-4444-4444-666666666666"); +is($subtransactions[0]->amount, -100000); +is($subtransactions[0]->memo, undef); +is($subtransactions[0]->payee_id, undef); +is($subtransactions[0]->category_id, "33333333-3333-3333-3333-444444444444"); +is($subtransactions[0]->transfer_account_id, undef); +ok(!$subtransactions[0]->deleted); + +is(scalar $ua->test_requests, 1); + +done_testing; diff --git a/t/category.t b/t/category.t new file mode 100644 index 0000000..4d38ecc --- /dev/null +++ b/t/category.t @@ -0,0 +1,36 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use lib 't/lib'; + +use WWW::YNAB; +use WWW::YNAB::MockUA; + +my $ua = WWW::YNAB::MockUA->new; +my $ynab = WWW::YNAB->new( + access_token => 'abcdef', + ua => $ua, +); + +is(scalar $ua->test_requests, 0); + +my $budget = $ynab->budget('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); +is(scalar $ua->test_requests, 1); +isa_ok($budget, 'WWW::YNAB::Budget'); + +my $category = $budget->category('33333333-3333-3333-3333-333333333333'); +is(scalar $ua->test_requests, 2); +isa_ok($category, 'WWW::YNAB::Category'); +is($category->id, "33333333-3333-3333-3333-333333333333"); +is($category->category_group_id, "22222222-2222-2222-2222-333333333333"); +is($category->name, "Restaurants"); +ok(!$category->hidden); +is($category->note, undef); +is($category->budgeted, 234560); +is($category->activity, -34560); +is($category->balance, 200000); +ok(!$category->deleted); + +done_testing; diff --git a/t/lib/WWW/YNAB/MockUA.pm b/t/lib/WWW/YNAB/MockUA.pm new file mode 100644 index 0000000..24519cd --- /dev/null +++ b/t/lib/WWW/YNAB/MockUA.pm @@ -0,0 +1,565 @@ +package WWW::YNAB::MockUA; +use Moose; + +extends 'HTTP::Tiny'; + +our $VERSION = 1; + +our %responses = ( + 'https://api.youneedabudget.com/v1/budgets' => <<'EOF', +{ + "data": { + "budgets": [ + { + "id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + "name": "My Budget", + "last_modified_on": "2018-06-23T17:04:12+00:00", + "date_format": { + "format": "YYYY-MM-DD" + }, + "currency_format": { + "iso_code": "USD", + "example_format": "123,456.78", + "decimal_digits": 2, + "decimal_separator": ".", + "symbol_first": true, + "group_separator": ",", + "currency_symbol": "$", + "display_symbol": true + }, + "first_month": "2016-06-01", + "last_month": "2018-07-01" + } + ] + } +} +EOF + 'https://api.youneedabudget.com/v1/user' => <<'EOF', +{ + "data": { + "user": { + "id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb" + } + } +} +EOF + 'https://api.youneedabudget.com/v1/budgets/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' => <<'EOF', +{ + "data": { + "budget": { + "id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + "name": "My Budget", + "last_modified_on": "2018-06-23T17:04:12+00:00", + "date_format": { + "format": "YYYY-MM-DD" + }, + "currency_format": { + "iso_code": "USD", + "example_format": "123,456.78", + "decimal_digits": 2, + "decimal_separator": ".", + "symbol_first": true, + "group_separator": ",", + "currency_symbol": "$", + "display_symbol": true + }, + "first_month": "2018-06-01", + "last_month": "2018-07-01", + "accounts": [ + { + "id": "00000000-0000-0000-0000-000000000000", + "name": "Savings Account", + "type": "savings", + "on_budget": true, + "closed": false, + "note": null, + "balance": 12345670, + "cleared_balance": 12345670, + "uncleared_balance": 0, + "deleted": false + }, + { + "id": "00000000-0000-0000-0000-111111111111", + "name": "Checking Account", + "type": "checking", + "on_budget": true, + "closed": false, + "note": null, + "balance": 2345670, + "cleared_balance": 2345670, + "uncleared_balance": 0, + "deleted": false + }, + { + "id": "00000000-0000-0000-0000-222222222222", + "name": "Credit Card", + "type": "creditCard", + "on_budget": true, + "closed": false, + "note": null, + "balance": -6543210, + "cleared_balance": -5432100, + "uncleared_balance": -1111110, + "deleted": false + } + ], + "payees": [ + { + "id": "11111111-1111-1111-1111-111111111111", + "name": "a restaurant", + "transfer_account_id": null, + "deleted": false + }, + { + "id": "11111111-1111-1111-1111-222222222222", + "name": "the power company", + "transfer_account_id": null, + "deleted": false + }, + { + "id": "11111111-1111-1111-1111-333333333333", + "name": "candy shop", + "transfer_account_id": null, + "deleted": false + } + ], + "payee_locations": [], + "category_groups": [ + { + "id": "22222222-2222-2222-2222-222222222222", + "name": "Home", + "hidden": false, + "deleted": false + }, + { + "id": "22222222-2222-2222-2222-333333333333", + "name": "Food", + "hidden": false, + "deleted": false + } + ], + "categories": [ + { + "id": "33333333-3333-3333-3333-333333333333", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Restaurants", + "hidden": false, + "note": null, + "budgeted": 234560, + "activity": -34560, + "balance": 200000, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-444444444444", + "category_group_id": "22222222-2222-2222-2222-222222222222", + "name": "Utilities", + "hidden": false, + "note": null, + "budgeted": 123450, + "activity": -123450, + "balance": 0, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-555555555555", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Groceries", + "hidden": false, + "note": null, + "budgeted": 345670, + "activity": -123450, + "balance": 222220, + "deleted": false + } + ], + "months": [ + { + "month": "2018-08-01", + "note": null, + "to_be_budgeted": 0, + "age_of_money": 88, + "categories": [ + { + "id": "33333333-3333-3333-3333-444444444444", + "category_group_id": "22222222-2222-2222-2222-222222222222", + "name": "Utilities", + "hidden": false, + "note": null, + "budgeted": 0, + "activity": 0, + "balance": 123450, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-555555555555", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Groceries", + "hidden": false, + "note": null, + "budgeted": 0, + "activity": 0, + "balance": 234560, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-333333333333", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Restaurants", + "hidden": false, + "note": null, + "budgeted": 0, + "activity": 0, + "balance": 567890, + "deleted": false + } + ] + }, + { + "month": "2018-07-01", + "note": null, + "to_be_budgeted": 0, + "age_of_money": 88, + "categories": [ + { + "id": "33333333-3333-3333-3333-444444444444", + "category_group_id": "22222222-2222-2222-2222-222222222222", + "name": "Utilities", + "hidden": false, + "note": null, + "budgeted": 132450, + "activity": 0, + "balance": 132450, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-555555555555", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Groceries", + "hidden": false, + "note": null, + "budgeted": 212130, + "activity": 0, + "balance": 345210, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-333333333333", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Restaurants", + "hidden": false, + "note": null, + "budgeted": 152430, + "activity": 0, + "balance": 215340, + "deleted": false + } + ] + }, + { + "month": "2018-06-01", + "note": null, + "to_be_budgeted": 19279540, + "age_of_money": 88, + "categories": [ + { + "id": "33333333-3333-3333-3333-444444444444", + "category_group_id": "22222222-2222-2222-2222-222222222222", + "name": "Utilities", + "hidden": false, + "note": null, + "budgeted": 98760, + "activity": -98760, + "balance": 0, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-555555555555", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Groceries", + "hidden": false, + "note": null, + "budgeted": 167890, + "activity": -134560, + "balance": 33330, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-333333333333", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Restaurants", + "hidden": false, + "note": null, + "budgeted": 456780, + "activity": -54320, + "balance": 402460, + "deleted": false + } + ] + } + ], + "transactions": [ + { + "id": "44444444-4444-4444-4444-444444444444", + "date": "2018-06-18", + "amount": -98760, + "memo": null, + "cleared": "cleared", + "approved": true, + "flag_color": null, + "account_id": "00000000-0000-0000-0000-111111111111", + "payee_id": "11111111-1111-1111-1111-222222222222", + "category_id": "33333333-3333-3333-3333-444444444444", + "transfer_account_id": null, + "import_id": "YNAB:-98760:2018-06-18:1", + "deleted": false + }, + { + "id": "44444444-4444-4444-4444-555555555555", + "date": "2018-06-17", + "amount": -5000, + "memo": null, + "cleared": "cleared", + "approved": true, + "flag_color": null, + "account_id": "00000000-0000-0000-0000-222222222222", + "payee_id": "11111111-1111-1111-1111-333333333333", + "category_id": "33333333-3333-3333-3333-555555555555", + "transfer_account_id": null, + "import_id": "YNAB:-5000:2018-06-18:1", + "deleted": false + }, + { + "id": "44444444-4444-4444-4444-666666666666", + "date": "2018-06-02", + "amount": -200000, + "memo": null, + "cleared": "cleared", + "approved": true, + "flag_color": null, + "account_id": "00000000-0000-0000-0000-222222222222", + "payee_id": "11111111-1111-1111-1111-111111111111", + "category_id": "33333333-3333-3333-3333-666666666666", + "transfer_account_id": null, + "import_id": "YNAB:-200000:2018-05-31:1", + "deleted": false + } + ], + "subtransactions": [ + { + "id": "55555555-5555-5555-5555-555555555555", + "transaction_id": "44444444-4444-4444-4444-666666666666", + "amount": -100000, + "memo": null, + "payee_id": null, + "category_id": "33333333-3333-3333-3333-444444444444", + "transfer_account_id": null, + "deleted": false + }, + { + "id": "55555555-5555-5555-5555-666666666666", + "transaction_id": "44444444-4444-4444-4444-666666666666", + "amount": -50000, + "memo": null, + "payee_id": null, + "category_id": "33333333-3333-3333-3333-555555555555", + "transfer_account_id": null, + "deleted": false + }, + { + "id": "55555555-5555-5555-5555-777777777777", + "transaction_id": "44444444-4444-4444-4444-666666666666", + "amount": -50000, + "memo": null, + "payee_id": null, + "category_id": "33333333-3333-3333-3333-333333333333", + "transfer_account_id": null, + "deleted": false + } + ], + "scheduled_transactions": [], + "scheduled_subtransactions": [] + }, + "server_knowledge": 1 + } +} +EOF + 'https://api.youneedabudget.com/v1/budgets/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/accounts/00000000-0000-0000-0000-222222222222' => <<'EOF', +{ + "data": { + "account": { + "id": "00000000-0000-0000-0000-222222222222", + "name": "Credit Card", + "type": "creditCard", + "on_budget": true, + "closed": false, + "note": null, + "balance": -6543210, + "cleared_balance": -5432100, + "uncleared_balance": -1111110, + "deleted": false + } + } +} +EOF + 'https://api.youneedabudget.com/v1/budgets/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/categories/33333333-3333-3333-3333-333333333333' => <<'EOF', +{ + "data": { + "category": { + "id": "33333333-3333-3333-3333-333333333333", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Restaurants", + "hidden": false, + "note": null, + "budgeted": 234560, + "activity": -34560, + "balance": 200000, + "deleted": false + } + } +} +EOF + 'https://api.youneedabudget.com/v1/budgets/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/payees/11111111-1111-1111-1111-333333333333' => <<'EOF', +{ + "data": { + "payee": { + "id": "11111111-1111-1111-1111-333333333333", + "name": "candy shop", + "transfer_account_id": null, + "deleted": false + } + } +} +EOF + 'https://api.youneedabudget.com/v1/budgets/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/months/2018-07-01' => <<'EOF', +{ + "data": { + "month": { + "month": "2018-07-01", + "note": null, + "to_be_budgeted": 0, + "age_of_money": 88, + "categories": [ + { + "id": "33333333-3333-3333-3333-555555555555", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Groceries", + "hidden": false, + "note": null, + "budgeted": 345670, + "activity": -123450, + "balance": 222220, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-333333333333", + "category_group_id": "22222222-2222-2222-2222-333333333333", + "name": "Restaurants", + "hidden": false, + "note": null, + "budgeted": 234560, + "activity": -34560, + "balance": 200000, + "deleted": false + }, + { + "id": "33333333-3333-3333-3333-444444444444", + "category_group_id": "22222222-2222-2222-2222-222222222222", + "name": "Utilities", + "hidden": false, + "note": null, + "budgeted": 123450, + "activity": -123450, + "balance": 0, + "deleted": false + } + ] + } + } +} +EOF + 'https://api.youneedabudget.com/v1/budgets/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/transactions/44444444-4444-4444-4444-666666666666' => <<'EOF', +{ + "data": { + "transaction": { + "id": "44444444-4444-4444-4444-666666666666", + "date": "2018-06-02", + "amount": -200000, + "memo": null, + "cleared": "cleared", + "approved": true, + "flag_color": null, + "account_id": "00000000-0000-0000-0000-222222222222", + "account_name": "Credit Card", + "payee_id": "11111111-1111-1111-1111-111111111111", + "payee_name": "a restaurant", + "category_id": "33333333-3333-3333-3333-666666666666", + "category_name": "Split (Multiple Categories)...", + "transfer_account_id": null, + "import_id": "YNAB:-200000:2018-05-31:1", + "deleted": false, + "subtransactions": [ + { + "id": "55555555-5555-5555-5555-555555555555", + "transaction_id": "44444444-4444-4444-4444-666666666666", + "amount": -100000, + "memo": null, + "payee_id": null, + "category_id": "33333333-3333-3333-3333-444444444444", + "transfer_account_id": null, + "deleted": false + }, + { + "id": "55555555-5555-5555-5555-666666666666", + "transaction_id": "44444444-4444-4444-4444-666666666666", + "amount": -50000, + "memo": null, + "payee_id": null, + "category_id": "33333333-3333-3333-3333-555555555555", + "transfer_account_id": null, + "deleted": false + }, + { + "id": "55555555-5555-5555-5555-777777777777", + "transaction_id": "44444444-4444-4444-4444-666666666666", + "amount": -50000, + "memo": null, + "payee_id": null, + "category_id": "33333333-3333-3333-3333-333333333333", + "transfer_account_id": null, + "deleted": false + } + ] + } + } +} +EOF +); + +sub get { + my $self = shift; + my ($uri, $params) = @_; + + $self->{__www_ynab_test_requests} ||= []; + push @{ $self->{__www_ynab_test_requests} }, [$uri, $params]; + + my $count = @{ $self->{__www_ynab_test_requests} }; + + return { + content => "$responses{$uri}", + headers => { + 'x-rate-limit' => "$count/200", + }, + success => 1, + } +} + +sub test_requests { + my $self = shift; + + @{ $self->{__www_ynab_test_requests} || [] } +} + +no Moose; + +1; diff --git a/t/month.t b/t/month.t new file mode 100644 index 0000000..06aaa72 --- /dev/null +++ b/t/month.t @@ -0,0 +1,43 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use lib 't/lib'; + +use WWW::YNAB; +use WWW::YNAB::MockUA; + +my $ua = WWW::YNAB::MockUA->new; +my $ynab = WWW::YNAB->new( + access_token => 'abcdef', + ua => $ua, +); + +is(scalar $ua->test_requests, 0); + +my $budget = $ynab->budget('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); +is(scalar $ua->test_requests, 1); +isa_ok($budget, 'WWW::YNAB::Budget'); + +my $month = $budget->month('2018-07-01'); +is(scalar $ua->test_requests, 2); +isa_ok($month, 'WWW::YNAB::Month'); +is($month->month, "2018-07-01"); +is($month->note, undef); +is($month->to_be_budgeted, 0); +is($month->age_of_money, 88); + +my @categories = $month->categories; +is(scalar @categories, 3); +is($categories[0]->id, "33333333-3333-3333-3333-555555555555"); +is($categories[0]->category_group_id, "22222222-2222-2222-2222-333333333333"); +is($categories[0]->name, "Groceries"); +ok(!$categories[0]->hidden); +is($categories[0]->note, undef); +is($categories[0]->budgeted, 345670); +is($categories[0]->activity, -123450); +is($categories[0]->balance, 222220); +ok(!$categories[0]->deleted); + +done_testing; diff --git a/t/payee.t b/t/payee.t new file mode 100644 index 0000000..4757534 --- /dev/null +++ b/t/payee.t @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use lib 't/lib'; + +use WWW::YNAB; +use WWW::YNAB::MockUA; + +my $ua = WWW::YNAB::MockUA->new; +my $ynab = WWW::YNAB->new( + access_token => 'abcdef', + ua => $ua, +); + +is(scalar $ua->test_requests, 0); + +my $budget = $ynab->budget('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); +is(scalar $ua->test_requests, 1); +isa_ok($budget, 'WWW::YNAB::Budget'); + +my $payee = $budget->payee('11111111-1111-1111-1111-333333333333'); +is(scalar $ua->test_requests, 2); +isa_ok($payee, 'WWW::YNAB::Payee'); +is($payee->id, "11111111-1111-1111-1111-333333333333"); +is($payee->name, "candy shop"); +is($payee->transfer_account_id, undef); +ok(!$payee->deleted); + +done_testing; diff --git a/t/transaction.t b/t/transaction.t new file mode 100644 index 0000000..3b900e3 --- /dev/null +++ b/t/transaction.t @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use lib 't/lib'; + +use WWW::YNAB; +use WWW::YNAB::MockUA; + +my $ua = WWW::YNAB::MockUA->new; +my $ynab = WWW::YNAB->new( + access_token => 'abcdef', + ua => $ua, +); + +is(scalar $ua->test_requests, 0); + +my $budget = $ynab->budget('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'); +is(scalar $ua->test_requests, 1); +isa_ok($budget, 'WWW::YNAB::Budget'); + +my $transaction = $budget->transaction('44444444-4444-4444-4444-666666666666'); +is(scalar $ua->test_requests, 2); +isa_ok($transaction, 'WWW::YNAB::Transaction'); +is($transaction->id, "44444444-4444-4444-4444-666666666666"); +is($transaction->date, "2018-06-02"); +is($transaction->amount, -200000); +is($transaction->memo, undef); +is($transaction->cleared, "cleared"); +ok($transaction->approved); +is($transaction->flag_color, undef); +is($transaction->account_id, "00000000-0000-0000-0000-222222222222"); +is($transaction->payee_id, "11111111-1111-1111-1111-111111111111"); +is($transaction->category_id, "33333333-3333-3333-3333-666666666666"); +is($transaction->transfer_account_id, undef); +is($transaction->import_id, "YNAB:-200000:2018-05-31:1"); +ok(!$transaction->deleted); + +my @subtransactions = $transaction->subtransactions; +is(scalar @subtransactions, 3); +is($subtransactions[0]->id, "55555555-5555-5555-5555-555555555555"); +is($subtransactions[0]->transaction_id, "44444444-4444-4444-4444-666666666666"); +is($subtransactions[0]->amount, -100000); +is($subtransactions[0]->memo, undef); +is($subtransactions[0]->payee_id, undef); +is($subtransactions[0]->category_id, "33333333-3333-3333-3333-444444444444"); +is($subtransactions[0]->transfer_account_id, undef); +ok(!$subtransactions[0]->deleted); + +done_testing; -- cgit v1.2.3