@ -789,7 +789,14 @@ func Test_SingleNodeRequest(t *testing.T) {
} ,
} ,
{
{
stmts : [ ] string {
stmts : [ ] string {
` SELECT * FROM foo ` ,
` INSERT INTO foo(id, name) VALUES(1234, "dana") ` ,
` INSERT INTO foo(id, name) VALUES(5678, "bob") ` ,
} ,
expected : ` [ { "last_insert_id":1234,"rows_affected":1}, { "last_insert_id":5678,"rows_affected":1}] ` ,
} ,
{
stmts : [ ] string {
` SELECT * FROM foo WHERE name='fiona' ` ,
` INSERT INTO foo(id, name) VALUES(66, "declan") ` ,
` INSERT INTO foo(id, name) VALUES(66, "declan") ` ,
} ,
} ,
expected : ` [ { "columns":["id","name"],"types":["integer","text"],"values":[[1,"fiona"]]}, { "last_insert_id":66,"rows_affected":1}] ` ,
expected : ` [ { "columns":["id","name"],"types":["integer","text"],"values":[[1,"fiona"]]}, { "last_insert_id":66,"rows_affected":1}] ` ,
@ -799,7 +806,7 @@ func Test_SingleNodeRequest(t *testing.T) {
` INSERT INTO foo(id, name) VALUES(77, "fiona") ` ,
` INSERT INTO foo(id, name) VALUES(77, "fiona") ` ,
` SELECT COUNT(*) FROM foo ` ,
` SELECT COUNT(*) FROM foo ` ,
} ,
} ,
expected : ` [ { "last_insert_id":77,"rows_affected":1}, { "columns":["COUNT(*)"],"types":["integer"],"values":[[ 3 ]]}]` ,
expected : ` [ { "last_insert_id":77,"rows_affected":1}, { "columns":["COUNT(*)"],"types":["integer"],"values":[[ 5 ]]}]` ,
} ,
} ,
{
{
stmts : [ ] string {
stmts : [ ] string {
@ -2448,7 +2455,7 @@ func Test_IsVoter(t *testing.T) {
}
}
}
}
func Test_ QueriesOnly ( t * testing . T ) {
func Test_ RWROCount ( t * testing . T ) {
s , ln := mustNewStore ( t )
s , ln := mustNewStore ( t )
defer ln . Close ( )
defer ln . Close ( )
@ -2471,62 +2478,66 @@ func Test_QueriesOnly(t *testing.T) {
}
}
tests := [ ] struct {
tests := [ ] struct {
name string
name string
stmts [ ] string
stmts [ ] string
queriesOnly bool
expRW int
expRO int
} {
} {
{
{
name : "Empty SQL" ,
name : "Empty SQL" ,
stmts : [ ] string { "" } ,
stmts : [ ] string { "" } ,
queriesOnly : true ,
} ,
} ,
{
{
name : "Junk SQL" ,
name : "Junk SQL" ,
stmts : [ ] string { "asdkflj asgkdj" } ,
stmts : [ ] string { "asdkflj asgkdj" } ,
queriesOnly: false ,
expRW: 1 ,
} ,
} ,
{
{
name : "CREATE TABLE statement, already exists" ,
name : "CREATE TABLE statement, already exists" ,
stmts : [ ] string { "CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)" } ,
stmts : [ ] string { "CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)" } ,
queriesOnly: false ,
expRW: 1 ,
} ,
} ,
{
{
name : "Single INSERT" ,
name : "Single INSERT" ,
stmts : [ ] string { "INSERT INTO foo(id, name) VALUES(1, 'fiona')" } ,
stmts : [ ] string { "INSERT INTO foo(id, name) VALUES(1, 'fiona')" } ,
queriesOnly: false ,
expRW: 1 ,
} ,
} ,
{
{
name : "Single INSERT, non-existent table" ,
name : "Single INSERT, non-existent table" ,
stmts : [ ] string { "INSERT INTO qux(id, name) VALUES(1, 'fiona')" } ,
stmts : [ ] string { "INSERT INTO qux(id, name) VALUES(1, 'fiona')" } ,
queriesOnly: false ,
expRW: 1 ,
} ,
} ,
{
{
name : "Single SELECT" ,
name : "Single SELECT" ,
stmts : [ ] string { "SELECT * FROM foo" } ,
stmts : [ ] string { "SELECT * FROM foo" } ,
queriesOnly: true ,
expRO: 1 ,
} ,
} ,
{
{
name : "Single SELECT from non-existent table" ,
name : "Single SELECT from non-existent table" ,
stmts : [ ] string { "SELECT * FROM qux" } ,
stmts : [ ] string { "SELECT * FROM qux" } ,
queriesOnly: false ,
expRW: 1 , // Yeah, this is unfortunate, but it's how SQLite works.
} ,
} ,
{
{
name : "Double SELECT" ,
name : "Double SELECT" ,
stmts : [ ] string { "SELECT * FROM foo" , "SELECT * FROM foo WHERE id = 1" } ,
stmts : [ ] string { "SELECT * FROM foo" , "SELECT * FROM foo WHERE id = 1" } ,
queriesOnly: true ,
expRO: 2 ,
} ,
} ,
{
{
name : "Mix queries and executes" ,
name : "Mix queries and executes" ,
stmts : [ ] string { "SELECT * FROM foo" , "INSERT INTO foo(id, name) VALUES(1, 'fiona')" } ,
stmts : [ ] string { "SELECT * FROM foo" , "INSERT INTO foo(id, name) VALUES(1, 'fiona')" } ,
queriesOnly : false ,
expRW : 1 ,
expRO : 1 ,
} ,
} ,
}
}
for _ , tt := range tests {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
t . Run ( tt . name , func ( t * testing . T ) {
requires := s . QueriesOnly ( executeQueryRequestFromStrings ( tt . stmts , proto . QueryRequest_QUERY_REQUEST_LEVEL_NONE , false , false ) )
rwN , roN := s . RORWCount ( executeQueryRequestFromStrings ( tt . stmts , proto . QueryRequest_QUERY_REQUEST_LEVEL_NONE , false , false ) )
if requires != tt . queriesOnly {
if rwN != tt . expRW {
t . Fatalf ( " test %s failed, unexpected requires: expected %v, got %v" , tt . name , tt . queriesOnly , requires )
t . Fatalf ( "wrong number of RW statements, exp %d, got %d" , tt . expRW , rwN )
}
if roN != tt . expRO {
t . Fatalf ( "wrong number of RO statements, exp %d, got %d" , tt . expRO , roN )
}
}
} )
} )
}
}