@@ -33,7 +33,7 @@ import (
3333 "testing"
3434)
3535
36- // Keyspace_table checks if Query .Keyspace() is updated based on prepared statement
36+ // Keyspace_table checks if Iter .Keyspace() is updated based on prepared statement
3737func TestKeyspaceTable (t * testing.T ) {
3838 cluster := createCluster ()
3939
@@ -45,8 +45,7 @@ func TestKeyspaceTable(t *testing.T) {
4545 t .Fatal ("createSession:" , err )
4646 }
4747
48- cluster .Keyspace = "wrong_keyspace"
49-
48+ wrongKeyspace := "testwrong"
5049 keyspace := "test1"
5150 table := "table1"
5251
@@ -55,6 +54,11 @@ func TestKeyspaceTable(t *testing.T) {
5554 t .Fatal ("unable to drop keyspace:" , err )
5655 }
5756
57+ err = createTable (session , `DROP KEYSPACE IF EXISTS ` + wrongKeyspace )
58+ if err != nil {
59+ t .Fatal ("unable to drop keyspace:" , err )
60+ }
61+
5862 err = createTable (session , fmt .Sprintf (`CREATE KEYSPACE %s
5963 WITH replication = {
6064 'class' : 'SimpleStrategy',
@@ -65,6 +69,16 @@ func TestKeyspaceTable(t *testing.T) {
6569 t .Fatal ("unable to create keyspace:" , err )
6670 }
6771
72+ err = createTable (session , fmt .Sprintf (`CREATE KEYSPACE %s
73+ WITH replication = {
74+ 'class' : 'SimpleStrategy',
75+ 'replication_factor' : 1
76+ }` , wrongKeyspace ))
77+
78+ if err != nil {
79+ t .Fatal ("unable to create keyspace:" , err )
80+ }
81+
6882 if err := session .control .awaitSchemaAgreement (); err != nil {
6983 t .Fatal (err )
7084 }
@@ -80,11 +94,24 @@ func TestKeyspaceTable(t *testing.T) {
8094 t .Fatal (err )
8195 }
8296
97+ session .Close ()
98+
99+ cluster = createCluster ()
100+
101+ fallback = RoundRobinHostPolicy ()
102+ cluster .PoolConfig .HostSelectionPolicy = TokenAwareHostPolicy (fallback )
103+ cluster .Keyspace = wrongKeyspace
104+
105+ session , err = cluster .CreateSession ()
106+ if err != nil {
107+ t .Fatal ("createSession:" , err )
108+ }
109+
83110 ctx := context .Background ()
84111
85112 // insert a row
86113 if err := session .Query (`INSERT INTO test1.table1(pk, ck, v) VALUES (?, ?, ?)` ,
87- 1 , 2 , 3 ).WithContext ( ctx ). Consistency (One ).Exec ( ); err != nil {
114+ 1 , 2 , 3 ).Consistency (One ).ExecContext ( ctx ); err != nil {
88115 t .Fatal (err )
89116 }
90117
@@ -93,8 +120,8 @@ func TestKeyspaceTable(t *testing.T) {
93120 /* Search for a specific set of records whose 'pk' column matches
94121 * the value of inserted row. */
95122 qry := session .Query (`SELECT pk FROM test1.table1 WHERE pk = ? LIMIT 1` ,
96- 1 ).WithContext ( ctx ). Consistency (One )
97- iter := qry .Iter ( )
123+ 1 ).Consistency (One )
124+ iter := qry .IterContext ( ctx )
98125 ok := iter .Scan (& pk )
99126 err = iter .Close ()
100127 if err != nil {
@@ -104,9 +131,9 @@ func TestKeyspaceTable(t *testing.T) {
104131 t .Fatal ("expected pk to be scanned" )
105132 }
106133
107- // cluster.Keyspace was set to "wrong_keyspace ", but during prepering statement
134+ // cluster.Keyspace was set to "testwrong ", but during prepering statement
108135 // Keyspace in Query should be changed to "test" and Table should be changed to table1
109- assertEqual (t , "qry.Keyspace()" , "test1 " , qry .Keyspace ())
136+ assertEqual (t , "qry.Keyspace()" , "testwrong " , qry .Keyspace ())
110137 assertEqual (t , "iter.Keyspace()" , "test1" , iter .Keyspace ())
111138 assertEqual (t , "iter.Table()" , "table1" , iter .Table ())
112139}
0 commit comments