Skip to content

Commit

Permalink
Added negative and misc expression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchiecarroll committed Oct 13, 2021
1 parent 4bb9f5f commit 049a80c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sttp/data/ExpressionTree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2765,6 +2765,7 @@ func (et *ExpressionTree) subtractOp(leftValue, rightValue *ValueExpression, val
}
}

//gocyclo: ignore
func (et *ExpressionTree) bitShiftLeftOp(leftValue, rightValue *ValueExpression) (*ValueExpression, error) {
// If left is Null, result is Null
if leftValue.IsNull() {
Expand Down Expand Up @@ -2814,6 +2815,7 @@ func (et *ExpressionTree) bitShiftLeftOp(leftValue, rightValue *ValueExpression)
}
}

//gocyclo: ignore
func (et *ExpressionTree) bitShiftRightOp(leftValue, rightValue *ValueExpression) (*ValueExpression, error) {
// If left is Null, result is Null
if leftValue.IsNull() {
Expand Down
96 changes: 96 additions & 0 deletions sttp/data/ExpressionTree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2485,6 +2485,102 @@ func TestBasicExpressions(t *testing.T) {
}
}

func TestNegativeExpressions(t *testing.T) {
_, err := EvaluateExpression("Convert(123, 'unknown')", false)

if err == nil {
t.Fatal("TestNegativeExpressions: expected error during EvaluateDataRowExpression")
}

_, err = EvaluateExpression("I am a bad expression", false)

if err == nil {
t.Fatal("TestNegativeExpressions: expected error during EvaluateDataRowExpression")
}
}

//gocyclo: ignore
func TestMiscExpressions(t *testing.T) {
var doc xml.XmlDocument
err := doc.LoadXmlFromFile("../../test/MetadataSample2.xml")

if err != nil {
t.Fatal("TestMiscExpressions: error loading XML document: " + err.Error())
}

dataSet := NewDataSet()
err = dataSet.ParseXmlDocument(&doc)

if err != nil {
t.Fatal("TestMiscExpressions: error loading DataSet from XML document: " + err.Error())
}

dataRow := dataSet.Table("DeviceDetail").Row(0)

valueExpression, err := EvaluateDataRowExpression(dataRow, "AccessID ^ 2 + FramesPerSecond XOR 4", false)

if err != nil {
t.Fatal("TestMiscExpressions: error during EvaluateDataRowExpression: " + err.Error())
}

if valueExpression.ValueType() != ExpressionValueType.Int32 {
t.Fatal("TestMiscExpressions: unexpected value expression type: " + valueExpression.Type().String())
}

i32, err := valueExpression.Int32Value()

if err != nil {
t.Fatal("TestMiscExpressions: error getting value: " + err.Error())
}

if i32 != 38 {
t.Fatal("TestMiscExpressions: unexpected value expression result")
}

// test computed column with expression defined in schema
g := guid.New()
valueExpression, err = EvaluateDataRowExpression(dataRow, g.String(), false)

if err != nil {
t.Fatal("TestMiscExpressions: error during EvaluateDataRowExpression: " + err.Error())
}

if valueExpression.ValueType() != ExpressionValueType.Guid {
t.Fatal("TestMiscExpressions: unexpected value expression type: " + valueExpression.Type().String())
}

ge, err := valueExpression.GuidValue()

if err != nil {
t.Fatal("TestMiscExpressions: error getting value: " + err.Error())
}

if !g.Equal(ge) {
t.Fatal("TestMiscExpressions: unexpected value expression result")
}

// test edge case of evaluating standalone Guid not used as a row identifier
valueExpression, err = EvaluateDataRowExpression(dataRow, "ComputedCol", false)

if err != nil {
t.Fatal("TestMiscExpressions: error during EvaluateDataRowExpression: " + err.Error())
}

if valueExpression.ValueType() != ExpressionValueType.Int32 {
t.Fatal("TestMiscExpressions: unexpected value expression type: " + valueExpression.Type().String())
}

i32, err = valueExpression.Int32Value()

if err != nil {
t.Fatal("TestMiscExpressions: error getting value: " + err.Error())
}

if i32 != 32 {
t.Fatal("TestMiscExpressions: unexpected value expression result")
}
}

func TestFilterExpressionStatementCount(t *testing.T) {
dataSet, _, _, statID, freqID := createDataSet()

Expand Down

0 comments on commit 049a80c

Please sign in to comment.