Skip to content

Commit

Permalink
Add testing draft
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Jan 9, 2022
1 parent 85a86bc commit 2aeb030
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 125 deletions.
85 changes: 0 additions & 85 deletions docs/drafts.md

This file was deleted.

8 changes: 5 additions & 3 deletions krab/action_migrate_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ func (a *ActionMigrateDown) Run(args []string) int {
}

resp, err := a.Cmd.Do(context.Background(), CmdOpts{Inputs: flags.Values()})
result := resp.([]ResponseMigrateDown)
result, ok := resp.([]ResponseMigrateDown)

if err != nil {
a.Ui.Error(err.Error())
return 1
}

for _, status := range result {
uiMigrationStatusFromResponseDown(a.Ui, status)
if ok {
for _, status := range result {
uiMigrationStatusFromResponseDown(a.Ui, status)
}
}

return 0
Expand Down
7 changes: 4 additions & 3 deletions krab/action_migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ func (a *ActionMigrateUp) Run(args []string) int {
}

resp, err := a.Cmd.Do(context.Background(), CmdOpts{Inputs: flags.Values()})
result := resp.([]ResponseMigrateUp)

if len(result) > 0 {
result, ok := resp.([]ResponseMigrateUp)

if ok && len(result) > 0 {
for _, status := range result {
uiMigrationStatusFromResponseUp(ui, status)
}
Expand All @@ -64,7 +65,7 @@ func (a *ActionMigrateUp) Run(args []string) int {
return 1
}

if len(result) == 0 {
if ok && len(result) == 0 {
ui.Info("No pending migrations")
}

Expand Down
28 changes: 28 additions & 0 deletions krab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Config struct {
Migrations map[string]*Migration
Actions map[string]*Action
Wasms map[string]*WebAssembly
TestSuites map[string]*TestSuite
TestExamples map[string]*TestExample
}

// NewConfig returns new configuration that was read from Parser.
Expand All @@ -23,6 +25,8 @@ func NewConfig(files []*File) (*Config, error) {
Migrations: map[string]*Migration{},
Actions: map[string]*Action{},
Wasms: map[string]*WebAssembly{},
TestSuites: map[string]*TestSuite{},
TestExamples: map[string]*TestExample{},
}

// append files
Expand Down Expand Up @@ -71,6 +75,30 @@ func NewConfig(files []*File) (*Config, error) {
}
}

for _, validatable := range c.Actions {
if err := validatable.Validate(); err != nil {
return nil, err
}
}

for _, validatable := range c.TestSuites {
if err := validatable.Validate(); err != nil {
return nil, err
}
}

for _, validatable := range c.TestExamples {
if err := validatable.Validate(); err != nil {
return nil, err
}
}

for _, validatable := range c.Wasms {
if err := validatable.Validate(); err != nil {
return nil, err
}
}

return c, nil
}

Expand Down
2 changes: 2 additions & 0 deletions krab/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type File struct {
Migrations []*Migration `hcl:"migration,block"`
MigrationSets []*MigrationSet `hcl:"migration_set,block"`
Actions []*Action `hcl:"action,block"`
TestSuites []*TestSuite `hcl:"test_suite,block"`
TestExamples []*TestExample `hcl:"test,block"`
Wasms []*WebAssembly `hcl:"wasm,block"`

Raw *RawFile
Expand Down
13 changes: 13 additions & 0 deletions krab/subtype_do.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package krab

import (
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
)

// Do subtype for other types.
type Do struct {
Action hcl.Expression `hcl:"action,optional"`
Inputs map[string]cty.Value `hcl:"inputs,optional"`
SQL string `hcl:"sql,optional"`
}
48 changes: 48 additions & 0 deletions krab/type_test_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package krab

import (
"github.com/ohkrab/krab/krabhcl"
)

// TestExample represents test runner configuration.
//
type TestExample struct {
TestSuiteRefName string `hcl:"test_suite,label"`
Name string `hcl:"name,label"`
Its []*TestExampleIt `hcl:"it,block"`
}

func (t *TestExample) Addr() krabhcl.Addr {
return krabhcl.Addr{Keyword: "test", Labels: []string{t.TestSuiteRefName, t.Name}}
}

func (t *TestExample) Validate() error {
return ErrorCoalesce(
ValidateRefName(t.TestSuiteRefName),
)
}

// TestExampleIt represents one use case for test example that contain assertions.
type TestExampleIt struct {
Comment string `hcl:"comment,label"`
Do *Do `hcl:"do,block"`
RowAsserts []*AssertRow `hcl:"row,block"`
RowsAsserts []*AssertRows `hcl:"rows,block"`
}

// AssertRows
type AssertRows struct {
Expectations []*Expect `hcl:"expect,block"`
}

// AssertRow
type AssertRow struct {
Scope string `hcl:"scope,label"`
Expectations []*Expect `hcl:"expect,block"`
}

// Expect
type Expect struct {
Subject string `hcl:"subject,label"`
Equal *string `hcl:"equal,optional"`
}
26 changes: 26 additions & 0 deletions krab/type_test_suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package krab

import (
"github.com/ohkrab/krab/krabhcl"
)

// TestSuite represents test runner configuration.
//
type TestSuite struct {
RefName string `hcl:"ref_name,label"`
Before []*TestSuiteBefore `hcl:"before,block"`
}

func (t *TestSuite) Addr() krabhcl.Addr {
return krabhcl.Addr{Keyword: "test_suite", Labels: []string{t.RefName}}
}

func (t *TestSuite) Validate() error {
return ErrorCoalesce(
ValidateRefName(t.RefName),
)
}

type TestSuiteBefore struct {
Dos []*Do `hcl:"do,block"`
}
2 changes: 1 addition & 1 deletion krabwasm/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New() *Runtime {
store := wasmer.NewStore(engine)

return &Runtime{
instances: map[string]*wasm{},
instances: map[string]*webAssembly{},
store: store,
}
}
Expand Down
44 changes: 44 additions & 0 deletions test/fixtures/args/migrations.krab.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
migration "create_animals" {
version = "v1"

up { sql = "CREATE TABLE animals(name VARCHAR)" }
down { sql = "DROP TABLE animals" }
}

migration "create_animals_view" {
version = "v2"

up { sql = "CREATE MATERIALIZED VIEW anims AS SELECT name FROM animals" }
down { sql = "DROP MATERIALIZED VIEW anims" }
}

migration "seed_animals" {
version = "v3"

up { sql = "INSERT INTO animals(name) VALUES('Elephant'),('Turtle'),('Cat')" }
down { sql = "TRUNCATE animals" }
}

migration_set "animals" {
arguments {
arg "name" {
description = "Materialized view to be refreshed"
}
}

migrations = [
migration.create_animals,
migration.create_animals_view,
migration.seed_animals,
]
}

action "view" "refresh" {
arguments {
arg "name" {
description = "Materialized view to be refreshed"
}
}

sql = "REFRESH MATERIALIZED VIEW {{ .Args.name | quote_ident }}"
}
24 changes: 0 additions & 24 deletions test/fixtures/connections.krab.hcl

This file was deleted.

29 changes: 20 additions & 9 deletions test/fixtures/ideas/connections.krab.hcl
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
connection "main" {
uri = "postgres://localhost:5432/krab_test"
globals {
uri = "global_var"
}

connection "default" {
uri = "postgres://krab:secret@localhost:5432/postgres"
}

connection "interpolated" {
uri = "postgres://${env("USER")}:${env("PASSWORD")}@localhost:5432/postgres"
}

connection "referenced" {
uri = global.uri
}

connection "duplicated" {
uri = connection.default.uri
}

# TODO: later
# connection_pool "main" {
# connections = [
# connection.main,
# ]
connection "from_env" {
uri = env("PG_URI")
}

# pool = 5
# }
File renamed without changes.
Loading

0 comments on commit 2aeb030

Please sign in to comment.