File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ import {
2+ setupTestTables ,
3+ cleanupTestData ,
4+ executor ,
5+ DomainUser ,
6+ } from "../../test-setup" ;
7+ import { Repository } from "../../src/repository/repository" ;
8+
9+ describe ( "Repository - insertOne" , ( ) => {
10+ let repository : Repository < DomainUser > ;
11+
12+ beforeAll ( async ( ) => {
13+ await setupTestTables ( ) ;
14+ repository = new Repository ( "users" , executor ) ;
15+ } ) ;
16+
17+ afterEach ( async ( ) => {
18+ await cleanupTestData ( ) ;
19+ } ) ;
20+
21+ it ( "should insert a single record into the database" , async ( ) => {
22+ const record = {
23+ name : "John Doe" ,
24+ email : "john@example.com" ,
25+ age : 30 ,
26+ } ;
27+ const result = await repository . insertOne ( record ) ;
28+
29+ expect ( result ) . toMatchObject ( {
30+ name : "John Doe" ,
31+ email : "john@example.com" ,
32+ age : 30 ,
33+ } ) ;
34+
35+ const insertedRecord = await executor . executeSQL (
36+ "SELECT * FROM users WHERE email = $1" ,
37+ [ "john@example.com" ]
38+ ) ;
39+
40+ expect ( insertedRecord . rows [ 0 ] ) . toMatchObject ( record ) ;
41+ } ) ;
42+
43+ it ( "should throw an error if the record violates constraints" , async ( ) => {
44+ const record = { name : null , email : "invalid@example.com" } ; // Assuming 'name' cannot be null
45+
46+ await expect ( repository . insertOne ( record ) ) . rejects . toThrow ( ) ;
47+ } ) ;
48+ } ) ;
You can’t perform that action at this time.
0 commit comments