...

Source file src/cloud.google.com/go/bigquery/nulls_test.go

Documentation: cloud.google.com/go/bigquery

     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  
    15  package bigquery
    16  
    17  import (
    18  	"encoding/json"
    19  	"math"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"cloud.google.com/go/civil"
    24  	"cloud.google.com/go/internal/testutil"
    25  )
    26  
    27  var (
    28  	nullsTestTime     = civil.Time{Hour: 7, Minute: 50, Second: 22, Nanosecond: 1000}
    29  	nullsTestDateTime = civil.DateTime{Date: civil.Date{Year: 2016, Month: 11, Day: 5}, Time: nullsTestTime}
    30  )
    31  
    32  func TestNullsJSON(t *testing.T) {
    33  	for _, test := range []struct {
    34  		in   interface{}
    35  		want string
    36  	}{
    37  		{&NullInt64{Valid: true, Int64: 3}, `3`},
    38  		{&NullFloat64{Valid: true, Float64: 3.14}, `3.14`},
    39  		{&NullBool{Valid: true, Bool: true}, `true`},
    40  		{&NullString{Valid: true, StringVal: "foo"}, `"foo"`},
    41  		{&NullGeography{Valid: true, GeographyVal: "ST_GEOPOINT(47.649154, -122.350220)"}, `"ST_GEOPOINT(47.649154, -122.350220)"`},
    42  		{&NullJSON{Valid: true, JSONVal: "{\"foo\": \"bar\"}"}, `"{\"foo\": \"bar\"}"`},
    43  		{&NullTimestamp{Valid: true, Timestamp: testTimestamp}, `"2016-11-05T07:50:22.000000008Z"`},
    44  		{&NullDate{Valid: true, Date: testDate}, `"2016-11-05"`},
    45  		{&NullTime{Valid: true, Time: nullsTestTime}, `"07:50:22.000001"`},
    46  		{&NullDateTime{Valid: true, DateTime: nullsTestDateTime}, `"2016-11-05 07:50:22.000001"`},
    47  
    48  		{&NullInt64{}, `null`},
    49  		{&NullFloat64{}, `null`},
    50  		{&NullBool{}, `null`},
    51  		{&NullString{}, `null`},
    52  		{&NullGeography{}, `null`},
    53  		{&NullJSON{}, `null`},
    54  		{&NullTimestamp{}, `null`},
    55  		{&NullDate{}, `null`},
    56  		{&NullTime{}, `null`},
    57  		{&NullDateTime{}, `null`},
    58  
    59  		{&NullFloat64{Valid: true, Float64: math.Inf(1)}, `"Infinity"`},
    60  		{&NullFloat64{Valid: true, Float64: math.Inf(-1)}, `"-Infinity"`},
    61  		{&NullFloat64{Valid: true, Float64: math.NaN()}, `"NaN"`},
    62  	} {
    63  		bytes, err := json.Marshal(test.in)
    64  		if err != nil {
    65  			t.Fatal(err)
    66  		}
    67  		if got, want := string(bytes), test.want; got != want {
    68  			t.Errorf("%#v: got %s, want %s", test.in, got, want)
    69  		}
    70  
    71  		typ := reflect.Indirect(reflect.ValueOf(test.in)).Type()
    72  		value := reflect.New(typ).Interface()
    73  		err = json.Unmarshal(bytes, value)
    74  		if err != nil {
    75  			t.Fatal(err)
    76  		}
    77  
    78  		if !testutil.Equal(value, test.in) {
    79  			t.Errorf("%#v: got %#v, want %#v", test.in, value, test.in)
    80  		}
    81  	}
    82  }
    83  
    84  func TestNullFloat64JSON(t *testing.T) {
    85  	for _, tc := range []struct {
    86  		name         string
    87  		in           string
    88  		unmarshalled NullFloat64
    89  		marshalled   string
    90  	}{
    91  		{
    92  			name:         "float value",
    93  			in:           "3.14",
    94  			unmarshalled: NullFloat64{Valid: true, Float64: 3.14},
    95  			marshalled:   "3.14",
    96  		},
    97  		{
    98  			name:         "null",
    99  			in:           "null",
   100  			unmarshalled: NullFloat64{},
   101  			marshalled:   "null",
   102  		},
   103  		{
   104  			name:         "long infinity",
   105  			in:           `"Infinity"`,
   106  			unmarshalled: NullFloat64{Valid: true, Float64: math.Inf(1)},
   107  			marshalled:   `"Infinity"`,
   108  		},
   109  		{
   110  			name:         "short infinity",
   111  			in:           `"Inf"`,
   112  			unmarshalled: NullFloat64{Valid: true, Float64: math.Inf(1)},
   113  			marshalled:   `"Infinity"`,
   114  		},
   115  		{
   116  			name:         "positive short infinity",
   117  			in:           `"+Inf"`,
   118  			unmarshalled: NullFloat64{Valid: true, Float64: math.Inf(1)},
   119  			marshalled:   `"Infinity"`,
   120  		},
   121  		{
   122  			name:         "minus infinity",
   123  			in:           `"-Infinity"`,
   124  			unmarshalled: NullFloat64{Valid: true, Float64: math.Inf(-1)},
   125  			marshalled:   `"-Infinity"`,
   126  		},
   127  		{
   128  			name:         "minus short infinity",
   129  			in:           `"-Inf"`,
   130  			unmarshalled: NullFloat64{Valid: true, Float64: math.Inf(-1)},
   131  			marshalled:   `"-Infinity"`,
   132  		},
   133  		{
   134  			name:         "NaN",
   135  			in:           `"NaN"`,
   136  			unmarshalled: NullFloat64{Valid: true, Float64: math.NaN()},
   137  			marshalled:   `"NaN"`,
   138  		},
   139  	} {
   140  		tc := tc
   141  		t.Run(tc.name, func(t *testing.T) {
   142  			t.Parallel()
   143  
   144  			var f NullFloat64
   145  			err := json.Unmarshal([]byte(tc.in), &f)
   146  			if err != nil {
   147  				t.Fatal(err)
   148  			}
   149  			if got, want := f, tc.unmarshalled; !testutil.Equal(got, want) {
   150  				t.Errorf("%#v: got %#v, want %#v", tc.in, got, want)
   151  			}
   152  
   153  			b, err := json.Marshal(f)
   154  			if err != nil {
   155  				t.Fatal(err)
   156  			}
   157  			if got, want := string(b), tc.marshalled; got != want {
   158  				t.Errorf("%#v: got %s, want %s", tc.in, got, want)
   159  			}
   160  		})
   161  	}
   162  }
   163  

View as plain text