write_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package gorqlite
  2. import "testing"
  3. // import "os"
  4. func TestWriteOne(t *testing.T) {
  5. var wr WriteResult
  6. var err error
  7. t.Logf("trying Open")
  8. conn, err := Open(testUrl())
  9. if err != nil {
  10. t.Logf("--> FATAL")
  11. t.Fatal(err)
  12. }
  13. t.Logf("trying WriteOne DROP")
  14. wr, err = conn.WriteOne("DROP TABLE IF EXISTS " + testTableName() + "")
  15. if err != nil {
  16. t.Logf("--> FAILED")
  17. t.Fail()
  18. }
  19. t.Logf("trying WriteOne CTHULHU (should fail, bad SQL)")
  20. wr, err = conn.WriteOne("CTHULHU")
  21. if err == nil {
  22. t.Logf("--> FAILED")
  23. t.Fail()
  24. }
  25. t.Logf("trying WriteOne CREATE")
  26. wr, err = conn.WriteOne("CREATE TABLE " + testTableName() + " (id integer, name text)")
  27. if err != nil {
  28. t.Logf("--> FAILED")
  29. t.Fail()
  30. }
  31. t.Logf("trying WriteOne INSERT")
  32. wr, err = conn.WriteOne("INSERT INTO " + testTableName() + " (id, name) VALUES ( 1, 'aaa bbb ccc' )")
  33. if err != nil {
  34. t.Logf("--> FAILED")
  35. t.Fail()
  36. }
  37. t.Logf("checking WriteOne RowsAffected")
  38. if wr.RowsAffected != 1 {
  39. t.Logf("--> FAILED")
  40. t.Fail()
  41. }
  42. t.Logf("trying WriteOne DROP")
  43. wr, err = conn.WriteOne("DROP TABLE IF EXISTS " + testTableName() + "")
  44. if err != nil {
  45. t.Logf("--> FAILED")
  46. t.Fail()
  47. }
  48. }
  49. func TestWrite(t *testing.T) {
  50. var results []WriteResult
  51. var err error
  52. var s []string
  53. t.Logf("trying Open")
  54. conn, err := Open(testUrl())
  55. if err != nil {
  56. t.Logf("--> FATAL")
  57. t.Fatal(err)
  58. }
  59. t.Logf("trying Write DROP & CREATE")
  60. s = make([]string, 0)
  61. s = append(s, "DROP TABLE IF EXISTS "+testTableName()+"")
  62. s = append(s, "CREATE TABLE "+testTableName()+" (id integer, name text)")
  63. results, err = conn.Write(s)
  64. if err != nil {
  65. t.Logf("--> FAILED")
  66. t.Fail()
  67. }
  68. t.Logf("trying Write INSERT")
  69. s = make([]string, 0)
  70. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 1, 'aaa bbb ccc' )")
  71. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 2, 'ddd eee fff' )")
  72. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 3, 'ggg hhh iii' )")
  73. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 4, 'jjj kkk lll' )")
  74. results, err = conn.Write(s)
  75. if err != nil {
  76. t.Logf("--> FAILED")
  77. t.Fail()
  78. }
  79. if len(results) != 4 {
  80. t.Logf("--> FAILED")
  81. t.Fail()
  82. }
  83. t.Logf("trying Write DROP")
  84. s = make([]string, 0)
  85. s = append(s, "DROP TABLE IF EXISTS "+testTableName()+"")
  86. results, err = conn.Write(s)
  87. if err != nil {
  88. t.Logf("--> FAILED")
  89. t.Fail()
  90. }
  91. }