Skip to content

Commit 7063fb4

Browse files
committed
test: Update test
1 parent 4c5284f commit 7063fb4

File tree

13 files changed

+227
-25
lines changed

13 files changed

+227
-25
lines changed

apps/json-api-server-e2e/src/json-api/json-api-sdk/atomic-sdk.spec.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,25 @@ describe('Atomic method:', () => {
8686
);
8787
}
8888

89-
await Promise.all(
90-
[...usersArray, ...addressArray, ...commentsArray, ...rolesArray].map(
91-
(i) => jsonSdk.jonApiSdkService.deleteOne(i)
92-
)
93-
);
89+
for (const item of [
90+
...usersArray,
91+
...addressArray,
92+
...commentsArray,
93+
...rolesArray,
94+
]) {
95+
await jsonSdk.jonApiSdkService.deleteOne(item);
96+
}
97+
});
98+
99+
it('Try check intreceptor', async () => {
100+
const newUser = getUser();
101+
newUser.addresses = addressArray[0];
102+
try {
103+
const result = await jsonSdk.atomicFactory().postOne(newUser).run();
104+
usersId.push(result[0].id);
105+
} catch (e) {
106+
console.log(e);
107+
}
94108
});
95109

96110
it('Should be correct work', async () => {

libs/json-api/json-api-nestjs/src/lib/helper/bind-controller.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('bindController', () => {
3535
requiredSelectField: false,
3636
pipeForId: ParseIntPipe,
3737
debug: false,
38+
useSoftDelete: false,
3839
};
3940
bindController(Controller, Users, DEFAULT_CONNECTION_NAME, config);
4041

@@ -99,6 +100,7 @@ describe('bindController', () => {
99100
requiredSelectField: false,
100101
pipeForId: ParseIntPipe,
101102
debug: false,
103+
useSoftDelete: false,
102104
};
103105
bindController(Controller, Users, DEFAULT_CONNECTION_NAME, config);
104106
expect(Object.getOwnPropertyNames(Controller.prototype)).toEqual([
@@ -131,6 +133,7 @@ describe('bindController', () => {
131133
requiredSelectField: false,
132134
pipeForId: SomePipes,
133135
debug: false,
136+
useSoftDelete: false,
134137
};
135138
bindController(Controller, Users, DEFAULT_CONNECTION_NAME, config);
136139

libs/json-api/json-api-nestjs/src/lib/helper/orm/methods/patch-one/patch-one.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('patchOne', () => {
155155
await typeormService.postOne(inputData);
156156
backaUp = db.backup();
157157
const changeUser = await userRepository.findOneBy({
158-
login: inputData.attributes.login,
158+
login: inputData.attributes.login as string,
159159
});
160160
if (!changeUser) {
161161
throw new Error('not found mock data');

libs/json-api/json-api-nestjs/src/lib/helper/orm/methods/post-one/post-one.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getRepository,
1111
mockDBTestModule,
1212
Notes,
13+
Pods,
1314
providerEntities,
1415
pullAllData,
1516
Roles,
@@ -40,6 +41,10 @@ describe('postOne', () => {
4041
let backaUp: IBackup;
4142
let typeormService: TypeormService<Users>;
4243
let transformDataService: TransformDataService<Users>;
44+
let podsRepository: Repository<Pods>;
45+
46+
let typeormServicePods: TypeormService<Pods>;
47+
let transformDataServicePods: TransformDataService<Pods>;
4348

4449
let userRepository: Repository<Users>;
4550
let addressesRepository: Repository<Addresses>;
@@ -82,13 +87,34 @@ describe('postOne', () => {
8287
],
8388
}).compile();
8489

90+
const modulePods: TestingModule = await Test.createTestingModule({
91+
imports: [mockDBTestModule(db)],
92+
providers: [
93+
...providerEntities(getDataSourceToken()),
94+
CurrentDataSourceProvider(DEFAULT_CONNECTION_NAME),
95+
{
96+
provide: CONTROL_OPTIONS_TOKEN,
97+
useValue: {
98+
requiredSelectField: false,
99+
debug: false,
100+
},
101+
},
102+
EntityRepositoryFactory(Pods),
103+
TypeormUtilsService,
104+
TransformDataService,
105+
TypeormServiceFactory(Pods),
106+
EntityPropsMapService,
107+
],
108+
}).compile();
109+
85110
({
86111
userRepository,
87112
addressesRepository,
88113
notesRepository,
89114
commentsRepository,
90115
rolesRepository,
91116
userGroupRepository,
117+
podsRepository,
92118
} = getRepository(module));
93119
await pullAllData(
94120
userRepository,
@@ -103,6 +129,10 @@ describe('postOne', () => {
103129
transformDataService =
104130
module.get<TransformDataService<Users>>(TransformDataService);
105131

132+
typeormServicePods = modulePods.get<TypeormService<Pods>>(TYPEORM_SERVICE);
133+
transformDataServicePods =
134+
modulePods.get<TransformDataService<Pods>>(TransformDataService);
135+
106136
notes = await notesRepository.find();
107137
users = await userRepository.find();
108138
roles = await rolesRepository.find();
@@ -147,6 +177,32 @@ describe('postOne', () => {
147177
backaUp.restore();
148178
});
149179

180+
it('should be ok without relation and with id', async () => {
181+
const spyOnTransformData = jest
182+
.spyOn(transformDataServicePods, 'transformData')
183+
.mockImplementationOnce(() => ({
184+
data: {} as any,
185+
}));
186+
const { relationships, ...other } = inputData;
187+
const id = '5';
188+
const returnData = await typeormServicePods.postOne({
189+
id,
190+
type: 'pods',
191+
attributes: {
192+
name: 'test',
193+
},
194+
});
195+
const result = await podsRepository.findOneBy({
196+
id,
197+
});
198+
199+
expect(spyOnTransformData).toBeCalledWith({
200+
...result,
201+
id,
202+
});
203+
expect(returnData).not.toHaveProperty('included');
204+
});
205+
150206
it('should be ok without relation', async () => {
151207
const spyOnTransformData = jest
152208
.spyOn(transformDataService, 'transformData')

libs/json-api/json-api-nestjs/src/lib/helper/orm/orm-helper.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
getPrimaryColumnsForRelation,
3636
getIsArrayRelation,
3737
getTypeForAllProps,
38+
getPropsFromDb,
3839
} from './orm-helper';
3940
import { ObjectTyped } from '../utils';
4041

@@ -76,7 +77,6 @@ describe('zod-helper', () => {
7677

7778
it('getField', async () => {
7879
const { relations, field } = getField(userRepository);
79-
8080
const userFieldProps = Object.getOwnPropertyNames(
8181
user
8282
) as EntityProps<Users>[];
@@ -171,8 +171,14 @@ describe('zod-helper', () => {
171171
it('getArrayPropsForEntity', () => {
172172
const result = getArrayPropsForEntity(userRepository);
173173
const check: ArrayPropsForEntity<Users> = {
174-
target: {},
175-
manager: {},
174+
target: {
175+
testReal: true,
176+
testArrayNull: true,
177+
},
178+
manager: {
179+
testReal: true,
180+
testArrayNull: true,
181+
},
176182
comments: {},
177183
notes: {},
178184
userGroup: {},
@@ -255,4 +261,13 @@ describe('zod-helper', () => {
255261
expect(result.comments.id).toBe(TypeField.number);
256262
expect(result.notes.id).toBe(TypeField.string);
257263
});
264+
265+
it('getPropsFromDb', () => {
266+
const result = getPropsFromDb(userRepository);
267+
expect(result['testReal']).toEqual({
268+
type: 'real',
269+
isArray: true,
270+
isNullable: false,
271+
});
272+
});
258273
});

libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-helper.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,14 @@ describe('zod-helper', () => {
432432
});
433433
describe('test zodInputPostSchema', () => {
434434
it('should be ok', () => {
435+
const real = 123.123;
435436
const date = new Date();
436437
const attributes = {
437438
lastName: 'sdfsdf',
438439
isActive: true,
439440
testDate: date.toISOString(),
441+
testReal: [`${real}`],
442+
testArrayNull: null,
440443
};
441444
const relationships = {
442445
notes: [
@@ -459,13 +462,21 @@ describe('zod-helper', () => {
459462
attributes,
460463
},
461464
};
465+
const check3 = {
466+
data: {
467+
id: '1',
468+
type: 'users',
469+
attributes,
470+
},
471+
};
462472

463473
const checkResult = {
464474
data: {
465475
type: 'users',
466476
attributes: {
467477
...attributes,
468478
['testDate']: date,
479+
testReal: [real],
469480
},
470481
relationships,
471482
},
@@ -476,12 +487,25 @@ describe('zod-helper', () => {
476487
attributes: {
477488
...attributes,
478489
['testDate']: date,
490+
testReal: [real],
491+
},
492+
},
493+
};
494+
const checkResult3 = {
495+
data: {
496+
id: '1',
497+
type: 'users',
498+
attributes: {
499+
...attributes,
500+
['testDate']: date,
501+
testReal: [real],
479502
},
480503
},
481504
};
482505

483506
expect(zodInputPostSchemaTest.parse(check)).toEqual(checkResult);
484507
expect(zodInputPostSchemaTest.parse(check2)).toEqual(checkResult2);
508+
expect(zodInputPostSchemaTest.parse(check3)).toEqual(checkResult3);
485509
});
486510

487511
it('should be not ok', () => {

libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-input-post-schema/attributes.spec.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { z, ZodError } from 'zod';
22
import { zodAttributesSchema, ZodAttributesSchema } from './attributes';
33
import { Addresses, Users } from '../../../mock-utils';
4-
import { FieldWithType, TypeField } from '../../orm';
4+
import { FieldWithType, PropsForField, TypeField } from '../../orm';
55

66
describe('attributes', () => {
77
let schemaUsers: ZodAttributesSchema<Users>;
@@ -18,6 +18,20 @@ describe('attributes', () => {
1818
login: TypeField.string,
1919
testDate: TypeField.date,
2020
updatedAt: TypeField.date,
21+
testReal: TypeField.array,
22+
testArrayNull: TypeField.array,
23+
};
24+
const propsDb: PropsForField<Users> = {
25+
id: { type: Number, isArray: false, isNullable: false },
26+
login: { type: 'varchar', isArray: false, isNullable: false },
27+
firstName: { type: 'varchar', isArray: false, isNullable: true },
28+
testReal: { type: 'real', isArray: true, isNullable: false },
29+
testArrayNull: { type: 'real', isArray: true, isNullable: true },
30+
lastName: { type: 'varchar', isArray: false, isNullable: true },
31+
isActive: { type: 'boolean', isArray: false, isNullable: true },
32+
createdAt: { type: 'timestamp', isArray: false, isNullable: true },
33+
testDate: { type: 'timestamp', isArray: false, isNullable: true },
34+
updatedAt: { type: 'timestamp', isArray: false, isNullable: true },
2135
};
2236
const fieldTypeAddresses: FieldWithType<Addresses> = {
2337
id: TypeField.number,
@@ -28,14 +42,18 @@ describe('attributes', () => {
2842
updatedAt: TypeField.date,
2943
country: TypeField.string,
3044
};
31-
schemaUsers = zodAttributesSchema<Users>(fieldTypeUsers);
32-
schemaAddresses = zodAttributesSchema<Addresses>(fieldTypeAddresses);
45+
schemaUsers = zodAttributesSchema<Users>(fieldTypeUsers, propsDb);
46+
schemaAddresses = zodAttributesSchema<Addresses>(
47+
fieldTypeAddresses,
48+
{} as PropsForField<Addresses>
49+
);
3350
});
3451

3552
it('should be ok', () => {
3653
const check: SchemaTypeUsers = {
3754
isActive: true,
3855
lastName: 'sdsdf',
56+
testReal: [123.123, 123.123],
3957
};
4058
const check2: SchemaTypeAddresses = {
4159
arrayField: ['test', 'test'],

libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-query-schema/filter.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe('Check "filter" zod schema', () => {
3232
'testDate',
3333
'isActive',
3434
'lastName',
35+
'testArrayNull',
36+
'testReal',
3537
'firstName',
3638
'login',
3739
'updatedAt',
@@ -47,14 +49,19 @@ describe('Check "filter" zod schema', () => {
4749
'testDate',
4850
'isActive',
4951
'lastName',
52+
'testArrayNull',
53+
'testReal',
5054
'firstName',
5155
'login',
5256
'updatedAt',
5357
'createdAt',
5458
'id',
5559
];
5660

57-
const usersPropsArray: PropsArray<Users> = {};
61+
const usersPropsArray: PropsArray<Users> = {
62+
testArrayNull: true,
63+
testReal: true,
64+
};
5865
const rolesPropsArray: PropsArray<Roles> = {};
5966
const userGroupPropsArray: PropsArray<UserGroups> = {};
6067
const commentsPropsArray: PropsArray<Comments> = {};
@@ -70,6 +77,8 @@ describe('Check "filter" zod schema', () => {
7077
lastName: TypeField.string,
7178
isActive: TypeField.boolean,
7279
createdAt: TypeField.date,
80+
testReal: TypeField.array,
81+
testArrayNull: TypeField.array,
7382
testDate: TypeField.date,
7483
updatedAt: TypeField.date,
7584
addresses: {
@@ -86,6 +95,8 @@ describe('Check "filter" zod schema', () => {
8695
login: TypeField.string,
8796
firstName: TypeField.string,
8897
lastName: TypeField.string,
98+
testReal: TypeField.array,
99+
testArrayNull: TypeField.array,
89100
isActive: TypeField.boolean,
90101
createdAt: TypeField.date,
91102
testDate: TypeField.date,

libs/json-api/json-api-nestjs/src/lib/helper/zod/zod-query-schema/select.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ describe('Check "select" zod schema', () => {
2121
'testDate',
2222
'isActive',
2323
'lastName',
24+
'testArrayNull',
25+
'testReal',
2426
'firstName',
2527
'login',
2628
'updatedAt',
@@ -36,6 +38,8 @@ describe('Check "select" zod schema', () => {
3638
'testDate',
3739
'isActive',
3840
'lastName',
41+
'testArrayNull',
42+
'testReal',
3943
'firstName',
4044
'login',
4145
'updatedAt',

0 commit comments

Comments
 (0)