github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/cassandra/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 cassandra_test 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/uber-go/dosa" 28 "github.com/uber-go/dosa/connectors/cassandra" 29 ) 30 31 func TestInsertStmt(t *testing.T) { 32 data := []struct { 33 keyspace string 34 table string 35 columns []string 36 values []interface{} 37 stmt string 38 ifnotexist bool 39 }{ 40 { 41 keyspace: "ks", 42 table: "t", 43 columns: []string{"c1", "c2"}, 44 values: []interface{}{"string", "sing"}, 45 stmt: "INSERT INTO \"ks\".\"t\" (\"c1\", \"c2\") VALUES (?, ?);", 46 }, 47 { 48 keyspace: "ks", 49 table: "t", 50 columns: []string{"c1", "c2"}, 51 values: []interface{}{"string", "sing"}, 52 ifnotexist: true, 53 stmt: "INSERT INTO \"ks\".\"t\" (\"c1\", \"c2\") VALUES (?, ?) IF NOT EXISTS;", 54 }, 55 { 56 keyspace: "ks", 57 table: "t", 58 columns: []string{"c1"}, 59 values: []interface{}{"string", "sing"}, 60 stmt: "INSERT INTO \"ks\".\"t\" (\"c1\") VALUES (?, ?);", 61 }, 62 } 63 for _, d := range data { 64 stmt, err := cassandra.InsertStmt( 65 cassandra.Keyspace(d.keyspace), 66 cassandra.Table(d.table), 67 cassandra.Columns(d.columns), 68 cassandra.Values(d.values), 69 cassandra.IfNotExist(d.ifnotexist), 70 ) 71 assert.Nil(t, err) 72 assert.Equal(t, stmt, d.stmt) 73 } 74 } 75 76 func TestSelectStmt(t *testing.T) { 77 conds := []*cassandra.ColumnCondition{ 78 { 79 Name: "a", 80 Condition: &dosa.Condition{Op: dosa.Eq, Value: 4}, 81 }, 82 { 83 Name: "a", 84 Condition: &dosa.Condition{Op: dosa.Lt, Value: 5}, 85 }, 86 { 87 Name: "a", 88 Condition: &dosa.Condition{Op: dosa.LtOrEq, Value: 2}, 89 }, 90 { 91 Name: "d", 92 Condition: &dosa.Condition{Op: dosa.Gt, Value: 0}, 93 }, 94 { 95 Name: "a", 96 Condition: &dosa.Condition{Op: dosa.GtOrEq, Value: 3}, 97 }, 98 { 99 Name: "b", 100 Condition: &dosa.Condition{Op: dosa.Eq, Value: 9}, 101 }, 102 { 103 Name: "c", 104 Condition: &dosa.Condition{Op: dosa.Lt, Value: 1}, 105 }, 106 { 107 Name: "c", 108 Condition: &dosa.Condition{Op: dosa.Gt, Value: 0}, 109 }, 110 } 111 data := []struct { 112 keyspace string 113 table string 114 columns []string 115 stmt string 116 limit int 117 conds []*cassandra.ColumnCondition 118 }{ 119 { 120 keyspace: "ks", 121 table: "t", 122 columns: []string{"c1", "c2"}, 123 conds: conds, 124 stmt: "SELECT \"c1\", \"c2\" FROM \"ks\".\"t\" WHERE \"a\"=? AND \"a\"<? AND \"a\"<=? AND \"d\">? AND \"a\">=? AND \"b\"=? AND \"c\"<? AND \"c\">?;", 125 }, 126 { 127 keyspace: "ks", 128 table: "t", 129 columns: []string{"c1", "c2"}, 130 conds: conds, 131 limit: 5, 132 stmt: "SELECT \"c1\", \"c2\" FROM \"ks\".\"t\" WHERE \"a\"=? AND \"a\"<? AND \"a\"<=? AND \"d\">? AND \"a\">=? AND \"b\"=? AND \"c\"<? AND \"c\">? LIMIT 5;", 133 }, 134 { 135 keyspace: "ks", 136 table: "t", 137 columns: []string{"c1"}, 138 conds: conds, 139 stmt: "SELECT \"c1\" FROM \"ks\".\"t\" WHERE \"a\"=? AND \"a\"<? AND \"a\"<=? AND \"d\">? AND \"a\">=? AND \"b\"=? AND \"c\"<? AND \"c\">?;", 140 }, 141 { 142 keyspace: "ks", 143 table: "t", 144 columns: []string{"c1"}, 145 limit: 0, 146 stmt: "SELECT \"c1\" FROM \"ks\".\"t\";", 147 }, 148 } 149 for _, d := range data { 150 stmt, err := cassandra.SelectStmt( 151 cassandra.Keyspace(d.keyspace), 152 cassandra.Table(d.table), 153 cassandra.Columns(d.columns), 154 cassandra.Conditions(d.conds), 155 cassandra.Limit(d.limit), 156 ) 157 assert.NoError(t, err) 158 assert.Equal(t, stmt, d.stmt) 159 } 160 } 161 162 func TestDeleteStmt(t *testing.T) { 163 conds := []*cassandra.ColumnCondition{ 164 { 165 Name: "a", 166 Condition: &dosa.Condition{Op: dosa.Eq, Value: 4}, 167 }, 168 { 169 Name: "b", 170 Condition: &dosa.Condition{Op: dosa.Eq, Value: 9}, 171 }, 172 { 173 Name: "c", 174 Condition: &dosa.Condition{Op: dosa.Eq, Value: 1}, 175 }, 176 } 177 data := []struct { 178 keyspace string 179 table string 180 stmt string 181 conds []*cassandra.ColumnCondition 182 }{ 183 { 184 keyspace: "ks", 185 table: "t", 186 conds: conds, 187 stmt: "DELETE FROM \"ks\".\"t\" WHERE \"a\"=? AND \"b\"=? AND \"c\"=?;", 188 }, 189 } 190 for _, d := range data { 191 stmt, err := cassandra.DeleteStmt( 192 cassandra.Keyspace(d.keyspace), 193 cassandra.Table(d.table), 194 cassandra.Conditions(d.conds), 195 ) 196 assert.NoError(t, err) 197 assert.Equal(t, stmt, d.stmt) 198 } 199 }