From 50f9d7fc8d81b4c2a2a4a0b7bd99e412d87f2f57 Mon Sep 17 00:00:00 2001 From: ccamel Date: Wed, 14 Feb 2024 15:17:35 +0100 Subject: [PATCH 1/3] refactor(logic)!: adopt unstructured expression for substitutions Adopt expression for the values substitued represented as a prolog term. Previous format was an incorrect mix of structured/unstructured. --- proto/logic/v1beta2/types.proto | 20 +- x/logic/keeper/interpreter.go | 126 +++++++----- x/logic/types/logic.go | 43 ---- x/logic/types/types.pb.go | 345 +++++--------------------------- x/logic/wasm/types.go | 35 +--- 5 files changed, 139 insertions(+), 430 deletions(-) delete mode 100644 x/logic/types/logic.go diff --git a/proto/logic/v1beta2/types.proto b/proto/logic/v1beta2/types.proto index c495c635..299e0d56 100644 --- a/proto/logic/v1beta2/types.proto +++ b/proto/logic/v1beta2/types.proto @@ -6,30 +6,14 @@ import "gogoproto/gogo.proto"; option go_package = "github.com/okp4/okp4d/x/logic/types"; -// Term is the representation of a piece of data and can be a constant, a variable, or an atom. -message Term { - option (gogoproto.goproto_stringer) = true; - - // name is the name of the term. - string name = 1 [(gogoproto.moretags) = "yaml:\"name\",omitempty"]; - // arguments are the arguments of the term, which can be constants, variables, or atoms. - repeated Term arguments = 2 [ - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"arguments\",omitempty" - ]; -} - // Substitution represents a substitution made to the variables in the query to obtain the answer. message Substitution { option (gogoproto.goproto_stringer) = true; // variable is the name of the variable. string variable = 1 [(gogoproto.moretags) = "yaml:\"variable\",omitempty"]; - // term is the term that the variable is substituted with. - Term term = 2 [ - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"term\",omitempty" - ]; + // expression is the value substituted for the variable, represented directly as a Prolog term (e.g., atom, number, compound). + string expression = 2 [(gogoproto.moretags) = "yaml:\"expression\",omitempty"]; } // Result represents the result of a query. diff --git a/x/logic/keeper/interpreter.go b/x/logic/keeper/interpreter.go index da5ca07b..4d95349e 100644 --- a/x/logic/keeper/interpreter.go +++ b/x/logic/keeper/interpreter.go @@ -1,15 +1,16 @@ package keeper import ( - goctx "context" + "context" "math" + "strings" "github.com/ichiban/prolog" + "github.com/ichiban/prolog/engine" "github.com/samber/lo" errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" - storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -27,21 +28,22 @@ const ( defaultWeightFactor = uint64(1) ) -func (k Keeper) limits(ctx goctx.Context) types.Limits { +func (k Keeper) limits(ctx context.Context) types.Limits { params := k.GetParams(sdk.UnwrapSDKContext(ctx)) return params.GetLimits() } -func (k Keeper) enhanceContext(ctx goctx.Context) goctx.Context { +func (k Keeper) enhanceContext(ctx context.Context) context.Context { sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx = sdkCtx.WithValue(types.AuthKeeperContextKey, k.authKeeper) sdkCtx = sdkCtx.WithValue(types.BankKeeperContextKey, k.bankKeeper) return sdkCtx } -func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryServiceAskResponse, error) { +func (k Keeper) execute(ctx context.Context, program, query string) (*types.QueryServiceAskResponse, error) { ctx = k.enhanceContext(ctx) sdkCtx := sdk.UnwrapSDKContext(ctx) + limits := k.limits(sdkCtx) i, userOutputBuffer, err := k.newInterpreter(ctx) if err != nil { @@ -51,22 +53,15 @@ func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryS return nil, errorsmod.Wrapf(types.InvalidArgument, "error compiling query: %v", err.Error()) } - sols, err := i.QueryContext(ctx, query) + answer, err := k.queryInterpreter(ctx, i, query, *limits.MaxResultCount) if err != nil { return nil, errorsmod.Wrapf(types.InvalidArgument, "error executing query: %v", err.Error()) } - defer func() { - _ = sols.Close() - }() var userOutput string if userOutputBuffer != nil { userOutput = userOutputBuffer.String() } - answer, err := k.solsToAnswer(sdkCtx, sols) - if err != nil { - return nil, err - } return &types.QueryServiceAskResponse{ Height: uint64(sdkCtx.BlockHeight()), @@ -76,55 +71,47 @@ func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryS }, nil } -// solsToAnswer consumes the given prolog solutions and convert it to an answer. -func (k Keeper) solsToAnswer(sdkCtx sdk.Context, sols *prolog.Solutions) (*types.Answer, error) { - solSuccess := false - solError := "" - limits := k.limits(sdkCtx) - var variables []string - results := make([]types.Result, 0) - for nb := sdkmath.ZeroUint(); nb.LT(*limits.MaxResultCount) && sols.Next(); nb = nb.Incr() { - solSuccess = true - - m := types.TermResults{} - if err := sols.Scan(m); err != nil { - return nil, errorsmod.Wrapf(types.Internal, "error scanning solution: %v", err.Error()) - } - if nb.IsZero() { - variables = m.ToVariables() - } - - results = append(results, types.Result{Substitutions: m.ToSubstitutions()}) +// queryInterpreter executes the given query on the given interpreter and returns the answer. +func (k Keeper) queryInterpreter(ctx context.Context, i *prolog.Interpreter, query string, limit sdkmath.Uint) (*types.Answer, error) { + p := engine.NewParser(&i.VM, strings.NewReader(query)) + t, err := p.Term() + if err != nil { + return nil, err } - if err := sols.Err(); err != nil { - if sdkCtx.GasMeter().IsOutOfGas() { - panic(storetypes.ErrorOutOfGas{Descriptor: "Prolog interpreter execution"}) + var env *engine.Env + count := sdkmath.ZeroUint() + envs := make([]*engine.Env, 0, limit.Uint64()) + _, callErr := engine.Call(&i.VM, t, func(env *engine.Env) *engine.Promise { + if count.LT(limit) { + envs = append(envs, env) } - solError = sols.Err().Error() + count = count.Incr() + return engine.Bool(count.GT(limit)) + }, env).Force(ctx) + + answerErr := lo.IfF(callErr != nil, func() string { + return callErr.Error() + }).Else("") + success := len(envs) > 0 + hasMore := count.GT(limit) + vars := parsedVarsToVars(p.Vars) + results, err := envsToResults(envs, p.Vars, i) + if err != nil { + return nil, err } return &types.Answer{ - Success: solSuccess, - Error: solError, - HasMore: sols.Next(), - Variables: variables, + Success: success, + Error: answerErr, + HasMore: hasMore, + Variables: vars, Results: results, }, nil } -func checkLimits(request *types.QueryServiceAskRequest, limits types.Limits) error { - size := sdkmath.NewUint(uint64(len(request.GetQuery()))) - limit := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64)) - if size.GT(limit) { - return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size, limit) - } - - return nil -} - // newInterpreter creates a new interpreter properly configured. -func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, *util.BoundedBuffer, error) { +func (k Keeper) newInterpreter(ctx context.Context) (*prolog.Interpreter, *util.BoundedBuffer, error) { sdkctx := sdk.UnwrapSDKContext(ctx) params := k.GetParams(sdkctx) @@ -173,6 +160,16 @@ func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, *util.Bo return i, userOutputBuffer, err } +func checkLimits(request *types.QueryServiceAskRequest, limits types.Limits) error { + size := sdkmath.NewUint(uint64(len(request.GetQuery()))) + limit := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64)) + if size.GT(limit) { + return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size, limit) + } + + return nil +} + // toPredicate converts the given predicate costs to a function that returns the cost for the given predicate as // a pair of predicate name and cost. func toPredicate(defaultCost uint64, predicateCosts []types.PredicateCost) func(string, int) lo.Tuple2[string, uint64] { @@ -196,3 +193,30 @@ func nonNilNorZeroOrDefaultUint64(v *sdkmath.Uint, defaultValue uint64) uint64 { return v.Uint64() } + +func parsedVarsToVars(vars []engine.ParsedVariable) []string { + return lo.Map(vars, func(v engine.ParsedVariable, _ int) string { + return v.Name.String() + }) +} + +func envsToResults(envs []*engine.Env, vars []engine.ParsedVariable, i *prolog.Interpreter) ([]types.Result, error) { + results := make([]types.Result, 0, len(envs)) + for _, rEnv := range envs { + substitutions := make([]types.Substitution, 0, len(vars)) + for _, v := range vars { + var expression prolog.TermString + err := expression.Scan(&i.VM, v.Variable, rEnv) + if err != nil { + return nil, err + } + substitution := types.Substitution{ + Variable: v.Name.String(), + Expression: string(expression), + } + substitutions = append(substitutions, substitution) + } + results = append(results, types.Result{Substitutions: substitutions}) + } + return results, nil +} diff --git a/x/logic/types/logic.go b/x/logic/types/logic.go deleted file mode 100644 index de9f6b75..00000000 --- a/x/logic/types/logic.go +++ /dev/null @@ -1,43 +0,0 @@ -package types - -import ( - "sort" - - "github.com/ichiban/prolog" -) - -// TermResults is a map from variable strings to prolog term values. -type TermResults map[string]prolog.TermString - -// ToSubstitutions converts a TermResults value to a slice of Substitution values. -// The slice is sorted in ascending order of variable names. -func (t TermResults) ToSubstitutions() []Substitution { - substitutions := make([]Substitution, 0, len(t)) - for v, ts := range t { - substitution := Substitution{ - Variable: v, - Term: Term{ - Name: string(ts), - }, - } - substitutions = append(substitutions, substitution) - } - - sort.Slice(substitutions, func(i, j int) bool { - return substitutions[i].Variable < substitutions[j].Variable - }) - - return substitutions -} - -// ToVariables extract from a TermResults value the variable names. -// The variable names are sorted in ascending order. -func (t TermResults) ToVariables() []string { - variables := make([]string, 0, len(t)) - for v := range t { - variables = append(variables, v) - } - sort.Strings(variables) - - return variables -} diff --git a/x/logic/types/types.pb.go b/x/logic/types/types.pb.go index 32a2ee93..e9f3be5f 100644 --- a/x/logic/types/types.pb.go +++ b/x/logic/types/types.pb.go @@ -23,74 +23,19 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -// Term is the representation of a piece of data and can be a constant, a variable, or an atom. -type Term struct { - // name is the name of the term. - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty" yaml:"name",omitempty` - // arguments are the arguments of the term, which can be constants, variables, or atoms. - Arguments []Term `protobuf:"bytes,2,rep,name=arguments,proto3" json:"arguments" yaml:"arguments",omitempty` -} - -func (m *Term) Reset() { *m = Term{} } -func (m *Term) String() string { return proto.CompactTextString(m) } -func (*Term) ProtoMessage() {} -func (*Term) Descriptor() ([]byte, []int) { - return fileDescriptor_f3c73c95465ca7a8, []int{0} -} -func (m *Term) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *Term) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_Term.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *Term) XXX_Merge(src proto.Message) { - xxx_messageInfo_Term.Merge(m, src) -} -func (m *Term) XXX_Size() int { - return m.Size() -} -func (m *Term) XXX_DiscardUnknown() { - xxx_messageInfo_Term.DiscardUnknown(m) -} - -var xxx_messageInfo_Term proto.InternalMessageInfo - -func (m *Term) GetName() string { - if m != nil { - return m.Name - } - return "" -} - -func (m *Term) GetArguments() []Term { - if m != nil { - return m.Arguments - } - return nil -} - // Substitution represents a substitution made to the variables in the query to obtain the answer. type Substitution struct { // variable is the name of the variable. Variable string `protobuf:"bytes,1,opt,name=variable,proto3" json:"variable,omitempty" yaml:"variable",omitempty` - // term is the term that the variable is substituted with. - Term Term `protobuf:"bytes,2,opt,name=term,proto3" json:"term" yaml:"term",omitempty` + // expression is the value substituted for the variable, represented directly as a Prolog term (e.g., atom, number, compound). + Expression string `protobuf:"bytes,2,opt,name=expression,proto3" json:"expression,omitempty" yaml:"expression",omitempty` } func (m *Substitution) Reset() { *m = Substitution{} } func (m *Substitution) String() string { return proto.CompactTextString(m) } func (*Substitution) ProtoMessage() {} func (*Substitution) Descriptor() ([]byte, []int) { - return fileDescriptor_f3c73c95465ca7a8, []int{1} + return fileDescriptor_f3c73c95465ca7a8, []int{0} } func (m *Substitution) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -126,11 +71,11 @@ func (m *Substitution) GetVariable() string { return "" } -func (m *Substitution) GetTerm() Term { +func (m *Substitution) GetExpression() string { if m != nil { - return m.Term + return m.Expression } - return Term{} + return "" } // Result represents the result of a query. @@ -143,7 +88,7 @@ func (m *Result) Reset() { *m = Result{} } func (m *Result) String() string { return proto.CompactTextString(m) } func (*Result) ProtoMessage() {} func (*Result) Descriptor() ([]byte, []int) { - return fileDescriptor_f3c73c95465ca7a8, []int{2} + return fileDescriptor_f3c73c95465ca7a8, []int{1} } func (m *Result) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -197,7 +142,7 @@ func (m *Answer) Reset() { *m = Answer{} } func (m *Answer) String() string { return proto.CompactTextString(m) } func (*Answer) ProtoMessage() {} func (*Answer) Descriptor() ([]byte, []int) { - return fileDescriptor_f3c73c95465ca7a8, []int{3} + return fileDescriptor_f3c73c95465ca7a8, []int{2} } func (m *Answer) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -262,7 +207,6 @@ func (m *Answer) GetResults() []Result { } func init() { - proto.RegisterType((*Term)(nil), "logic.v1beta2.Term") proto.RegisterType((*Substitution)(nil), "logic.v1beta2.Substitution") proto.RegisterType((*Result)(nil), "logic.v1beta2.Result") proto.RegisterType((*Answer)(nil), "logic.v1beta2.Answer") @@ -271,82 +215,34 @@ func init() { func init() { proto.RegisterFile("logic/v1beta2/types.proto", fileDescriptor_f3c73c95465ca7a8) } var fileDescriptor_f3c73c95465ca7a8 = []byte{ - // 489 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x53, 0x41, 0x6f, 0xd3, 0x30, - 0x18, 0xad, 0xdb, 0xac, 0x6b, 0x0d, 0xbb, 0x18, 0x86, 0xd2, 0xc1, 0x92, 0x28, 0x48, 0xa8, 0x07, - 0x48, 0xc4, 0x98, 0x10, 0x4c, 0x42, 0x82, 0x1c, 0xb8, 0x71, 0x60, 0xc0, 0x85, 0x0b, 0x72, 0x8a, - 0x95, 0x46, 0xd4, 0x75, 0x65, 0x3b, 0x83, 0xfe, 0x0b, 0xc4, 0x05, 0x8e, 0xfc, 0x9c, 0x4a, 0x5c, - 0x76, 0xe4, 0x14, 0xa1, 0xf6, 0x1f, 0xe4, 0x17, 0xa0, 0xd8, 0x4e, 0xe7, 0x54, 0xda, 0x25, 0x8a, - 0xbe, 0xf7, 0x7d, 0xef, 0x3d, 0x3f, 0xfb, 0x83, 0xa3, 0x19, 0xcb, 0xf2, 0x49, 0x7c, 0xf1, 0x38, - 0x25, 0x12, 0x9f, 0xc4, 0x72, 0xb9, 0x20, 0x22, 0x5a, 0x70, 0x26, 0x19, 0x3a, 0x50, 0x50, 0x64, - 0xa0, 0xa3, 0xdb, 0x19, 0xcb, 0x98, 0x42, 0xe2, 0xfa, 0x4f, 0x37, 0x85, 0x3f, 0x00, 0x74, 0xde, - 0x13, 0x4e, 0xd1, 0x23, 0xe8, 0xcc, 0x31, 0x25, 0x2e, 0x08, 0xc0, 0x78, 0x98, 0x8c, 0xaa, 0xd2, - 0x3f, 0x5c, 0x62, 0x3a, 0x3b, 0x0b, 0xeb, 0x6a, 0xf8, 0x90, 0xd1, 0x5c, 0x12, 0xba, 0x90, 0xcb, - 0x73, 0xd5, 0x86, 0x3e, 0xc0, 0x21, 0xe6, 0x59, 0x41, 0xc9, 0x5c, 0x0a, 0xb7, 0x1b, 0xf4, 0xc6, - 0x37, 0x4e, 0x6e, 0x45, 0x2d, 0xc1, 0xa8, 0xa6, 0x4d, 0xc2, 0x55, 0xe9, 0x77, 0xaa, 0xd2, 0x3f, - 0xd2, 0x64, 0xdb, 0x19, 0x9b, 0xf1, 0x8a, 0xe9, 0xcc, 0xf9, 0xf5, 0xdb, 0x07, 0xe1, 0x4f, 0x00, - 0x6f, 0xbe, 0x2b, 0x52, 0x21, 0x73, 0x59, 0xc8, 0x9c, 0xcd, 0xd1, 0x73, 0x38, 0xb8, 0xc0, 0x3c, - 0xc7, 0xe9, 0xac, 0x31, 0x78, 0x5c, 0x95, 0xfe, 0x48, 0x73, 0x36, 0x88, 0x4d, 0xb9, 0x6d, 0x47, - 0xaf, 0xa1, 0x23, 0x09, 0xa7, 0x6e, 0x37, 0x00, 0xd7, 0x79, 0x3c, 0x36, 0x1e, 0xcd, 0x81, 0xeb, - 0xf6, 0xd6, 0x81, 0xeb, 0x82, 0x71, 0xb6, 0x84, 0xfd, 0x73, 0x22, 0x8a, 0x99, 0x44, 0x39, 0x3c, - 0x10, 0x96, 0x45, 0xe1, 0x02, 0x15, 0xc2, 0xdd, 0x1d, 0x01, 0xfb, 0x18, 0xc9, 0x03, 0x23, 0xe4, - 0x69, 0xa1, 0xd6, 0xbc, 0xad, 0xd8, 0x66, 0x36, 0xd2, 0x7f, 0xba, 0xb0, 0xff, 0x6a, 0x2e, 0xbe, - 0x12, 0x8e, 0x9e, 0xc2, 0x7d, 0x51, 0x4c, 0x26, 0x44, 0x08, 0x95, 0xc6, 0x20, 0xb9, 0x57, 0x95, - 0xbe, 0xdb, 0x90, 0x2a, 0xc0, 0xa6, 0x6b, 0x9a, 0xd1, 0x29, 0xdc, 0x23, 0x9c, 0x33, 0xee, 0xee, - 0xa9, 0x0c, 0xbd, 0x55, 0xe9, 0x83, 0xaa, 0xf4, 0xef, 0xe8, 0x49, 0x05, 0xd9, 0x73, 0xba, 0x19, - 0x3d, 0x83, 0x83, 0x29, 0x16, 0x9f, 0x28, 0xe3, 0x44, 0xa5, 0x38, 0xb0, 0xc3, 0x6f, 0x90, 0x96, - 0xde, 0x14, 0x8b, 0x37, 0x8c, 0x13, 0xf4, 0x12, 0x0e, 0x9b, 0x7b, 0x10, 0x6e, 0x2f, 0xe8, 0x8d, - 0x87, 0xea, 0x3d, 0x80, 0xab, 0xf7, 0xb0, 0x85, 0x5b, 0xef, 0x61, 0x5b, 0x45, 0x6f, 0xe1, 0x3e, - 0x57, 0x79, 0x0b, 0xd7, 0x51, 0xf9, 0x1e, 0xee, 0xe4, 0xab, 0x6f, 0x23, 0x09, 0x4c, 0xb2, 0x26, - 0x04, 0x33, 0xd3, 0x32, 0x65, 0x6a, 0x3a, 0xcd, 0xe4, 0xc5, 0x6a, 0xed, 0x81, 0xcb, 0xb5, 0x07, - 0xfe, 0xad, 0x3d, 0xf0, 0x7d, 0xe3, 0x75, 0x2e, 0x37, 0x5e, 0xe7, 0xef, 0xc6, 0xeb, 0x7c, 0xbc, - 0x9f, 0xe5, 0x72, 0x5a, 0xa4, 0xd1, 0x84, 0xd1, 0x98, 0x7d, 0x59, 0x9c, 0xaa, 0xcf, 0xe7, 0xf8, - 0x5b, 0xac, 0x37, 0x4d, 0x6d, 0x58, 0xda, 0x57, 0xdb, 0xf3, 0xe4, 0x7f, 0x00, 0x00, 0x00, 0xff, - 0xff, 0xc0, 0x13, 0x1d, 0xac, 0x7f, 0x03, 0x00, 0x00, -} - -func (m *Term) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *Term) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *Term) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Arguments) > 0 { - for iNdEx := len(m.Arguments) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Arguments[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintTypes(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - } - } - if len(m.Name) > 0 { - i -= len(m.Name) - copy(dAtA[i:], m.Name) - i = encodeVarintTypes(dAtA, i, uint64(len(m.Name))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil + // 430 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x92, 0x41, 0x6f, 0xd3, 0x30, + 0x14, 0xc7, 0xe3, 0x75, 0xeb, 0x52, 0xc3, 0x2e, 0x16, 0xa0, 0x6c, 0x03, 0x3b, 0x0a, 0x12, 0xea, + 0x01, 0x25, 0x62, 0x4c, 0x08, 0x26, 0x21, 0x20, 0x77, 0x0e, 0x8c, 0x1b, 0x17, 0x94, 0x04, 0x2b, + 0xb5, 0x48, 0xea, 0xc8, 0x76, 0xc6, 0xf2, 0x2d, 0x76, 0xe4, 0xc8, 0xc7, 0xa9, 0xc4, 0x65, 0x47, + 0x4e, 0x11, 0x6a, 0xbf, 0x41, 0x3e, 0x01, 0xaa, 0xe3, 0x50, 0x67, 0x97, 0xaa, 0xca, 0xff, 0xfd, + 0xdf, 0xff, 0xf9, 0xf7, 0x1e, 0x3c, 0x2e, 0x78, 0xce, 0xb2, 0xe8, 0xea, 0x45, 0x4a, 0x55, 0x72, + 0x16, 0xa9, 0xa6, 0xa2, 0x32, 0xac, 0x04, 0x57, 0x1c, 0x1d, 0x69, 0x29, 0x34, 0xd2, 0xc9, 0x83, + 0x9c, 0xe7, 0x5c, 0x2b, 0xd1, 0xf6, 0x5f, 0x5f, 0x14, 0xdc, 0x00, 0x78, 0xff, 0x73, 0x9d, 0x4a, + 0xc5, 0x54, 0xad, 0x18, 0x5f, 0xa2, 0x37, 0xd0, 0xbd, 0x4a, 0x04, 0x4b, 0xd2, 0x82, 0x7a, 0xc0, + 0x07, 0xf3, 0x59, 0xfc, 0xa4, 0x6b, 0xc9, 0x71, 0x93, 0x94, 0xc5, 0x45, 0x30, 0x28, 0xc1, 0x73, + 0x5e, 0x32, 0x45, 0xcb, 0x4a, 0x35, 0x97, 0xff, 0xcb, 0xd1, 0x3b, 0x08, 0xe9, 0x75, 0x25, 0xa8, + 0x94, 0x8c, 0x2f, 0xbd, 0x3d, 0x6d, 0x26, 0x5d, 0x4b, 0x4e, 0x7b, 0xf3, 0x4e, 0xb3, 0xed, 0x96, + 0xe5, 0x62, 0xff, 0xe7, 0x2f, 0x02, 0x82, 0x06, 0x4e, 0x2f, 0xa9, 0xac, 0x0b, 0x85, 0x18, 0x3c, + 0x92, 0xd6, 0x6c, 0xd2, 0x03, 0xfe, 0x64, 0x7e, 0xef, 0xec, 0x34, 0x1c, 0xbd, 0x2c, 0xb4, 0xe7, + 0x8f, 0x9f, 0xad, 0x5a, 0xe2, 0x74, 0x2d, 0xc1, 0x7d, 0xe8, 0xc8, 0x6f, 0xe7, 0x8e, 0x3b, 0x9b, + 0xe8, 0xdf, 0x7b, 0x70, 0xfa, 0x61, 0x29, 0x7f, 0x50, 0x81, 0x5e, 0xc1, 0x43, 0x59, 0x67, 0x19, + 0x95, 0x52, 0x63, 0x70, 0xe3, 0xc7, 0x5d, 0x4b, 0xbc, 0xa1, 0xa9, 0x16, 0xec, 0x76, 0x43, 0x31, + 0x3a, 0x87, 0x07, 0x54, 0x08, 0x2e, 0xbc, 0x03, 0xfd, 0x7e, 0xbc, 0x6a, 0x09, 0xe8, 0x5a, 0xf2, + 0xc8, 0x30, 0xd8, 0x4a, 0xb6, 0xaf, 0x2f, 0x46, 0xaf, 0xa1, 0xbb, 0x48, 0xe4, 0xd7, 0x92, 0x0b, + 0xaa, 0xc1, 0xb9, 0x36, 0xf5, 0x41, 0x19, 0xe5, 0x2d, 0x12, 0xf9, 0x91, 0x0b, 0x8a, 0xde, 0xc3, + 0xd9, 0xb0, 0x00, 0xe9, 0x4d, 0xfc, 0xc9, 0x7c, 0x16, 0x07, 0x26, 0xf3, 0x64, 0xbc, 0xb4, 0xd1, + 0xbc, 0x3b, 0x13, 0xfa, 0x04, 0x0f, 0x85, 0xe6, 0x2d, 0xbd, 0x7d, 0xcd, 0xf7, 0xe1, 0x1d, 0xbe, + 0xfd, 0x36, 0x62, 0xdf, 0x90, 0x35, 0x10, 0x8c, 0x67, 0x34, 0x94, 0xf9, 0xd6, 0xd3, 0x8c, 0xdf, + 0xae, 0xd6, 0x18, 0xdc, 0xae, 0x31, 0xf8, 0xbb, 0xc6, 0xe0, 0x66, 0x83, 0x9d, 0xdb, 0x0d, 0x76, + 0xfe, 0x6c, 0xb0, 0xf3, 0xe5, 0x69, 0xce, 0xd4, 0xa2, 0x4e, 0xc3, 0x8c, 0x97, 0x11, 0xff, 0x5e, + 0x9d, 0xeb, 0x9f, 0x6f, 0xd1, 0x75, 0xd4, 0x5f, 0xb3, 0xbe, 0xe2, 0x74, 0xaa, 0x2f, 0xf4, 0xe5, + 0xbf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x19, 0x3c, 0x55, 0x00, 0xe3, 0x02, 0x00, 0x00, } func (m *Substitution) Marshal() (dAtA []byte, err error) { @@ -369,16 +265,13 @@ func (m *Substitution) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - { - size, err := m.Term.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintTypes(dAtA, i, uint64(size)) + if len(m.Expression) > 0 { + i -= len(m.Expression) + copy(dAtA[i:], m.Expression) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Expression))) + i-- + dAtA[i] = 0x12 } - i-- - dAtA[i] = 0x12 if len(m.Variable) > 0 { i -= len(m.Variable) copy(dAtA[i:], m.Variable) @@ -510,37 +403,20 @@ func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } -func (m *Term) Size() (n int) { +func (m *Substitution) Size() (n int) { if m == nil { return 0 } var l int _ = l - l = len(m.Name) + l = len(m.Variable) if l > 0 { n += 1 + l + sovTypes(uint64(l)) } - if len(m.Arguments) > 0 { - for _, e := range m.Arguments { - l = e.Size() - n += 1 + l + sovTypes(uint64(l)) - } - } - return n -} - -func (m *Substitution) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.Variable) + l = len(m.Expression) if l > 0 { n += 1 + l + sovTypes(uint64(l)) } - l = m.Term.Size() - n += 1 + l + sovTypes(uint64(l)) return n } @@ -596,122 +472,6 @@ func sovTypes(x uint64) (n int) { func sozTypes(x uint64) (n int) { return sovTypes(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } -func (m *Term) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Term: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Term: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTypes - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTypes - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Name = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Arguments", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthTypes - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthTypes - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Arguments = append(m.Arguments, Term{}) - if err := m.Arguments[len(m.Arguments)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipTypes(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthTypes - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func (m *Substitution) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 @@ -775,9 +535,9 @@ func (m *Substitution) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Term", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Expression", wireType) } - var msglen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowTypes @@ -787,24 +547,23 @@ func (m *Substitution) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if msglen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthTypes } - postIndex := iNdEx + msglen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthTypes } if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.Term.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } + m.Expression = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex diff --git a/x/logic/wasm/types.go b/x/logic/wasm/types.go index d5c5e07e..e8d6a217 100644 --- a/x/logic/wasm/types.go +++ b/x/logic/wasm/types.go @@ -13,9 +13,10 @@ type AskQuery struct { // AskResponse implements the Ask query response JSON schema in a wasm custom query purpose, it redefines the existing // generated type from proto to ensure a dedicated serialization logic. type AskResponse struct { - Height uint64 `json:"height"` - GasUsed uint64 `json:"gas_used"` - Answer *Answer `json:"answer,omitempty"` + Height uint64 `json:"height"` + GasUsed uint64 `json:"gas_used"` + Answer *Answer `json:"answer,omitempty"` + UserOutput string `json:"user_output,omitempty"` } func (to *AskResponse) from(from types.QueryServiceAskResponse) { @@ -27,12 +28,14 @@ func (to *AskResponse) from(from types.QueryServiceAskResponse) { answer.from(*from.Answer) to.Answer = answer } + to.UserOutput = from.UserOutput } // Answer denotes the Answer element JSON representation in an AskResponse for wasm custom query purpose, it redefines // the existing generated type from proto to ensure a dedicated serialization logic. type Answer struct { Success bool `json:"success"` + Error string `json:"error,omitempty"` HasMore bool `json:"has_more"` Variables []string `json:"variables"` Results []Result `json:"results"` @@ -40,6 +43,7 @@ type Answer struct { func (to *Answer) from(from types.Answer) { to.Success = from.Success + to.Error = from.Error to.HasMore = from.HasMore to.Variables = from.Variables if to.Variables == nil { @@ -71,30 +75,11 @@ func (to *Result) from(from types.Result) { // Substitution denotes the Substitution element JSON representation in an AskResponse for wasm custom query purpose, it redefines // the existing generated type from proto to ensure a dedicated serialization logic. type Substitution struct { - Variable string `json:"variable"` - Term Term `json:"term"` + Variable string `json:"variable"` + Expression string `json:"expression"` } func (to *Substitution) from(from types.Substitution) { to.Variable = from.Variable - term := new(Term) - term.from(from.Term) - to.Term = *term -} - -// Term denotes the Term element JSON representation in an AskResponse for wasm custom query purpose, it redefines -// the existing generated type from proto to ensure a dedicated serialization logic. -type Term struct { - Name string `json:"name"` - Arguments []Term `json:"arguments"` -} - -func (to *Term) from(from types.Term) { - to.Name = from.Name - to.Arguments = make([]Term, 0, len(from.Arguments)) - for _, fromTerm := range from.Arguments { - term := new(Term) - term.from(fromTerm) - to.Arguments = append(to.Arguments, *term) - } + to.Expression = from.Expression } From 084ee2c250bf31cb7d49ff966bdb4aae0da4abcb Mon Sep 17 00:00:00 2001 From: ccamel Date: Wed, 14 Feb 2024 15:22:25 +0100 Subject: [PATCH 2/3] test(logic): improve tests (after refactoring) --- x/logic/keeper/grpc_query_ask_test.go | 92 +++++++++++++++++++-------- x/logic/predicate/address_test.go | 21 +++--- x/logic/predicate/bank_test.go | 60 ++++++++--------- x/logic/predicate/builtin_test.go | 3 +- x/logic/predicate/crypto_test.go | 43 ++++++------- x/logic/predicate/did_test.go | 41 ++++++------ x/logic/predicate/encoding_test.go | 15 ++--- x/logic/predicate/file_test.go | 33 +++++----- x/logic/predicate/json_test.go | 77 +++++++++++----------- x/logic/predicate/string_test.go | 29 ++++----- x/logic/predicate/uri_test.go | 41 ++++++------ x/logic/testutil/logic.go | 2 + x/logic/types/logic_test.go | 87 ------------------------- 13 files changed, 245 insertions(+), 299 deletions(-) delete mode 100644 x/logic/types/logic_test.go diff --git a/x/logic/keeper/grpc_query_ask_test.go b/x/logic/keeper/grpc_query_ask_test.go index dc54544d..26b47be4 100644 --- a/x/logic/keeper/grpc_query_ask_test.go +++ b/x/logic/keeper/grpc_query_ask_test.go @@ -32,6 +32,16 @@ func TestGRPCAsk(t *testing.T) { expectedAnswer *types.Answer expectedError string }{ + { + program: "foo.", + query: "foo.", + expectedAnswer: &types.Answer{ + Success: true, + HasMore: false, + Variables: nil, + Results: []types.Result{{Substitutions: nil}}, + }, + }, { program: "father(bob, alice).", query: "father(bob, X).", @@ -40,11 +50,8 @@ func TestGRPCAsk(t *testing.T) { HasMore: false, Variables: []string{"X"}, Results: []types.Result{{Substitutions: []types.Substitution{{ - Variable: "X", - Term: types.Term{ - Name: "alice", - Arguments: nil, - }, + Variable: "X", + Expression: "alice", }}}}, }, }, @@ -56,11 +63,8 @@ func TestGRPCAsk(t *testing.T) { HasMore: false, Variables: []string{"X"}, Results: []types.Result{{Substitutions: []types.Substitution{{ - Variable: "X", - Term: types.Term{ - Name: "[a,l,i,c,e]", - Arguments: nil, - }, + Variable: "X", + Expression: "[a,l,i,c,e]", }}}}, }, }, @@ -72,11 +76,8 @@ func TestGRPCAsk(t *testing.T) { HasMore: true, Variables: []string{"X"}, Results: []types.Result{{Substitutions: []types.Substitution{{ - Variable: "X", - Term: types.Term{ - Name: "alice", - Arguments: nil, - }, + Variable: "X", + Expression: "alice", }}}}, }, }, @@ -98,11 +99,8 @@ func TestGRPCAsk(t *testing.T) { HasMore: false, Variables: []string{"X"}, Results: []types.Result{{Substitutions: []types.Substitution{{ - Variable: "X", - Term: types.Term{ - Name: "0", - Arguments: nil, - }, + Variable: "X", + Expression: "0", }}}}, }, }, @@ -114,11 +112,53 @@ func TestGRPCAsk(t *testing.T) { HasMore: false, Variables: []string{"X"}, Results: []types.Result{{Substitutions: []types.Substitution{{ - Variable: "X", - Term: types.Term{ - Name: "élodie", - Arguments: nil, - }, + Variable: "X", + Expression: "élodie", + }}}}, + }, + }, + { + program: "foo(foo(bar)).", + query: "foo(X).", + expectedAnswer: &types.Answer{ + Success: true, + HasMore: false, + Variables: []string{"X"}, + Results: []types.Result{{Substitutions: []types.Substitution{{ + Variable: "X", + Expression: "foo(bar)", + }}}}, + }, + }, + { + program: "father(bob, alice).", + query: "father(A, B).", + expectedAnswer: &types.Answer{ + Success: true, + HasMore: false, + Variables: []string{"A", "B"}, + Results: []types.Result{{Substitutions: []types.Substitution{{ + Variable: "A", + Expression: "bob", + }, { + Variable: "B", + Expression: "alice", + }}}}, + }, + }, + { + program: "father(bob, alice).", + query: "father(B, A).", + expectedAnswer: &types.Answer{ + Success: true, + HasMore: false, + Variables: []string{"B", "A"}, + Results: []types.Result{{Substitutions: []types.Substitution{{ + Variable: "B", + Expression: "bob", + }, { + Variable: "A", + Expression: "alice", }}}}, }, }, @@ -129,7 +169,7 @@ func TestGRPCAsk(t *testing.T) { Success: false, Error: "error(existence_error(procedure,father/3),root)", HasMore: false, - Variables: nil, + Variables: []string{"X", "O"}, Results: nil, }, }, diff --git a/x/logic/predicate/address_test.go b/x/logic/predicate/address_test.go index 4b9362ea..3d44ca47 100644 --- a/x/logic/predicate/address_test.go +++ b/x/logic/predicate/address_test.go @@ -20,7 +20,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" ) func TestBech32(t *testing.T) { @@ -28,13 +27,13 @@ func TestBech32(t *testing.T) { cases := []struct { program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ { query: `bech32_address(-(Hrp, Address), 'okp415wn30a9z4uc692s0kkx5fp5d4qfr3ac7sj9dqn').`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Hrp": "okp4", "Address": "[163,167,23,244,162,175,49,162,170,15,181,141,68,134,141,168,18,56,247,30]", }}, @@ -42,7 +41,7 @@ func TestBech32(t *testing.T) { }, { query: `bech32_address(Address, 'okp415wn30a9z4uc692s0kkx5fp5d4qfr3ac7sj9dqn').`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Address": "okp4-[163,167,23,244,162,175,49,162,170,15,181,141,68,134,141,168,18,56,247,30]", }}, wantSuccess: true, @@ -54,7 +53,7 @@ func TestBech32(t *testing.T) { }, { query: `bech32_address(-('okp4', Address), 'okp415wn30a9z4uc692s0kkx5fp5d4qfr3ac7sj9dqn').`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Address": "[163,167,23,244,162,175,49,162,170,15,181,141,68,134,141,168,18,56,247,30]", }}, wantSuccess: true, @@ -65,24 +64,24 @@ func TestBech32(t *testing.T) { }, { query: `bech32_address(-('okp4', [163,167,23,244,162,175,49,162,170,15,181,141,68,134,141,168,18,56,247,30]), Bech32).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Bech32": "okp415wn30a9z4uc692s0kkx5fp5d4qfr3ac7sj9dqn", }}, wantSuccess: true, }, { query: `bech32_address(-('okp4', [163,167,23,244,162,175,49,162,170,15,181,141,68,134,141,168,18,56,247,30]), 'okp415wn30a9z4uc692s0kkx5fp5d4qfr3ac7sj9dqn').`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { query: `bech32_address(-(Hrp, [163,167,23,244,162,175,49,162,170,15,181,141,68,134,141,168,18,56,247,30]), 'okp415wn30a9z4uc692s0kkx5fp5d4qfr3ac7sj9dqn').`, - wantResult: []types.TermResults{{"Hrp": "okp4"}}, + wantResult: []testutil.TermResults{{"Hrp": "okp4"}}, wantSuccess: true, }, { query: `bech32_address(-(Hrp, [163,167,23,244,162,175,49,162,170,15,181,141,68,134,141,168,18,56,247,30]), 'okp415wn30a9z4uc692s0kkx5fp5d4qfr3ac7sj9dqn').`, - wantResult: []types.TermResults{{"Hrp": "okp4"}}, + wantResult: []testutil.TermResults{{"Hrp": "okp4"}}, wantSuccess: true, }, { @@ -144,9 +143,9 @@ func TestBech32(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/predicate/bank_test.go b/x/logic/predicate/bank_test.go index c0944250..6e131ad9 100644 --- a/x/logic/predicate/bank_test.go +++ b/x/logic/predicate/bank_test.go @@ -37,7 +37,7 @@ func TestBank(t *testing.T) { lockedCoins []bank.Balance program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error }{ { @@ -48,7 +48,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', X).`, - wantResult: []types.TermResults{{"X": "[uknow-100]"}}, + wantResult: []testutil.TermResults{{"X": "[uknow-100]"}}, }, { balances: []bank.Balance{ @@ -58,7 +58,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [X]).`, - wantResult: []types.TermResults{{"X": "uatom-100"}}, + wantResult: []testutil.TermResults{{"X": "uatom-100"}}, }, { balances: []bank.Balance{ @@ -68,7 +68,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [X, Y]).`, - wantResult: []types.TermResults{{"X": "uatom-589", "Y": "uknow-420"}}, + wantResult: []testutil.TermResults{{"X": "uatom-589", "Y": "uknow-420"}}, }, { balances: []bank.Balance{ @@ -78,7 +78,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [-(D, A) | _]).`, - wantResult: []types.TermResults{{"D": "uatom", "A": "589"}}, + wantResult: []testutil.TermResults{{"D": "uatom", "A": "589"}}, }, { balances: []bank.Balance{ @@ -92,7 +92,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_balances('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', [_, X]).`, - wantResult: []types.TermResults{{"X": "uknow-589"}}, + wantResult: []testutil.TermResults{{"X": "uknow-589"}}, }, { balances: []bank.Balance{ @@ -107,7 +107,7 @@ func TestBank(t *testing.T) { }, program: `bank_balances_has_coin(A, D, V) :- bank_balances(A, R), member(D-V, R).`, query: `bank_balances_has_coin('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', 'uknow', V).`, - wantResult: []types.TermResults{{"V": "589"}}, + wantResult: []testutil.TermResults{{"V": "589"}}, }, { balances: []bank.Balance{ @@ -125,7 +125,7 @@ func TestBank(t *testing.T) { }, program: `bank_balances_has_sufficient_coin(A, C, S) :- bank_balances(A, R), member(C, R), -(_, V) = C, compare(>, V, S).`, query: `bank_balances_has_sufficient_coin('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', C, 600).`, - wantResult: []types.TermResults{{"C": "uakt-4099"}, {"C": "uatom-693"}, {"C": "uband-4282"}, {"C": "ukava-836"}}, + wantResult: []testutil.TermResults{{"C": "uakt-4099"}, {"C": "uatom-693"}, {"C": "uband-4282"}, {"C": "ukava-836"}}, }, { balances: []bank.Balance{ @@ -139,7 +139,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_balances(Accounts, Balances).`, - wantResult: []types.TermResults{ + wantResult: []testutil.TermResults{ {"Accounts": "okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm", "Balances": "[uknow-420]"}, {"Accounts": "okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38", "Balances": "[uatom-589]"}, }, @@ -147,7 +147,7 @@ func TestBank(t *testing.T) { { balances: []bank.Balance{}, query: `bank_balances('foo', X).`, - wantResult: []types.TermResults{{"X": "[uknow-100]"}}, + wantResult: []testutil.TermResults{{"X": "[uknow-100]"}}, wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],unknown)", strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")), }, @@ -165,7 +165,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_spendable_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', X).`, - wantResult: []types.TermResults{{"X": "[uknow-100]"}}, + wantResult: []testutil.TermResults{{"X": "[uknow-100]"}}, }, { spendableCoins: []bank.Balance{ @@ -175,7 +175,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_spendable_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [X]).`, - wantResult: []types.TermResults{{"X": "uatom-100"}}, + wantResult: []testutil.TermResults{{"X": "uatom-100"}}, }, { spendableCoins: []bank.Balance{ @@ -185,7 +185,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_spendable_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [X, Y]).`, - wantResult: []types.TermResults{{"X": "uatom-589", "Y": "uknow-420"}}, + wantResult: []testutil.TermResults{{"X": "uatom-589", "Y": "uknow-420"}}, }, { spendableCoins: []bank.Balance{ @@ -195,7 +195,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_spendable_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [-(D, A) | _]).`, - wantResult: []types.TermResults{{"D": "uatom", "A": "589"}}, + wantResult: []testutil.TermResults{{"D": "uatom", "A": "589"}}, }, { spendableCoins: []bank.Balance{ @@ -209,7 +209,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_spendable_balances('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', [_, X]).`, - wantResult: []types.TermResults{{"X": "uknow-589"}}, + wantResult: []testutil.TermResults{{"X": "uknow-589"}}, }, { spendableCoins: []bank.Balance{ @@ -224,7 +224,7 @@ func TestBank(t *testing.T) { }, program: `bank_spendable_has_coin(A, D, V) :- bank_spendable_balances(A, R), member(D-V, R).`, query: `bank_spendable_has_coin('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', 'uknow', V).`, - wantResult: []types.TermResults{{"V": "589"}}, + wantResult: []testutil.TermResults{{"V": "589"}}, }, { spendableCoins: []bank.Balance{ @@ -243,7 +243,7 @@ func TestBank(t *testing.T) { program: `bank_spendable_has_sufficient_coin(A, C, S) :- bank_spendable_balances(A, R), member(C, R), -(_, V) = C, compare(>, V, S).`, query: `bank_spendable_has_sufficient_coin('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', C, 600).`, - wantResult: []types.TermResults{{"C": "uakt-4099"}, {"C": "uatom-693"}, {"C": "uband-4282"}, {"C": "ukava-836"}}, + wantResult: []testutil.TermResults{{"C": "uakt-4099"}, {"C": "uatom-693"}, {"C": "uband-4282"}, {"C": "ukava-836"}}, }, { balances: []bank.Balance{ @@ -267,7 +267,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_spendable_balances(Accounts, SpendableCoins).`, - wantResult: []types.TermResults{ + wantResult: []testutil.TermResults{ {"Accounts": "okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm", "SpendableCoins": "[uknow-420]"}, {"Accounts": "okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38", "SpendableCoins": "[uatom-589]"}, }, @@ -275,7 +275,7 @@ func TestBank(t *testing.T) { { spendableCoins: []bank.Balance{}, query: `bank_spendable_balances('foo', X).`, - wantResult: []types.TermResults{{"X": "[uknow-100]"}}, + wantResult: []testutil.TermResults{{"X": "[uknow-100]"}}, wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],unknown)", strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")), }, @@ -300,7 +300,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_locked_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', X).`, - wantResult: []types.TermResults{{"X": "[uknow-900]"}}, + wantResult: []testutil.TermResults{{"X": "[uknow-900]"}}, }, { lockedCoins: []bank.Balance{ @@ -310,7 +310,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_locked_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [X]).`, - wantResult: []types.TermResults{{"X": "uatom-100"}}, + wantResult: []testutil.TermResults{{"X": "uatom-100"}}, }, { lockedCoins: []bank.Balance{ @@ -320,7 +320,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_locked_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [X, Y]).`, - wantResult: []types.TermResults{{"X": "uatom-589", "Y": "uknow-420"}}, + wantResult: []testutil.TermResults{{"X": "uatom-589", "Y": "uknow-420"}}, }, { lockedCoins: []bank.Balance{ @@ -330,7 +330,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_locked_balances('okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm', [-(D, A) | _]).`, - wantResult: []types.TermResults{{"D": "uatom", "A": "589"}}, + wantResult: []testutil.TermResults{{"D": "uatom", "A": "589"}}, }, { lockedCoins: []bank.Balance{ @@ -344,7 +344,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_locked_balances('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', [_, X]).`, - wantResult: []types.TermResults{{"X": "uknow-589"}}, + wantResult: []testutil.TermResults{{"X": "uknow-589"}}, }, { lockedCoins: []bank.Balance{ @@ -359,7 +359,7 @@ func TestBank(t *testing.T) { }, program: `bank_locked_has_coin(A, D, V) :- bank_locked_balances(A, R), member(D-V, R).`, query: `bank_locked_has_coin('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', 'uknow', V).`, - wantResult: []types.TermResults{{"V": "589"}}, + wantResult: []testutil.TermResults{{"V": "589"}}, }, { lockedCoins: []bank.Balance{ @@ -378,7 +378,7 @@ func TestBank(t *testing.T) { program: `bank_locked_has_sufficient_coin(A, C, S) :- bank_locked_balances(A, R), member(C, R), -(_, V) = C, compare(>, V, S).`, query: `bank_locked_has_sufficient_coin('okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38', C, 600).`, - wantResult: []types.TermResults{{"C": "uakt-4099"}, {"C": "uatom-693"}, {"C": "uband-4282"}, {"C": "ukava-836"}}, + wantResult: []testutil.TermResults{{"C": "uakt-4099"}, {"C": "uatom-693"}, {"C": "uband-4282"}, {"C": "ukava-836"}}, }, { balances: []bank.Balance{ @@ -412,7 +412,7 @@ func TestBank(t *testing.T) { }, }, query: `bank_locked_balances(Accounts, LockedCoins).`, - wantResult: []types.TermResults{ + wantResult: []testutil.TermResults{ {"Accounts": "okp41ffd5wx65l407yvm478cxzlgygw07h79sq0m3fm", "LockedCoins": "[uknow-800]"}, {"Accounts": "okp41wze8mn5nsgl9qrgazq6a92fvh7m5e6pslyrz38", "LockedCoins": "[uatom-7411]"}, }, @@ -420,7 +420,7 @@ func TestBank(t *testing.T) { { lockedCoins: []bank.Balance{}, query: `bank_locked_balances('foo', X).`, - wantResult: []types.TermResults{{"X": "[uknow-100]"}}, + wantResult: []testutil.TermResults{{"X": "[uknow-100]"}}, wantError: fmt.Errorf("error(resource_error(resource_module(bank)),[%s],unknown)", strings.Join(strings.Split("decoding bech32 failed: invalid bech32 string length 3", ""), ",")), }, @@ -481,9 +481,9 @@ func TestBank(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/predicate/builtin_test.go b/x/logic/predicate/builtin_test.go index 9990285f..642b3013 100644 --- a/x/logic/predicate/builtin_test.go +++ b/x/logic/predicate/builtin_test.go @@ -19,7 +19,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" "github.com/okp4/okp4d/x/logic/util" ) @@ -59,7 +58,7 @@ func TestWrite(t *testing.T) { So(err, ShouldBeNil) So(sols, ShouldNotBeNil) - m := types.TermResults{} + m := testutil.TermResults{} for sols.Next() { err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/predicate/crypto_test.go b/x/logic/predicate/crypto_test.go index f661542c..c90d9666 100644 --- a/x/logic/predicate/crypto_test.go +++ b/x/logic/predicate/crypto_test.go @@ -20,7 +20,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" ) func TestCryptoOperations(t *testing.T) { @@ -28,14 +27,14 @@ func TestCryptoOperations(t *testing.T) { cases := []struct { program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ { program: `test(Hex) :- crypto_data_hash('hello world', Hash, []), hex_bytes(Hex, Hash).`, query: `test(Hex).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Hex": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", }}, wantSuccess: true, @@ -43,13 +42,13 @@ func TestCryptoOperations(t *testing.T) { { program: `test :- crypto_data_hash('hello world', Hash, []), hex_bytes('b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', Hash).`, query: `test.`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { program: `test(Hex) :- crypto_data_hash('hello world', Hash, [algorithm(sha256)]), hex_bytes(Hex, Hash).`, query: `test(Hex).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Hex": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", }}, wantSuccess: true, @@ -57,7 +56,7 @@ func TestCryptoOperations(t *testing.T) { { program: `test(Hex) :- crypto_data_hash('hello world', Hash, [encoding(utf8)]), hex_bytes(Hex, Hash).`, query: `test(Hex).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Hex": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", }}, wantSuccess: true, @@ -65,7 +64,7 @@ func TestCryptoOperations(t *testing.T) { { program: `test(Hex) :- crypto_data_hash('68656c6c6f20776f726c64', Hash, [encoding(hex)]), hex_bytes(Hex, Hash).`, query: `test(Hex).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Hex": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", }}, wantSuccess: true, @@ -73,7 +72,7 @@ func TestCryptoOperations(t *testing.T) { { program: `test(Hex) :- crypto_data_hash([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], Hash, [algorithm(sha256),encoding(octet)]), hex_bytes(Hex, Hash).`, query: `test(Hex).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Hex": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", }}, wantSuccess: true, @@ -86,7 +85,7 @@ func TestCryptoOperations(t *testing.T) { { program: `test(Hex) :- crypto_data_hash('hello world', Hash, [algorithm(sha512)]), hex_bytes(Hex, Hash).`, query: `test(Hex).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Hex": "'309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f'", }}, wantSuccess: true, @@ -94,7 +93,7 @@ func TestCryptoOperations(t *testing.T) { { program: `test(Hex) :- crypto_data_hash('hello world', Hash, [algorithm(md5)]), hex_bytes(Hex, Hash).`, query: `test(Hex).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Hex": "'5eb63bbbe01eeed093cb22bb8f5acdc3'", }}, wantSuccess: true, @@ -123,9 +122,9 @@ func TestCryptoOperations(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) @@ -164,7 +163,7 @@ func TestXVerify(t *testing.T) { cases := []struct { program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ @@ -176,7 +175,7 @@ func TestXVerify(t *testing.T) { hex_bytes('889bcfd331e8e43b5ebf430301dffb6ac9e2fce69f6227b43552fe3dc8cc1ee00c1cc53452a8712e9d5f80086dff8cf4999c1b93ed6c6e403c09334cb61ddd0b', Sig), eddsa_verify(PubKey, Msg, Sig, [encoding(octet), type(ed25519)]).`, query: `verify.`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { // All good with hex encoding @@ -185,7 +184,7 @@ func TestXVerify(t *testing.T) { hex_bytes('889bcfd331e8e43b5ebf430301dffb6ac9e2fce69f6227b43552fe3dc8cc1ee00c1cc53452a8712e9d5f80086dff8cf4999c1b93ed6c6e403c09334cb61ddd0b', Sig), eddsa_verify(PubKey, '9b038f8ef6918cbb56040dfda401b56bb1ce79c472e7736e8677758c83367a9d', Sig, encoding(hex)).`, query: `verify.`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { // Wrong Msg @@ -246,7 +245,7 @@ func TestXVerify(t *testing.T) { hex_bytes('30450220099e6f9dd218e0e304efa7a4224b0058a8e3aec73367ec239bee4ed8ed7d85db022100b504d3d0d2e879b04705c0e5a2b40b0521a5ab647ea207bd81134e1a4eb79e47', Sig), ecdsa_verify(PubKey, Msg, Sig, [encoding(octet), type(secp256r1)]).`, query: `verify.`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { // Invalid secp signature @@ -278,7 +277,7 @@ func TestXVerify(t *testing.T) { hex_bytes('30450220099e6f9dd218e0e304efa7a4224b0058a8e3aec73367ec239bee4ed8ed7d85db022100b504d3d0d2e879b04705c0e5a2b40b0521a5ab647ea207bd81134e1a4eb79e47', Sig), ecdsa_verify(PubKey, Msg, Sig, encoding(octet)).`, query: `verify.`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: false, }, { @@ -289,7 +288,7 @@ func TestXVerify(t *testing.T) { hex_bytes('30450220099e6f9dd218e0e304efa7a4224b0058a8e3aec73367ec239bee4ed8ed7d85db022100b504d3d0d2e879b04705c0e5a2b40b0521a5ab647ea207bd81134e1a4eb79e48', Sig), ecdsa_verify(PubKey, Msg, Sig, encoding(octet)).`, query: `verify.`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: false, }, // ECDSA - secp256k1 @@ -301,7 +300,7 @@ func TestXVerify(t *testing.T) { hex_bytes('304402201448201bb4408549b0997f4b9ad9ed36f3cf8bb9c433fc7f3ba48c6b6e39476e022053f7d056f7ffeab9a79f3a36bc2ba969ddd530a3a1495d1ed7bba00039820223', Sig), ecdsa_verify(PubKey, Msg, Sig, [encoding(octet), type(secp256k1)]).`, query: `verify.`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { @@ -312,7 +311,7 @@ func TestXVerify(t *testing.T) { hex_bytes('304402201448201bb4408549b0997f4b9ad9ed36f3cf8bb9c433fc7f3ba48c6b6e39476e022053f7d056f7ffeab9a79f3a36bc2ba969ddd530a3a1495d1ed7bba00039820223', Sig), ecdsa_verify(PubKey, Msg, Sig, [encoding(octet), type(secp256k1)]).`, query: `verify.`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: false, }, } @@ -340,9 +339,9 @@ func TestXVerify(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/predicate/did_test.go b/x/logic/predicate/did_test.go index 9ce2d71a..a6f3b3a0 100644 --- a/x/logic/predicate/did_test.go +++ b/x/logic/predicate/did_test.go @@ -20,7 +20,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" ) func TestDID(t *testing.T) { @@ -28,84 +27,84 @@ func TestDID(t *testing.T) { cases := []struct { program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error }{ { query: `did_components('did:example:123456',did_components(X,Y,_,_,_)).`, - wantResult: []types.TermResults{{"X": "example", "Y": "'123456'"}}, + wantResult: []testutil.TermResults{{"X": "example", "Y": "'123456'"}}, }, { query: `did_components('did:example:123456',did_components(X,Y,Z,_,_)).`, - wantResult: []types.TermResults{{"X": "example", "Y": "'123456'", "Z": "_1"}}, + wantResult: []testutil.TermResults{{"X": "example", "Y": "'123456'", "Z": "_1"}}, }, { query: `did_components('did:example:123456/path', X).`, - wantResult: []types.TermResults{{"X": "did_components(example,'123456',path,_1,_2)"}}, + wantResult: []testutil.TermResults{{"X": "did_components(example,'123456',path,_1,_2)"}}, }, { query: `did_components('did:example:123456?versionId=1', X).`, - wantResult: []types.TermResults{{"X": "did_components(example,'123456',_1,'versionId=1',_2)"}}, + wantResult: []testutil.TermResults{{"X": "did_components(example,'123456',_1,'versionId=1',_2)"}}, }, { query: `did_components('did:example:123456/path%20with/space', X).`, - wantResult: []types.TermResults{{"X": "did_components(example,'123456','path%20with/space',_1,_2)"}}, + wantResult: []testutil.TermResults{{"X": "did_components(example,'123456','path%20with/space',_1,_2)"}}, }, { query: `did_components(X,did_components(example,'123456',_,'versionId=1',_)).`, - wantResult: []types.TermResults{{"X": "'did:example:123456?versionId=1'"}}, + wantResult: []testutil.TermResults{{"X": "'did:example:123456?versionId=1'"}}, }, { query: `did_components(X,did_components(example,'123456','/foo/bar','versionId=1','test')).`, - wantResult: []types.TermResults{{"X": "'did:example:123456/foo/bar?versionId=1#test'"}}, + wantResult: []testutil.TermResults{{"X": "'did:example:123456/foo/bar?versionId=1#test'"}}, }, { query: `did_components(X,did_components(example,'123456','path%20with/space',_,test)).`, - wantResult: []types.TermResults{{"X": "'did:example:123456/path%20with/space#test'"}}, + wantResult: []testutil.TermResults{{"X": "'did:example:123456/path%20with/space#test'"}}, }, { query: `did_components(X,did_components(example,'123456','/foo/bar','version%20Id=1','test')).`, - wantResult: []types.TermResults{{"X": "'did:example:123456/foo/bar?version%20Id=1#test'"}}, + wantResult: []testutil.TermResults{{"X": "'did:example:123456/foo/bar?version%20Id=1#test'"}}, }, { query: `did_components(X,Y).`, - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, wantError: fmt.Errorf("error(instantiation_error,did_components/2)"), }, { query: `did_components('foo',X).`, - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, wantError: fmt.Errorf("error(domain_error(encoding(did),foo),[%s],did_components/2)", strings.Join(strings.Split("invalid DID", ""), ",")), }, { query: `did_components(123,X).`, - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, wantError: fmt.Errorf("error(type_error(atom,123),did_components/2)"), }, { query: `did_components(X, 123).`, - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, wantError: fmt.Errorf("error(type_error(did_components,123),did_components/2)"), }, { query: `did_components(X,foo('bar')).`, - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, wantError: fmt.Errorf("error(domain_error(did_components,foo(bar)),did_components/2)"), }, { query: `did_components(X,did_components('bar')).`, - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, wantError: fmt.Errorf("error(domain_error(did_components,did_components(bar)),did_components/2)"), }, { query: `did_components(X,did_components(example,'123456','path with/space',5,test)).`, - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, wantError: fmt.Errorf("error(type_error(atom,5),did_components/2)"), }, { query: `did_components('did:example:123456',foo(X)).`, - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, }, } for nc, tc := range cases { @@ -130,9 +129,9 @@ func TestDID(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/predicate/encoding_test.go b/x/logic/predicate/encoding_test.go index 87df8753..ad47cfb2 100644 --- a/x/logic/predicate/encoding_test.go +++ b/x/logic/predicate/encoding_test.go @@ -20,7 +20,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" ) func TestHexBytesPredicate(t *testing.T) { @@ -28,19 +27,19 @@ func TestHexBytesPredicate(t *testing.T) { cases := []struct { program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ { query: `hex_bytes(Hex, [44,38,180,107,104,255,198,143,249,155,69,60,29,48,65,52,19,66,45,112,100,131,191,160,249,138,94,136,98,102,231,174]).`, - wantResult: []types.TermResults{{"Hex": "'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'"}}, + wantResult: []testutil.TermResults{{"Hex": "'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'"}}, wantSuccess: true, }, { query: `hex_bytes('2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae', Bytes).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Bytes": "[44,38,180,107,104,255,198,143,249,155,69,60,29,48,65,52,19,66,45,112,100,131,191,160,249,138,94,136,98,102,231,174]", }}, wantSuccess: true, @@ -48,7 +47,7 @@ func TestHexBytesPredicate(t *testing.T) { { query: `hex_bytes('2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae', [44,38,180,107,104,255,198,143,249,155,69,60,29,48,65,52,19,66,45,112,100,131,191,160,249,138,94,136,98,102,231,174]).`, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { @@ -108,10 +107,10 @@ func TestHexBytesPredicate(t *testing.T) { }) } -func checkSolutions(sols *prolog.Solutions, wantResult []types.TermResults, wantSuccess bool, wantError error) { - var got []types.TermResults +func checkSolutions(sols *prolog.Solutions, wantResult []testutil.TermResults, wantSuccess bool, wantError error) { + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/predicate/file_test.go b/x/logic/predicate/file_test.go index 5d314570..2402f999 100644 --- a/x/logic/predicate/file_test.go +++ b/x/logic/predicate/file_test.go @@ -26,7 +26,6 @@ import ( "github.com/okp4/okp4d/x/logic/fs" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" ) func TestSourceFile(t *testing.T) { @@ -37,7 +36,7 @@ func TestSourceFile(t *testing.T) { cases := []struct { interpreter func(ctx goctx.Context) (i *prolog.Interpreter) query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ @@ -49,13 +48,13 @@ func TestSourceFile(t *testing.T) { { interpreter: testutil.NewLightInterpreterMust, query: "consult(file1), consult(file2), source_file(file1).", - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { interpreter: testutil.NewLightInterpreterMust, query: "consult(file1), consult(file2), consult(file3), source_file(file2).", - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { @@ -71,19 +70,19 @@ func TestSourceFile(t *testing.T) { { interpreter: testutil.NewLightInterpreterMust, query: "consult(file1), consult(file2), source_file(X).", - wantResult: []types.TermResults{{"X": "file1"}, {"X": "file2"}}, + wantResult: []testutil.TermResults{{"X": "file1"}, {"X": "file2"}}, wantSuccess: true, }, { interpreter: testutil.NewLightInterpreterMust, query: "consult(file2), consult(file3), consult(file1), source_file(X).", - wantResult: []types.TermResults{{"X": "file1"}, {"X": "file2"}, {"X": "file3"}}, + wantResult: []testutil.TermResults{{"X": "file1"}, {"X": "file2"}, {"X": "file3"}}, wantSuccess: true, }, { interpreter: testutil.NewLightInterpreterMust, query: "source_file(foo(bar)).", - wantResult: []types.TermResults{}, + wantResult: []testutil.TermResults{}, wantError: fmt.Errorf("error(type_error(atom,foo(bar)),source_file/1)"), }, @@ -95,7 +94,7 @@ func TestSourceFile(t *testing.T) { { interpreter: testutil.NewComprehensiveInterpreterMust, query: "consult(file1), consult(file2), source_files([file1, file2]).", - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, wantSuccess: true, }, { @@ -111,13 +110,13 @@ func TestSourceFile(t *testing.T) { { interpreter: testutil.NewComprehensiveInterpreterMust, query: "consult(file2), consult(file1), source_files(X).", - wantResult: []types.TermResults{{"X": "[file1,file2]"}}, + wantResult: []testutil.TermResults{{"X": "[file1,file2]"}}, wantSuccess: true, }, { interpreter: testutil.NewComprehensiveInterpreterMust, query: "consult(file2), consult(file1), source_files([file1, X]).", - wantResult: []types.TermResults{{"X": "file2"}}, + wantResult: []testutil.TermResults{{"X": "file2"}}, wantSuccess: true, }, } @@ -151,9 +150,9 @@ func TestSourceFile(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) @@ -198,7 +197,7 @@ func TestOpen(t *testing.T) { files map[string][]byte program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ @@ -208,7 +207,7 @@ func TestOpen(t *testing.T) { }, program: "get_first_char(C) :- open(file, read, Stream, _), get_char(Stream, C).", query: `get_first_char(C).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "C": "d", }}, wantSuccess: true, @@ -219,7 +218,7 @@ func TestOpen(t *testing.T) { }, program: "get_first_char(C) :- open(file, read, Stream, []), get_char(Stream, C).", query: `get_first_char(C).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "C": "'H'", }}, wantSuccess: true, @@ -344,9 +343,9 @@ func TestOpen(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/predicate/json_test.go b/x/logic/predicate/json_test.go index 1af4021f..c532cd15 100644 --- a/x/logic/predicate/json_test.go +++ b/x/logic/predicate/json_test.go @@ -20,7 +20,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" ) func TestJsonProlog(t *testing.T) { @@ -29,7 +28,7 @@ func TestJsonProlog(t *testing.T) { description string program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ @@ -51,7 +50,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert direct string (valid json) into prolog", query: `json_prolog('"foo"', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "foo", }}, wantSuccess: true, @@ -59,7 +58,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert direct string with space (valid json) into prolog", query: `json_prolog('"a string with space"', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "'a string with space'", }}, wantSuccess: true, @@ -69,7 +68,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json object into prolog", query: `json_prolog('{"foo": "bar"}', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "json([foo-bar])", }}, wantSuccess: true, @@ -77,7 +76,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json object with multiple attribute into prolog", query: `json_prolog('{"foo": "bar", "foobar": "bar foo"}', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "json([foo-bar,foobar-'bar foo'])", }}, wantSuccess: true, @@ -85,7 +84,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json object with attribute with a space into prolog", query: `json_prolog('{"string with space": "bar"}', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "json(['string with space'-bar])", }}, wantSuccess: true, @@ -93,7 +92,7 @@ func TestJsonProlog(t *testing.T) { { description: "ensure determinism on object attribute key sorted alphabetically", query: `json_prolog('{"b": "a", "a": "b"}', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "json([a-b,b-a])", }}, wantSuccess: true, @@ -103,7 +102,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json number into prolog", query: `json_prolog('10', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "10", }}, wantSuccess: true, @@ -127,7 +126,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json true boolean into prolog", query: `json_prolog('true', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "@(true)", }}, wantSuccess: true, @@ -135,7 +134,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json false boolean into prolog", query: `json_prolog('false', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "@(false)", }}, wantSuccess: true, @@ -145,7 +144,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json null value into prolog", query: `json_prolog('null', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "@(null)", }}, wantSuccess: true, @@ -155,7 +154,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert empty json array into prolog", query: `json_prolog('[]', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "@([])", }}, wantSuccess: true, @@ -163,7 +162,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json array into prolog", query: `json_prolog('["foo", "bar"]', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "[foo,bar]", }}, wantSuccess: true, @@ -171,7 +170,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json array with null element into prolog", query: `json_prolog('[null]', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "[@(null)]", }}, wantSuccess: true, @@ -179,7 +178,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json string array into prolog", query: `json_prolog('["string with space", "bar"]', Term).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Term": "['string with space',bar]", }}, wantSuccess: true, @@ -190,7 +189,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert string term to json", query: `json_prolog(Json, 'foo').`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'\"foo\"'", }}, wantSuccess: true, @@ -198,7 +197,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert atom term to json", query: `json_prolog(Json, foo).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'\"foo\"'", }}, wantSuccess: true, @@ -206,7 +205,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert string with space to json", query: `json_prolog(Json, 'foo bar').`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'\"foo bar\"'", }}, wantSuccess: true, @@ -214,7 +213,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert string with space to json", query: `json_prolog(Json, 'foo bar').`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'\"foo bar\"'", }}, wantSuccess: true, @@ -222,7 +221,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert empty-list atom term to json", query: `json_prolog(Json, []).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'\"[]\"'", }}, wantSuccess: true, @@ -232,7 +231,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json object from prolog", query: `json_prolog(Json, json([foo-bar])).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'{\"foo\":\"bar\"}'", }}, wantSuccess: true, @@ -240,7 +239,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json object with multiple attribute from prolog", query: `json_prolog(Json, json([foo-bar,foobar-'bar foo'])).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'{\"foo\":\"bar\",\"foobar\":\"bar foo\"}'", }}, wantSuccess: true, @@ -248,7 +247,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json object with attribute with a space into prolog", query: `json_prolog(Json, json(['string with space'-bar])).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'{\"string with space\":\"bar\"}'", }}, wantSuccess: true, @@ -256,7 +255,7 @@ func TestJsonProlog(t *testing.T) { { description: "ensure determinism on object attribute key sorted alphabetically", query: `json_prolog(Json, json([b-a,a-b])).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'{\"a\":\"b\",\"b\":\"a\"}'", }}, wantSuccess: true, @@ -284,7 +283,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json number from prolog", query: `json_prolog(Json, 10).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'10'", }}, wantSuccess: true, @@ -300,7 +299,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert empty json array from prolog", query: `json_prolog(Json, @([])).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "[]", }}, wantSuccess: true, @@ -308,7 +307,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json array from prolog", query: `json_prolog(Json, [foo,bar]).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'[\"foo\",\"bar\"]'", }}, wantSuccess: true, @@ -316,7 +315,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json array with null element from prolog", query: `json_prolog(Json, [@(null)]).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'[null]'", }}, wantSuccess: true, @@ -324,7 +323,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json string array from prolog", query: `json_prolog(Json, ['string with space',bar]).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "'[\"string with space\",\"bar\"]'", }}, wantSuccess: true, @@ -340,7 +339,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert true boolean from prolog", query: `json_prolog(Json, @(true)).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "true", }}, wantSuccess: true, @@ -348,7 +347,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert false boolean from prolog", query: `json_prolog(Json, @(false)).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "false", }}, wantSuccess: true, @@ -358,7 +357,7 @@ func TestJsonProlog(t *testing.T) { { description: "convert json null value into prolog", query: `json_prolog(Json, @(null)).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Json": "null", }}, wantSuccess: true, @@ -386,9 +385,9 @@ func TestJsonProlog(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) @@ -490,9 +489,9 @@ func TestJsonPrologWithMoreComplexStructBidirectional(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) @@ -529,9 +528,9 @@ func TestJsonPrologWithMoreComplexStructBidirectional(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/predicate/string_test.go b/x/logic/predicate/string_test.go index 896ef258..08a39acf 100644 --- a/x/logic/predicate/string_test.go +++ b/x/logic/predicate/string_test.go @@ -20,7 +20,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" ) func TestReadString(t *testing.T) { @@ -29,7 +28,7 @@ func TestReadString(t *testing.T) { input string program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ @@ -37,7 +36,7 @@ func TestReadString(t *testing.T) { input: "foo", program: "read_input(String) :- current_input(Stream), read_string(Stream, _, String).", query: `read_input(String).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "foo", }}, wantSuccess: true, @@ -46,7 +45,7 @@ func TestReadString(t *testing.T) { input: "foo bar", program: "read_input(String) :- current_input(Stream), read_string(Stream, _, String).", query: `read_input(String).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "'foo bar'", }}, wantSuccess: true, @@ -55,7 +54,7 @@ func TestReadString(t *testing.T) { input: "foo bar", program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", query: `read_input(String, Len).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "'foo bar'", "Len": "7", }}, @@ -65,7 +64,7 @@ func TestReadString(t *testing.T) { input: "foo bar", program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", query: `read_input(String, 3).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "foo", }}, wantSuccess: true, @@ -74,7 +73,7 @@ func TestReadString(t *testing.T) { input: "foo bar", program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", query: `read_input(String, 7).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "'foo bar'", }}, wantSuccess: true, @@ -83,7 +82,7 @@ func TestReadString(t *testing.T) { input: "foo bar 🧙", program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", query: `read_input(String, _).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "'foo bar 🧙'", }}, wantSuccess: true, @@ -92,7 +91,7 @@ func TestReadString(t *testing.T) { input: "foo bar 🧙", program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", query: `read_input(String, Len).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "'foo bar 🧙'", "Len": "12", }}, @@ -102,7 +101,7 @@ func TestReadString(t *testing.T) { input: "🧙", program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", query: `read_input(String, Len).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "'🧙'", "Len": "4", }}, @@ -112,7 +111,7 @@ func TestReadString(t *testing.T) { input: "🧙", program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", query: `read_input(String, 1).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "'🧙'", }}, wantSuccess: false, @@ -121,7 +120,7 @@ func TestReadString(t *testing.T) { input: "Hello World!", program: "read_input(String, Len) :- current_input(Stream), read_string(Stream, Len, String).", query: `read_input(String, 15).`, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "String": "'Hello World!'", }}, wantSuccess: false, @@ -164,9 +163,9 @@ func TestReadString(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) @@ -358,7 +357,7 @@ func TestStringBytes(t *testing.T) { } else { nb := 0 for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} So(sols.Scan(m), ShouldBeNil) nb++ } diff --git a/x/logic/predicate/uri_test.go b/x/logic/predicate/uri_test.go index cc3979a9..1cd9925a 100644 --- a/x/logic/predicate/uri_test.go +++ b/x/logic/predicate/uri_test.go @@ -20,7 +20,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/logic/testutil" - "github.com/okp4/okp4d/x/logic/types" ) func TestURIEncoded(t *testing.T) { @@ -28,7 +27,7 @@ func TestURIEncoded(t *testing.T) { cases := []struct { program string query string - wantResult []types.TermResults + wantResult []testutil.TermResults wantError error wantSuccess bool }{ @@ -40,106 +39,106 @@ func TestURIEncoded(t *testing.T) { { query: `uri_encoded(path, Decoded, foo).`, wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Decoded": "foo", }}, }, { query: `uri_encoded(path, Decoded, 'foo%20bar').`, wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Decoded": "'foo bar'", }}, }, { query: `uri_encoded(path, foo, Encoded).`, wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Encoded": "foo", }}, }, { query: `uri_encoded(query_value, 'foo bar', Encoded).`, wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Encoded": "'foo%20bar'", }}, }, { query: "uri_encoded(query_value, ' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', Encoded).", wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Encoded": "'%20!%22%23$%25%26\\'()*%2B,-./0123456789%3A%3B%3C%3D%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~'", }}, }, { query: "uri_encoded(path, ' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', Encoded).", wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Encoded": "'%20!%22%23$%25&\\'()*+,-./0123456789%3A;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~'", }}, }, { query: "uri_encoded(segment, ' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', Encoded).", wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Encoded": "'%20!%22%23$%25&\\'()*+,-.%2F0123456789%3A;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~'", }}, }, { query: "uri_encoded(fragment, ' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', Encoded).", wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Encoded": "'%20!%22%23$%25&\\'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~'", }}, }, { query: "uri_encoded(query_value, Decoded, '%20!%22%23$%25%26\\'()*%2B,-./0123456789%3A%3B%3C%3D%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~+').", wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Decoded": "' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~+'", }}, }, { query: "uri_encoded(path, Decoded, '%20!%22%23$%25&\\'()*+,-./0123456789%3A;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~').", wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Decoded": "' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'", }}, }, { query: "uri_encoded(segment, Decoded, '%20!%22%23$%25&\\'()*+,-.%2F0123456789%3A;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~').", wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Decoded": "' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'", }}, }, { query: "uri_encoded(fragment, Decoded, '%20!%22%23$%25&\\'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~').", wantSuccess: true, - wantResult: []types.TermResults{{ + wantResult: []testutil.TermResults{{ "Decoded": "' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'", }}, }, { query: "uri_encoded(query_value, ' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~+', '%20!%22%23$%25%26\\'()*%2B,-./0123456789%3A%3B%3C%3D%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~%2B').", wantSuccess: true, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, }, { query: "uri_encoded(path, ' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', '%20!%22%23$%25&\\'()*+,-./0123456789%3A;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~').", wantSuccess: true, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, }, { query: "uri_encoded(segment, ' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', '%20!%22%23$%25&\\'()*+,-.%2F0123456789%3A;%3C=%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~').", wantSuccess: true, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, }, { query: "uri_encoded(fragment, ' !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', '%20!%22%23$%25&\\'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D~').", wantSuccess: true, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, }, { query: "uri_encoded(fragment, 'foo bar', 'bar%20foo').", @@ -158,7 +157,7 @@ func TestURIEncoded(t *testing.T) { { query: "uri_encoded(path, 'foo', compound(2)).", wantSuccess: false, - wantResult: []types.TermResults{{}}, + wantResult: []testutil.TermResults{{}}, }, { query: "uri_encoded(path, X, compound(2)).", @@ -194,9 +193,9 @@ func TestURIEncoded(t *testing.T) { So(sols, ShouldNotBeNil) Convey("and the bindings should be as expected", func() { - var got []types.TermResults + var got []testutil.TermResults for sols.Next() { - m := types.TermResults{} + m := testutil.TermResults{} err := sols.Scan(m) So(err, ShouldBeNil) diff --git a/x/logic/testutil/logic.go b/x/logic/testutil/logic.go index 482c132f..3418de11 100644 --- a/x/logic/testutil/logic.go +++ b/x/logic/testutil/logic.go @@ -11,6 +11,8 @@ import ( "github.com/okp4/okp4d/x/logic/interpreter/bootstrap" ) +type TermResults map[string]prolog.TermString + // NewLightInterpreterMust returns a new Interpreter with the given context or panics if it fails. // The Interpreter is configured with minimal settings to support testing. func NewLightInterpreterMust(ctx context.Context) (i *prolog.Interpreter) { diff --git a/x/logic/types/logic_test.go b/x/logic/types/logic_test.go deleted file mode 100644 index 6fbf00df..00000000 --- a/x/logic/types/logic_test.go +++ /dev/null @@ -1,87 +0,0 @@ -package types - -import ( - "fmt" - "testing" - - . "github.com/smartystreets/goconvey/convey" -) - -func TestToSubstitutions(t *testing.T) { - Convey("Given test cases", t, func() { - cases := []struct { - term TermResults - wantResult []Substitution - }{ - { - term: TermResults{}, - wantResult: []Substitution{}, - }, - { - term: TermResults{ - "X": "foo", - }, - wantResult: []Substitution{ - { - Variable: "X", - Term: Term{ - Name: "foo", - }, - }, - }, - }, - { - term: TermResults{ - "X": "foo", - "Y": "bar", - }, - wantResult: []Substitution{ - { - Variable: "X", - Term: Term{ - Name: "foo", - }, - }, - { - Variable: "Y", - Term: Term{ - Name: "bar", - }, - }, - }, - }, - { - term: TermResults{ - "Y": "bar", - "X": "foo", - }, - wantResult: []Substitution{ - { - Variable: "X", - Term: Term{ - Name: "foo", - }, - }, - { - Variable: "Y", - Term: Term{ - Name: "bar", - }, - }, - }, - }, - } - - for nc, tc := range cases { - Convey(fmt.Sprintf("Given test case #%d", nc), func() { - Convey("When ToSubstitutions() function is called", func() { - substitutions := tc.term.ToSubstitutions() - - Convey("Then the result should match expectations", func() { - So(substitutions, ShouldResemble, tc.wantResult) - }) - }) - }) - } - }) -} From 6c4cb3d9d12d41ed7893b10bdfa654c016b54fd1 Mon Sep 17 00:00:00 2001 From: ccamel Date: Wed, 14 Feb 2024 15:25:56 +0100 Subject: [PATCH 3/3] docs(logic): re-generate documentation after logic contract change --- docs/proto/logic.md | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/docs/proto/logic.md b/docs/proto/logic.md index de99954e..d3cb8090 100644 --- a/docs/proto/logic.md +++ b/docs/proto/logic.md @@ -211,7 +211,6 @@ utilized within a query, or limiting the depth of the backtracking algorithm. - [Answer](#logic.v1beta2.Answer) - [Result](#logic.v1beta2.Result) - [Substitution](#logic.v1beta2.Substitution) - - [Term](#logic.v1beta2.Term) - [logic/v1beta2/query.proto](#logic/v1beta2/query.proto) - [QueryServiceAskRequest](#logic.v1beta2.QueryServiceAskRequest) @@ -377,18 +376,7 @@ Substitution represents a substitution made to the variables in the query to obt | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | `variable` | [string](#string) | | variable is the name of the variable. | -| `term` | [Term](#logic.v1beta2.Term) | | term is the term that the variable is substituted with. | - - - -### Term - -Term is the representation of a piece of data and can be a constant, a variable, or an atom. - -| Field | Type | Label | Description | -| ----- | ---- | ----- | ----------- | -| `name` | [string](#string) | | name is the name of the term. | -| `arguments` | [Term](#logic.v1beta2.Term) | repeated | arguments are the arguments of the term, which can be constants, variables, or atoms. | +| `expression` | [string](#string) | | expression is the value substituted for the variable, represented directly as a Prolog term (e.g., atom, number, compound). | [//]: # (end messages)