Beispiel #1
0
func assertExpected(suite suite.Suite, expected interface{}, obtainedCursor *r.Cursor, obtainedErr error) {
	if expected == compare.AnythingIsFine {
		suite.NoError(obtainedErr, "Query returned unexpected error")
		return
	}

	switch expected := expected.(type) {
	case Err:
		expected.assert(suite, obtainedCursor, obtainedErr)
	case Expected:
		expected.assert(suite, obtainedCursor, obtainedErr)
	default:
		Expected(compare.Expected{Val: expected}).assert(suite, obtainedCursor, obtainedErr)
	}
}
Beispiel #2
0
func (expected Expected) assert(suite suite.Suite, obtainedCursor *r.Cursor, obtainedErr error) {
	if suite.NoError(obtainedErr, "Query returned unexpected error") {
		return
	}

	expectedVal := reflect.ValueOf(expected.Val)

	// If expected value is nil then ensure cursor is nil (assume that an
	// invalid reflect value is because expected value is nil)
	if !expectedVal.IsValid() || (expectedVal.Kind() == reflect.Ptr && expectedVal.IsNil()) {
		suite.True(obtainedCursor.IsNil(), "Expected nil cursor")
		return
	}

	expectedType := expectedVal.Type()
	expectedKind := expectedType.Kind()

	if expectedKind == reflect.Array || expectedKind == reflect.Slice || expected.Fetch {
		if expectedType.Elem().Kind() == reflect.Uint8 {
			// Decode byte slices slightly differently
			var obtained = []byte{}
			err := obtainedCursor.One(&obtained)
			suite.NoError(err, "Error returned when reading query response")
			compare.Assert(suite.T(), expected, obtained)
		} else {
			var obtained = []interface{}{}
			if expected.Fetch {
				var v interface{}
				for obtainedCursor.Next(&v) {
					obtained = append(obtained, v)

					if expected.FetchCount != 0 && len(obtained) >= expected.FetchCount {
						break
					}
				}
				suite.NoError(obtainedCursor.Err(), "Error returned when reading query response")
			} else {
				err := obtainedCursor.All(&obtained)
				suite.NoError(err, "Error returned when reading query response")
			}

			compare.Assert(suite.T(), expected, obtained)
		}
	} else if expectedKind == reflect.Map {
		var obtained map[string]interface{}
		err := obtainedCursor.One(&obtained)
		suite.NoError(err, "Error returned when reading query response")
		compare.Assert(suite.T(), expected, obtained)
	} else {
		var obtained interface{}
		err := obtainedCursor.One(&obtained)
		suite.NoError(err, "Error returned when reading query response")
		compare.Assert(suite.T(), expected, obtained)
	}
}