dnote/core/core_test.go
Sung Won Cho fa1da50fc5
Implement state-based sync (#144)
* Migrate uuids of books that already exist in server

* Remove actions

* Add dirty flag for notes and books

* Drop actions table

* Implement sync

* Add debug

* Update uuid after posting resources to the server

* Fix dev script
2018-12-02 11:04:16 +10:00

77 lines
2.1 KiB
Go

package core
import (
"testing"
"github.com/dnote/cli/testutils"
"github.com/pkg/errors"
)
func TestInitSystemKV(t *testing.T) {
// Setup
ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true)
defer testutils.TeardownEnv(ctx)
db := ctx.DB
var originalCount int
testutils.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &originalCount)
// Execute
tx, err := db.Begin()
if err != nil {
t.Fatal(errors.Wrap(err, "beginning a transaction"))
}
if err := initSystemKV(tx, "testKey", "testVal"); err != nil {
tx.Rollback()
t.Fatal(errors.Wrap(err, "executing"))
}
tx.Commit()
// Test
var count int
testutils.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &count)
testutils.AssertEqual(t, count, originalCount+1, "system count mismatch")
var val string
testutils.MustScan(t, "getting system value",
db.QueryRow("SELECT value FROM system WHERE key = ?", "testKey"), &val)
testutils.AssertEqual(t, val, "testVal", "system value mismatch")
}
func TestInitSystemKV_existing(t *testing.T) {
// Setup
ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true)
defer testutils.TeardownEnv(ctx)
db := ctx.DB
testutils.MustExec(t, "inserting a system config", db, "INSERT INTO system (key, value) VALUES (?, ?)", "testKey", "testVal")
var originalCount int
testutils.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &originalCount)
// Execute
tx, err := db.Begin()
if err != nil {
t.Fatal(errors.Wrap(err, "beginning a transaction"))
}
if err := initSystemKV(tx, "testKey", "newTestVal"); err != nil {
tx.Rollback()
t.Fatal(errors.Wrap(err, "executing"))
}
tx.Commit()
// Test
var count int
testutils.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &count)
testutils.AssertEqual(t, count, originalCount, "system count mismatch")
var val string
testutils.MustScan(t, "getting system value",
db.QueryRow("SELECT value FROM system WHERE key = ?", "testKey"), &val)
testutils.AssertEqual(t, val, "testVal", "system value should not have been updated")
}