From 5190435b68158138e29759a5a1fc43d963266a62 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Fri, 24 Sep 2021 17:24:41 +0700 Subject: [PATCH] Fixed tests and service for async --- prisma/dev.db | Bin 36864 -> 40960 bytes prisma/dev.db-journal | Bin 0 -> 16928 bytes .../20210924101547_add_profile/migration.sql | 20 ++++++++++++++++++ src/User/UserService.ts | 18 ++++++++-------- tests/user.test.ts | 20 +++++++++--------- 5 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 prisma/dev.db-journal create mode 100644 prisma/migrations/20210924101547_add_profile/migration.sql diff --git a/prisma/dev.db b/prisma/dev.db index b04aa6c076075505037b1664e1c4aa3c0d8c68b8..63e144b84eac1f75beb00fef056880072c8d0b9c 100644 GIT binary patch delta 732 zcma))O=#0l9KiFE)iy6}Uo%(ZD9+GbEZ8ldO@iRa+JJ@G)~*2sCFB!}a~o4};=uN> zos}GS8M}HIlB=KyUOcF~6pwm!XJH2qrnTbNWe@(m4}S0eet+J-w}AiG=dIfQEm%5!=deMKMRM9b!XJ%)?Hgn&0 zR{T}}q1D=03%Y+TVYO}O9fR%Yi%SMu9716WzGSP-#&V~v*Bc$SIKE{)xaV)wJuA55 zKPrY4b*bsYB*Pa#pX#o9{^mbeOpQ-c<2TGk{hDd8^+wIO{$JJ@57Th^+)Xmc6bfJ) zjikpJ#uE$|;ky&5U2Yq(e!bdZn+?`%)rKWj_2sHwGhhiFW6AkEFsV~aDGwfxQz zSC)i|RCZL`FUuk?DUKq$nxZ%r*>gQfb3~5kM2Qm}wZaRMrYeeHt3F?GIZ1XM*^6n< zQv=uLcujQ$U*Ls+7ZtF@T>bD}-~^u21nDf#@v@{?w&z*vKmTS#AbB`P}De z!7*8Z>(t~#PODUB$6#ki7gvxep#d%+OQ9lMnlWtb;`*YDjf`OTh63HGmztZHnFDnz zNPr!yLot+YUd-A#= zfUK+hlOOsEGcs-d>MtU|e3AbdQ0yqbs0gzzV}3!gLvm?SYDs2cv0ie1F4N?T`BIA} J2rMcP000v6e_j9p diff --git a/prisma/dev.db-journal b/prisma/dev.db-journal new file mode 100644 index 0000000000000000000000000000000000000000..ffd9f7ee935bb4931277a24e0b8b60a4b83d0b70 GIT binary patch literal 16928 zcmeI(J8M)y7zW^T&a!cjkd1{9OIwQrbD#N2QNczKBz2f81fshv;sugoQz=Mltrh=` zSZQNpWvl2Pa5i3o!uAH=ub3%j2Hx+PZeW@Q^`8rLn-yR*z0qv#P2blE1px>^00Izz z00bZa0SG_<0uWd)u$+$0?<_Z)t4%ZRsW205u|v|H4N4d_1n2XhC090B9m(2Ybc#t) zkc=~_m<-Nvp~zV)yvy7~rgRKC)!iw%5+Q|ug{}q~k52-ndff9>APR0!!N-9F3xi29WF;d@sRU0hTP}=GhNKKKIxme% zTvH$u6J_R-(8MXp5{f57l!7VI+0Z_^@?`SuB$DKk)s}?%BhqU9%kpBoGWroIGvdyX z8=PTkP{pQ!6juWU>!l9X#F(vdx;T|(QqJW%h*(bqDNM|=7SEFTdX&dxqKMiBQ~TqY zNadoXt+9>{zy6LTv=;oYSVxDSU%z_M)r;+aUT$0GbL%e}D-eJH1Rwwb2tWV=5P$## zAOHafoIQc@sBOD$H0t}?JMWHb{$K9^t5zmwuOB3a00bZa0SG_<0uX=z1Rwwb2tZ)H z!09`{^AE=db>C$5FQD6c(` c > 0).catch((e) => 0); + return exist > 0; } async function findOneProfileByEmail(email: string) { @@ -30,15 +30,15 @@ async function findOneProfileByEmail(email: string) { return profile; } - -function isExistByEmail(email: string) { - const exist = prisma.user.count({ + +async function isExistByEmail(email: string) { + const exist = await prisma.user.count({ where: { email: email, }, }); - return exist.then((c) => c > 0).catch((e) => 0); + return exist > 0; } async function findOneByEmail( @@ -59,7 +59,7 @@ async function findOneByEmail( } async function createUser(request: UserRequest) { - if (isExistByEmail(request.email)) { + if (await isExistByEmail(request.email)) { return findOneByEmail(request.email); } @@ -76,7 +76,7 @@ async function createUser(request: UserRequest) { } async function createProfile(user: User, request: ProfileRequest) { - if (isProfileExistByEmail(user.email)) { + if (await isProfileExistByEmail(user.email)) { return findOneProfileByEmail(user.email); } diff --git a/tests/user.test.ts b/tests/user.test.ts index 89b4ba6..bf05bbc 100644 --- a/tests/user.test.ts +++ b/tests/user.test.ts @@ -15,13 +15,13 @@ describe("user", function () { const bio = "Software Developer"; // create user test - it("createUser", async (done) => { + it("createUser", (done) => { // create user - await createUser({ + createUser({ email: email, name: name, }) - .then(async (user) => { + .then((user) => { if (user == null) { throw Error("user is null"); } @@ -31,7 +31,7 @@ describe("user", function () { expect(name).toBe(user.name); // create profile - await createProfile(user, { + createProfile(user, { bio: "Software Developer", }) .then((profile) => { @@ -50,8 +50,8 @@ describe("user", function () { }); // check user test - it("checkUser", async (done) => { - await findOneByEmail(email) + it("checkUser", (done) => { + findOneByEmail(email) .then((user) => { if (user == null) { throw Error("user is null"); @@ -66,8 +66,8 @@ describe("user", function () { }); // check profile included user test - it("checkProfileUser", async (done) => { - await findOneProfileByEmail(email) + it("checkProfileUser", (done) => { + findOneProfileByEmail(email) .then((profile) => { if (profile == null) { throw Error("profile is null"); @@ -83,8 +83,8 @@ describe("user", function () { }); // try to finish - afterAll(async (done) => { - await prisma.$disconnect(); + afterAll((done) => { + prisma.$disconnect(); done(); });