github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/cassandra/schema_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 cassandra
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/gocql/gocql"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/uber-go/dosa"
    29  	"github.com/uber-go/dosa/testentity"
    30  )
    31  
    32  func TestCompareStructToSchemaWrongPk(t *testing.T) {
    33  	ed := dosa.EntityDefinition{Key: &dosa.PrimaryKey{
    34  		PartitionKeys: []string{"p1"}},
    35  		Name: "test",
    36  		Columns: []*dosa.ColumnDefinition{
    37  			{Name: "p1", Type: dosa.String},
    38  			{Name: "c1", Type: dosa.String},
    39  		},
    40  	}
    41  	md := gocql.TableMetadata{PartitionKey: []*gocql.ColumnMetadata{
    42  		{Name: "c1", Type: TestType{typ: gocql.TypeVarchar}},
    43  	},
    44  		Columns: map[string]*gocql.ColumnMetadata{
    45  			"p1": {Name: "p1", Type: TestType{typ: gocql.TypeVarchar}},
    46  		}}
    47  	missing := RepairableSchemaMismatchError{}
    48  	err := compareStructToSchema(&ed, &md, &missing)
    49  	assert.Error(t, err)
    50  	assert.Contains(t, err.Error(), `"test"`)
    51  }
    52  
    53  func TestCompareStructToSchemaMissingColumn(t *testing.T) {
    54  	ed := dosa.EntityDefinition{Key: &dosa.PrimaryKey{
    55  		PartitionKeys: []string{"p1"}},
    56  		Name: "test",
    57  		Columns: []*dosa.ColumnDefinition{
    58  			{Name: "p1", Type: dosa.String},
    59  			{Name: "c1", Type: dosa.String},
    60  		},
    61  	}
    62  	md := gocql.TableMetadata{PartitionKey: []*gocql.ColumnMetadata{
    63  		{Name: "p1", Type: TestType{typ: gocql.TypeVarchar}},
    64  	},
    65  		Columns: map[string]*gocql.ColumnMetadata{
    66  			"p1": {Name: "p1", Type: TestType{typ: gocql.TypeVarchar}},
    67  		}}
    68  	missing := RepairableSchemaMismatchError{}
    69  	err := compareStructToSchema(&ed, &md, &missing)
    70  	assert.Nil(t, err)
    71  	assert.True(t, missing.HasMissing())
    72  	assert.Equal(t, 1, len(missing.MissingColumns))
    73  	assert.Equal(t, "test", missing.MissingColumns[0].Tablename)
    74  	assert.Equal(t, "c1", missing.MissingColumns[0].Column.Name)
    75  	assert.Contains(t, missing.Error(), "Missing 1 column")
    76  }
    77  
    78  type TestType struct {
    79  	typ gocql.Type
    80  }
    81  
    82  func (t TestType) New() interface{} {
    83  	panic("not implemented")
    84  }
    85  func (t TestType) Version() byte {
    86  	panic("not implemented")
    87  }
    88  func (t TestType) Custom() string {
    89  	panic("not implemented")
    90  }
    91  func (t TestType) Type() gocql.Type {
    92  	return t.typ
    93  }
    94  
    95  func TestCreateTableString(t *testing.T) {
    96  	// grab this full-featured entity from the code
    97  	testEntity, _ := dosa.TableFromInstance(&testentity.TestEntity{})
    98  	testCases := []struct {
    99  		name     string
   100  		ed       dosa.EntityDefinition
   101  		expected string
   102  	}{
   103  		{"simple",
   104  			dosa.EntityDefinition{Key: &dosa.PrimaryKey{
   105  				PartitionKeys: []string{"c1"}},
   106  				Name: "t1",
   107  				Columns: []*dosa.ColumnDefinition{
   108  					{Name: "c1", Type: dosa.String},
   109  				},
   110  			},
   111  			`CREATE TABLE "keyspace"."t1" (` +
   112  				`"c1" text, ` +
   113  				`PRIMARY KEY (("c1"))) ` +
   114  				`WITH COMPACTION = {'class':'LeveledCompactionStrategy'}`,
   115  		},
   116  		{"testentity",
   117  			testEntity.EntityDefinition,
   118  			`CREATE TABLE "keyspace"."awesome_test_entity" (` +
   119  				`"an_uuid_key" uuid, ` +
   120  				`"strkey" text, ` +
   121  				`"int64key" bigint, ` +
   122  				`"uuidv" uuid, ` +
   123  				`"strv" text, ` +
   124  				`"an_int64_value" bigint, ` +
   125  				`"int32v" int, ` +
   126  				`"doublev" double, ` +
   127  				`"boolv" boolean, ` +
   128  				`"blobv" blob, ` +
   129  				`"tsv" timestamp, ` +
   130  				`"uuidvp" uuid, ` +
   131  				`"strvp" text, ` +
   132  				`"int64vp" bigint, ` +
   133  				`"int32vp" int, ` +
   134  				`"doublevp" double, ` +
   135  				`"boolvp" boolean, ` +
   136  				`"tsvp" timestamp, ` +
   137  				`PRIMARY KEY (("an_uuid_key"),"strkey","int64key")) ` +
   138  				`WITH CLUSTERING ORDER BY ("strkey" ASC,"int64key" DESC) ` +
   139  				`AND COMPACTION = {'class':'LeveledCompactionStrategy'}`},
   140  	}
   141  	for _, testcase := range testCases {
   142  		t.Run(testcase.name, func(t *testing.T) {
   143  			assert.Equal(t, testcase.expected, createTableString("keyspace", &testcase.ed))
   144  		})
   145  	}
   146  }