Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3v
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
Expand Down Expand Up @@ -503,6 +504,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
Expand All @@ -526,6 +528,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
183 changes: 182 additions & 1 deletion internal/extensions/basestream/basestream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func Test_Index(t *testing.T) {
b.sqlGetRangeValue("2024-01-01", "2024-01-02"): mockDateScalar("value", []utils.ValueWithDate{
{Date: "2024-01-01", Value: 150000},
{Date: "2024-01-02", Value: 300000},
}), // 150.000, 300.000
}), // 150.000, 300.000
b.sqlGetLastBefore("2024-01-01"): mockDateScalar("value", []utils.ValueWithDate{{Date: "2024-01-01", Value: 266666}}), // 266.666
}

app := &common.App{
Expand All @@ -52,6 +53,9 @@ func Test_Index(t *testing.T) {

assert.NoError(t, err)
assert.Equal(t, []utils.ValueWithDate{{Date: "2024-01-01", Value: 200000}, {Date: "2024-01-02", Value: 400000}}, returned) // 200.000 * 1000, 400.000 * 1000

returned, err = b.index(scope, app, "2024-01-01", nil)
assert.NoError(t, err)
}

func Test_Value(t *testing.T) {
Expand Down Expand Up @@ -127,3 +131,180 @@ func (m *mockQuerier) Execute(ctx context.Context, tx sql.DB, dbid, query string
}
return res, nil
}

type baseStreamTest struct {
ctx *precompiles.DeploymentContext
scope *precompiles.ProcedureContext
app *common.App
baseStream *BaseStreamExt
}

func newBaseStreamTest() *baseStreamTest {
return &baseStreamTest{
ctx: &precompiles.DeploymentContext{
Schema: &common.Schema{
Tables: []*common.Table{
{
Name: "price",
Columns: []*common.Column{
{
Name: "date",
Type: common.TEXT,
},
{
Name: "value",
Type: common.INT,
},
},
},
},
},
},
scope: &precompiles.ProcedureContext{},
app: &common.App{},
baseStream: &BaseStreamExt{},
}
}

func TestInitializeBasestream(t *testing.T) {
metadata := map[string]string{
"table_name": "price",
"date_column": "date",
"value_column": "value",
}

instance := newBaseStreamTest()
t.Run("success - it should initialize the basestream", func(t *testing.T) {
_, err := InitializeBasestream(instance.ctx, nil, metadata)
assert.NoError(t, err)
})

t.Run("validation - it should return an error if the table does not exist", func(t *testing.T) {
wrongMetadata := map[string]string{
"wrong_table_name": "price",
}
_, err := InitializeBasestream(instance.ctx, nil, wrongMetadata)
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing table")
})

t.Run("validation - it should return date type must be text", func(t *testing.T) {
wrongInstance := newBaseStreamTest()
wrongInstance.ctx.Schema.Tables[0].Columns[0].Type = common.INT
_, err := InitializeBasestream(wrongInstance.ctx, nil, metadata)
assert.Error(t, err)
assert.Contains(t, err.Error(), "date column date must be of type TEXT")
})

t.Run("validation - it should return value type must be int", func(t *testing.T) {
wrongInstance := newBaseStreamTest()
wrongInstance.ctx.Schema.Tables[0].Columns[1].Type = common.TEXT
_, err := InitializeBasestream(wrongInstance.ctx, nil, metadata)
assert.Error(t, err)
assert.Contains(t, err.Error(), "value column value must be of type INT")
})

t.Run("validation - it should return an error if the date column does not exist", func(t *testing.T) {
wrongMetadata := map[string]string{
"table_name": "price",
"date_column": "wrong_date",
"value_column": "value",
}
_, err := InitializeBasestream(instance.ctx, nil, wrongMetadata)
assert.Error(t, err)
assert.Contains(t, err.Error(), "not found")
})

t.Run("validation - it should return an error if the value column does not exist", func(t *testing.T) {
wrongMetadata := map[string]string{
"table_name": "price",
"date_column": "date",
"value_column": "wrong_value",
}
_, err := InitializeBasestream(instance.ctx, nil, wrongMetadata)
assert.Error(t, err)
assert.Contains(t, err.Error(), "not found")
})

t.Run("validation - it should return an error if the table does not exist", func(t *testing.T) {
wrongMetadata := map[string]string{
"table_name": "wrong_table",
"date_column": "date",
}
_, err := InitializeBasestream(instance.ctx, nil, wrongMetadata)
assert.Error(t, err)
assert.Contains(t, err.Error(), "not found")
})
}

func TestBaseStreamExt_Call(t *testing.T) {
instance := newBaseStreamTest()
mockEngine := mocks.NewEngine(t)
instance.app.Engine = mockEngine
//instance.scope.SetValue("caller", "caller")
//instance.scope.SetValue("args", "args")

t.Run("success - it should return the index", func(t *testing.T) {
mockEngine.ExpectedCalls = nil
mockEngine.EXPECT().Execute(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(mockDateScalar("value", []utils.ValueWithDate{{Date: "2024-01-01", Value: 200000}}), nil)
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_index", []any{"2024-01-01", "2024-01-02"})
assert.NoError(t, err)
})

t.Run("success - it should return the value", func(t *testing.T) {
mockEngine.ExpectedCalls = nil
mockEngine.EXPECT().Execute(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(mockDateScalar("value", []utils.ValueWithDate{{Date: "2024-01-01", Value: 150000}}), nil)
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_value", []any{"2024-01-01", "2024-01-02"})
assert.NoError(t, err)
})

t.Run("validation - it should return an error if the method is unknown", func(t *testing.T) {
_, err := instance.baseStream.Call(instance.scope, instance.app, "unknown", nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "unknown method")
})

t.Run("validation - it should return expected 2 inputs when args are not 2", func(t *testing.T) {
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_index", []any{"2024-01-01"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "expected 2 arguments")
})

t.Run("validation - it should return expected string when date is not a string", func(t *testing.T) {
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_index", []any{1, "2024-01-02"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "expected string")
})

t.Run("validation - it should return invalid date_to when date_to is not a valid date", func(t *testing.T) {
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_index", []any{"2024-01-01", 1})
assert.Error(t, err)
assert.Contains(t, err.Error(), "expected string for date_to")
})

t.Run("validation - it should return invalid date when date is not a valid date", func(t *testing.T) {
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_index", []any{"wrong_date", "2024-01-02"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid date")
})

t.Run("validation - it should return invalid date when date_to is not a valid date", func(t *testing.T) {
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_index", []any{"2024-01-01", "wrong_date"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid date")
})

t.Run("validation - it should return is before date when date_to is before date", func(t *testing.T) {
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_index", []any{"2024-01-02", "2024-01-01"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "is before date")
})

t.Run("error - it should return error when the engine returns an error", func(t *testing.T) {
mockEngine.ExpectedCalls = nil
mockEngine.EXPECT().Execute(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, assert.AnError)
_, err := instance.baseStream.Call(instance.scope, instance.app, "get_index", []any{"2024-01-01", "2024-01-02"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "error getting current value on db execute")
})
}
6 changes: 1 addition & 5 deletions internal/extensions/compose_streams/compose_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func InitializeStream(ctx *precompiles.DeploymentContext, service *common.Servic
if err != nil {
return nil, err
}
DBID, err := utils.GetDBIDFromPath(ctx, dbIdOrPath)
if err != nil {
return nil, err
}
DBID := utils.GetDBIDFromPath(ctx, dbIdOrPath)
totalWeight += weightInt
weightMap[DBID] = weightInt
}
Expand Down Expand Up @@ -123,7 +120,6 @@ func (s *Stream) CalculateWeightedResultsWithFn(fn func(string) ([]utils.ValueWi
// for each database, get the value and multiply by the weight
for dbId, weight := range s.weightMap {
results, err := fn(dbId)
//results, err := CallOnTargetDBID(scoper, method, dbId, date, dateTo)
if err != nil {
fmt.Println("error getting results from dbid", dbId, ":", err)
return nil, err
Expand Down
Loading