Skip to content

Commit

Permalink
Add test2 proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Mar 5, 2023
1 parent dabea11 commit a6b0dfe
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 71 deletions.
2 changes: 1 addition & 1 deletion krab/type_test_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type TestExample struct {
TestSuiteRefName string `hcl:"test_suite,label"`
Name string `hcl:"name,label"`
Set *SetRuntimeParameters `hcl:"set,block"`
// Set *SetRuntimeParameters `hcl:"set,block"`
Its []*TestExampleIt `hcl:"it,block"`
}

Expand Down
24 changes: 2 additions & 22 deletions krab/type_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,14 @@ import (
)

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

Tests []*TestExample
Tests []*TestExample
}

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

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

type TestSuiteBefore struct {
Dos []*Do `hcl:"do,block"`
}

func (t *TestSuiteBefore) Validate() error {
for _, do := range t.Dos {
if err := do.Validate(); err != nil {
return err
}
}
return nil
}
8 changes: 2 additions & 6 deletions test/fixtures/tests/versions.krab.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ migration "create_version_function" {
BEGIN
_v := _ver;
CASE
CASE
WHEN _type = 'major' THEN
_v.major = _v.major + 1;
_v.minor = 0;
Expand Down Expand Up @@ -58,11 +58,7 @@ migration "create_version_function" {
}

migration_set "versions" {
arguments {
arg "schema" {}
}

schema = "{{.Args.schema}}"
schema = "public"

migrations = [
migration.create_version_type,
Expand Down
48 changes: 6 additions & 42 deletions test/fixtures/tests/versions_test.krab.hcl
Original file line number Diff line number Diff line change
@@ -1,80 +1,44 @@
test_suite "versions" {
before {
do {
inputs = {
schema = "testing"
}

migrate "up" { migration_set = migration_set.versions }
migrate "down" {
migration_set = migration_set.versions
inputs = { version = "v2" }
}
migrate "up" { migration_set = migration_set.versions }
}
}
}

test "versions" "version_inc()" {
set {
search_path = "testing"
}

it "increases `major` component by default when no type specified" {
do {
sql = "SELECT version_inc(row(1,1,1)::sem_version) AS ver"
}

row "0" {
column "ver" { assert = "(2,0,0)" }
col "must be 2.0.0" { assert = "ver = row(2,0,0)::sem_version" }
}
}

it "increases `major` component and resets `minor` and `patch`" {
do { sql = "SELECT version_inc(row(1,1,1)::sem_version, 'major') AS ver" }

row "0" {
column "ver" { assert = "ver = row(2,0,0)::sem_version" }
col "must be 2.0.0" { assert = "ver = row(2,0,0)::sem_version" }
}
}

describe "increases `minor` component and resets `patch` leaving `major` untouched" {
it "increases `minor` component and resets `patch` leaving `major` untouched" {
do { sql = "SELECT version_inc(row(1,1,1)::sem_version, 'minor') AS ver" }

# v1 - set scope
row "0" {
it "ver" { expect = "ver = row(2,0,0)::sem_version" }
its { expect = "ver = row(2,0,0)::sem_version" }
col "must be 1.2.0" { assert = "ver = row(1,2,0)::sem_version" }
}

# v2
it "0" "ver" { expect = "ver = row(2,0,0)::sem_version" }
its "0" { expect = "ver = row(2,0,0)::sem_version" }
}

it "increases `patch` component and leaves `major` and `minor` untouched" {
do { sql = "SELECT version_inc(row(1,1,1)::sem_version, 'patch') AS ver" }

row "0" {
column "ver" { assert = "(1,1,2)" }
col "must be 1.1.2" { assert = "ver = row(1,1,2)::sem_version" }
}
}

/* it "raises error when increasing invalid component" { */
/* do { sql = "SELECT version_inc(row(1,1,1)::sem_version, 'invalid') AS ver" } */

/* rows { */
/* raises_error { */
/* message = "Failed to increase version using type = 'invalid' for version 1.1.1" */
/* } */
/* } */
/* } */

it "returns null on null input" {
do { sql = "SELECT version_inc(null) AS ver" }

row "0" {
column "ver" { assert = null }
col "must be null" { assert = "ver IS NULL" }
}
}
}
31 changes: 31 additions & 0 deletions test/fixtures/tests/versions_test2.krab.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
test "versions" "version_inc()" {
query "SELECT version_inc(row(1,1,1)::sem_version) AS ver" {
row "0" {
it "increases major version, resets others" { assert = "ver = row(2,0,0)::sem_version" }
}
}

query "SELECT version_inc(row(1,1,1)::sem_version, 'major') AS ver" {
row "0" {
it "increases major version, resets others" { assert = "ver = row(2,0,0)::sem_version" }
}
}

query "SELECT version_inc(row(1,1,1)::sem_version, 'minor') AS ver" {
row "0" {
it "increases minor, resets patch" { assert = "ver = row(1,2,0)::sem_version" }
}
}

query "SELECT version_inc(row(1,1,1)::sem_version, 'patch') AS ver" {
row "0" {
it "increases patch" { assert = "ver = row(1,1,2)::sem_version" }
}
}

query "SELECT version_inc(null) AS ver" {
row "0" {
it "returns null on null input" { assert = "ver IS NULL" }
}
}
}

0 comments on commit a6b0dfe

Please sign in to comment.