-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.spec.ts
127 lines (111 loc) · 3.86 KB
/
db.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import 'reflect-metadata';
import db from './fixtures/db';
import {CookbookModel} from "./fixtures/models";
import {Entity, Model} from '../index'
describe('app', () => {
describe('getters', () => {
it('should get tables', () =>{
const tables = db.getTables();
expect(tables).toBeInstanceOf(Array);
expect(tables).toHaveLength(3);
})
it('should get table by name', () => {
const table = db.getTable('CookbookTable');
expect(table).toHaveProperty('name', 'CookbookTable')
})
it('should get models', () => {
const models = db.getModels();
expect(models).toBeInstanceOf(Array);
expect(models).toHaveLength(2);
})
it('should get model by name', () => {
const model = db.getModel('CookbookModel');
expect(model).toHaveProperty('name', 'CookbookModel')
})
})
describe('factories', () => {
it('should make model instance', () => {
const model = db.model('CookbookModel');
expect(model).toBeInstanceOf(CookbookModel)
})
it('should make entity into model instance', () => {
const model = db.model('CookbookEntity');
const attributes = model.getAttributes();
const expectedAttributes = {
title: { type: 'S', required: false, value: 'Southern Cornbread' },
summary: { type: 'S', required: false, value: undefined },
description: { type: 'S', required: false, value: undefined },
author: { type: 'S', required: false, value: undefined },
image: { type: 'SS', required: true, value: undefined },
reviews: { type: 'N', required: false, value: undefined }
}
expect(model.constructor.name).toBe('DynamicModel')
expect(attributes).toMatchObject(expectedAttributes);
expect(model).toBeInstanceOf(Model)
})
it('should get entities from model', () => {
const model = db.model('CookbookModel');
const entity = model.getEntity(true);
expect(entity).toBeInstanceOf(Entity)
})
it('should get entities from model made from entity', () => {
const model = db.model('CookbookEntity');
const entity = model.getEntity(true);
expect(entity).toBeInstanceOf(Entity)
})
})
describe('model attributes', () => {
let model:Model;
beforeEach(() => {
model = db.model('CookbookModel');
})
it('should throw error if attribute is not defined', () => {
const expectedError = () => model.get('noAttribute');
expect(expectedError).toThrow();
})
it('should get an attribute default value', () => {
const title = model.get('title');
expect(title).toEqual('Southern Cornbread');
})
it('should set an attribute', () => {
model.set('title', 'Down South Cooking');
const title = model.get('title');
expect(title).toEqual('Down South Cooking')
})
it('should fill attributes', () => {
const expectedAttributes = {
title: 'Good Cookin',
summary: 'A collection of good recipes'
}
model.fill(expectedAttributes)
const attributes = model.getAttributeValues();
expect(attributes).toMatchObject(expectedAttributes);
})
})
describe('save item', () => {
it('should save item', () => {
const model = db.model('AuthorEntity');
model.fill({
firstName: 'Jack',
lastName: 'Black',
email: '[email protected]'
})
model.save();
})
})
describe('query', () => {
it('should query table', async () => {
const model = await db.query('Cookbooks')
.where('title', '=', 'Southern Sweets')
.first();
const attributes = model.getAttributeValues();
const expectedAttributes = {
title: 'Southern Sweets',
author: '[email protected]',
image: [ 'logo.png' ]
}
expect(model).toBeInstanceOf(Model);
expect(attributes).toMatchObject(expectedAttributes)
})
})
})