@@ -2,9 +2,12 @@ package psql
22
33import (
44 "context"
5+ "database/sql"
6+ "errors"
57 "testing"
68
79 "github.com/stephenafamo/bob"
10+ "github.com/stephenafamo/bob/dialect/psql/dialect"
811 "github.com/stephenafamo/bob/dialect/psql/sm"
912 "github.com/stephenafamo/bob/expr"
1013)
@@ -74,6 +77,14 @@ func TestImmutableViewQueryWithDoesNotMutateOriginal(t *testing.T) {
7477 }
7578}
7679
80+ func TestViewQueryWithNilReceiver (t * testing.T ) {
81+ var q * ViewQuery [* someStruct , []* someStruct ]
82+
83+ if got := q .With (sm .Where (Quote ("id" ).EQ (Arg (1 )))); got != nil {
84+ t .Fatalf ("expected nil view query, got %#v" , got )
85+ }
86+ }
87+
7788func TestImmutableSelectQueryApplyDoesNotMutateOriginal (t * testing.T ) {
7889 base := Select (
7990 sm .Columns ("id" ),
@@ -243,8 +254,12 @@ func TestViewSelectQueryHooksUseImmutableSelectQuery(t *testing.T) {
243254 view := NewView [* someStruct , bob.Expression ]("public" , "some_struct" , expr.ColsForStruct [someStruct ]("some_struct" ))
244255
245256 var hookSQL string
246- view .SelectQueryHooks .AppendHooks (func (ctx context.Context , exec bob.Executor , q * SelectQuery ) (context.Context , error ) {
247- sql , _ , err := q .Build (ctx )
257+ view .SelectQueryHooks .AppendHooks (func (ctx context.Context , exec bob.Executor , q * dialect.SelectQuery ) (context.Context , error ) {
258+ sql , _ , err := bob.BaseQuery [* dialect.SelectQuery ]{
259+ Expression : q ,
260+ Dialect : dialect .Dialect ,
261+ QueryType : bob .QueryTypeSelect ,
262+ }.Build (ctx )
248263 if err != nil {
249264 return ctx , err
250265 }
@@ -272,6 +287,19 @@ func TestViewSelectQueryHooksUseImmutableSelectQuery(t *testing.T) {
272287 }
273288}
274289
290+ func TestImmutableSelectQueryBuildReturnsErrNoNamedArgs (t * testing.T ) {
291+ _ , _ , err := Select (
292+ sm .Columns (sql .Named ("id" , 1 )),
293+ sm .From ("users" ),
294+ ).Build (t .Context ())
295+ if err == nil {
296+ t .Fatal ("expected named arg error" )
297+ }
298+ if ! errors .Is (err , bob .ErrNoNamedArgs ) {
299+ t .Fatalf ("expected ErrNoNamedArgs, got %v" , err )
300+ }
301+ }
302+
275303func TestImmutableSelectQueryApplySupportsCommonDerivedMods (t * testing.T ) {
276304 base := Select (
277305 sm .Columns ("users.id" , "users.name" ),
0 commit comments