Skip to content

Commit

Permalink
TEST: send comment tests to real database (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorchu authored Oct 10, 2019
1 parent 43dcde7 commit 2b0519c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
24 changes: 18 additions & 6 deletions comments_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dbr

import (
"fmt"
"testing"

"github.com/gocraft/dbr/v2/dialect"
"github.com/stretchr/testify/require"
)

func TestComments(t *testing.T) {
dialects := []Dialect{dialect.MySQL, dialect.PostgreSQL, dialect.SQLite3}
for _, test := range []struct {
name string
comments Comments
Expand All @@ -31,22 +31,34 @@ func TestComments(t *testing.T) {
},
} {

for _, d := range dialects {
for _, sess := range testSession {
name := ""
switch d {
switch sess.Dialect {
case dialect.MySQL:
name = "MySQL"
case dialect.PostgreSQL:
name = "PostgreSQL"
case dialect.SQLite3:
name = "SQLite3"
}
t.Run(name+" "+test.name, func(t *testing.T) {
t.Run(fmt.Sprintf("%s/%s", name, test.name), func(t *testing.T) {
buf := NewBuffer()
test.comments.Build(d, buf)
err := test.comments.Build(sess.Dialect, buf)
require.NoError(t, err)
require.Equal(t, test.expect, buf.String())

stmt := sess.Select("1")
stmt.comments = test.comments

buf2 := NewBuffer()
err = stmt.Build(sess.Dialect, buf2)
require.NoError(t, err)
require.Equal(t, test.expect+"SELECT 1", buf2.String())

one, err := stmt.ReturnInt64()
require.NoError(t, err)
require.EqualValues(t, 1, one)
})
}
}

}
5 changes: 4 additions & 1 deletion delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func (b *DeleteStmt) Build(d Dialect, buf Buffer) error {
return ErrTableNotSpecified
}

b.comments.Build(d, buf)
err := b.comments.Build(d, buf)
if err != nil {
return err
}

buf.WriteString("DELETE FROM ")
buf.WriteString(d.QuoteIdent(b.Table))
Expand Down
5 changes: 4 additions & 1 deletion insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func (b *InsertStmt) Build(d Dialect, buf Buffer) error {
return ErrColumnNotSpecified
}

b.comments.Build(d, buf)
err := b.comments.Build(d, buf)
if err != nil {
return err
}

buf.WriteString("INSERT INTO ")
buf.WriteString(d.QuoteIdent(b.Table))
Expand Down
5 changes: 4 additions & 1 deletion select.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func (b *SelectStmt) Build(d Dialect, buf Buffer) error {
return ErrColumnNotSpecified
}

b.comments.Build(d, buf)
err := b.comments.Build(d, buf)
if err != nil {
return err
}

buf.WriteString("SELECT ")

Expand Down
5 changes: 4 additions & 1 deletion update.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func (b *UpdateStmt) Build(d Dialect, buf Buffer) error {
return ErrColumnNotSpecified
}

b.comments.Build(d, buf)
err := b.comments.Build(d, buf)
if err != nil {
return err
}

buf.WriteString("UPDATE ")
buf.WriteString(d.QuoteIdent(b.Table))
Expand Down

0 comments on commit 2b0519c

Please sign in to comment.