123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- // Copyright 2015 Google Inc. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package bigquery
- import (
- "fmt"
- "reflect"
- "testing"
- "time"
- bq "google.golang.org/api/bigquery/v2"
- )
- func TestConvertBasicValues(t *testing.T) {
- schema := []*FieldSchema{
- {Type: StringFieldType},
- {Type: IntegerFieldType},
- {Type: FloatFieldType},
- {Type: BooleanFieldType},
- }
- row := &bq.TableRow{
- F: []*bq.TableCell{
- {V: "a"},
- {V: "1"},
- {V: "1.2"},
- {V: "true"},
- },
- }
- got, err := convertRow(row, schema)
- if err != nil {
- t.Fatalf("error converting: %v", err)
- }
- want := []Value{"a", 1, 1.2, true}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("converting basic values: got:\n%v\nwant:\n%v", got, want)
- }
- }
- func TestConvertTime(t *testing.T) {
- schema := []*FieldSchema{
- {Type: TimestampFieldType},
- }
- thyme := time.Date(1970, 1, 1, 10, 0, 0, 10, time.UTC)
- row := &bq.TableRow{
- F: []*bq.TableCell{
- {V: fmt.Sprintf("%.10f", float64(thyme.UnixNano())/1e9)},
- },
- }
- got, err := convertRow(row, schema)
- if err != nil {
- t.Fatalf("error converting: %v", err)
- }
- if !got[0].(time.Time).Equal(thyme) {
- t.Errorf("converting basic values: got:\n%v\nwant:\n%v", got, thyme)
- }
- }
- func TestConvertNullValues(t *testing.T) {
- schema := []*FieldSchema{
- {Type: StringFieldType},
- }
- row := &bq.TableRow{
- F: []*bq.TableCell{
- {V: nil},
- },
- }
- got, err := convertRow(row, schema)
- if err != nil {
- t.Fatalf("error converting: %v", err)
- }
- want := []Value{nil}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("converting null values: got:\n%v\nwant:\n%v", got, want)
- }
- }
- func TestBasicRepetition(t *testing.T) {
- schema := []*FieldSchema{
- {Type: IntegerFieldType, Repeated: true},
- }
- row := &bq.TableRow{
- F: []*bq.TableCell{
- {
- V: []interface{}{
- map[string]interface{}{
- "v": "1",
- },
- map[string]interface{}{
- "v": "2",
- },
- map[string]interface{}{
- "v": "3",
- },
- },
- },
- },
- }
- got, err := convertRow(row, schema)
- if err != nil {
- t.Fatalf("error converting: %v", err)
- }
- want := []Value{[]Value{1, 2, 3}}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("converting basic repeated values: got:\n%v\nwant:\n%v", got, want)
- }
- }
- func TestNestedRecordContainingRepetition(t *testing.T) {
- schema := []*FieldSchema{
- {
- Type: RecordFieldType,
- Schema: Schema{
- {Type: IntegerFieldType, Repeated: true},
- },
- },
- }
- row := &bq.TableRow{
- F: []*bq.TableCell{
- {
- V: map[string]interface{}{
- "f": []interface{}{
- map[string]interface{}{
- "v": []interface{}{
- map[string]interface{}{"v": "1"},
- map[string]interface{}{"v": "2"},
- map[string]interface{}{"v": "3"},
- },
- },
- },
- },
- },
- },
- }
- got, err := convertRow(row, schema)
- if err != nil {
- t.Fatalf("error converting: %v", err)
- }
- want := []Value{[]Value{[]Value{1, 2, 3}}}
- if !reflect.DeepEqual(got, want) {
- t.Errorf("converting basic repeated values: got:\n%v\nwant:\n%v", got, want)
- }
- }
- func TestRepeatedRecordContainingRepetition(t *testing.T) {
- schema := []*FieldSchema{
- {
- Type: RecordFieldType,
- Repeated: true,
- Schema: Schema{
- {Type: IntegerFieldType, Repeated: true},
- },
- },
- }
- row := &bq.TableRow{F: []*bq.TableCell{
- {
- V: []interface{}{ // repeated records.
- map[string]interface{}{ // first record.
- "v": map[string]interface{}{ // pointless single-key-map wrapper.
- "f": []interface{}{ // list of record fields.
- map[string]interface{}{ // only record (repeated ints)
- "v": []interface{}{ // pointless wrapper.
- map[string]interface{}{
- "v": "1",
- },
- map[string]interface{}{
- "v": "2",
- },
- map[string]interface{}{
- "v": "3",
- },
- },
- },
- },
- },
- },
- map[string]interface{}{ // second record.
- "v": map[string]interface{}{
- "f": []interface{}{
- map[string]interface{}{
- "v": []interface{}{
- map[string]interface{}{
- "v": "4",
- },
- map[string]interface{}{
- "v": "5",
- },
- map[string]interface{}{
- "v": "6",
- },
- },
- },
- },
- },
- },
- },
- },
- }}
- got, err := convertRow(row, schema)
- if err != nil {
- t.Fatalf("error converting: %v", err)
- }
- want := []Value{ // the row is a list of length 1, containing an entry for the repeated record.
- []Value{ // the repeated record is a list of length 2, containing an entry for each repetition.
- []Value{ // the record is a list of length 1, containing an entry for the repeated integer field.
- []Value{1, 2, 3}, // the repeated integer field is a list of length 3.
- },
- []Value{ // second record
- []Value{4, 5, 6},
- },
- },
- }
- if !reflect.DeepEqual(got, want) {
- t.Errorf("converting repeated records with repeated values: got:\n%v\nwant:\n%v", got, want)
- }
- }
- func TestRepeatedRecordContainingRecord(t *testing.T) {
- schema := []*FieldSchema{
- {
- Type: RecordFieldType,
- Repeated: true,
- Schema: Schema{
- {
- Type: StringFieldType,
- },
- {
- Type: RecordFieldType,
- Schema: Schema{
- {Type: IntegerFieldType},
- {Type: StringFieldType},
- },
- },
- },
- },
- }
- row := &bq.TableRow{F: []*bq.TableCell{
- {
- V: []interface{}{ // repeated records.
- map[string]interface{}{ // first record.
- "v": map[string]interface{}{ // pointless single-key-map wrapper.
- "f": []interface{}{ // list of record fields.
- map[string]interface{}{ // first record field (name)
- "v": "first repeated record",
- },
- map[string]interface{}{ // second record field (nested record).
- "v": map[string]interface{}{ // pointless single-key-map wrapper.
- "f": []interface{}{ // nested record fields
- map[string]interface{}{
- "v": "1",
- },
- map[string]interface{}{
- "v": "two",
- },
- },
- },
- },
- },
- },
- },
- map[string]interface{}{ // second record.
- "v": map[string]interface{}{
- "f": []interface{}{
- map[string]interface{}{
- "v": "second repeated record",
- },
- map[string]interface{}{
- "v": map[string]interface{}{
- "f": []interface{}{
- map[string]interface{}{
- "v": "3",
- },
- map[string]interface{}{
- "v": "four",
- },
- },
- },
- },
- },
- },
- },
- },
- },
- }}
- got, err := convertRow(row, schema)
- if err != nil {
- t.Fatalf("error converting: %v", err)
- }
- // TODO: test with flattenresults.
- want := []Value{ // the row is a list of length 1, containing an entry for the repeated record.
- []Value{ // the repeated record is a list of length 2, containing an entry for each repetition.
- []Value{ // record contains a string followed by a nested record.
- "first repeated record",
- []Value{
- 1,
- "two",
- },
- },
- []Value{ // second record.
- "second repeated record",
- []Value{
- 3,
- "four",
- },
- },
- },
- }
- if !reflect.DeepEqual(got, want) {
- t.Errorf("converting repeated records containing record : got:\n%v\nwant:\n%v", got, want)
- }
- }
|