Skip to content

Commit

Permalink
feat: mock relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Garcia committed Mar 20, 2023
1 parent 02b0479 commit 4697da2
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
55 changes: 55 additions & 0 deletions libs/json-api-nestjs/src/lib/mock-utils/db-for-test
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,39 @@ CREATE SEQUENCE public.comments_id_seq
ALTER SEQUENCE public.comments_id_seq OWNED BY public.comments.id;


--
-- Name: notes; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.notes (
id integer NOT NULL,
text text NOT NULL,
created_by integer,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


--
-- Name: notes_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.notes_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: notes_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.notes_id_seq OWNED BY public.notes.id;


--
-- Name: migrations; Type: TABLE; Schema: public; Owner: -
--
Expand Down Expand Up @@ -388,6 +421,13 @@ ALTER TABLE ONLY public.addresses ALTER COLUMN id SET DEFAULT nextval('public.ad
ALTER TABLE ONLY public.comments ALTER COLUMN id SET DEFAULT nextval('public.comments_id_seq'::regclass);


--
-- Name: notes id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.notes ALTER COLUMN id SET DEFAULT nextval('public.notes_id_seq'::regclass);


--
-- Name: migrations id; Type: DEFAULT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -468,6 +508,13 @@ ALTER TABLE ONLY public.comments
ADD CONSTRAINT "PK_8bf68bc960f2b69e818bdb90dcb" PRIMARY KEY (id);


--
-- Name: notes PK_notes; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.notes
ADD CONSTRAINT "PK_notes" PRIMARY KEY (id);

--
-- Name: migrations PK_8c82d7f526340ab734260ea46be; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -584,6 +631,14 @@ ALTER TABLE ONLY public.comments
ADD CONSTRAINT "FK_980bfefe00ed11685f325d0bd4c" FOREIGN KEY (created_by) REFERENCES public.users(id);


--
-- Name: notes FK_980bfefe00ed11685f325d0bd4c; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.notes
ADD CONSTRAINT "FK_notes" FOREIGN KEY (created_by) REFERENCES public.users(id);


--
-- Name: requests_have_pod_locks FK_c7531fe6bbb926bba3f69fcbb55; Type: FK CONSTRAINT; Schema: public; Owner: -
--
Expand Down
1 change: 1 addition & 0 deletions libs/json-api-nestjs/src/lib/mock-utils/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './pods';
export * from './comments';
export * from './addresses';
export * from './user-groups';
export * from './notes';
49 changes: 49 additions & 0 deletions libs/json-api-nestjs/src/lib/mock-utils/entities/notes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
PrimaryGeneratedColumn,
Column,
Entity,
JoinColumn,
ManyToOne,
UpdateDateColumn,
} from 'typeorm';
import { IsEmpty, IsNotEmpty } from 'class-validator';

import { Users, IUsers } from '.';

@Entity('notes')
export class Notes {
@PrimaryGeneratedColumn()
public id: number;

@IsNotEmpty()
@Column({
type: 'text',
nullable: false,
})
public text: string;

@IsEmpty()
@Column({
name: 'created_at',
type: 'timestamp',
nullable: true,
default: 'CURRENT_TIMESTAMP',
})
public createdAt: Date;

@IsEmpty()
@UpdateDateColumn({
name: 'updated_at',
type: 'timestamp',
nullable: true,
default: 'CURRENT_TIMESTAMP',
})
public updatedAt: Date;

@ManyToOne(() => Users, (item) => item.notes)
@IsNotEmpty()
@JoinColumn({
name: 'created_by',
})
public createdBy: IUsers;
}
6 changes: 4 additions & 2 deletions libs/json-api-nestjs/src/lib/mock-utils/entities/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import {

import { Exclude } from 'class-transformer';

import { Addresses, Roles, Comments } from '.';
import { UserGroups } from './user-groups';
import { Addresses, Roles, Comments, Notes, UserGroups } from '.';

export type IUsers = Users;

Expand Down Expand Up @@ -131,6 +130,9 @@ export class Users {
@OneToMany(() => Comments, (item) => item.createdBy)
public comments: Comments[];

@OneToMany(() => Notes, (item) => item.createdBy)
public notes: Notes[];

@ManyToOne(() => UserGroups, (userGroup) => userGroup.id)
@IsNotEmpty()
@JoinColumn({ name: 'user_groups_id' })
Expand Down
3 changes: 3 additions & 0 deletions libs/json-api-nestjs/src/lib/mock-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Comments,
Addresses,
UserGroups,
Notes,
} from './entities';
import { DataSource } from 'typeorm';

Expand All @@ -27,6 +28,7 @@ export const entities = [
Pods,
Comments,
Addresses,
Notes,
];

const dump = readFileSync(join(__dirname, 'db-for-test'), { encoding: 'utf8' });
Expand Down Expand Up @@ -61,6 +63,7 @@ export function mockDBTestModule(): DynamicModule {
Pods,
Comments,
Addresses,
Notes,
],
};
},
Expand Down

0 comments on commit 4697da2

Please sign in to comment.