session_context_test.go 828 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "context"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestQueryContext(t *testing.T) {
  12. type ContextQueryStruct struct {
  13. Id int64
  14. Name string
  15. }
  16. assert.NoError(t, prepareEngine())
  17. assertSync(t, new(ContextQueryStruct))
  18. _, err := testEngine.Insert(&ContextQueryStruct{Name: "1"})
  19. assert.NoError(t, err)
  20. ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
  21. defer cancel()
  22. time.Sleep(time.Nanosecond)
  23. has, err := testEngine.Context(ctx).Exist(&ContextQueryStruct{Name: "1"})
  24. assert.Error(t, err)
  25. assert.Contains(t, err.Error(), "context deadline exceeded")
  26. assert.False(t, has)
  27. }