167 lines
3.8 KiB
TypeScript
167 lines
3.8 KiB
TypeScript
import assert from 'node:assert/strict';
|
|||
|
|
import { after, before, describe, it } from 'node:test';
|
||
|
|
import { Prisma } from '@prisma/client';
|
||
|
|
import { createPrismaClient } from '../src/lib/prisma.js';
|
||
|
|
|
||
|
|
const databaseUrl =
|
||
|
|
process.env.DATABASE_URL ?? 'mysql://novya:novya@127.0.0.1:3306/novya_test';
|
||
|
|
|
||
|
|
describe('User and UserProfile persistence', () => {
|
||
|
|
let prisma = createPrismaClient(databaseUrl);
|
||
|
|
let dbAvailable = false;
|
||
|
|
const createdUserIds: bigint[] = [];
|
||
|
|
|
||
|
|
before(async () => {
|
||
|
|
try {
|
||
|
|
await prisma.$connect();
|
||
|
|
dbAvailable = await prisma.$queryRaw`SELECT 1`.then(() => true).catch(() => false);
|
||
|
|
} catch {
|
||
|
|
dbAvailable = false;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
after(async () => {
|
||
|
|
if (dbAvailable) {
|
||
|
|
if (createdUserIds.length > 0) {
|
||
|
|
await prisma.user.deleteMany({
|
||
|
|
where: { id: { in: createdUserIds } },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
await prisma.$disconnect();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('creates User with stable externalId', async (t) => {
|
||
|
|
if (!dbAvailable) {
|
||
|
|
t.skip('MySQL is not running');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const user = await prisma.user.create({ data: {} });
|
||
|
|
createdUserIds.push(user.id);
|
||
|
|
|
||
|
|
assert.ok(user.id);
|
||
|
|
assert.match(user.externalId, /^[0-9a-f-]{36}$/i);
|
||
|
|
assert.ok(user.createdAt instanceof Date);
|
||
|
|
assert.ok(user.updatedAt instanceof Date);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('creates UserProfile with name and age', async (t) => {
|
||
|
|
if (!dbAvailable) {
|
||
|
|
t.skip('MySQL is not running');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const user = await prisma.user.create({
|
||
|
|
data: {
|
||
|
|
profile: {
|
||
|
|
create: {
|
||
|
|
name: 'علی رضایی',
|
||
|
|
age: 29,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
include: { profile: true },
|
||
|
|
});
|
||
|
|
createdUserIds.push(user.id);
|
||
|
|
|
||
|
|
assert.ok(user.profile);
|
||
|
|
assert.equal(user.profile.name, 'علی رضایی');
|
||
|
|
assert.equal(user.profile.age, 29);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('loads User → UserProfile relation', async (t) => {
|
||
|
|
if (!dbAvailable) {
|
||
|
|
t.skip('MySQL is not running');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const user = await prisma.user.create({
|
||
|
|
data: {
|
||
|
|
profile: {
|
||
|
|
create: {
|
||
|
|
name: 'Sara Ahmadi',
|
||
|
|
age: 34,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
createdUserIds.push(user.id);
|
||
|
|
|
||
|
|
const loaded = await prisma.user.findUnique({
|
||
|
|
where: { id: user.id },
|
||
|
|
include: { profile: true },
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.ok(loaded?.profile);
|
||
|
|
assert.equal(loaded.profile.name, 'Sara Ahmadi');
|
||
|
|
assert.equal(loaded.profile.age, 34);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('persists name and age updates on profile', async (t) => {
|
||
|
|
if (!dbAvailable) {
|
||
|
|
t.skip('MySQL is not running');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const user = await prisma.user.create({
|
||
|
|
data: {
|
||
|
|
profile: {
|
||
|
|
create: {
|
||
|
|
name: 'Initial Name',
|
||
|
|
age: 25,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
include: { profile: true },
|
||
|
|
});
|
||
|
|
createdUserIds.push(user.id);
|
||
|
|
|
||
|
|
const updated = await prisma.userProfile.update({
|
||
|
|
where: { userId: user.id },
|
||
|
|
data: {
|
||
|
|
name: 'Updated Name',
|
||
|
|
age: 26,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.equal(updated.name, 'Updated Name');
|
||
|
|
assert.equal(updated.age, 26);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('prevents duplicate profile for one user', async (t) => {
|
||
|
|
if (!dbAvailable) {
|
||
|
|
t.skip('MySQL is not running');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const user = await prisma.user.create({
|
||
|
|
data: {
|
||
|
|
profile: {
|
||
|
|
create: {
|
||
|
|
name: 'First Profile',
|
||
|
|
age: 30,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
createdUserIds.push(user.id);
|
||
|
|
|
||
|
|
await assert.rejects(
|
||
|
|
() =>
|
||
|
|
prisma.userProfile.create({
|
||
|
|
data: {
|
||
|
|
userId: user.id,
|
||
|
|
name: 'Duplicate Profile',
|
||
|
|
age: 31,
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
(err: unknown) => {
|
||
|
|
assert.ok(err instanceof Prisma.PrismaClientKnownRequestError);
|
||
|
|
assert.equal(err.code, 'P2002');
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
);
|
||
|
|
});
|
||
|
|
});
|