1- import { reset , db } from "./harness" ;
1+ import { migrate , db , reset } from "./harness" ;
22
33jest . setTimeout ( 1000 * 60 ) ;
44
5+ beforeAll ( async ( ) => {
6+ await migrate ( ) ;
7+ } ) ;
8+
59beforeEach ( async ( ) => {
610 await reset ( ) ;
711} ) ;
812
9- const person = {
13+ const PERSON = {
1014 gender : "male" ,
1115 first_name : "jeff" ,
1216 last_name : "bezos" ,
@@ -17,13 +21,43 @@ it("insert and read", async () => {
1721 . insertInto ( "person" )
1822 . values ( {
1923 id : db . generated ,
20- ...person ,
24+ ...PERSON ,
2125 } )
2226 . execute ( ) ;
2327
2428 const result = await db . selectFrom ( "person" ) . selectAll ( ) . execute ( ) ;
2529 expect ( result ) . toHaveLength ( 1 ) ;
26- expect ( result [ 0 ] ) . toMatchObject ( person ) ;
30+ expect ( result [ 0 ] ) . toMatchObject ( PERSON ) ;
31+ } ) ;
32+
33+ it ( "join" , async ( ) => {
34+ const person = await db
35+ . insertInto ( "person" )
36+ . values ( {
37+ id : db . generated ,
38+ ...PERSON ,
39+ } )
40+ . returning ( [ "id" ] )
41+ . executeTakeFirst ( ) ;
42+
43+ await db
44+ . insertInto ( "pet" )
45+ . values ( {
46+ id : db . generated ,
47+ name : "fido" ,
48+ species : "dog" ,
49+ owner_id : person . id ,
50+ } )
51+ . execute ( ) ;
52+
53+ const result = await db
54+ . selectFrom ( "person" )
55+ . innerJoin ( "pet" , "pet.owner_id" , "person.id" )
56+ . select ( [ "pet.name as pet_name" , "person.first_name" ] )
57+ . executeTakeFirst ( ) ;
58+
59+ expect ( result . first_name ) . toEqual ( "jeff" ) ;
60+ expect ( result . pet_name ) . toEqual ( "fido" ) ;
2761} ) ;
2862
2963it ( "transaction" , async ( ) => {
@@ -32,12 +66,12 @@ it("transaction", async () => {
3266 . insertInto ( "person" )
3367 . values ( {
3468 id : db . generated ,
35- ...person ,
69+ ...PERSON ,
3670 } )
3771 . execute ( ) ;
3872 } ) ;
3973
4074 const result = await db . selectFrom ( "person" ) . selectAll ( ) . execute ( ) ;
4175 expect ( result ) . toHaveLength ( 1 ) ;
42- expect ( result [ 0 ] ) . toMatchObject ( person ) ;
76+ expect ( result [ 0 ] ) . toMatchObject ( PERSON ) ;
4377} ) ;
0 commit comments