You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

74 lines
2.2 KiB

  1. // Copyright 2015 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bigquery
  15. import (
  16. "encoding/json"
  17. "reflect"
  18. "testing"
  19. "cloud.google.com/go/civil"
  20. "cloud.google.com/go/internal/testutil"
  21. )
  22. var (
  23. nullsTestTime = civil.Time{Hour: 7, Minute: 50, Second: 22, Nanosecond: 1000}
  24. nullsTestDateTime = civil.DateTime{Date: civil.Date{Year: 2016, Month: 11, Day: 5}, Time: nullsTestTime}
  25. )
  26. func TestNullsJSON(t *testing.T) {
  27. for _, test := range []struct {
  28. in interface{}
  29. want string
  30. }{
  31. {&NullInt64{Valid: true, Int64: 3}, `3`},
  32. {&NullFloat64{Valid: true, Float64: 3.14}, `3.14`},
  33. {&NullBool{Valid: true, Bool: true}, `true`},
  34. {&NullString{Valid: true, StringVal: "foo"}, `"foo"`},
  35. {&NullTimestamp{Valid: true, Timestamp: testTimestamp}, `"2016-11-05T07:50:22.000000008Z"`},
  36. {&NullDate{Valid: true, Date: testDate}, `"2016-11-05"`},
  37. {&NullTime{Valid: true, Time: nullsTestTime}, `"07:50:22.000001"`},
  38. {&NullDateTime{Valid: true, DateTime: nullsTestDateTime}, `"2016-11-05 07:50:22.000001"`},
  39. {&NullInt64{}, `null`},
  40. {&NullFloat64{}, `null`},
  41. {&NullBool{}, `null`},
  42. {&NullString{}, `null`},
  43. {&NullTimestamp{}, `null`},
  44. {&NullDate{}, `null`},
  45. {&NullTime{}, `null`},
  46. {&NullDateTime{}, `null`},
  47. } {
  48. bytes, err := json.Marshal(test.in)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if got, want := string(bytes), test.want; got != want {
  53. t.Errorf("%#v: got %s, want %s", test.in, got, want)
  54. }
  55. typ := reflect.Indirect(reflect.ValueOf(test.in)).Type()
  56. value := reflect.New(typ).Interface()
  57. err = json.Unmarshal(bytes, value)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if !testutil.Equal(value, test.in) {
  62. t.Errorf("%#v: got %#v, want %#v", test.in, value, test.in)
  63. }
  64. }
  65. }