Exemplo n.º 1
0
func (expected Err) assert(suite suite.Suite, obtainerCursor *r.Cursor, obtainedErr error) {
	// If the error is nil then attempt to read from the cursor and see if an
	// error is returned
	if obtainedErr == nil {
		var res []interface{}
		obtainedErr = obtainerCursor.All(&res)
	}

	if suite.Error(obtainedErr) {
		return
	}

	obtainedType := reflect.TypeOf(obtainedErr).String()
	obtainedMessage := strings.TrimPrefix(obtainedErr.Error(), "gorethink: ")
	obtainedMessage = exceptionRegex.ReplaceAllString(obtainedMessage, "${message}")

	suite.Equal(expected.Type, obtainedType)
	if expected.Regex != "" {
		suite.Regexp(expected.Regex, obtainedMessage)
	}
	if expected.Message != "" {
		suite.Equal(expected.Message, obtainedMessage)
	}
}
Exemplo n.º 2
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)
	}
}
Exemplo n.º 3
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)
	}
}