From 47dd0a757bbad7b45402f7963e6cdb7b1e9cd3fe Mon Sep 17 00:00:00 2001 From: Garet Halliday <me@garet.holiday> Date: Wed, 13 Sep 2023 15:02:15 -0500 Subject: [PATCH] wow --- test/capturer.go | 2 +- test/errors.go | 43 ++++++++++++++++++++++++++ test/inst/bind.go | 10 ++++++ test/inst/describe.go | 13 ++++++++ test/inst/execute.go | 7 +++++ test/inst/parse.go | 10 ++++++ test/runner.go | 62 +++++++++++++++++++++++++++++++++++--- test/test.go | 1 + test/tester.go | 9 +++++- test/tester_test.go | 5 ++- test/tests/eqp.go | 24 +++++++++++++++ test/tests/simple_query.go | 1 + test/tests/sync.go | 1 + test/tests/transaction.go | 1 + 14 files changed, 181 insertions(+), 8 deletions(-) create mode 100644 test/errors.go create mode 100644 test/inst/bind.go create mode 100644 test/inst/describe.go create mode 100644 test/inst/execute.go create mode 100644 test/inst/parse.go create mode 100644 test/tests/eqp.go diff --git a/test/capturer.go b/test/capturer.go index 5b6dfeb8..cc301226 100644 --- a/test/capturer.go +++ b/test/capturer.go @@ -19,7 +19,7 @@ func (T *Capturer) WritePacket(packet fed.Packet) error { func (T *Capturer) Check(other *Capturer) error { if len(T.Packets) != len(other.Packets) { - return fmt.Errorf("not enough packets! got %d but expected %d", len(other.Packets), len(T.Packets)) + return fmt.Errorf("wrong number of packets! got %d but expected %d", len(other.Packets), len(T.Packets)) } for i := range T.Packets { diff --git a/test/errors.go b/test/errors.go new file mode 100644 index 00000000..4ced3793 --- /dev/null +++ b/test/errors.go @@ -0,0 +1,43 @@ +package test + +import ( + "strings" +) + +type ErrorIn struct { + Name string + Err error +} + +func (T ErrorIn) Error() string { + var b strings.Builder + b.WriteString(`Error in "`) + b.WriteString(T.Name) + b.WriteString("\":\n\t") + + sub := T.Err.Error() + for _, r := range sub { + if r == '\n' { + b.WriteString("\n\t") + } else { + b.WriteRune(r) + } + } + + return b.String() +} + +var _ error = ErrorIn{} + +type Errors []error + +func (T Errors) Error() string { + var b strings.Builder + for _, err := range T { + b.WriteString(err.Error()) + b.WriteRune('\n') + } + return b.String() +} + +var _ error = Errors{} diff --git a/test/inst/bind.go b/test/inst/bind.go new file mode 100644 index 00000000..5f9de170 --- /dev/null +++ b/test/inst/bind.go @@ -0,0 +1,10 @@ +package inst + +type Bind struct { + Destination string + Source string +} + +func (Bind) instruction() {} + +var _ Instruction = Bind{} diff --git a/test/inst/describe.go b/test/inst/describe.go new file mode 100644 index 00000000..d8ff58d2 --- /dev/null +++ b/test/inst/describe.go @@ -0,0 +1,13 @@ +package inst + +type DescribePortal string + +func (DescribePortal) instruction() {} + +var _ Instruction = DescribePortal("") + +type DescribePreparedStatement string + +func (DescribePreparedStatement) instruction() {} + +var _ Instruction = DescribePreparedStatement("") diff --git a/test/inst/execute.go b/test/inst/execute.go new file mode 100644 index 00000000..222b6ca6 --- /dev/null +++ b/test/inst/execute.go @@ -0,0 +1,7 @@ +package inst + +type Execute string + +func (Execute) instruction() {} + +var _ Instruction = Execute("") diff --git a/test/inst/parse.go b/test/inst/parse.go new file mode 100644 index 00000000..0f19c92e --- /dev/null +++ b/test/inst/parse.go @@ -0,0 +1,10 @@ +package inst + +type Parse struct { + Destination string + Query string +} + +func (Parse) instruction() {} + +var _ Instruction = Parse{} diff --git a/test/runner.go b/test/runner.go index 3ec8071a..0ead60fb 100644 --- a/test/runner.go +++ b/test/runner.go @@ -36,6 +36,35 @@ func (T *Runner) prepare(client *gsql.Client) []Capturer { client.Do(&results[i], q.IntoPacket()) case inst.Sync: client.Do(&results[i], fed.NewPacket(packets.TypeSync)) + case inst.Parse: + p := packets.Parse{ + Destination: v.Destination, + Query: v.Query, + } + client.Do(&results[i], p.IntoPacket()) + case inst.Bind: + p := packets.Bind{ + Destination: v.Destination, + Source: v.Source, + } + client.Do(&results[i], p.IntoPacket()) + case inst.DescribePortal: + p := packets.Describe{ + Which: 'P', + Target: string(v), + } + client.Do(&results[i], p.IntoPacket()) + case inst.DescribePreparedStatement: + p := packets.Describe{ + Which: 'S', + Target: string(v), + } + client.Do(&results[i], p.IntoPacket()) + case inst.Execute: + p := packets.Execute{ + Target: string(v), + } + client.Do(&results[i], p.IntoPacket()) } } @@ -108,30 +137,53 @@ func (T *Runner) Run() error { // control expected, err := T.runControl() if err != nil { - return err + return ErrorIn{ + Name: "Control", + Err: err, + } } + var errs []error + // modes for name, mode := range T.config.Modes { actual, err := T.runMode(mode) if err != nil { - return err + errs = append(errs, ErrorIn{ + Name: name, + Err: err, + }) + continue } if len(expected) != len(actual) { - return fmt.Errorf("wrong number of results! expected %d but got %d", len(expected), len(actual)) + errs = append(errs, ErrorIn{ + Name: name, + Err: fmt.Errorf("wrong number of results! expected %d but got %d", len(expected), len(actual)), + }) + continue } + var modeErrs []error + for i, exp := range expected { act := actual[i] if err = exp.Check(&act); err != nil { - return err + modeErrs = append(modeErrs, fmt.Errorf("instruction %d: %v", i+1, err)) } } - _ = name + if len(modeErrs) > 0 { + errs = append(errs, ErrorIn{ + Name: name, + Err: Errors(modeErrs), + }) + } } + if len(errs) > 0 { + return Errors(errs) + } return nil } diff --git a/test/test.go b/test/test.go index 49b095d0..000fa8ff 100644 --- a/test/test.go +++ b/test/test.go @@ -3,5 +3,6 @@ package test import "pggat/test/inst" type Test struct { + Name string Instructions []inst.Instruction } diff --git a/test/tester.go b/test/tester.go index c06ce268..7b78af11 100644 --- a/test/tester.go +++ b/test/tester.go @@ -11,11 +11,18 @@ func NewTester(config Config) *Tester { } func (T *Tester) Run(tests ...Test) error { + var errors []error for _, test := range tests { runner := MakeRunner(T.config, test) if err := runner.Run(); err != nil { - return err + errors = append(errors, ErrorIn{ + Name: test.Name, + Err: err, + }) } } + if len(errors) > 0 { + return Errors(errors) + } return nil } diff --git a/test/tester_test.go b/test/tester_test.go index a558e62a..30b5ae70 100644 --- a/test/tester_test.go +++ b/test/tester_test.go @@ -1,6 +1,7 @@ package test_test import ( + "fmt" "testing" "pggat/lib/auth/credentials" @@ -37,7 +38,9 @@ func TestTester(t *testing.T) { tests.SimpleQuery, tests.Transaction, tests.Sync, + tests.EQP, ); err != nil { - t.Error(err) + fmt.Print(err.Error()) + t.Fail() } } diff --git a/test/tests/eqp.go b/test/tests/eqp.go new file mode 100644 index 00000000..430802da --- /dev/null +++ b/test/tests/eqp.go @@ -0,0 +1,24 @@ +package tests + +import ( + "pggat/test" + "pggat/test/inst" +) + +var EQP = test.Test{ + Name: "EQP", + Instructions: []inst.Instruction{ + inst.Parse{ + Destination: "test", + Query: "select 0;", + }, + inst.DescribePreparedStatement("test"), + inst.Bind{ + Destination: "test", + Source: "test", + }, + inst.DescribePortal("test"), + inst.Execute("test"), + inst.Sync{}, + }, +} diff --git a/test/tests/simple_query.go b/test/tests/simple_query.go index fa662b84..c7e4ed1c 100644 --- a/test/tests/simple_query.go +++ b/test/tests/simple_query.go @@ -6,6 +6,7 @@ import ( ) var SimpleQuery = test.Test{ + Name: "Simple Query", Instructions: []inst.Instruction{ inst.SimpleQuery("select 1;"), inst.SimpleQuery("SELECT 2, 3, 4;"), diff --git a/test/tests/sync.go b/test/tests/sync.go index 976ee8e1..123d69c7 100644 --- a/test/tests/sync.go +++ b/test/tests/sync.go @@ -6,6 +6,7 @@ import ( ) var Sync = test.Test{ + Name: "Sync", Instructions: []inst.Instruction{ inst.Sync{}, inst.SimpleQuery("BEGIN;"), diff --git a/test/tests/transaction.go b/test/tests/transaction.go index ad7ca675..674282be 100644 --- a/test/tests/transaction.go +++ b/test/tests/transaction.go @@ -6,6 +6,7 @@ import ( ) var Transaction = test.Test{ + Name: "Transaction", Instructions: []inst.Instruction{ inst.SimpleQuery("BEGIN;"), inst.SimpleQuery("select 1;"), -- GitLab