Skip to content

Commit 34c81b8

Browse files
author
Mateus Garcia
committed
feat: unit tests
1 parent 2977214 commit 34c81b8

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed

libs/json-api-nestjs/src/lib/mixin/pipes/body-input-post/body-input-post.pipe.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ describe('BodyInputPostPipe', () => {
269269
id: '1',
270270
},
271271
},
272+
userGroup: {
273+
data: {
274+
type: 'user-groups',
275+
id: '1',
276+
},
277+
},
272278
},
273279
},
274280
};
@@ -302,6 +308,12 @@ describe('BodyInputPostPipe', () => {
302308
id: '1',
303309
},
304310
},
311+
userGroup: {
312+
data: {
313+
type: 'user-groups',
314+
id: '1',
315+
},
316+
},
305317
},
306318
},
307319
};

libs/json-api-nestjs/src/lib/mixin/service/typeorm/utils/utils-methode.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Users,
88
Roles,
99
Addresses,
10+
UserGroups,
1011
} from '../../../../mock-utils';
1112
import { DEFAULT_CONNECTION_NAME } from '../../../../constants';
1213
import { UtilsMethode } from './utils-methode';
@@ -46,6 +47,12 @@ describe('Utils methode test', () => {
4647
})
4748
);
4849

50+
const userGroup = await repository.manager.getRepository(UserGroups).save(
51+
Object.assign(new UserGroups(), {
52+
label: 'Editors',
53+
})
54+
);
55+
4956
await repository.manager.getRepository(Roles).save(
5057
Object.assign(new Roles(), {
5158
name: 'user',
@@ -62,6 +69,7 @@ describe('Utils methode test', () => {
6269
isActive: true,
6370
login: 'login',
6471
addresses: address,
72+
userGroups: userGroup,
6573
})
6674
);
6775
});
@@ -680,6 +688,30 @@ describe('Utils methode test', () => {
680688
expect(result[0].type).toBe(null);
681689
expect(result[0].propsName).toBe(Object.keys(data)[0]);
682690
});
691+
692+
it('should be ok if relation entity name is compound', async () => {
693+
const data: ResourceRequestObject<Users>['data']['relationships'] = {
694+
userGroup: {
695+
data: {
696+
type: 'user-groups',
697+
id: '1',
698+
},
699+
},
700+
};
701+
const result = [];
702+
for await (const tmp of UtilsMethode.asyncIterateFindRelationships(
703+
data,
704+
repository
705+
)) {
706+
result.push(tmp);
707+
}
708+
expect(result.length).toBe(1);
709+
expect(result[0].id).toBe(data.userGroup.data.id);
710+
expect(Array.isArray(result[0].rel)).toBe(false);
711+
expect(result[0].rel).toBeInstanceOf(UserGroups);
712+
expect(result[0].type).toBe(data.userGroup.data.type);
713+
expect(result[0].propsName).toBe(Object.keys(data)[0]);
714+
});
683715
});
684716

685717
describe('UtilsMethode.validateRelationRequestData', () => {

libs/json-api-nestjs/src/lib/mock-utils/db-for-test

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,21 @@ CREATE TABLE public.users (
286286
is_active boolean DEFAULT false,
287287
manager_id integer,
288288
addresses_id integer NOT NULL,
289+
user_groups_id integer,
289290
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
290291
test_date timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
291292
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
292293
);
293294

295+
--
296+
-- Name: user_groups; Type: TABLE; Schema: public; Owner: -
297+
--
298+
299+
CREATE TABLE public.user_groups (
300+
id integer NOT NULL,
301+
label character varying NOT NULL
302+
);
303+
294304

295305
--
296306
-- Name: users_have_roles; Type: TABLE; Schema: public; Owner: -
@@ -345,6 +355,25 @@ CREATE SEQUENCE public.users_id_seq
345355
ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
346356

347357

358+
--
359+
-- Name: user_groups_id_seq; Type: SEQUENCE; Schema: public; Owner: -
360+
--
361+
362+
CREATE SEQUENCE public.user_groups_id_seq
363+
AS integer
364+
START WITH 1
365+
INCREMENT BY 1
366+
NO MINVALUE
367+
NO MAXVALUE
368+
CACHE 1;
369+
370+
--
371+
-- Name: user_groups_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
372+
--
373+
374+
ALTER SEQUENCE public.user_groups_id_seq OWNED BY public.user_groups.id;
375+
376+
348377
--
349378
-- Name: addresses id; Type: DEFAULT; Schema: public; Owner: -
350379
--
@@ -401,6 +430,13 @@ ALTER TABLE ONLY public.roles ALTER COLUMN id SET DEFAULT nextval('public.roles_
401430
ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);
402431

403432

433+
--
434+
-- Name: users id; Type: DEFAULT; Schema: public; Owner: -
435+
--
436+
437+
ALTER TABLE ONLY public.user_groups ALTER COLUMN id SET DEFAULT nextval('public.user_groups_id_seq'::regclass);
438+
439+
404440
--
405441
-- Name: users_have_roles id; Type: DEFAULT; Schema: public; Owner: -
406442
--
@@ -456,6 +492,13 @@ ALTER TABLE ONLY public.users
456492
ADD CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY (id);
457493

458494

495+
--
496+
-- Name: users PK_user_groups; Type: CONSTRAINT; Schema: public; Owner: -
497+
--
498+
499+
ALTER TABLE ONLY public.user_groups
500+
ADD CONSTRAINT "PK_user_groups" PRIMARY KEY (id);
501+
459502
--
460503
-- Name: pods PK_b00bbc2c7fb41627be2b169f0dd; Type: CONSTRAINT; Schema: public; Owner: -
461504
--
@@ -518,6 +561,13 @@ ALTER TABLE ONLY public.users
518561
ADD CONSTRAINT "FK_2f8d527df0d3acb8aa51945a968" FOREIGN KEY (addresses_id) REFERENCES public.addresses(id);
519562

520563

564+
--
565+
-- Name: users FK_user_groups; Type: FK CONSTRAINT; Schema: public; Owner: -
566+
--
567+
ALTER TABLE ONLY public.users
568+
ADD CONSTRAINT "FK_user_groups" FOREIGN KEY (user_groups_id) REFERENCES public.user_groups(id);
569+
570+
521571
--
522572
-- Name: users_have_roles FK_6e768e03083247102b401b74b46; Type: FK CONSTRAINT; Schema: public; Owner: -
523573
--

libs/json-api-nestjs/src/lib/mock-utils/entities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './requests';
55
export * from './pods';
66
export * from './comments';
77
export * from './addresses';
8+
export * from './user-groups';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { PrimaryGeneratedColumn, OneToMany, Entity, Column } from 'typeorm';
2+
import { IsNotEmpty, Length } from 'class-validator';
3+
4+
import { IUsers, Users } from '.';
5+
6+
@Entity('user_groups')
7+
export class UserGroups {
8+
@PrimaryGeneratedColumn()
9+
public id: number;
10+
11+
@IsNotEmpty()
12+
@Length(3, 50)
13+
@Column({
14+
type: 'varchar',
15+
length: 50,
16+
nullable: false,
17+
unique: true,
18+
})
19+
public label: string;
20+
21+
@OneToMany(() => Users, (item) => item.userGroup)
22+
public users: IUsers[];
23+
}

libs/json-api-nestjs/src/lib/mock-utils/entities/users.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Entity,
99
Column,
1010
UpdateDateColumn,
11+
ManyToOne,
1112
} from 'typeorm';
1213
import {
1314
Length,
@@ -20,6 +21,7 @@ import {
2021
import { Exclude } from 'class-transformer';
2122

2223
import { Addresses, Roles, Comments } from '.';
24+
import { UserGroups } from './user-groups';
2325

2426
export type IUsers = Users;
2527

@@ -128,4 +130,9 @@ export class Users {
128130

129131
@OneToMany(() => Comments, (item) => item.createdBy)
130132
public comments: Comments[];
133+
134+
@ManyToOne(() => UserGroups, (userGroup) => userGroup.id)
135+
@IsNotEmpty()
136+
@JoinColumn({ name: 'user_groups_id' })
137+
public userGroup: UserGroups;
131138
}

libs/json-api-nestjs/src/lib/mock-utils/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import {
1212
Pods,
1313
Comments,
1414
Addresses,
15+
UserGroups,
1516
} from './entities';
1617
import { DataSource } from 'typeorm';
1718

1819
export * from './entities';
1920

2021
export const entities = [
2122
Users,
23+
UserGroups,
2224
Roles,
2325
RequestsHavePodLocks,
2426
Requests,
@@ -52,6 +54,7 @@ export function mockDBTestModule(): DynamicModule {
5254
type: 'postgres',
5355
entities: [
5456
Users,
57+
UserGroups,
5558
Roles,
5659
RequestsHavePodLocks,
5760
Requests,

0 commit comments

Comments
 (0)