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.
 
 
 

76 lines
2.4 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. {&NullGeography{Valid: true, GeographyVal: "ST_GEOPOINT(47.649154, -122.350220)"}, `"ST_GEOPOINT(47.649154, -122.350220)"`},
  36. {&NullTimestamp{Valid: true, Timestamp: testTimestamp}, `"2016-11-05T07:50:22.000000008Z"`},
  37. {&NullDate{Valid: true, Date: testDate}, `"2016-11-05"`},
  38. {&NullTime{Valid: true, Time: nullsTestTime}, `"07:50:22.000001"`},
  39. {&NullDateTime{Valid: true, DateTime: nullsTestDateTime}, `"2016-11-05 07:50:22.000001"`},
  40. {&NullInt64{}, `null`},
  41. {&NullFloat64{}, `null`},
  42. {&NullBool{}, `null`},
  43. {&NullString{}, `null`},
  44. {&NullGeography{}, `null`},
  45. {&NullTimestamp{}, `null`},
  46. {&NullDate{}, `null`},
  47. {&NullTime{}, `null`},
  48. {&NullDateTime{}, `null`},
  49. } {
  50. bytes, err := json.Marshal(test.in)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if got, want := string(bytes), test.want; got != want {
  55. t.Errorf("%#v: got %s, want %s", test.in, got, want)
  56. }
  57. typ := reflect.Indirect(reflect.ValueOf(test.in)).Type()
  58. value := reflect.New(typ).Interface()
  59. err = json.Unmarshal(bytes, value)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if !testutil.Equal(value, test.in) {
  64. t.Errorf("%#v: got %#v, want %#v", test.in, value, test.in)
  65. }
  66. }
  67. }