mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
WIP server side.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { create, expect } from '~/testInit';
|
||||
import Account from '@/models/Account';
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
import AccountType from '@/models/AccountType';
|
||||
|
||||
describe('Model: Account', () => {
|
||||
@@ -8,9 +7,32 @@ describe('Model: Account', () => {
|
||||
const accountType = await create('account_type');
|
||||
const account = await create('account', { account_type_id: accountType.id });
|
||||
|
||||
const accountModel = await Account.where('id', account.id).fetch();
|
||||
const accountTypeModel = await accountModel.type().fetch();
|
||||
const accountModel = await Account.query()
|
||||
.where('id', account.id)
|
||||
.withGraphFetched('type')
|
||||
.first();
|
||||
|
||||
expect(accountTypeModel.attributes.id).equals(account.id);
|
||||
expect(accountModel.type.id).equals(accountType.id);
|
||||
});
|
||||
|
||||
it('Should account model has one balance model that associated to the account model.', async () => {
|
||||
const accountBalance = await create('account_balance');
|
||||
|
||||
const accountModel = await Account.query()
|
||||
.where('id', accountBalance.accountId)
|
||||
.withGraphFetched('balance')
|
||||
.first();
|
||||
|
||||
expect(accountModel.balance.amount).equals(accountBalance.amount);
|
||||
});
|
||||
|
||||
it('Should account model has many transactions models that associated to the account model.', async () => {
|
||||
const account = await create('account');
|
||||
const accountTransaction = await create('account_transaction', { account_id: account.id });
|
||||
|
||||
const accountModel = await Account.query().where('id', account.id).first();
|
||||
const transactionsModels = await accountModel.$relatedQuery('transactions');
|
||||
|
||||
expect(transactionsModels.length).equals(1);
|
||||
});
|
||||
});
|
||||
|
||||
0
server/tests/models/AccountBalance.test.js
Normal file
0
server/tests/models/AccountBalance.test.js
Normal file
@@ -8,8 +8,8 @@ describe('Model: AccountType', () => {
|
||||
await create('account', { account_type_id: accountType.id });
|
||||
await create('account', { account_type_id: accountType.id });
|
||||
|
||||
const accountTypeModel = await AccountType.where('id', accountType.id).fetch();
|
||||
const typeAccounts = await accountTypeModel.accounts().fetch();
|
||||
const accountTypeModel = await AccountType.query().where('id', accountType.id).first();
|
||||
const typeAccounts = await accountTypeModel.$relatedQuery('accounts');
|
||||
|
||||
expect(typeAccounts.length).equals(2);
|
||||
});
|
||||
|
||||
34
server/tests/models/Expense.test.js
Normal file
34
server/tests/models/Expense.test.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import { create, expect } from '~/testInit';
|
||||
import Expense from '@/models/Expense';
|
||||
import factory from '../../src/database/factories';
|
||||
|
||||
describe('Model: Expense', () => {
|
||||
describe('relations', () => {
|
||||
it('Expense model may belongs to associated payment account.', async () => {
|
||||
const expense = await factory.create('expense');
|
||||
|
||||
const expenseModel = await Expense.query().findById(expense.id);
|
||||
const paymentAccountModel = await expenseModel.$relatedQuery('paymentAccount');
|
||||
|
||||
expect(paymentAccountModel.id).equals(expense.paymentAccountId);
|
||||
});
|
||||
|
||||
it('Expense model may belongs to associated expense account.', async () => {
|
||||
const expense = await factory.create('expense');
|
||||
|
||||
const expenseModel = await Expense.query().findById(expense.id);
|
||||
const expenseAccountModel = await expenseModel.$relatedQuery('expenseAccount');
|
||||
|
||||
expect(expenseAccountModel.id).equals(expense.expenseAccountId);
|
||||
});
|
||||
|
||||
it('Expense model may belongs to associated user model.', async () => {
|
||||
const expense = await factory.create('expense');
|
||||
|
||||
const expenseModel = await Expense.query().findById(expense.id);
|
||||
const expenseUserModel = await expenseModel.$relatedQuery('user');
|
||||
|
||||
expect(expenseUserModel.id).equals(expense.userId);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -9,8 +9,8 @@ describe('Model: Item', () => {
|
||||
const category = await create('item_category');
|
||||
const item = await create('item', { category_id: category.id });
|
||||
|
||||
const itemModel = await Item.where('id', item.id).fetch();
|
||||
const itemCategoryModel = await itemModel.category().fetch();
|
||||
const itemModel = await Item.query().where('id', item.id);
|
||||
const itemCategoryModel = await itemModel.$relatedQuery('category');
|
||||
|
||||
expect(itemCategoryModel.attributes.id).equals(category.id);
|
||||
});
|
||||
@@ -20,8 +20,8 @@ describe('Model: Item', () => {
|
||||
await create('item_metadata', { item_id: item.id });
|
||||
await create('item_metadata', { item_id: item.id });
|
||||
|
||||
const itemModel = await Item.where('id', item.id).fetch();
|
||||
const itemMetadataCollection = await itemModel.metadata().fetch();
|
||||
const itemModel = await Item.query().where('id', item.id);
|
||||
const itemMetadataCollection = await itemModel.$relatedQuery('metadata');
|
||||
|
||||
expect(itemMetadataCollection.length).equals(2);
|
||||
});
|
||||
|
||||
13
server/tests/models/Option.test.js
Normal file
13
server/tests/models/Option.test.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import { create, expect } from '~/testInit';
|
||||
import Option from '@/models/Option';
|
||||
import MetableCollection from '@/lib/Metable/MetableCollection';
|
||||
|
||||
describe('Model: Option', () => {
|
||||
it('Should result collection be instance of `MetableCollection` class.', async () => {
|
||||
await create('option');
|
||||
await create('option');
|
||||
const options = await Option.query();
|
||||
|
||||
expect(options).to.be.an.instanceof(MetableCollection);
|
||||
});
|
||||
});
|
||||
@@ -6,10 +6,10 @@ import '@/models/ResourceField';
|
||||
describe('Model: Resource', () => {
|
||||
it('Resource model may has many associated views.', async () => {
|
||||
const view = await create('view');
|
||||
await create('view', { resource_id: view.resource_id });
|
||||
await create('view', { resource_id: view.resourceId });
|
||||
|
||||
const resourceModel = await Resource.where('id', view.resource_id).fetch();
|
||||
const resourceViews = await resourceModel.views().fetch();
|
||||
const resourceModel = await Resource.query().findById(view.resourceId);
|
||||
const resourceViews = await resourceModel.$relatedQuery('views');
|
||||
|
||||
expect(resourceViews).to.have.lengthOf(2);
|
||||
});
|
||||
@@ -17,8 +17,8 @@ describe('Model: Resource', () => {
|
||||
it('Resource model may has many fields.', async () => {
|
||||
const resourceField = await create('resource_field');
|
||||
|
||||
const resourceModel = await Resource.where('id', resourceField.resource_id).fetch();
|
||||
const resourceFields = await resourceModel.fields().fetch();
|
||||
const resourceModel = await Resource.query().findById(resourceField.resourceId);
|
||||
const resourceFields = await resourceModel.$relatedQuery('fields');
|
||||
|
||||
expect(resourceFields).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
@@ -6,10 +6,10 @@ import '@/models/Resource';
|
||||
describe('Model: Role', () => {
|
||||
it('Role model may has many associated users', async () => {
|
||||
const userHasRole = await create('user_has_role');
|
||||
await create('user_has_role', { role_id: userHasRole.role_id });
|
||||
await create('user_has_role', { role_id: userHasRole.roleId });
|
||||
|
||||
const roleModel = await Role.where('id', userHasRole.role_id).fetch();
|
||||
const roleUsers = await roleModel.users().fetch();
|
||||
const roleModel = await Role.query().findById(userHasRole.roleId);
|
||||
const roleUsers = await roleModel.$relatedQuery('users');
|
||||
|
||||
expect(roleUsers).to.have.lengthOf(2);
|
||||
});
|
||||
@@ -17,8 +17,8 @@ describe('Model: Role', () => {
|
||||
it('Role model may has many associated permissions.', async () => {
|
||||
const roleHasPermissions = await create('role_has_permission');
|
||||
|
||||
const roleModel = await Role.where('id', roleHasPermissions.role_id).fetch();
|
||||
const rolePermissions = await roleModel.permissions().fetch();
|
||||
const roleModel = await Role.query().findById(roleHasPermissions.roleId);
|
||||
const rolePermissions = await roleModel.$relatedQuery('permissions');
|
||||
|
||||
expect(rolePermissions).to.have.lengthOf(1);
|
||||
});
|
||||
@@ -26,8 +26,8 @@ describe('Model: Role', () => {
|
||||
it('Role model may has many associated resources that has some or all permissions.', async () => {
|
||||
const roleHasPermissions = await create('role_has_permission');
|
||||
|
||||
const roleModel = await Role.where('id', roleHasPermissions.role_id).fetch();
|
||||
const roleResources = await roleModel.resources().fetch();
|
||||
const roleModel = await Role.query().findById(roleHasPermissions.roleId);
|
||||
const roleResources = await roleModel.$relatedQuery('resources');
|
||||
|
||||
expect(roleResources).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
@@ -8,10 +8,10 @@ describe('Model: User', () => {
|
||||
const userHasRole = await create('user_has_role');
|
||||
await create('user_has_role', { user_id: userHasRole.user_id });
|
||||
|
||||
const userModel = await User.where('id', userHasRole.user_id).fetch();
|
||||
const userRoles = await userModel.roles().fetch();
|
||||
const userModel = await User.query().where('id', userHasRole.userId).first();
|
||||
const userRoles = await userModel.$relatedQuery('roles');
|
||||
|
||||
expect(userRoles).to.have.lengthOf(2);
|
||||
expect(userRoles).to.have.lengthOf(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user