query_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package gorqlite
  2. import "testing"
  3. func TestQueryOne(t *testing.T) {
  4. var wr WriteResult
  5. var qr QueryResult
  6. var wResults []WriteResult
  7. var qResults []QueryResult
  8. var err error
  9. t.Logf("trying Open")
  10. conn, err := Open(testUrl())
  11. if err != nil {
  12. t.Logf("--> FATAL")
  13. t.Fatal()
  14. }
  15. t.Logf("trying WriteOne DROP")
  16. wr, err = conn.WriteOne("DROP TABLE IF EXISTS " + testTableName())
  17. if err != nil {
  18. t.Logf("--> FATAL")
  19. t.Fatal()
  20. }
  21. t.Logf("trying WriteOne CREATE")
  22. wr, err = conn.WriteOne("CREATE TABLE " + testTableName() + " (id integer, name text)")
  23. if err != nil {
  24. t.Logf("--> FATAL")
  25. t.Fatal()
  26. }
  27. t.Logf("trying Write INSERT")
  28. s := make([]string, 0)
  29. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 1, 'Romulan' )")
  30. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 2, 'Vulcan' )")
  31. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 3, 'Klingon' )")
  32. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 4, 'Ferengi' )")
  33. s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 5, 'Cardassian' )")
  34. wResults, err = conn.Write(s)
  35. if err != nil {
  36. t.Logf("--> FATAL")
  37. t.Fatal()
  38. }
  39. t.Logf("trying QueryOne")
  40. qr, err = conn.QueryOne("SELECT name FROM " + testTableName() + " WHERE id > 3")
  41. if err != nil {
  42. t.Logf("--> FAILED")
  43. t.Fail()
  44. }
  45. t.Logf("trying Next()")
  46. na := qr.Next()
  47. if na != true {
  48. t.Logf("--> FAILED")
  49. t.Fail()
  50. }
  51. t.Logf("trying Map()")
  52. r, err := qr.Map()
  53. if err != nil {
  54. t.Logf("--> FAILED")
  55. t.Fail()
  56. }
  57. if r["name"].(string) != "Ferengi" {
  58. t.Logf("--> FAILED")
  59. t.Fail()
  60. }
  61. t.Logf("trying Scan(), also float64->int64 in Scan()")
  62. var id int64
  63. var name string
  64. err = qr.Scan(&id, &name)
  65. if err == nil {
  66. t.Logf("--> FAILED (%s)", err.Error())
  67. t.Fail()
  68. }
  69. err = qr.Scan(&name)
  70. if err != nil {
  71. t.Logf("--> FAILED (%s)", err.Error())
  72. t.Fail()
  73. }
  74. if name != "Ferengi" {
  75. t.Logf("--> FAILED, name should be 'Ferengi' but it's '%s'", name)
  76. t.Fail()
  77. }
  78. qr.Next()
  79. err = qr.Scan(&name)
  80. if name != "Cardassian" {
  81. t.Logf("--> FAILED")
  82. t.Fail()
  83. }
  84. t.Logf("trying WriteOne DROP")
  85. wr, err = conn.WriteOne("DROP TABLE IF EXISTS " + testTableName() + "")
  86. if err != nil {
  87. t.Logf("--> FAILED")
  88. t.Fail()
  89. }
  90. t.Logf("trying Close")
  91. conn.Close()
  92. t.Logf("trying WriteOne after Close")
  93. wr, err = conn.WriteOne("DROP TABLE IF EXISTS " + testTableName() + "")
  94. if err == nil {
  95. t.Logf("--> FAILED")
  96. t.Fail()
  97. }
  98. _ = wr
  99. t.Logf("trying Write after Close")
  100. t1 := make([]string, 0)
  101. t1 = append(t1, "DROP TABLE IF EXISTS "+testTableName()+"")
  102. t1 = append(t1, "DROP TABLE IF EXISTS "+testTableName()+"")
  103. wResults, err = conn.Write(t1)
  104. if err == nil {
  105. t.Logf("--> FAILED")
  106. t.Fail()
  107. }
  108. _ = wResults
  109. t.Logf("trying QueryOne after Close")
  110. qr, err = conn.QueryOne("SELECT id FROM " + testTableName() + "")
  111. if err == nil {
  112. t.Logf("--> FAILED")
  113. t.Fail()
  114. }
  115. _ = qr
  116. t.Logf("trying Query after Close")
  117. t2 := make([]string, 0)
  118. t2 = append(t2, "SELECT id FROM "+testTableName()+"")
  119. t2 = append(t2, "SELECT name FROM "+testTableName()+"")
  120. t2 = append(t2, "SELECT id,name FROM "+testTableName()+"")
  121. qResults, err = conn.Query(t2)
  122. if err == nil {
  123. t.Logf("--> FAILED")
  124. t.Fail()
  125. }
  126. _ = qResults
  127. }