WIP pass the failed tests.

This commit is contained in:
Ahmed Bouhuolia
2020-05-17 07:08:12 +02:00
parent 00de156c9f
commit 10f636d2bc
77 changed files with 2164 additions and 1403 deletions

View File

@@ -1,13 +1,20 @@
import { create, expect } from '~/testInit';
import {
expect,
} from '~/testInit';
import Account from '@/models/Account';
import AccountType from '@/models/AccountType';
import {
tenantFactory,
tenantWebsite
} from '~/dbInit';
describe('Model: Account', () => {
it('Should account model belongs to the associated account type model.', async () => {
const accountType = await create('account_type');
const account = await create('account', { account_type_id: accountType.id });
const accountType = await tenantFactory.create('account_type');
const account = await tenantFactory.create('account', { account_type_id: accountType.id });
const accountModel = await Account.query()
const accountModel = await Account.tenant().query()
.where('id', account.id)
.withGraphFetched('type')
.first();
@@ -16,9 +23,9 @@ describe('Model: Account', () => {
});
it('Should account model has one balance model that associated to the account model.', async () => {
const accountBalance = await create('account_balance');
const accountBalance = await tenantFactory.create('account_balance');
const accountModel = await Account.query()
const accountModel = await Account.tenant().query()
.where('id', accountBalance.accountId)
.withGraphFetched('balance')
.first();
@@ -27,10 +34,10 @@ describe('Model: Account', () => {
});
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 account = await tenantFactory.create('account');
const accountTransaction = await tenantFactory.create('account_transaction', { account_id: account.id });
const accountModel = await Account.query().where('id', account.id).first();
const accountModel = await Account.tenant().query().where('id', account.id).first();
const transactionsModels = await accountModel.$relatedQuery('transactions');
expect(transactionsModels.length).equals(1);

View File

@@ -1,14 +1,20 @@
import { create, expect } from '~/testInit';
import '@/models/Account';
import AccountType from '@/models/AccountType';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('Model: AccountType', () => {
it('Shoud account type model has many associated accounts.', async () => {
const accountType = await create('account_type');
await create('account', { account_type_id: accountType.id });
await create('account', { account_type_id: accountType.id });
const accountType = await tenantFactory.create('account_type');
await tenantFactory.create('account', { account_type_id: accountType.id });
await tenantFactory.create('account', { account_type_id: accountType.id });
const accountTypeModel = await AccountType.query().where('id', accountType.id).first();
const accountTypeModel = await AccountType.tenant().query().where('id', accountType.id).first();
const typeAccounts = await accountTypeModel.$relatedQuery('accounts');
expect(typeAccounts.length).equals(2);

View File

@@ -1,34 +0,0 @@
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);
});
});
});

View File

@@ -2,27 +2,21 @@ import { create, expect } from '~/testInit';
import Item from '@/models/Item';
// eslint-disable-next-line no-unused-vars
import itemCategory from '@/models/ItemCategory';
import '@/models/ItemMetadata';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('Model: Item', () => {
it('Should item model belongs to the associated category model.', async () => {
const category = await create('item_category');
const item = await create('item', { category_id: category.id });
const category = await tenantFactory.create('item_category');
const item = await tenantFactory.create('item', { category_id: category.id });
const itemModel = await Item.query().where('id', item.id);
const itemModel = await Item.tenant().query().where('id', item.id).first();
const itemCategoryModel = await itemModel.$relatedQuery('category');
expect(itemCategoryModel.attributes.id).equals(category.id);
});
it('Should item model has many metadata that assciated to the item model.', async () => {
const item = await create('item');
await create('item_metadata', { item_id: item.id });
await create('item_metadata', { item_id: item.id });
const itemModel = await Item.query().where('id', item.id);
const itemMetadataCollection = await itemModel.$relatedQuery('metadata');
expect(itemMetadataCollection.length).equals(2);
expect(itemCategoryModel.id).equals(category.id);
});
});

View File

@@ -1,15 +1,23 @@
import { create, expect } from '~/testInit';
import '@/models/Item';
import ItemCategory from '@/models/ItemCategory';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('Model: ItemCategories', () => {
it('Shoud item category model has many associated items.', async () => {
const category = await create('item_category');
await create('item', { category_id: category.id });
await create('item', { category_id: category.id });
const category = await tenantFactory.create('item_category');
await tenantFactory.create('item', { category_id: category.id });
await tenantFactory.create('item', { category_id: category.id });
const categoryModel = await ItemCategory.where('id', category.id).fetch();
const categoryItems = await categoryModel.items().fetch();
const categoryModel = await ItemCategory.tenant().query()
.where('id', category.id).first();
const categoryItems = await categoryModel.$relatedQuery('items');
expect(categoryItems.length).equals(2);
});

View File

@@ -1,12 +1,17 @@
import { create, expect } from '~/testInit';
import Option from '@/models/Option';
import MetableCollection from '@/lib/Metable/MetableCollection';
import {
tenantFactory,
tenantWebsite,
} from '~/dbInit';
describe('Model: Option', () => {
it('Should result collection be instance of `MetableCollection` class.', async () => {
await create('option');
await create('option');
const options = await Option.query();
await tenantFactory.create('option');
await tenantFactory.create('option');
const options = await Option.tenant().query();
expect(options).to.be.an.instanceof(MetableCollection);
});

View File

@@ -1,21 +0,0 @@
import { create } from '~/testInit';
import Resource from '@/models/Resource';
import '@/models/Role';
describe('Model: Permission', () => {
it('Permission model may has associated role.', async () => {
const roleHasPermissions = await create('role_has_permission');
const resourceModel = await Resource.where('id', roleHasPermissions.resource_id).fetch();
const roleModel = await resourceModel.role().fetch();
console.log(roleModel);
});
it('Permission model may has associated resource.', async () => {
const roleHasPermissions = await create('role_has_permission');
const resourceModel = await Resource.where('id', roleHasPermissions.resource_id).fetch();
const permissionModel = await resourceModel.permission().fetch();
console.log(permissionModel);
});
});

View File

@@ -2,22 +2,28 @@ import { create, expect } from '~/testInit';
import Resource from '@/models/Resource';
import '@/models/View';
import '@/models/ResourceField';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('Model: Resource', () => {
it('Resource model may has many associated views.', async () => {
const view = await create('view');
await create('view', { resource_id: view.resourceId });
const view = await tenantFactory.create('view');
await tenantFactory.create('view', { resource_id: view.resourceId });
const resourceModel = await Resource.query().findById(view.resourceId);
const resourceModel = await Resource.tenant().query().findById(view.resourceId);
const resourceViews = await resourceModel.$relatedQuery('views');
expect(resourceViews).to.have.lengthOf(2);
});
it('Resource model may has many fields.', async () => {
const resourceField = await create('resource_field');
const resourceField = await tenantFactory.create('resource_field');
const resourceModel = await Resource.query().findById(resourceField.resourceId);
const resourceModel = await Resource.tenant().query().findById(resourceField.resourceId);
const resourceFields = await resourceModel.$relatedQuery('fields');
expect(resourceFields).to.have.lengthOf(1);

View File

@@ -1,18 +0,0 @@
import { create, expect } from '~/testInit';
import Resource from '@/models/Resource';
import ResourceField from '@/models/ResourceField';
import '@/models/View';
describe('Model: ResourceField', () => {
it('Resource field model may belongs to associated resource.', async () => {
const resourceField = await create('resource_field');
const resourceFieldModel = await ResourceField.where('id', resourceField.id).fetch();
const resourceModel = resourceFieldModel.resource().fetch();
const foundResource = await Resource.where('id', resourceField.resource_id).fetch();
expect(resourceModel.attributes.id).equals(foundResource.id);
expect(resourceModel.attributes.name).equals(foundResource.name);
});
});

View File

@@ -1,34 +0,0 @@
import { expect, create } from '~/testInit';
import Role from '@/models/Role';
import '@/models/Permission';
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.roleId });
const roleModel = await Role.query().findById(userHasRole.roleId);
const roleUsers = await roleModel.$relatedQuery('users');
expect(roleUsers).to.have.lengthOf(2);
});
it('Role model may has many associated permissions.', async () => {
const roleHasPermissions = await create('role_has_permission');
const roleModel = await Role.query().findById(roleHasPermissions.roleId);
const rolePermissions = await roleModel.$relatedQuery('permissions');
expect(rolePermissions).to.have.lengthOf(1);
});
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.query().findById(roleHasPermissions.roleId);
const roleResources = await roleModel.$relatedQuery('resources');
expect(roleResources).to.have.lengthOf(1);
});
});

View File

@@ -1,197 +0,0 @@
import sinon from 'sinon';
import { create, expect } from '~/testInit';
import Setting from '@/models/Setting';
import knex from '../../src/database/knex';
describe('Model: Setting', () => {
afterEach(() => {
Setting.purgeMetadata();
});
describe('Setting.AllMeta()', async () => {
it('Should fetch all metadata from storage in the first call.', async () => {
await create('setting');
const querySpy = sinon.spy(Setting, 'query');
const metadata = await Setting.allMeta();
expect(querySpy.calledOnce).equals(true);
expect(metadata).to.have.lengthOf(1);
querySpy.restore();
});
it('Should get all meta data from stored cache in the second call.', async () => {
await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.allMeta();
await Setting.allMeta();
expect(querySpy.calledOnce).equals(true);
expect(Setting.metadata).to.have.lengthOf(1);
querySpy.restore();
});
});
describe('Setting.getMeta()', () => {
it('Should fetch metadata of the given key from storage.', async () => {
const setting = await create('setting');
const metadata = await Setting.getMeta(setting.key);
expect(metadata).equals(setting.value);
});
it('Should retrieve the default value if the metadata key was not found.', async () => {
const metadata = await Setting.getMeta('setting', 'default');
expect(metadata).equals('default');
});
it('Should get the same metadata key from cache in the second call.', async () => {
const setting = await create('setting');
await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.getMeta(setting.key);
expect(querySpy.calledOnce).equals(true);
await Setting.getMeta(setting.key);
expect(querySpy.calledOnce).equals(true);
querySpy.restore();
});
it('Should get the different metadata key from storage.', async () => {
const setting = await create('setting');
const settingAnother = await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.getMeta(setting.key);
expect(querySpy.calledOnce).equals(true);
await Setting.getMeta(settingAnother.key);
expect(querySpy.calledOnce).equals(true);
querySpy.restore();
});
it('Should hard fetching the metadata from the storage when passing `force` parameter.', async () => {
const setting = await create('setting');
await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.allMeta();
expect(querySpy.calledOnce).equals(true);
expect(Setting.metadata).to.have.lengthOf(2);
await Setting.getMeta(setting.key, null, true);
expect(querySpy.calledTwice).equals(true);
expect(Setting.metadata).to.have.lengthOf(2);
querySpy.restore();
});
});
describe('Setting.setMeta()', () => {
it('Should mark the given metadata as updated in the stack.', async () => {
const setting = await create('setting');
await Setting.setMeta(setting.key, 'Ahmed');
const foundMeta = Setting.metadata.find((metadata) => (
metadata.key === setting.key && metadata.markAsUpdated === true
&& metadata.value === 'Ahmed'
));
expect(!!foundMeta).equals(true);
});
it('Should mark the set metadata as inserted metadata in the stack.', async () => {
await create('setting');
await Setting.setMeta('key', 'value');
const foundMeta = Setting.metadata.find((metadata) => (
metadata.key === 'key' && metadata.markAsInserted === true
&& metadata.value === 'value'
));
expect(!!foundMeta).equals(true);
});
it('Should fetch the metadata from the storage in case the metadata was exist.', async () => {
const setting = await create('setting');
const querySpy = sinon.spy(Setting, 'query');
await Setting.setMeta(setting.key, 'value');
expect(querySpy.calledOnce).equals(true);
await Setting.setMeta(setting.key, 'updated-value');
expect(querySpy.calledOnce).equals(true);
});
it('Should mark the updated bluk metadata as updated in the stock.', async () => {
});
it('Should mark the inserted bluk metadata as inserted in the stock.', async () => {
});
});
describe('Setting.removeMeta()', () => {
it('Should mark the given metadata as deleted', async () => {
const setting = await create('setting');
await Setting.removeMeta(setting.key);
const foundMeta = Setting.metadata.find((metadata) => (
metadata.key === setting.key && metadata.markAsDeleted === true
));
expect(!!foundMeta).equals(true);
});
it('Should not query the storage when found cached the metadata.', async () => {
const setting = await create('setting');
await Setting.allMeta();
const querySpy = sinon.spy(Setting, 'query');
await Setting.removeMeta(setting.key);
expect(querySpy.calledOnce).equals(false);
querySpy.restore();
});
});
describe('Setting.saveMeta()', () => {
it('Should insert the metadata that set to the stock.', async () => {
await Setting.setMeta('key', 'value');
await Setting.saveMeta();
const storedMetadata = await knex('settings');
expect(storedMetadata).to.have.lengthOf(1);
});
it('Should update the metadata that updated in the stock.', async () => {
const setting = await create('setting');
await Setting.setMeta(setting.key, 'value');
await Setting.saveMeta();
const storedMetadata = await knex('settings');
expect(storedMetadata).to.have.lengthOf(1);
expect(storedMetadata[0].value).equals('value');
});
it('Should delete the metadata that removed from the stock.', async () => {
const setting = await create('setting');
await Setting.removeMeta(setting.key);
await Setting.saveMeta();
const storedMetadata = await knex('settings');
expect(storedMetadata).to.have.lengthOf(0);
});
});
});

View File

@@ -1,46 +1,23 @@
import { create, expect } from '~/testInit';
import User from '@/models/User';
import User from '@/models/TenantUser';
import '@/models/Role';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('Model: User', () => {
describe('relations', () => {
it('User model may has many associated roles.', async () => {
const userHasRole = await create('user_has_role');
await create('user_has_role', { user_id: userHasRole.user_id });
const userHasRole = await tenantFactory.create('user_has_role');
await tenantFactory.create('user_has_role', { user_id: userHasRole.user_id });
const userModel = await User.query().where('id', userHasRole.userId).first();
const userModel = await User.tenant().query().where('id', userHasRole.userId).first();
const userRoles = await userModel.$relatedQuery('roles');
expect(userRoles).to.have.lengthOf(1);
});
});
describe('hasPermissions', () => {
it('Should return true in case user has the given permissions.', async () => {
const resource = await create('resource');
const permission = await create('permission');
const roleHasPerms = await create('role_has_permission', {
resource_id: resource.id,
permission_id: permission.id,
});
const userHasRole = await create('user_has_role', { role_id: roleHasPerms.role_id });
await create('user_has_role', { user_id: userHasRole.user_id });
const userModel = await User.where('id', userHasRole.user_id).fetch();
const hasPermission = await userModel.hasPermissions(resource.name, [permission.name]);
expect(hasPermission).to.equals(true);
});
it('Should return false in case user has no the given permissions.', async () => {
const roleHasPerms = await create('role_has_permission');
const userHasRole = await create('user_has_role', { role_id: roleHasPerms.role_id });
await create('user_has_role', { user_id: userHasRole.user_id });
const userModel = await User.where('id', userHasRole.user_id).fetch();
const hasPermission = await userModel.hasPermissions('resource', ['permission']);
expect(hasPermission).to.equals(false);
});
});
});

View File

@@ -3,37 +3,43 @@ import View from '@/models/View';
import Resource from '@/models/Resource';
import ResourceField from '@/models/ResourceField';
import ViewRole from '@/models/ViewRole';
import {
tenantWebsite,
tenantFactory,
loginRes
} from '~/dbInit';
describe('Model: View', () => {
it('View model may has many associated resource.', async () => {
const view = await create('view');
const view = await tenantFactory.create('view');
const viewModel = await View.query().findById(view.id);
const viewModel = await View.tenant().query().findById(view.id);
const viewResource = await viewModel.$relatedQuery('resource');
const foundResource = await Resource.query().findById(view.resourceId);
const foundResource = await Resource.tenant().query().findById(view.resourceId);
expect(viewResource.id).equals(foundResource.id);
expect(viewResource.name).equals(foundResource.name);
});
it('View model may has many associated view roles.', async () => {
const view = await create('view');
await create('view_role', { view_id: view.id });
await create('view_role', { view_id: view.id });
const view = await tenantFactory.create('view');
await tenantFactory.create('view_role', { view_id: view.id });
await tenantFactory.create('view_role', { view_id: view.id });
const viewModel = await View.query().findById(view.id);
const viewRoles = await viewModel.$relatedQuery('viewRoles');
const viewModel = await View.tenant().query().findById(view.id);
const viewRoles = await viewModel.$relatedQuery('roles');
expect(viewRoles).to.have.lengthOf(2);
});
it('View model may has many associated view columns', async () => {
const view = await create('view');
await create('view_column', { view_id: view.id });
await create('view_column', { view_id: view.id });
const view = await tenantFactory.create('view');
await tenantFactory.create('view_column', { view_id: view.id });
await tenantFactory.create('view_column', { view_id: view.id });
const viewModel = await View.query().findById(view.id);
const viewModel = await View.tenant().query().findById(view.id);
const viewColumns = await viewModel.$relatedQuery('columns');
expect(viewColumns).to.have.lengthOf(2);