-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from k1LoW/include
Support `include:` in steps
- Loading branch information
Showing
8 changed files
with
179 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package runn | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
) | ||
|
||
const includeRunnerKey = "include" | ||
|
||
type includeRunner struct { | ||
operator *operator | ||
} | ||
|
||
func newIncludeRunner(o *operator) (*includeRunner, error) { | ||
return &includeRunner{ | ||
operator: o, | ||
}, nil | ||
} | ||
|
||
func (rnr *includeRunner) Run(ctx context.Context, path string) error { | ||
oo, err := rnr.operator.newNestedOperator(Book(filepath.Join(rnr.operator.root, path))) | ||
if err != nil { | ||
return err | ||
} | ||
if err := oo.Run(ctx); err != nil { | ||
return err | ||
} | ||
rnr.operator.store.steps = append(rnr.operator.store.steps, map[string]interface{}{ | ||
"steps": oo.store.steps, | ||
}) | ||
return nil | ||
} | ||
|
||
func (o *operator) newNestedOperator(opts ...Option) (*operator, error) { | ||
for k, r := range o.httpRunners { | ||
opts = append(opts, HTTPRunner(k, r.endpoint.String(), r.client)) | ||
} | ||
for k, r := range o.dbRunners { | ||
opts = append(opts, DBRunner(k, r.client)) | ||
} | ||
for k, v := range o.store.vars { | ||
opts = append(opts, Var(k, v)) | ||
} | ||
opts = append(opts, Var("parent", o.store.steps)) | ||
opts = append(opts, Debug(o.debug)) | ||
oo, err := New(opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
oo.t = o.t | ||
return oo, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package runn | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestIncludeRunnerRun(t *testing.T) { | ||
tests := []struct { | ||
book string | ||
want int | ||
}{ | ||
{"testdata/book/db.yml", 8}, | ||
} | ||
ctx := context.Background() | ||
for _, tt := range tests { | ||
db, err := os.CreateTemp("", "tmp") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(db.Name()) | ||
o, err := New(Runner("db", fmt.Sprintf("sqlite://%s", db.Name()))) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
r, err := newIncludeRunner(o) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := r.Run(ctx, tt.book); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
{ | ||
got := len(r.operator.store.steps) | ||
if want := 1; got != want { | ||
t.Errorf("got %v\nwant %v", got, want) | ||
} | ||
} | ||
{ | ||
got := len(r.operator.store.steps[0]["steps"].([]map[string]interface{})) | ||
if got != tt.want { | ||
t.Errorf("got %v\nwant %v", got, tt.want) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,22 @@ | ||
desc: Test using SQLite3 | ||
steps: | ||
- | ||
db: | ||
query: | | ||
CREATE TABLE users ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
username TEXT UNIQUE NOT NULL, | ||
password TEXT NOT NULL, | ||
email TEXT UNIQUE NOT NULL, | ||
created NUMERIC NOT NULL, | ||
updated NUMERIC | ||
) | ||
- | ||
db: | ||
query: INSERT INTO users (username, password, email, created) VALUES ('alice', 'passw0rd', '[email protected]', datetime('2017-12-05')) | ||
include: initdb.yml | ||
- | ||
db: | ||
query: SELECT * FROM users; | ||
- | ||
test: 'steps[2].rows[0].username == "alice"' | ||
test: 'steps[1].rows[0].username == "alice"' | ||
- | ||
db: | ||
query: INSERT INTO users (username, password, email, created) VALUES ('bob', 'passw0rd', 'bob@example.com', datetime('2022-02-22')) | ||
query: INSERT INTO users (username, password, email, created) VALUES ('charlie', 'passw0rd', 'charlie@example.com', datetime('2022-02-22')) | ||
- | ||
db: | ||
query: SELECT * FROM users WHERE id = {{ steps[4].last_insert_id }} | ||
query: SELECT * FROM users WHERE id = {{ steps[3].last_insert_id }} | ||
- | ||
test: 'steps[5].rows[0].username == "bob"' | ||
test: 'steps[4].rows[0].username == "charlie"' | ||
- | ||
db: | ||
query: SELECT COUNT(*) AS c FROM users | ||
- | ||
test: 'steps[7].rows[0].c == 2' | ||
test: 'steps[6].rows[0].c == 3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
desc: Initialize SQLite3 | ||
steps: | ||
- | ||
db: | ||
query: | | ||
CREATE TABLE users ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
username TEXT UNIQUE NOT NULL, | ||
password TEXT NOT NULL, | ||
email TEXT UNIQUE NOT NULL, | ||
created NUMERIC NOT NULL, | ||
updated NUMERIC | ||
) | ||
- | ||
db: | ||
query: INSERT INTO users (username, password, email, created) VALUES ('alice', 'passw0rd', '[email protected]', datetime('2017-12-05')) | ||
- | ||
db: | ||
query: INSERT INTO users (username, password, email, created) VALUES ('bob', 'passw0rd', '[email protected]', datetime('2022-02-22')) |