From b260e21abe103ec6797c86b8c1f0bb2455e567da Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 26 Apr 2023 14:23:17 +0200 Subject: [PATCH] test(logic): json_prolog object and string test --- x/logic/predicate/json_test.go | 99 ++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 x/logic/predicate/json_test.go diff --git a/x/logic/predicate/json_test.go b/x/logic/predicate/json_test.go new file mode 100644 index 00000000..277bd288 --- /dev/null +++ b/x/logic/predicate/json_test.go @@ -0,0 +1,99 @@ +//nolint:gocognit,lll +package predicate + +import ( + "fmt" + "testing" + + tmdb "github.com/cometbft/cometbft-db" + "github.com/cometbft/cometbft/libs/log" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + "github.com/cosmos/cosmos-sdk/store" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/ichiban/prolog/engine" + "github.com/okp4/okp4d/x/logic/testutil" + "github.com/okp4/okp4d/x/logic/types" + . "github.com/smartystreets/goconvey/convey" +) + +func TestJsonProlog(t *testing.T) { + Convey("Given a test cases", t, func() { + cases := []struct { + program string + query string + wantResult []types.TermResults + wantError error + wantSuccess bool + }{ + { + query: `json_prolog('"foo"', Term).`, + wantResult: []types.TermResults{{ + "Term": "foo", + }}, + wantSuccess: true, + }, + { + query: `json_prolog('{"foo": "bar"}', Term).`, + wantResult: []types.TermResults{{ + "Term": "json([-(foo, 'bar')])", + }}, + wantSuccess: true, + }, + } + for nc, tc := range cases { + Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() { + Convey("and a context", func() { + db := tmdb.NewMemDB() + stateStore := store.NewCommitMultiStore(db) + ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) + + Convey("and a vm", func() { + interpreter := testutil.NewLightInterpreterMust(ctx) + interpreter.Register2(engine.NewAtom("json_prolog"), JsonProlog) + + err := interpreter.Compile(ctx, tc.program) + So(err, ShouldBeNil) + + Convey("When the predicate is called", func() { + sols, err := interpreter.QueryContext(ctx, tc.query) + + Convey("Then the error should be nil", func() { + So(err, ShouldBeNil) + So(sols, ShouldNotBeNil) + + Convey("and the bindings should be as expected", func() { + var got []types.TermResults + for sols.Next() { + m := types.TermResults{} + err := sols.Scan(m) + So(err, ShouldBeNil) + + got = append(got, m) + } + if tc.wantError != nil { + So(sols.Err(), ShouldNotBeNil) + So(sols.Err().Error(), ShouldEqual, tc.wantError.Error()) + } else { + So(sols.Err(), ShouldBeNil) + + if tc.wantSuccess { + So(len(got), ShouldBeGreaterThan, 0) + So(len(got), ShouldEqual, len(tc.wantResult)) + for iGot, resultGot := range got { + for varGot, termGot := range resultGot { + So(testutil.ReindexUnknownVariables(termGot), ShouldEqual, tc.wantResult[iGot][varGot]) + } + } + } else { + So(len(got), ShouldEqual, 0) + } + } + }) + }) + }) + }) + }) + }) + } + }) +}