Fork github.com/mattn/go-sqlite3 with adjustment for go1.16.2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7 KiB

  1. // +build !libsqlite3 sqlite_serialize
  2. package sqlite3
  3. import (
  4. "context"
  5. "database/sql"
  6. "os"
  7. "testing"
  8. )
  9. func TestSerializeDeserialize(t *testing.T) {
  10. // Connect to the source database.
  11. srcTempFilename := TempFilename(t)
  12. defer os.Remove(srcTempFilename)
  13. srcDb, err := sql.Open(driverName, srcTempFilename)
  14. if err != nil {
  15. t.Fatal("Failed to open the source database:", err)
  16. }
  17. defer srcDb.Close()
  18. err = srcDb.Ping()
  19. if err != nil {
  20. t.Fatal("Failed to connect to the source database:", err)
  21. }
  22. // Connect to the destination database.
  23. destTempFilename := TempFilename(t)
  24. defer os.Remove(destTempFilename)
  25. destDb, err := sql.Open(driverName, destTempFilename)
  26. if err != nil {
  27. t.Fatal("Failed to open the destination database:", err)
  28. }
  29. defer destDb.Close()
  30. err = destDb.Ping()
  31. if err != nil {
  32. t.Fatal("Failed to connect to the destination database:", err)
  33. }
  34. // Write data to source database.
  35. _, err = srcDb.Exec(`CREATE TABLE foo (name string)`)
  36. if err != nil {
  37. t.Fatal("Failed to create table in source database:", err)
  38. }
  39. _, err = srcDb.Exec(`INSERT INTO foo(name) VALUES("alice")`)
  40. if err != nil {
  41. t.Fatal("Failed to insert data into source database", err)
  42. }
  43. // Serialize the source database
  44. srcConn, err := srcDb.Conn(context.Background())
  45. if err != nil {
  46. t.Fatal("Failed to get connection to source database:", err)
  47. }
  48. defer srcConn.Close()
  49. var serialized []byte
  50. if err := srcConn.Raw(func(raw interface{}) error {
  51. var err error
  52. serialized, err = raw.(*SQLiteConn).Serialize("")
  53. return err
  54. }); err != nil {
  55. t.Fatal("Failed to serialize source database:", err)
  56. }
  57. srcConn.Close()
  58. // Confirm that the destination database is initially empty.
  59. var destTableCount int
  60. err = destDb.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'").Scan(&destTableCount)
  61. if err != nil {
  62. t.Fatal("Failed to check the destination table count:", err)
  63. }
  64. if destTableCount != 0 {
  65. t.Fatalf("The destination database is not empty; %v table(s) found.", destTableCount)
  66. }
  67. // Deserialize to destination database
  68. destConn, err := destDb.Conn(context.Background())
  69. if err != nil {
  70. t.Fatal("Failed to get connection to destination database:", err)
  71. }
  72. defer destConn.Close()
  73. if err := destConn.Raw(func(raw interface{}) error {
  74. return raw.(*SQLiteConn).Deserialize(serialized, "")
  75. }); err != nil {
  76. t.Fatal("Failed to deserialize source database:", err)
  77. }
  78. destConn.Close()
  79. // Confirm that destination database has been loaded correctly.
  80. var destRowCount int
  81. err = destDb.QueryRow(`SELECT COUNT(*) FROM foo`).Scan(&destRowCount)
  82. if err != nil {
  83. t.Fatal("Failed to count rows in destination database table", err)
  84. }
  85. if destRowCount != 1 {
  86. t.Fatalf("Destination table does not have the expected records")
  87. }
  88. }