github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/cassandra/cassandra_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_test 22 23 import ( 24 "context" 25 "strconv" 26 "testing" 27 28 "github.com/gocql/gocql" 29 "github.com/uber-go/dosa" 30 "github.com/uber-go/dosa/connectors/cassandra" 31 _ "github.com/uber-go/dosa/connectors/cassandra" 32 33 "github.com/stretchr/testify/assert" 34 "github.com/uber-go/dosa/testentity" 35 ) 36 37 func TestNewConnector(t *testing.T) { 38 EnsureLocalCassandraStarted() 39 40 c, err := cassandra.NewConnector( 41 gocql.NewCluster("127.0.0.1:"+strconv.Itoa(cassandra.CassandraPort)), 42 &cassandra.UseNamePrefix{}, 43 nil, 44 ) 45 if !assert.NoError(t, err, "Error connecting to Cassandra") { 46 t.Fatal(err) 47 } 48 49 ctx := context.Background() 50 if err := c.CreateScope(ctx, "example"); err != nil { 51 t.Fatal(err) 52 } 53 ei, _ := dosa.TableFromInstance(&testentity.TestEntity{}) 54 _, err = c.UpsertSchema(ctx, "example", "example", []*dosa.EntityDefinition{&ei.EntityDefinition}) 55 if err != nil { 56 t.Fatal(err) 57 } 58 59 sr := dosa.SchemaRef{Scope: "example", NamePrefix: "example"} 60 61 err = c.Upsert(ctx, &dosa.EntityInfo{Ref: &sr, Def: &ei.EntityDefinition}, map[string]dosa.FieldValue{ 62 "an_uuid_key": dosa.UUID("c778ba9e-a241-471c-9b5b-4b4c1ef1c5b7"), 63 "strkey": "test", 64 "int64key": int64(11), 65 }) 66 67 if err != nil { 68 t.Fatal(err) 69 } 70 } 71 72 // This test verifies that we can connect to a local database when no configuration is specified 73 func TestNewConnectorWithNilConfig(t *testing.T) { 74 c, err := dosa.GetConnector("cassandra", nil) 75 assert.NoError(t, err) 76 assert.IsType(t, &cassandra.Connector{}, c) 77 c.Shutdown() 78 } 79 func TestNewConnectorWithKeyspaceMapper(t *testing.T) { 80 c, err := dosa.GetConnector("cassandra", map[string]interface{}{"keyspace_mapper": &cassandra.UseNamePrefix{}}) 81 assert.NoError(t, err) 82 assert.IsType(t, &cassandra.Connector{}, c) 83 assert.IsType(t, &cassandra.UseNamePrefix{}, c.(*cassandra.Connector).KsMapper) 84 c.Shutdown() 85 } 86 87 func TestNewConnectorWithBadHost(t *testing.T) { 88 _, err := dosa.GetConnector("cassandra", map[string]interface{}{"yaml": `hosts: ["127.0.0.254"]`}) 89 assert.Error(t, err) 90 assert.Contains(t, err.Error(), "127.0.0.254") 91 } 92 93 func TestNewConnectorWithBadYaml(t *testing.T) { 94 _, err := dosa.GetConnector("cassandra", map[string]interface{}{"yaml": "notvalidyaml"}) 95 assert.Error(t, err) 96 assert.Contains(t, err.Error(), "gocql.ClusterConfig") 97 } 98 99 func TestNewConnectorWithBadKeyspaceMapper(t *testing.T) { 100 _, err := dosa.GetConnector("cassandra", map[string]interface{}{"keyspace_mapper": 42}) 101 assert.Error(t, err) 102 assert.Contains(t, err.Error(), "type int") 103 }