Skip to content

Commit

Permalink
feat: integration tests for postgres and mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Jan 12, 2021
1 parent e33a23b commit 345fe8d
Show file tree
Hide file tree
Showing 50 changed files with 6,369 additions and 318 deletions.
59 changes: 0 additions & 59 deletions core/api_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion core/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"

"github.com/dosco/graphjin/core/internal/psql"
"github.com/dosco/graphjin/jsn"
"github.com/dosco/graphjin/internal/jsn"
)

// argList function is used to create a list of arguments to pass
Expand Down
4 changes: 2 additions & 2 deletions core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (gj *GraphJin) buildRoleStmt(cq *cquery, role string) error {

if len(vars) != 0 {
if err := json.Unmarshal(vars, &vm); err != nil {
return err
return fmt.Errorf("variables: %w", err)
}
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func (gj *GraphJin) buildMultiStmt(cq *cquery) error {

if len(vars) != 0 {
if err := json.Unmarshal(vars, &vm); err != nil {
return err
return fmt.Errorf("variables: %w", err)
}
}

Expand Down
214 changes: 189 additions & 25 deletions core/core_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,199 @@
package core
package core_test

/*
import (
"database/sql"
"fmt"
"os"
"testing"

func simpleMutation(t *testing.T) {
gql := `mutation {
product(id: 15, insert: { name: "Test", price: 20.5 }) {
id
name
}
}`
"github.com/orlangure/gnomock"
"github.com/orlangure/gnomock/preset/mysql"
"github.com/orlangure/gnomock/preset/postgres"
)

type dbinfo struct {
name string
driver string
connstr string
disable bool
preset gnomock.Preset
}

sql := `test`
var (
dbType string
db *sql.DB
)

backgroundCtx := context.Background()
ctx := &coreContext{Context: backgroundCtx}
func TestMain(m *testing.M) {
// var err error

resSQL, err := compileGQLToPSQL(gql)
if err != nil {
t.Fatal(err)
}
// schema, err := sdata.GetTestSchema()
// if err != nil {
// log.Fatal(err)
// }

// t, err := schema.GetTableInfo("customers", "")
// if err != nil {
// log.Fatal(err)
// }

// remoteVal := sdata.DBRel{Type: sdata.RelRemote}
// remoteVal.Left.Col = t.PrimaryCol
// remoteVal.Right.VTable = fmt.Sprintf("__%s_%s", t.Name, t.PrimaryCol.Name)

// if err := schema.SetRel("payments", "customers", remoteVal); err != nil {
// log.Fatal(err)
// }

// qcompile, err = qcode.NewCompiler(schema, qcode.Config{})
// if err != nil {
// log.Fatal(err)
// }

// err = qcompile.AddRole("user", "product", qcode.TRConfig{
// Query: qcode.QueryConfig{
// Columns: []string{"id", "name", "price", "users", "customers"},
// Filters: []string{
// "{ price: { gt: 0 } }",
// "{ price: { lt: 8 } }",
// },
// },
// Insert: qcode.InsertConfig{
// Presets: map[string]string{
// "price": "$get_price",
// "user_id": "$user_id",
// "created_at": "now",
// "updated_at": "now",
// },
// },
// Update: qcode.UpdateConfig{
// Filters: []string{"{ user_id: { eq: $user_id } }"},
// Presets: map[string]string{"updated_at": "now"},
// },
// Delete: qcode.DeleteConfig{
// Filters: []string{
// "{ price: { gt: 0 } }",
// "{ price: { lt: 8 } }",
// },
// },
// })
// if err != nil {
// log.Fatal(err)
// }

// err = qcompile.AddRole("anon", "product", qcode.TRConfig{
// Query: qcode.QueryConfig{
// Columns: []string{"id", "name"},
// },
// })
// if err != nil {
// log.Fatal(err)
// }

// err = qcompile.AddRole("anon1", "product", qcode.TRConfig{
// Query: qcode.QueryConfig{
// Columns: []string{"id", "name", "price"},
// DisableFunctions: true,
// },
// })
// if err != nil {
// log.Fatal(err)
// }

// err = qcompile.AddRole("user", "users", qcode.TRConfig{
// Query: qcode.QueryConfig{
// Columns: []string{"id", "full_name", "avatar", "email", "products"},
// },
// })
// if err != nil {
// log.Fatal(err)
// }

// err = qcompile.AddRole("bad_dude", "users", qcode.TRConfig{
// Query: qcode.QueryConfig{
// Filters: []string{"false"},
// DisableFunctions: true,
// },
// Insert: qcode.InsertConfig{},
// Update: qcode.UpdateConfig{
// Filters: []string{"false"},
// },
// })
// if err != nil {
// log.Fatal(err)
// }

fmt.Println(">", string(resSQL))
// err = qcompile.AddRole("user", "mes", qcode.TRConfig{
// Query: qcode.QueryConfig{
// Columns: []string{"id", "full_name", "avatar", "email"},
// Filters: []string{
// "{ id: { eq: $user_id } }",
// },
// },
// })
// if err != nil {
// log.Fatal(err)
// }

if string(resSQL) != sql {
t.Fatal(errNotExpected)
// err = qcompile.AddRole("user", "customers", qcode.TRConfig{
// Query: qcode.QueryConfig{
// Columns: []string{"id", "email", "full_name", "products"},
// },
// })

// if err != nil {
// log.Fatal(err)
// }

// vars := map[string]string{
// "admin_account_id": "5",
// "get_price": "sql:select price from prices where id = $product_id",
// }

// pcompile = psql.NewCompiler(psql.Config{
// Vars: vars,
// })

dbinfoList := []dbinfo{
{
name: "postgres",
driver: "postgres",
connstr: "postgres://tester:tester@%s/db?sslmode=disable",
preset: postgres.Preset(
postgres.WithUser("tester", "tester"),
postgres.WithDatabase("db"),
postgres.WithQueriesFile("./postgres.sql"),
)},
{
disable: true,
name: "mysql",
driver: "mysql",
connstr: "user:user@tcp(%s)/db",
preset: mysql.Preset(
mysql.WithUser("user", "user"),
mysql.WithDatabase("db"),
mysql.WithQueriesFile("./mysql.sql"),
)},
}
}

func TestCompileGQL(t *testing.T) {
t.Run("withComplexArgs", withComplexArgs)
t.Run("simpleMutation", simpleMutation)
}
for _, v := range dbinfoList {
if v.disable {
continue
}
con, err := gnomock.Start(v.preset)
if err != nil {
panic(err)
}
defer func() { _ = gnomock.Stop(con) }()

db, err = sql.Open(v.driver, fmt.Sprintf(v.connstr, con.DefaultAddress()))
if err != nil {
panic(err)
}
dbType = v.name

*/
if res := m.Run(); res != 0 {
os.Exit(res)
}
}
}
2 changes: 1 addition & 1 deletion core/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/dosco/graphjin/core/internal/crypto"
"github.com/dosco/graphjin/core/internal/qcode"
"github.com/dosco/graphjin/jsn"
"github.com/dosco/graphjin/internal/jsn"
)

type cursors struct {
Expand Down
5 changes: 4 additions & 1 deletion core/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (gj *GraphJin) initConfig() error {
if !gj.abacEnabled {
gj.log.Printf("WRN attribute based access control disabled: no custom roles (with 'match' defined)")
}
} else {
} else if len(gj.roles) > 2 {
gj.log.Printf("INF attribute based access control disabled: roles_query not set")
}

Expand Down Expand Up @@ -164,6 +164,9 @@ func addTables(c *Config, di *sdata.DBInfo) error {

func addJsonTable(di *sdata.DBInfo, cols []Column, t Table) error {
// This is for jsonb columns that want to be tables.
if t.Table == "" {
return fmt.Errorf("json table: set the 'table' for column '%s'", t.Name)
}
bc, err := di.GetColumn(t.Table, t.Name)
if err != nil {
return fmt.Errorf("json table: %w", err)
Expand Down
Loading

0 comments on commit 345fe8d

Please sign in to comment.