Skip to content

Commit adca8d7

Browse files
author
Mateus Garcia
committed
feat: test case
1 parent d98c769 commit adca8d7

File tree

7 files changed

+147
-73
lines changed

7 files changed

+147
-73
lines changed

libs/json-api-nestjs/src/lib/factory/ajv/ajv.factory.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,34 @@ describe('AJV factory', () => {
228228
required: ['type', 'id'],
229229
});
230230

231+
expect(
232+
dataProperty['relationships']['properties']['notes']['properties'][
233+
'data'
234+
]['type']
235+
).toBe('array');
236+
expect(
237+
dataProperty['relationships']['properties']['notes']['properties'][
238+
'data'
239+
]['items']
240+
).toEqual({
241+
type: 'object',
242+
properties: {
243+
id: {
244+
type: 'string',
245+
description: 'Use string should be as number string',
246+
pattern:
247+
'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
248+
maxLength: 36,
249+
minLength: 36,
250+
},
251+
type: {
252+
type: 'string',
253+
enum: ['notes'],
254+
},
255+
},
256+
required: ['type', 'id'],
257+
});
258+
231259
expect(Object.keys(dataProperty['relationships']['properties'])).toEqual(
232260
relationField
233261
);

libs/json-api-nestjs/src/lib/factory/ajv/utils/input-body-post-schema.ts

+3
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ export function inputBodyPostSchema(
126126
maxLength: 36,
127127
}
128128
: {}),
129+
description: uuidRelations[item]?.id
130+
? 'Use string should be as uuid string'
131+
: 'Use string should be as number string',
129132
},
130133
},
131134
},

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

+3-34
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ SET xmloption = content;
1616
SET client_min_messages = warning;
1717
SET row_security = off;
1818

19+
create extension "uuid-ossp";
20+
1921
--
2022
-- Name: comment_kind_enum; Type: TYPE; Schema: public; Owner: -
2123
--
@@ -103,33 +105,14 @@ ALTER SEQUENCE public.comments_id_seq OWNED BY public.comments.id;
103105
--
104106

105107
CREATE TABLE public.notes (
106-
id integer NOT NULL,
108+
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
107109
text text NOT NULL,
108110
created_by integer,
109111
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
110112
updated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
111113
);
112114

113115

114-
--
115-
-- Name: notes_id_seq; Type: SEQUENCE; Schema: public; Owner: -
116-
--
117-
118-
CREATE SEQUENCE public.notes_id_seq
119-
AS integer
120-
START WITH 1
121-
INCREMENT BY 1
122-
NO MINVALUE
123-
NO MAXVALUE
124-
CACHE 1;
125-
126-
127-
--
128-
-- Name: notes_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
129-
--
130-
131-
ALTER SEQUENCE public.notes_id_seq OWNED BY public.notes.id;
132-
133116

134117
--
135118
-- Name: migrations; Type: TABLE; Schema: public; Owner: -
@@ -421,13 +404,6 @@ ALTER TABLE ONLY public.addresses ALTER COLUMN id SET DEFAULT nextval('public.ad
421404
ALTER TABLE ONLY public.comments ALTER COLUMN id SET DEFAULT nextval('public.comments_id_seq'::regclass);
422405

423406

424-
--
425-
-- Name: notes id; Type: DEFAULT; Schema: public; Owner: -
426-
--
427-
428-
ALTER TABLE ONLY public.notes ALTER COLUMN id SET DEFAULT nextval('public.notes_id_seq'::regclass);
429-
430-
431407
--
432408
-- Name: migrations id; Type: DEFAULT; Schema: public; Owner: -
433409
--
@@ -508,13 +484,6 @@ ALTER TABLE ONLY public.comments
508484
ADD CONSTRAINT "PK_8bf68bc960f2b69e818bdb90dcb" PRIMARY KEY (id);
509485

510486

511-
--
512-
-- Name: notes PK_notes; Type: CONSTRAINT; Schema: public; Owner: -
513-
--
514-
515-
ALTER TABLE ONLY public.notes
516-
ADD CONSTRAINT "PK_notes" PRIMARY KEY (id);
517-
518487
--
519488
-- Name: migrations PK_8c82d7f526340ab734260ea46be; Type: CONSTRAINT; Schema: public; Owner: -
520489
--

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { Users, IUsers } from '.';
1212

1313
@Entity('notes')
1414
export class Notes {
15-
@PrimaryGeneratedColumn()
16-
public id: number;
15+
@PrimaryGeneratedColumn('uuid')
16+
public id: string;
1717

1818
@IsNotEmpty()
1919
@Column({

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TypeOrmModule } from '@nestjs/typeorm';
22
import { DynamicModule } from '@nestjs/common';
3-
import { newDb } from 'pg-mem';
3+
import { DataType, newDb } from 'pg-mem';
44
import { readFileSync } from 'fs';
55
import { join } from 'path';
66

@@ -17,6 +17,8 @@ import {
1717
} from './entities';
1818
import { DataSource } from 'typeorm';
1919

20+
import { v4 } from 'uuid';
21+
2022
export * from './entities';
2123

2224
export const entities = [
@@ -48,6 +50,15 @@ export function mockDBTestModule(): DynamicModule {
4850
'PostgreSQL 12.5 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.2.1_pre1) 10.2.1 20201203, 64-bit',
4951
});
5052

53+
db.registerExtension('uuid-ossp', (schema) => {
54+
schema.registerFunction({
55+
name: 'uuid_generate_v4',
56+
returns: DataType.uuid,
57+
implementation: v4,
58+
impure: true,
59+
});
60+
});
61+
5162
db.public.none(dump);
5263
const backup = db.backup();
5364
return TypeOrmModule.forRootAsync({

0 commit comments

Comments
 (0)