|
| 1 | +import { sql } from 'kysely'; |
| 2 | +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'; |
| 3 | +import { db } from './database'; |
| 4 | +import * as PersonRepository from './person-repository'; |
| 5 | + |
| 6 | +describe('person-repository', () => { |
| 7 | + beforeEach(async () => { |
| 8 | + await db |
| 9 | + .insertInto('person') |
| 10 | + .values({ |
| 11 | + id: 123, |
| 12 | + first_name: 'Arnold', |
| 13 | + last_name: 'Schwarzenegger', |
| 14 | + gender: 'other', |
| 15 | + }) |
| 16 | + .executeTakeFirstOrThrow(); |
| 17 | + }); |
| 18 | + |
| 19 | + beforeAll(async () => { |
| 20 | + await db.schema |
| 21 | + .createTable('person') |
| 22 | + .addColumn('id', 'integer', (cb) => cb.primaryKey().autoIncrement().notNull()) |
| 23 | + .addColumn('first_name', 'varchar(255)', (cb) => cb.notNull()) |
| 24 | + .addColumn('last_name', 'varchar(255)') |
| 25 | + .addColumn('gender', 'varchar(50)', (cb) => cb.notNull()) |
| 26 | + .addColumn('created_at', 'timestamp', (cb) => cb.notNull().defaultTo(sql`current_timestamp`)) |
| 27 | + .execute(); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(async () => { |
| 31 | + await sql`delete from ${sql.table('person')}`.execute(db); |
| 32 | + }); |
| 33 | + |
| 34 | + afterAll(async () => { |
| 35 | + await db.schema.dropTable('person').execute(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should find a person with a given id', async () => { |
| 39 | + expect(await PersonRepository.findPersonById(123)).toMatchObject({ |
| 40 | + id: 123, |
| 41 | + first_name: 'Arnold', |
| 42 | + last_name: 'Schwarzenegger', |
| 43 | + gender: 'other', |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should find all people named Arnold', async () => { |
| 48 | + const people = await PersonRepository.findPeople({ first_name: 'Arnold' }); |
| 49 | + |
| 50 | + expect(people).toHaveLength(1); |
| 51 | + expect(people[0]).toMatchObject({ |
| 52 | + id: 123, |
| 53 | + first_name: 'Arnold', |
| 54 | + last_name: 'Schwarzenegger', |
| 55 | + gender: 'other', |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should update gender of a person with a given id', async () => { |
| 60 | + await PersonRepository.updatePerson(123, { gender: 'woman' }); |
| 61 | + |
| 62 | + expect(await PersonRepository.findPersonById(123)).toMatchObject({ |
| 63 | + id: 123, |
| 64 | + first_name: 'Arnold', |
| 65 | + last_name: 'Schwarzenegger', |
| 66 | + gender: 'woman', |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should create a person', async () => { |
| 71 | + await PersonRepository.createPerson({ |
| 72 | + first_name: 'Jennifer', |
| 73 | + last_name: 'Aniston', |
| 74 | + gender: 'woman', |
| 75 | + }); |
| 76 | + |
| 77 | + expect(await PersonRepository.findPeople({ first_name: 'Jennifer' })).toHaveLength(1); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should create multiple persons', async () => { |
| 81 | + const created = await PersonRepository.createPersons([ |
| 82 | + { first_name: 'Brad', last_name: 'Pitt', gender: 'man' }, |
| 83 | + { first_name: 'Angelina', last_name: 'Jolie', gender: 'woman' }, |
| 84 | + ]); |
| 85 | + await expect(PersonRepository.findPeople({ first_name: 'Brad' })).resolves.toBeTruthy(); |
| 86 | + await expect(PersonRepository.findPeople({ first_name: 'Angelina' })).resolves.toBeTruthy(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should delete a person with a given id', async () => { |
| 90 | + await PersonRepository.deletePerson(123); |
| 91 | + |
| 92 | + expect(await PersonRepository.findPersonById(123)).toBeUndefined(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments