Skip to content

Commit

Permalink
test: Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
klerick committed Dec 5, 2024
1 parent 4c5284f commit 7063fb4
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,25 @@ describe('Atomic method:', () => {
);
}

await Promise.all(
[...usersArray, ...addressArray, ...commentsArray, ...rolesArray].map(
(i) => jsonSdk.jonApiSdkService.deleteOne(i)
)
);
for (const item of [
...usersArray,
...addressArray,
...commentsArray,
...rolesArray,
]) {
await jsonSdk.jonApiSdkService.deleteOne(item);
}
});

it('Try check intreceptor', async () => {
const newUser = getUser();
newUser.addresses = addressArray[0];
try {
const result = await jsonSdk.atomicFactory().postOne(newUser).run();
usersId.push(result[0].id);
} catch (e) {
console.log(e);
}
});

it('Should be correct work', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('bindController', () => {
requiredSelectField: false,
pipeForId: ParseIntPipe,
debug: false,
useSoftDelete: false,
};
bindController(Controller, Users, DEFAULT_CONNECTION_NAME, config);

Expand Down Expand Up @@ -99,6 +100,7 @@ describe('bindController', () => {
requiredSelectField: false,
pipeForId: ParseIntPipe,
debug: false,
useSoftDelete: false,
};
bindController(Controller, Users, DEFAULT_CONNECTION_NAME, config);
expect(Object.getOwnPropertyNames(Controller.prototype)).toEqual([
Expand Down Expand Up @@ -131,6 +133,7 @@ describe('bindController', () => {
requiredSelectField: false,
pipeForId: SomePipes,
debug: false,
useSoftDelete: false,
};
bindController(Controller, Users, DEFAULT_CONNECTION_NAME, config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('patchOne', () => {
await typeormService.postOne(inputData);
backaUp = db.backup();
const changeUser = await userRepository.findOneBy({
login: inputData.attributes.login,
login: inputData.attributes.login as string,
});
if (!changeUser) {
throw new Error('not found mock data');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getRepository,
mockDBTestModule,
Notes,
Pods,
providerEntities,
pullAllData,
Roles,
Expand Down Expand Up @@ -40,6 +41,10 @@ describe('postOne', () => {
let backaUp: IBackup;
let typeormService: TypeormService<Users>;
let transformDataService: TransformDataService<Users>;
let podsRepository: Repository<Pods>;

let typeormServicePods: TypeormService<Pods>;
let transformDataServicePods: TransformDataService<Pods>;

let userRepository: Repository<Users>;
let addressesRepository: Repository<Addresses>;
Expand Down Expand Up @@ -82,13 +87,34 @@ describe('postOne', () => {
],
}).compile();

const modulePods: TestingModule = await Test.createTestingModule({
imports: [mockDBTestModule(db)],
providers: [
...providerEntities(getDataSourceToken()),
CurrentDataSourceProvider(DEFAULT_CONNECTION_NAME),
{
provide: CONTROL_OPTIONS_TOKEN,
useValue: {
requiredSelectField: false,
debug: false,
},
},
EntityRepositoryFactory(Pods),
TypeormUtilsService,
TransformDataService,
TypeormServiceFactory(Pods),
EntityPropsMapService,
],
}).compile();

({
userRepository,
addressesRepository,
notesRepository,
commentsRepository,
rolesRepository,
userGroupRepository,
podsRepository,
} = getRepository(module));
await pullAllData(
userRepository,
Expand All @@ -103,6 +129,10 @@ describe('postOne', () => {
transformDataService =
module.get<TransformDataService<Users>>(TransformDataService);

typeormServicePods = modulePods.get<TypeormService<Pods>>(TYPEORM_SERVICE);
transformDataServicePods =
modulePods.get<TransformDataService<Pods>>(TransformDataService);

notes = await notesRepository.find();
users = await userRepository.find();
roles = await rolesRepository.find();
Expand Down Expand Up @@ -147,6 +177,32 @@ describe('postOne', () => {
backaUp.restore();
});

it('should be ok without relation and with id', async () => {
const spyOnTransformData = jest
.spyOn(transformDataServicePods, 'transformData')
.mockImplementationOnce(() => ({
data: {} as any,
}));
const { relationships, ...other } = inputData;
const id = '5';
const returnData = await typeormServicePods.postOne({
id,
type: 'pods',
attributes: {
name: 'test',
},
});
const result = await podsRepository.findOneBy({
id,
});

expect(spyOnTransformData).toBeCalledWith({
...result,
id,
});
expect(returnData).not.toHaveProperty('included');
});

it('should be ok without relation', async () => {
const spyOnTransformData = jest
.spyOn(transformDataService, 'transformData')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
getPrimaryColumnsForRelation,
getIsArrayRelation,
getTypeForAllProps,
getPropsFromDb,
} from './orm-helper';
import { ObjectTyped } from '../utils';

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

it('getField', async () => {
const { relations, field } = getField(userRepository);

const userFieldProps = Object.getOwnPropertyNames(
user
) as EntityProps<Users>[];
Expand Down Expand Up @@ -171,8 +171,14 @@ describe('zod-helper', () => {
it('getArrayPropsForEntity', () => {
const result = getArrayPropsForEntity(userRepository);
const check: ArrayPropsForEntity<Users> = {
target: {},
manager: {},
target: {
testReal: true,
testArrayNull: true,
},
manager: {
testReal: true,
testArrayNull: true,
},
comments: {},
notes: {},
userGroup: {},
Expand Down Expand Up @@ -255,4 +261,13 @@ describe('zod-helper', () => {
expect(result.comments.id).toBe(TypeField.number);
expect(result.notes.id).toBe(TypeField.string);
});

it('getPropsFromDb', () => {
const result = getPropsFromDb(userRepository);
expect(result['testReal']).toEqual({
type: 'real',
isArray: true,
isNullable: false,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,14 @@ describe('zod-helper', () => {
});
describe('test zodInputPostSchema', () => {
it('should be ok', () => {
const real = 123.123;
const date = new Date();
const attributes = {
lastName: 'sdfsdf',
isActive: true,
testDate: date.toISOString(),
testReal: [`${real}`],
testArrayNull: null,
};
const relationships = {
notes: [
Expand All @@ -459,13 +462,21 @@ describe('zod-helper', () => {
attributes,
},
};
const check3 = {
data: {
id: '1',
type: 'users',
attributes,
},
};

const checkResult = {
data: {
type: 'users',
attributes: {
...attributes,
['testDate']: date,
testReal: [real],
},
relationships,
},
Expand All @@ -476,12 +487,25 @@ describe('zod-helper', () => {
attributes: {
...attributes,
['testDate']: date,
testReal: [real],
},
},
};
const checkResult3 = {
data: {
id: '1',
type: 'users',
attributes: {
...attributes,
['testDate']: date,
testReal: [real],
},
},
};

expect(zodInputPostSchemaTest.parse(check)).toEqual(checkResult);
expect(zodInputPostSchemaTest.parse(check2)).toEqual(checkResult2);
expect(zodInputPostSchemaTest.parse(check3)).toEqual(checkResult3);
});

it('should be not ok', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z, ZodError } from 'zod';
import { zodAttributesSchema, ZodAttributesSchema } from './attributes';
import { Addresses, Users } from '../../../mock-utils';
import { FieldWithType, TypeField } from '../../orm';
import { FieldWithType, PropsForField, TypeField } from '../../orm';

describe('attributes', () => {
let schemaUsers: ZodAttributesSchema<Users>;
Expand All @@ -18,6 +18,20 @@ describe('attributes', () => {
login: TypeField.string,
testDate: TypeField.date,
updatedAt: TypeField.date,
testReal: TypeField.array,
testArrayNull: TypeField.array,
};
const propsDb: PropsForField<Users> = {
id: { type: Number, isArray: false, isNullable: false },
login: { type: 'varchar', isArray: false, isNullable: false },
firstName: { type: 'varchar', isArray: false, isNullable: true },
testReal: { type: 'real', isArray: true, isNullable: false },
testArrayNull: { type: 'real', isArray: true, isNullable: true },
lastName: { type: 'varchar', isArray: false, isNullable: true },
isActive: { type: 'boolean', isArray: false, isNullable: true },
createdAt: { type: 'timestamp', isArray: false, isNullable: true },
testDate: { type: 'timestamp', isArray: false, isNullable: true },
updatedAt: { type: 'timestamp', isArray: false, isNullable: true },
};
const fieldTypeAddresses: FieldWithType<Addresses> = {
id: TypeField.number,
Expand All @@ -28,14 +42,18 @@ describe('attributes', () => {
updatedAt: TypeField.date,
country: TypeField.string,
};
schemaUsers = zodAttributesSchema<Users>(fieldTypeUsers);
schemaAddresses = zodAttributesSchema<Addresses>(fieldTypeAddresses);
schemaUsers = zodAttributesSchema<Users>(fieldTypeUsers, propsDb);
schemaAddresses = zodAttributesSchema<Addresses>(
fieldTypeAddresses,
{} as PropsForField<Addresses>
);
});

it('should be ok', () => {
const check: SchemaTypeUsers = {
isActive: true,
lastName: 'sdsdf',
testReal: [123.123, 123.123],
};
const check2: SchemaTypeAddresses = {
arrayField: ['test', 'test'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe('Check "filter" zod schema', () => {
'testDate',
'isActive',
'lastName',
'testArrayNull',
'testReal',
'firstName',
'login',
'updatedAt',
Expand All @@ -47,14 +49,19 @@ describe('Check "filter" zod schema', () => {
'testDate',
'isActive',
'lastName',
'testArrayNull',
'testReal',
'firstName',
'login',
'updatedAt',
'createdAt',
'id',
];

const usersPropsArray: PropsArray<Users> = {};
const usersPropsArray: PropsArray<Users> = {
testArrayNull: true,
testReal: true,
};
const rolesPropsArray: PropsArray<Roles> = {};
const userGroupPropsArray: PropsArray<UserGroups> = {};
const commentsPropsArray: PropsArray<Comments> = {};
Expand All @@ -70,6 +77,8 @@ describe('Check "filter" zod schema', () => {
lastName: TypeField.string,
isActive: TypeField.boolean,
createdAt: TypeField.date,
testReal: TypeField.array,
testArrayNull: TypeField.array,
testDate: TypeField.date,
updatedAt: TypeField.date,
addresses: {
Expand All @@ -86,6 +95,8 @@ describe('Check "filter" zod schema', () => {
login: TypeField.string,
firstName: TypeField.string,
lastName: TypeField.string,
testReal: TypeField.array,
testArrayNull: TypeField.array,
isActive: TypeField.boolean,
createdAt: TypeField.date,
testDate: TypeField.date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe('Check "select" zod schema', () => {
'testDate',
'isActive',
'lastName',
'testArrayNull',
'testReal',
'firstName',
'login',
'updatedAt',
Expand All @@ -36,6 +38,8 @@ describe('Check "select" zod schema', () => {
'testDate',
'isActive',
'lastName',
'testArrayNull',
'testReal',
'firstName',
'login',
'updatedAt',
Expand Down
Loading

0 comments on commit 7063fb4

Please sign in to comment.