|
| 1 | +// Copyright 2025 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package aggregation |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + |
| 22 | + "github.com/dolthub/go-mysql-server/sql" |
| 23 | + "github.com/dolthub/go-mysql-server/sql/expression" |
| 24 | +) |
| 25 | + |
| 26 | +func TestStd(t *testing.T) { |
| 27 | + sum := NewStdDevPop(expression.NewGetField(0, nil, "", false)) |
| 28 | + |
| 29 | + testCases := []struct { |
| 30 | + name string |
| 31 | + rows []sql.Row |
| 32 | + expected interface{} |
| 33 | + }{ |
| 34 | + { |
| 35 | + "string int values", |
| 36 | + []sql.Row{{"1"}, {"2"}, {"3"}, {"4"}}, |
| 37 | + 1.118033988749895, |
| 38 | + }, |
| 39 | + { |
| 40 | + "string float values", |
| 41 | + []sql.Row{{"1.5"}, {"2"}, {"3"}, {"4"}}, |
| 42 | + 0.9601432184835761, |
| 43 | + }, |
| 44 | + { |
| 45 | + "string non-int values", |
| 46 | + []sql.Row{{"a"}, {"b"}, {"c"}, {"d"}}, |
| 47 | + float64(0), |
| 48 | + }, |
| 49 | + { |
| 50 | + "float values", |
| 51 | + []sql.Row{{1.}, {2.5}, {3.}, {4.}}, |
| 52 | + 1.0825317547305484, |
| 53 | + }, |
| 54 | + { |
| 55 | + "no rows", |
| 56 | + []sql.Row{}, |
| 57 | + nil, |
| 58 | + }, |
| 59 | + { |
| 60 | + "nil values", |
| 61 | + []sql.Row{{nil}, {nil}}, |
| 62 | + nil, |
| 63 | + }, |
| 64 | + { |
| 65 | + "int64 values", |
| 66 | + []sql.Row{{int64(1)}, {int64(3)}}, |
| 67 | + 1.0, |
| 68 | + }, |
| 69 | + { |
| 70 | + "int32 values", |
| 71 | + []sql.Row{{int32(1)}, {int32(3)}}, |
| 72 | + 1.0, |
| 73 | + }, |
| 74 | + { |
| 75 | + "int32 and nil values", |
| 76 | + []sql.Row{{int32(1)}, {int32(3)}, {nil}}, |
| 77 | + 1.0, |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tt := range testCases { |
| 82 | + t.Run(tt.name, func(t *testing.T) { |
| 83 | + require := require.New(t) |
| 84 | + |
| 85 | + ctx := sql.NewEmptyContext() |
| 86 | + buf, _ := sum.NewBuffer() |
| 87 | + for _, row := range tt.rows { |
| 88 | + require.NoError(buf.Update(ctx, row)) |
| 89 | + } |
| 90 | + |
| 91 | + result, err := buf.Eval(sql.NewEmptyContext()) |
| 92 | + require.NoError(err) |
| 93 | + require.Equal(tt.expected, result) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments