|
| 1 | +package expressions |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + . "github.com/smartystreets/goconvey/convey" |
| 8 | + |
| 9 | + "github.com/MontFerret/ferret/pkg/runtime/collections" |
| 10 | + "github.com/MontFerret/ferret/pkg/runtime/core" |
| 11 | + "github.com/MontFerret/ferret/pkg/runtime/expressions/clauses" |
| 12 | + "github.com/MontFerret/ferret/pkg/runtime/values" |
| 13 | +) |
| 14 | + |
| 15 | +type ( |
| 16 | + testIterator struct { |
| 17 | + values []*core.Scope |
| 18 | + pos int |
| 19 | + causeErrorInNext bool |
| 20 | + } |
| 21 | + testIterable struct { |
| 22 | + values []*core.Scope |
| 23 | + causeErrorInIterate bool |
| 24 | + causeErrorInNext bool |
| 25 | + } |
| 26 | + testExpression struct { |
| 27 | + causeErrorInExec bool |
| 28 | + } |
| 29 | + testError struct{} |
| 30 | +) |
| 31 | + |
| 32 | +func (iterator *testIterator) Next(ctx context.Context, scope *core.Scope) (*core.Scope, error) { |
| 33 | + if iterator.causeErrorInNext { |
| 34 | + return nil, testError{} |
| 35 | + } |
| 36 | + |
| 37 | + if len(iterator.values) > iterator.pos { |
| 38 | + val := iterator.values[iterator.pos] |
| 39 | + iterator.pos++ |
| 40 | + |
| 41 | + return val, nil |
| 42 | + } |
| 43 | + |
| 44 | + return nil, core.ErrNoMoreData |
| 45 | +} |
| 46 | + |
| 47 | +func (iterable *testIterable) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) { |
| 48 | + if iterable.causeErrorInIterate { |
| 49 | + return nil, testError{} |
| 50 | + } |
| 51 | + |
| 52 | + return &testIterator{iterable.values, 0, iterable.causeErrorInNext}, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (expression *testExpression) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) { |
| 56 | + if expression.causeErrorInExec { |
| 57 | + return nil, testError{} |
| 58 | + } |
| 59 | + return nil, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (error testError) Error() string { |
| 63 | + return "error" |
| 64 | +} |
| 65 | + |
| 66 | +func testInitTestIterable(values []*core.Scope, causeErrorInIterate, causeErrorInNext bool) *testIterable { |
| 67 | + return &testIterable{values, causeErrorInIterate, causeErrorInNext} |
| 68 | +} |
| 69 | + |
| 70 | +func TestNewForExpression(t *testing.T) { |
| 71 | + Convey(".NewForExpression", t, func() { |
| 72 | + Convey("Should return new ForExpresssion.", func() { |
| 73 | + forExp, err := NewForExpression(core.SourceMap{}, testInitTestIterable([]*core.Scope{}, false, false), &testExpression{}, false, false, false) |
| 74 | + So(forExp, ShouldNotBeNil) |
| 75 | + So(err, ShouldBeNil) |
| 76 | + }) |
| 77 | + |
| 78 | + Convey("Should return error when a dataSource is nil", func() { |
| 79 | + forExp, err := NewForExpression(core.SourceMap{}, nil, &testExpression{}, false, false, false) |
| 80 | + So(forExp, ShouldBeNil) |
| 81 | + So(err, ShouldNotBeNil) |
| 82 | + So(err, ShouldEqual, err) |
| 83 | + }) |
| 84 | + |
| 85 | + Convey("Should return error when a predicate is nil", func() { |
| 86 | + forExp, err := NewForExpression(core.SourceMap{}, testInitTestIterable([]*core.Scope{}, false, false), nil, false, false, false) |
| 87 | + So(forExp, ShouldBeNil) |
| 88 | + So(err, ShouldNotBeNil) |
| 89 | + So(err, ShouldEqual, err) |
| 90 | + }) |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +func TestAddLimit(t *testing.T) { |
| 95 | + Convey(".AddLimit", t, func() { |
| 96 | + Convey("Should success. (An Error Should be nil.)", func() { |
| 97 | + forExpression, _ := NewForExpression( |
| 98 | + core.SourceMap{}, |
| 99 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 100 | + &testExpression{}, |
| 101 | + false, |
| 102 | + false, |
| 103 | + false, |
| 104 | + ) |
| 105 | + |
| 106 | + err := forExpression.AddLimit(core.SourceMap{}, &testExpression{}, &testExpression{}) |
| 107 | + |
| 108 | + So(err, ShouldBeNil) |
| 109 | + }) |
| 110 | + |
| 111 | + Convey("Should return an error.", func() { |
| 112 | + forExpression, _ := NewForExpression( |
| 113 | + core.SourceMap{}, |
| 114 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 115 | + &testExpression{}, |
| 116 | + false, |
| 117 | + false, |
| 118 | + false, |
| 119 | + ) |
| 120 | + forExpression.dataSource = nil |
| 121 | + |
| 122 | + err := forExpression.AddLimit(core.SourceMap{}, &testExpression{}, &testExpression{}) |
| 123 | + |
| 124 | + So(err, ShouldNotBeNil) |
| 125 | + }) |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func TestAddFilter(t *testing.T) { |
| 130 | + Convey(".AddFilter", t, func() { |
| 131 | + Convey("Should success. (An Error Should be nil.)", func() { |
| 132 | + forExpression, _ := NewForExpression( |
| 133 | + core.SourceMap{}, |
| 134 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 135 | + &testExpression{}, |
| 136 | + false, |
| 137 | + false, |
| 138 | + false, |
| 139 | + ) |
| 140 | + |
| 141 | + err := forExpression.AddFilter(core.SourceMap{}, &testExpression{}) |
| 142 | + |
| 143 | + So(err, ShouldBeNil) |
| 144 | + }) |
| 145 | + |
| 146 | + Convey("Should return an error.", func() { |
| 147 | + forExpression, _ := NewForExpression( |
| 148 | + core.SourceMap{}, |
| 149 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 150 | + &testExpression{}, |
| 151 | + false, |
| 152 | + false, |
| 153 | + false, |
| 154 | + ) |
| 155 | + forExpression.dataSource = nil |
| 156 | + |
| 157 | + err := forExpression.AddFilter(core.SourceMap{}, &testExpression{}) |
| 158 | + |
| 159 | + So(err, ShouldNotBeNil) |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | +func TestAddSort(t *testing.T) { |
| 166 | + Convey(".AddSort", t, func() { |
| 167 | + Convey("Should success.(An Error Should be nil.)", func() { |
| 168 | + forExpression, _ := NewForExpression( |
| 169 | + core.SourceMap{}, |
| 170 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 171 | + &testExpression{}, |
| 172 | + false, |
| 173 | + false, |
| 174 | + false, |
| 175 | + ) |
| 176 | + |
| 177 | + err := forExpression.AddSort(core.SourceMap{}, &clauses.SorterExpression{}) |
| 178 | + |
| 179 | + So(err, ShouldBeNil) |
| 180 | + }) |
| 181 | + |
| 182 | + Convey("Should return an error.", func() { |
| 183 | + forExpression, _ := NewForExpression( |
| 184 | + core.SourceMap{}, |
| 185 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 186 | + &testExpression{}, |
| 187 | + false, |
| 188 | + false, |
| 189 | + false, |
| 190 | + ) |
| 191 | + forExpression.dataSource = nil |
| 192 | + |
| 193 | + err := forExpression.AddSort(core.SourceMap{}, &clauses.SorterExpression{}) |
| 194 | + |
| 195 | + So(err, ShouldNotBeNil) |
| 196 | + }) |
| 197 | + }) |
| 198 | +} |
| 199 | + |
| 200 | +func TestAddCollect(t *testing.T) { |
| 201 | + Convey(".AddCollect", t, func() { |
| 202 | + Convey("Should success. (Error Should be nil.)", func() { |
| 203 | + forExpression, _ := NewForExpression( |
| 204 | + core.SourceMap{}, |
| 205 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 206 | + &testExpression{}, |
| 207 | + false, |
| 208 | + false, |
| 209 | + false, |
| 210 | + ) |
| 211 | + |
| 212 | + err := forExpression.AddCollect(core.SourceMap{}, &clauses.Collect{}) |
| 213 | + |
| 214 | + So(err, ShouldBeNil) |
| 215 | + }) |
| 216 | + |
| 217 | + Convey("Should return an error.", func() { |
| 218 | + forExpression, _ := NewForExpression( |
| 219 | + core.SourceMap{}, |
| 220 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 221 | + &testExpression{}, |
| 222 | + false, |
| 223 | + false, |
| 224 | + false, |
| 225 | + ) |
| 226 | + forExpression.dataSource = nil |
| 227 | + |
| 228 | + err := forExpression.AddCollect(core.SourceMap{}, &clauses.Collect{}) |
| 229 | + |
| 230 | + So(err, ShouldNotBeNil) |
| 231 | + }) |
| 232 | + }) |
| 233 | +} |
| 234 | + |
| 235 | +func TestAddStatement(t *testing.T) { |
| 236 | + Convey(".AddStatement (Error Should be nil.)", t, func() { |
| 237 | + Convey("Should success.", func() { |
| 238 | + forExpression, _ := NewForExpression( |
| 239 | + core.SourceMap{}, |
| 240 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 241 | + &testExpression{}, |
| 242 | + false, |
| 243 | + false, |
| 244 | + false, |
| 245 | + ) |
| 246 | + |
| 247 | + err := forExpression.AddStatement(&testExpression{}) |
| 248 | + |
| 249 | + So(err, ShouldBeNil) |
| 250 | + }) |
| 251 | + |
| 252 | + Convey("Should return an error.", func() { |
| 253 | + forExpression, _ := NewForExpression( |
| 254 | + core.SourceMap{}, |
| 255 | + testInitTestIterable([]*core.Scope{}, false, false), |
| 256 | + &testExpression{}, |
| 257 | + false, |
| 258 | + false, |
| 259 | + false, |
| 260 | + ) |
| 261 | + forExpression.dataSource = nil |
| 262 | + |
| 263 | + err := forExpression.AddStatement(&testExpression{}) |
| 264 | + |
| 265 | + So(err, ShouldNotBeNil) |
| 266 | + }) |
| 267 | + }) |
| 268 | +} |
| 269 | + |
| 270 | +func TestExec(t *testing.T) { |
| 271 | + Convey(".Exec", t, func() { |
| 272 | + Convey("Should success.", func() { |
| 273 | + forExpression, _ := NewForExpression( |
| 274 | + core.SourceMap{}, |
| 275 | + testInitTestIterable([]*core.Scope{{}, {}, {}}, false, false), |
| 276 | + &testExpression{}, |
| 277 | + false, |
| 278 | + false, |
| 279 | + false, |
| 280 | + ) |
| 281 | + |
| 282 | + _, err := forExpression.Exec(context.Background(), &core.Scope{}) |
| 283 | + |
| 284 | + So(err, ShouldBeNil) |
| 285 | + }) |
| 286 | + |
| 287 | + Convey("Should stop an execution when context is cancelled.", func() { |
| 288 | + forExpression, _ := NewForExpression( |
| 289 | + core.SourceMap{}, |
| 290 | + testInitTestIterable([]*core.Scope{{}, {}, {}}, false, false), |
| 291 | + &testExpression{}, |
| 292 | + false, |
| 293 | + false, |
| 294 | + false, |
| 295 | + ) |
| 296 | + |
| 297 | + ctx0 := context.Background() |
| 298 | + ctx1, cancelFn := context.WithCancel(ctx0) |
| 299 | + cancelFn() |
| 300 | + |
| 301 | + _, err := forExpression.Exec(ctx1, &core.Scope{}) |
| 302 | + |
| 303 | + So(err, ShouldNotBeNil) |
| 304 | + }) |
| 305 | + |
| 306 | + Convey("Should return an error when a dataSource expression is invalid.", func() { |
| 307 | + forExpression, _ := NewForExpression( |
| 308 | + core.SourceMap{}, |
| 309 | + testInitTestIterable([]*core.Scope{{}, {}, {}}, true, false), |
| 310 | + &testExpression{}, |
| 311 | + false, |
| 312 | + false, |
| 313 | + false, |
| 314 | + ) |
| 315 | + |
| 316 | + result, err := forExpression.Exec(context.Background(), &core.Scope{}) |
| 317 | + So(result, ShouldEqual, values.None) |
| 318 | + So(err, ShouldNotBeNil) |
| 319 | + }) |
| 320 | + |
| 321 | + Convey("Should return an error when element expressions of dataSource is invalid.", func() { |
| 322 | + forExpression, _ := NewForExpression( |
| 323 | + core.SourceMap{}, |
| 324 | + testInitTestIterable([]*core.Scope{{}, {}, {}}, false, true), |
| 325 | + &testExpression{}, |
| 326 | + false, |
| 327 | + false, |
| 328 | + false, |
| 329 | + ) |
| 330 | + |
| 331 | + result, err := forExpression.Exec(context.Background(), &core.Scope{}) |
| 332 | + So(result, ShouldEqual, values.None) |
| 333 | + So(err, ShouldNotBeNil) |
| 334 | + }) |
| 335 | + |
| 336 | + Convey("Should return an error when an predicate expression is invalidated.", func() { |
| 337 | + forExpression, _ := NewForExpression( |
| 338 | + core.SourceMap{}, |
| 339 | + testInitTestIterable([]*core.Scope{{}, {}, {}}, false, false), |
| 340 | + &testExpression{true}, |
| 341 | + false, |
| 342 | + false, |
| 343 | + false, |
| 344 | + ) |
| 345 | + |
| 346 | + result, err := forExpression.Exec(context.Background(), &core.Scope{}) |
| 347 | + So(result, ShouldEqual, values.None) |
| 348 | + So(err, ShouldNotBeNil) |
| 349 | + }) |
| 350 | + }) |
| 351 | +} |
0 commit comments