@@ -84,6 +84,30 @@ func QueryJoin(t *testing.T, repo rel.Repository) {
8484 run (t , repo , tests )
8585}
8686
87+ // QueryJoinAssoc tests query specifications with join.
88+ func QueryJoinAssoc (t * testing.T , repo rel.Repository ) {
89+ dbUser := User {Addresses : []Address {{}}}
90+ repo .MustInsert (ctx , & dbUser )
91+
92+ waitForReplication ()
93+
94+ t .Run ("HasMany" , func (t * testing.T ) {
95+ assert .Nil (t , repo .Find (ctx , & User {}, rel .JoinAssoc ("addresses" ).Where (where .Eq ("id" , dbUser .ID ))))
96+ })
97+
98+ t .Run ("HasOne" , func (t * testing.T ) {
99+ var user User
100+ assert .Nil (t , repo .Find (ctx , & user , rel .Select ("*" , "primary_address.*" ).JoinAssoc ("primary_address" ).Where (where .Eq ("id" , dbUser .ID ))))
101+ assert .Equal (t , dbUser .Addresses [0 ].ID , user .PrimaryAddress .ID )
102+ })
103+
104+ t .Run ("BelongsTo" , func (t * testing.T ) {
105+ var address Address
106+ assert .Nil (t , repo .Find (ctx , & address , rel .Select ("*" , "user.*" ).JoinAssoc ("user" ).Where (where .Eq ("id" , dbUser .Addresses [0 ].ID ))))
107+ assert .Equal (t , dbUser .ID , address .User .ID )
108+ })
109+ }
110+
87111// Query tests query specifications without join.
88112func QueryWhereSubQuery (t * testing.T , repo rel.Repository , flags ... Flag ) {
89113 tests := []rel.Querier {
0 commit comments