forked from Nhogs/nestjs-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-import-person.e2e.spec.ts
221 lines (203 loc) · 7.44 KB
/
async-import-person.e2e.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { AppAsyncModule } from './src/app.async.module';
import { Neo4jService } from '../../lib';
import { PersonDto } from './src/person/dto/person.dto';
import { LikeService } from './src/person/service/like.service';
import { PersonService } from './src/person/service/person.service';
import { CompanyService } from './src/person/service/company.service';
import { WorkInService } from './src/person/service/work.in.service';
import { LikeDto } from './src/person/dto/like.dto';
import { CompanyDto } from './src/person/dto/company.dto';
async function cleanDb(neo4jService: Neo4jService) {
await neo4jService.run(
{ cypher: 'MATCH (n) DETACH DELETE n' },
{
write: true,
},
);
}
describe('Persons E2e', () => {
let app: INestApplication;
let neo4jService: Neo4jService;
let likeService: LikeService;
let personService: PersonService;
let companyService: CompanyService;
let workInService: WorkInService;
const emma: PersonDto = {
name: 'Wong',
firstname: 'Emi',
age: 18,
surname: 'emma',
};
const alex: PersonDto = {
name: 'Smith',
firstname: 'Alexander',
age: 22,
surname: 'alex',
};
const emma_like_alex: LikeDto = {
when: '2020',
since: '2000',
};
const corp: CompanyDto = {
name: 'Corp',
};
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppAsyncModule],
}).compile();
app = moduleRef.createNestApplication();
await app.init();
neo4jService = app.get<Neo4jService>(Neo4jService);
likeService = app.get<LikeService>(LikeService);
personService = app.get<PersonService>(PersonService);
companyService = app.get<CompanyService>(CompanyService);
workInService = app.get<WorkInService>(WorkInService);
});
beforeEach(async () => {
await cleanDb(neo4jService);
});
it(`/post /get Person`, (done) => {
request(app.getHttpServer())
.post('/person')
.send(emma)
.expect(201)
.then(() => {
request(app.getHttpServer())
.post('/person')
.send(alex)
.expect(201)
.then(() => {
request(app.getHttpServer())
.post('/LIKE/Wong/Smith')
.send(emma_like_alex)
.expect(201)
.then(() => {
request(app.getHttpServer())
.get('/person')
.expect(200)
.expect([{ ...alex }, { ...emma }])
.then(() => {
request(app.getHttpServer())
.get('/LIKE/Wong')
.expect(200)
.expect([[emma_like_alex, { ...alex }]])
.then(() => {
done();
});
});
});
});
});
});
it(`/post /get Person`, (done) => {
request(app.getHttpServer())
.post('/person/company')
.send({
person: alex,
company: corp,
})
.expect(201)
.then(() => {
request(app.getHttpServer())
.get('/WORK_IN')
.expect(200)
.then((value) => {
expect([value.body[0][0], value.body[0][2]]).toMatchInlineSnapshot(`
[
{
"age": 22,
"firstname": "Alexander",
"name": "Smith",
"surname": "alex",
},
{
"name": "Corp",
},
]
`);
done();
});
});
});
it('should generate All constraints', () => {
expect(neo4jService.getCypherConstraints()).toMatchInlineSnapshot(`
[
"CREATE CONSTRAINT \`person_name_key\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE (p.\`name\`, p.\`firstname\`) IS NODE KEY",
"CREATE CONSTRAINT \`person_firstname_exists\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE p.\`firstname\` IS NOT NULL",
"CREATE CONSTRAINT \`person_surname_unique\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE p.\`surname\` IS UNIQUE",
"CREATE CONSTRAINT \`person_surname_exists\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE p.\`surname\` IS NOT NULL",
"CREATE CONSTRAINT \`person_age_exists\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE p.\`age\` IS NOT NULL",
"CREATE CONSTRAINT \`company_name_key\` IF NOT EXISTS FOR (p:\`Company\`) REQUIRE p.\`name\` IS NODE KEY",
"CREATE CONSTRAINT \`like_when_exists\` IF NOT EXISTS FOR ()-[p:\`LIKE\`]-() REQUIRE p.\`when\` IS NOT NULL",
"CREATE CONSTRAINT \`like_since_exists\` IF NOT EXISTS FOR ()-[p:\`LIKE\`]-() REQUIRE p.\`since\` IS NOT NULL",
"CREATE CONSTRAINT \`work_in_since_exists\` IF NOT EXISTS FOR ()-[p:\`WORK_IN\`]-() REQUIRE p.\`since\` IS NOT NULL",
]
`);
});
it('should generate Person constraints', () => {
expect(neo4jService.getCypherConstraints('Person')).toMatchInlineSnapshot(`
[
"CREATE CONSTRAINT \`person_name_key\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE (p.\`name\`, p.\`firstname\`) IS NODE KEY",
"CREATE CONSTRAINT \`person_firstname_exists\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE p.\`firstname\` IS NOT NULL",
"CREATE CONSTRAINT \`person_surname_unique\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE p.\`surname\` IS UNIQUE",
"CREATE CONSTRAINT \`person_surname_exists\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE p.\`surname\` IS NOT NULL",
"CREATE CONSTRAINT \`person_age_exists\` IF NOT EXISTS FOR (p:\`Person\`) REQUIRE p.\`age\` IS NOT NULL",
]
`);
});
it('should generate Company constraints', () => {
expect(neo4jService.getCypherConstraints('Company')).toMatchInlineSnapshot(`
[
"CREATE CONSTRAINT \`company_name_key\` IF NOT EXISTS FOR (p:\`Company\`) REQUIRE p.\`name\` IS NODE KEY",
]
`);
});
it('should generate LIKE constraints', () => {
expect(neo4jService.getCypherConstraints('LIKE')).toMatchInlineSnapshot(`
[
"CREATE CONSTRAINT \`like_when_exists\` IF NOT EXISTS FOR ()-[p:\`LIKE\`]-() REQUIRE p.\`when\` IS NOT NULL",
"CREATE CONSTRAINT \`like_since_exists\` IF NOT EXISTS FOR ()-[p:\`LIKE\`]-() REQUIRE p.\`since\` IS NOT NULL",
]
`);
});
it('should generate WORK_IN constraints', () => {
expect(neo4jService.getCypherConstraints('WORK_IN')).toMatchInlineSnapshot(`
[
"CREATE CONSTRAINT \`work_in_since_exists\` IF NOT EXISTS FOR ()-[p:\`WORK_IN\`]-() REQUIRE p.\`since\` IS NOT NULL",
]
`);
});
it(`Should create like query`, () => {
const query = likeService.create(
{ when: '2020', since: '2000' },
{ name: 'Wong' },
{ name: 'Smith' },
personService,
personService,
);
expect(query.query).toMatchInlineSnapshot(`
{
"cypher": "MATCH (\`f\`:\`Person\`), (\`t\`:\`Person\`) WHERE f.\`name\` = $fp.\`name\` AND t.\`name\` = $tp.\`name\` CREATE (f)-[r:\`LIKE\`]->(t) SET r=$p RETURN properties (f) AS from, properties(r) AS created, properties(t) AS to",
"parameters": {
"fp": {
"name": "Wong",
},
"p": {
"since": "2000",
"when": "2020",
},
"tp": {
"name": "Smith",
},
},
}
`);
});
afterAll(async () => {
await cleanDb(neo4jService);
return await app.close();
});
});