github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/schema/cql/cql_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package cql
    22  
    23  import (
    24  	"fmt"
    25  	"testing"
    26  
    27  	"time"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/uber-go/dosa"
    31  )
    32  
    33  type AllTypes struct {
    34  	dosa.Entity `dosa:"primaryKey=BoolType"`
    35  	I1          dosa.Index `dosa:"key=Int32Type"`
    36  	I2          dosa.Index `dosa:"key=Int64Type"`
    37  	BoolType    bool
    38  	Int32Type   int32
    39  	Int64Type   int64
    40  	DoubleType  float64
    41  	StringType  string
    42  	BlobType    []byte
    43  	TimeType    time.Time
    44  	UUIDType    dosa.UUID
    45  }
    46  
    47  type SinglePrimaryKey struct {
    48  	dosa.Entity `dosa:"primaryKey=(PrimaryKey)"`
    49  	PrimaryKey  int64
    50  	Data        string
    51  }
    52  
    53  func TestCQL(t *testing.T) {
    54  	data := []struct {
    55  		Instance  dosa.DomainObject
    56  		Statement string
    57  	}{
    58  		{
    59  			Instance:  &SinglePrimaryKey{},
    60  			Statement: `create table "singleprimarykey" ("primarykey" bigint, "data" text, primary key (primarykey));`,
    61  		},
    62  		{
    63  			Instance: &AllTypes{},
    64  			Statement: `create table "alltypes" ("booltype" boolean, "int32type" int, "int64type" bigint, "doubletype" double, "stringtype" text, "blobtype" blob, "timetype" timestamp, "uuidtype" uuid, primary key (booltype));
    65  create materialized view "i1" as
    66    select * from "alltypes"
    67    where "int32type" is not null
    68    primary key (int32type, booltype ASC);
    69  create materialized view "i2" as
    70    select * from "alltypes"
    71    where "int64type" is not null
    72    primary key (int64type, booltype ASC);`,
    73  		},
    74  		// TODO: Add more test cases
    75  	}
    76  
    77  	for _, d := range data {
    78  		table, err := dosa.TableFromInstance(d.Instance)
    79  		assert.Nil(t, err) // this code does not test TableFromInstance
    80  		statement := ToCQL(&table.EntityDefinition)
    81  		assert.Equal(t, d.Statement, statement, fmt.Sprintf("Instance: %T", d.Instance))
    82  	}
    83  }
    84  
    85  func BenchmarkCQL(b *testing.B) {
    86  	table, _ := dosa.TableFromInstance(&AllTypes{})
    87  	for i := 0; i < b.N; i++ {
    88  		ToCQL(&table.EntityDefinition)
    89  	}
    90  }
    91  
    92  func TestTypemapUnknown(t *testing.T) {
    93  	assert.Equal(t, "unknown", typeMap(dosa.Invalid))
    94  }