Skip to content

Commit

Permalink
updated format tests to test format structs directly
Browse files Browse the repository at this point in the history
  • Loading branch information
jlgerber committed Jan 24, 2016
1 parent e103196 commit 97a4573
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 31 deletions.
82 changes: 68 additions & 14 deletions format1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
func TestMain(m *testing.M) {
//f := SetupLog()
log.Info("TestMain setup")
manager := GetQPManager()

res := m.Run()
f.Close()
//f.Close()
os.Exit(res)
}

Expand All @@ -43,6 +43,7 @@ func SetupLog() *os.File {
// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *Format1TestSuite) SetupSuite() {
manager := GetQPManager()
log.Info(" -- Format1 Test Suite --\n")
log.Debug("Format1TestSuite.SetupSuite() - setting active parsers to format1")
manager.SetActiveParsers("format1")
Expand All @@ -67,27 +68,45 @@ func (suite *Format1TestSuite) TestActiveParsers() {
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementRegExpError() {
_, err := parseQuery("()")
testString := "()"
f := &Format1{}

tf := f.CanParseString(testString)
suite.Equal(false, tf, "Should not be able to parse string")

_, err := f.ParseString(testString)

expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "No QueryFormats can parse input",
Message: "Invalid query format",
}
suite.Equal(expectedError, err, "Should be formating error")
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementFilterFormatError() {
_, err := parseQuery("and(foo, bar)")
testString := "and(foo, bar)"
f := &Format1{}

tf := f.CanParseString(testString)
suite.Equal(false, tf, "Should not be able to parse string")

_, err := f.ParseString(testString)

expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "No QueryFormats can parse input",
Message: "Invalid query filter format",
}
suite.Equal(expectedError, err, "Should be formating error")
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementJsonError() {
_, err := parseQuery("and([foo, bar,])")
testString := "and([foo, bar,])"
f := &Format1{}

tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should not be able to parse string")

_, err := f.ParseString(testString)

expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Expand All @@ -97,17 +116,29 @@ func (suite *Format1TestSuite) TestParseQueryAndOrStatementJsonError() {
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementNotAQuery() {
_, err := parseQuery("andwell_this_is_bad")
testString := "andwell_this_is_bad"
f := &Format1{}

tf := f.CanParseString(testString)
suite.Equal(false, tf, "Should not be able to parse string")

_, err := f.ParseString(testString)

expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "No QueryFormats can parse input",
Message: "Invalid query format",
}
suite.Equal(expectedError, err, "Should be formating error")
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementSliceOfFilters() {
rf, err := parseQuery(`and([["name", "is", "blorg"]])`)
testString := `and([["name", "is", "blorg"]])`
f := &Format1{}

tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should not be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
Expand All @@ -117,8 +148,13 @@ func (suite *Format1TestSuite) TestParseQueryAndOrStatementSliceOfFilters() {
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicAnd() {
testString := `and(["name", "is", "blorg"])`
f := &Format1{}

rf, err := parseQuery(`and(["name", "is", "blorg"])`)
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should not be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
Expand All @@ -128,7 +164,13 @@ func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicAnd() {
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicAndUpper() {
rf, err := parseQuery(`AND(["name", "is", "blorg"])`)
testString := `AND(["name", "is", "blorg"])`
f := &Format1{}

tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.AddCondition(newQueryCondition("name", "is", "blorg"))
Expand All @@ -138,7 +180,13 @@ func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicAndUpper() {
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicOr() {
rf, err := parseQuery(`or(["name", "is", "blorg"])`)
testString := `or(["name", "is", "blorg"])`
f := &Format1{}

tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.LogicalOperator = "or"
Expand All @@ -149,7 +197,13 @@ func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicOr() {
}

func (suite *Format1TestSuite) TestParseQueryAndOrStatementBasicOrUpper() {
rf, err := parseQuery(`OR(["name", "is", "blorg"])`)
testString := `OR(["name", "is", "blorg"])`
f := &Format1{}

tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.LogicalOperator = "or"
Expand Down
61 changes: 44 additions & 17 deletions format2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ func TestFormat2TestSuite(t *testing.T) {
}

func (suite *Format2TestSuite) TestParseQueryHashJsonError() {
_, err := parseQuery("{foo}")
f := &Format2{}
testString := "{foo}"
tf := f.CanParseString(testString)
suite.Equal(false, tf, "Should not be able to parse string")

_, err := f.ParseString(testString)

expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Expand All @@ -43,7 +48,12 @@ func (suite *Format2TestSuite) TestParseQueryHashJsonError() {
}

func (suite *Format2TestSuite) TestParseQueryHashConditionsOnly() {
_, err := parseQuery(`{"conditions": [["name", "is", "blorg"]]}`)
f := &Format2{}
teststring := `{"conditions": [["name", "is", "blorg"]]}`
tf := f.CanParseString(teststring)
suite.Equal(tf, false, "Should not be able to parse string")

_, err := f.ParseString(teststring)

expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Expand All @@ -53,27 +63,27 @@ func (suite *Format2TestSuite) TestParseQueryHashConditionsOnly() {
}

func (suite *Format2TestSuite) TestParseQueryHashLogicalOpOnly() {
_, err := parseQuery(`{"logical_operator": "and"}`)

expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "Missing key: 'conditions'",
}
suite.Equal(expectedError, err, "Should a json error")
}
f := &Format2{}
testString := `{"logical_operator": "and"}`
tf := f.CanParseString(testString)
suite.Equal(false, tf, "Should not be able to parse string")

func (suite *Format2TestSuite) TestParseQueryArrayJsonError() {
_, err := parseQuery("[[foo]]")
_, err := f.ParseString(testString)

expectedError := queryParseError{
StatusCode: http.StatusBadRequest,
Message: "invalid character 'o' in literal false (expecting 'a')",
Message: "Missing key: 'conditions'",
}
suite.Equal(expectedError, err, "Should a json error")
}

func (suite *Format2TestSuite) TestParseQueryHashStatementBasicAnd() {
rf, err := parseQuery(`{"logical_operator": "and", "conditions": [["name", "is", "blorg"]]}`)
f := &Format2{}
testString := `{"logical_operator": "and", "conditions": [["name", "is", "blorg"]]}`
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.LogicalOperator = "and"
Expand All @@ -84,7 +94,12 @@ func (suite *Format2TestSuite) TestParseQueryHashStatementBasicAnd() {
}

func (suite *Format2TestSuite) TestParseQueryHashStatementBasicOr() {
rf, err := parseQuery(`{"logical_operator": "or", "conditions": [["name", "is", "blorg"]]}`)
f := &Format2{}
testString := `{"logical_operator": "or", "conditions": [["name", "is", "blorg"]]}`
tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.LogicalOperator = "or"
Expand All @@ -95,7 +110,13 @@ func (suite *Format2TestSuite) TestParseQueryHashStatementBasicOr() {
}

func (suite *Format2TestSuite) TestParseQueryHashStatementAnd() {
rf, err := parseQuery(`{"logical_operator": "and", "conditions": [["name", "is", "blorg"], ["sg_status", "in", ["Active", "Bidding"]]]}`)
testString := `{"logical_operator": "and", "conditions": [["name", "is", "blorg"], ["sg_status", "in", ["Active", "Bidding"]]]}`
f := &Format2{}

tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.LogicalOperator = "and"
Expand All @@ -107,7 +128,13 @@ func (suite *Format2TestSuite) TestParseQueryHashStatementAnd() {
}

func (suite *Format2TestSuite) TestParseQueryHashStatementOr() {
rf, err := parseQuery(`{"logical_operator": "or", "conditions": [["name", "is", "blorg"]]}`)
testString := `{"logical_operator": "or", "conditions": [["name", "is", "blorg"]]}`
f := &Format2{}

tf := f.CanParseString(testString)
suite.Equal(true, tf, "Should be able to parse string")

rf, err := f.ParseString(testString)

rfExpected := newReadFilters()
rfExpected.LogicalOperator = "or"
Expand Down

0 comments on commit 97a4573

Please sign in to comment.