@ts-awesome/orm 1.7.1 → 2.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,164 +0,0 @@
1
- import { reader, IQueryData, dbTable, IDbField, dbField, DbValueType } from '../dist';
2
- import { Person } from './models';
3
-
4
- const DB_JSON: IDbField = {
5
- reader(raw: DbValueType): any {
6
- return typeof raw === 'string' ? JSON.parse(raw) : raw;
7
- },
8
- writer(value: any): DbValueType {
9
- return JSON.stringify(value);
10
- }
11
- };
12
-
13
- function generatePersons(quantity: number): IQueryData[] {
14
- const res = [];
15
- for (let i = 1; i <= quantity; i++) {
16
- const person = new Person();
17
- person['id'] = i;
18
- person['city'] = `TestCity${i}`;
19
- person['age'] = 18;
20
- person['name'] = `TestName${i}`;
21
- person['uid'] = `uid${i}`;
22
- // person['profiles'] = ['profile-a'];
23
- res.push(person);
24
- }
25
- return res;
26
- }
27
-
28
- describe('DbReader', () => {
29
-
30
- const persons = generatePersons(5);
31
-
32
- it('read raw', () => {
33
- const result = reader(persons);
34
- expect(result).toStrictEqual(persons);
35
- result.map(person => {
36
- expect(person).toBeInstanceOf(Person);
37
- expect(person).toStrictEqual(persons.find(p => p.id === person.id))
38
- });
39
- });
40
-
41
- it('read Model', () => {
42
- @dbTable
43
- class Model {
44
- @dbField public readonly id!: number;
45
- @dbField('raw') public readonly value!: string;
46
- @dbField({sensitive: true, nullable: true}) public readonly password?: string;
47
- @dbField({kind: DB_JSON, model: [Person]}) public readonly personal!: Person[];
48
-
49
- constructor(id, raw, personal) {
50
- this.id = id;
51
- this.value = raw;
52
- this.personal = personal
53
- }
54
- }
55
-
56
- const model = {
57
- id: "5",
58
- raw: false,
59
- personal: JSON.stringify(persons),
60
- password: 'test'
61
- }
62
- const expected = new Model(5, 'false', persons);
63
-
64
- const results = reader([model], Model);
65
- expect(results.length).toBe(1);
66
- const [result] = results;
67
- expect(result).toBeInstanceOf(Model);
68
- expect(result).toEqual(expected);
69
- });
70
-
71
- it('read Model incl sensitive', () => {
72
- @dbTable
73
- class Model {
74
- @dbField public readonly id!: number;
75
- @dbField('raw') public readonly value!: string;
76
- @dbField({sensitive: true, nullable: true}) public readonly password?: string;
77
- @dbField({kind: DB_JSON, model: [Person]}) public readonly personal!: Person[];
78
-
79
- constructor(id, raw, personal, password) {
80
- this.id = id;
81
- this.value = raw;
82
- this.personal = personal
83
- this.password = password
84
- }
85
- }
86
-
87
- const model = {
88
- id: "5",
89
- raw: false,
90
- personal: JSON.stringify(persons),
91
- password: 'test'
92
- }
93
- const expected = new Model(5, 'false', persons, 'test');
94
-
95
- const results = reader([model], Model, true);
96
- expect(results.length).toBe(1);
97
- const [result] = results;
98
- expect(result).toBeInstanceOf(Model);
99
- expect(result).toEqual(expected);
100
- });
101
-
102
- it('read Model from no data', () => {
103
- @dbTable
104
- class Model {
105
- @dbField public readonly id!: number;
106
- @dbField('raw') public readonly value!: string;
107
- @dbField({kind: DB_JSON, model: [Person]}) public readonly personal!: Person[];
108
-
109
- constructor(id, raw, personal) {
110
- this.id = id;
111
- this.value = raw;
112
- this.personal = personal
113
- }
114
- }
115
-
116
- const results = reader([], Model);
117
- expect(results.length).toBe(0);
118
- });
119
-
120
- it('read Object from no json', () => {
121
- @dbTable
122
- class Model {
123
- @dbField public readonly id!: number;
124
- @dbField('raw') public readonly value!: string;
125
- @dbField({kind: DB_JSON, model: Object, nullable: true}) public readonly some_other!: object | null;
126
-
127
- constructor(id, raw, some_other) {
128
- this.id = id;
129
- this.value = raw;
130
- this.some_other = some_other
131
- }
132
- }
133
-
134
- const results = reader([{id: "5", value: "test", some_other: JSON.stringify({hello: 2})}], Model);
135
- expect(results.length).toBe(1);
136
- expect(results[0]).toEqual({
137
- id: 5,
138
- value: "test",
139
- some_other: {hello: 2},
140
- })
141
- });
142
-
143
- it('read scalar', () => {
144
- const count = 10;
145
- const correctResult = reader([{field: count}], true);
146
- expect(correctResult).toBe(count);
147
- });
148
-
149
- it('read scalar from no data', () => {
150
- const fieldValue = 'string';
151
- const emptyData = reader([], true);
152
- expect(emptyData).toBe(0);
153
- expect(() => {
154
- reader([{field: fieldValue as any}], true)
155
- }).toThrowError(`Invalid scalar "${fieldValue}", number expected.`);
156
- });
157
-
158
- it('read scalar from invalid data', () => {
159
- const fieldValue = 'string';
160
- expect(() => {
161
- reader([{field: fieldValue as any}], true)
162
- }).toThrowError(`Invalid scalar "${fieldValue}", number expected.`);
163
- });
164
- });