2020-01-18 15:36:16 -09:00
package checklist
2019-06-24 04:26:48 -08:00
import (
2019-08-14 11:40:26 -08:00
"os"
"testing"
2019-07-13 12:16:28 -08:00
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
"geeks-accelerator/oss/saas-starter-kit/internal/platform/tests"
2019-06-24 04:26:48 -08:00
"github.com/google/go-cmp/cmp"
"github.com/huandu/go-sqlbuilder"
)
2019-08-14 11:40:26 -08:00
var (
test * tests . Test
repo * Repository
)
2019-06-24 04:26:48 -08:00
// TestMain is the entry point for testing.
func TestMain ( m * testing . M ) {
os . Exit ( testMain ( m ) )
}
func testMain ( m * testing . M ) int {
test = tests . New ( )
defer test . TearDown ( )
2019-08-14 11:40:26 -08:00
repo = NewRepository ( test . MasterDB )
2019-06-24 04:26:48 -08:00
return m . Run ( )
}
// TestFindRequestQuery validates findRequestQuery
func TestFindRequestQuery ( t * testing . T ) {
2019-08-05 17:12:28 -08:00
2019-06-24 04:26:48 -08:00
var (
limit uint = 12
offset uint = 34
)
2020-01-18 15:36:16 -09:00
req := ChecklistFindRequest {
2019-08-05 17:12:28 -08:00
Where : "field1 = ? or field2 = ?" ,
2019-06-24 04:26:48 -08:00
Args : [ ] interface { } {
"lee brown" ,
"103 East Main St." ,
} ,
Order : [ ] string {
"id asc" ,
"created_at desc" ,
} ,
Limit : & limit ,
Offset : & offset ,
}
2020-01-18 15:36:16 -09:00
expected := "SELECT " + checklistMapColumns + " FROM " + checklistTableName + " WHERE (field1 = ? or field2 = ?) ORDER BY id asc, created_at desc LIMIT 12 OFFSET 34"
2019-06-24 04:26:48 -08:00
res , args := findRequestQuery ( req )
if diff := cmp . Diff ( res . String ( ) , expected ) ; diff != "" {
t . Fatalf ( "\t%s\tExpected result query to match. Diff:\n%s" , tests . Failed , diff )
}
if diff := cmp . Diff ( args , req . Args ) ; diff != "" {
t . Fatalf ( "\t%s\tExpected result query to match. Diff:\n%s" , tests . Failed , diff )
}
}
// TestApplyClaimsSelect applyClaimsSelect
func TestApplyClaimsSelect ( t * testing . T ) {
var claimTests = [ ] struct {
name string
claims auth . Claims
expectedSql string
error error
} { }
t . Log ( "Given the need to validate ACLs are enforced by claims to a select query." )
{
for i , tt := range claimTests {
t . Logf ( "\tTest: %d\tWhen running test: %s" , i , tt . name )
{
ctx := tests . Context ( )
query := selectQuery ( )
err := applyClaimsSelect ( ctx , tt . claims , query )
if err != tt . error {
t . Logf ( "\t\tGot : %+v" , err )
t . Logf ( "\t\tWant: %+v" , tt . error )
t . Fatalf ( "\t%s\tapplyClaimsSelect failed." , tests . Failed )
}
sql , args := query . Build ( )
// Use mysql flavor so placeholders will get replaced for comparison.
sql , err = sqlbuilder . MySQL . Interpolate ( sql , args )
if err != nil {
t . Log ( "\t\tGot :" , err )
t . Fatalf ( "\t%s\tapplyClaimsSelect failed." , tests . Failed )
}
if diff := cmp . Diff ( sql , tt . expectedSql ) ; diff != "" {
t . Fatalf ( "\t%s\tExpected result query to match. Diff:\n%s" , tests . Failed , diff )
}
t . Logf ( "\t%s\tapplyClaimsSelect ok." , tests . Success )
}
}
}
}