uuid_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package pq
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. )
  7. func TestDecodeUUIDBinaryError(t *testing.T) {
  8. t.Parallel()
  9. _, err := decodeUUIDBinary([]byte{0x12, 0x34})
  10. if err == nil {
  11. t.Fatal("Expected error, got none")
  12. }
  13. if !strings.HasPrefix(err.Error(), "pq:") {
  14. t.Errorf("Expected error to start with %q, got %q", "pq:", err.Error())
  15. }
  16. if !strings.Contains(err.Error(), "bad length: 2") {
  17. t.Errorf("Expected error to contain length, got %q", err.Error())
  18. }
  19. }
  20. func BenchmarkDecodeUUIDBinary(b *testing.B) {
  21. x := []byte{0x03, 0xa3, 0x52, 0x2f, 0x89, 0x28, 0x49, 0x87, 0x84, 0xd6, 0x93, 0x7b, 0x36, 0xec, 0x27, 0x6f}
  22. for i := 0; i < b.N; i++ {
  23. decodeUUIDBinary(x)
  24. }
  25. }
  26. func TestDecodeUUIDBackend(t *testing.T) {
  27. db := openTestConn(t)
  28. defer db.Close()
  29. var s = "a0ecc91d-a13f-4fe4-9fce-7e09777cc70a"
  30. var scanned interface{}
  31. err := db.QueryRow(`SELECT $1::uuid`, s).Scan(&scanned)
  32. if err != nil {
  33. t.Fatalf("Expected no error, got %v", err)
  34. }
  35. if !reflect.DeepEqual(scanned, []byte(s)) {
  36. t.Errorf("Expected []byte(%q), got %T(%q)", s, scanned, scanned)
  37. }
  38. }