MCPcopy Index your code
hub / github.com/dnote/dnote / TestPrepareEmptyServerSync

Function TestPrepareEmptyServerSync

pkg/cli/cmd/sync/sync_test.go:3223–3287  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3221}
3222
3223func TestPrepareEmptyServerSync(t *testing.T) {
3224 // set up
3225 db := database.InitTestMemoryDB(t)
3226
3227 // Setup: local has synced data (usn > 0, dirty = false) and some deleted items
3228 database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 5, false, false)
3229 database.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b2-uuid", "b2-label", 8, false, false)
3230 database.MustExec(t, "inserting b3 deleted", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b3-uuid", "b3-label", 6, true, false)
3231 database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", "note 1", 6, false, false, 1541108743)
3232 database.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", "b2-uuid", "note 2", 9, false, false, 1541108743)
3233 database.MustExec(t, "inserting n3 deleted", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n3-uuid", "b1-uuid", "note 3", 7, true, false, 1541108743)
3234 database.MustExec(t, "setting last_max_usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 9)
3235
3236 // execute
3237 tx, err := db.Begin()
3238 if err != nil {
3239 t.Fatal(errors.Wrap(err, "beginning transaction"))
3240 }
3241
3242 if err := prepareEmptyServerSync(tx); err != nil {
3243 tx.Rollback()
3244 t.Fatal(errors.Wrap(err, "executing prepareEmptyServerSync"))
3245 }
3246
3247 tx.Commit()
3248
3249 // test - verify non-deleted items are marked dirty with usn=0, deleted items unchanged
3250 var b1, b2, b3 database.Book
3251 database.MustScan(t, "getting b1", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b1-uuid"), &b1.USN, &b1.Dirty, &b1.Deleted)
3252 database.MustScan(t, "getting b2", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b2-uuid"), &b2.USN, &b2.Dirty, &b2.Deleted)
3253 database.MustScan(t, "getting b3", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b3-uuid"), &b3.USN, &b3.Dirty, &b3.Deleted)
3254
3255 assert.Equal(t, b1.USN, 0, "b1 USN should be reset to 0")
3256 assert.Equal(t, b1.Dirty, true, "b1 should be marked dirty")
3257 assert.Equal(t, b1.Deleted, false, "b1 should not be deleted")
3258
3259 assert.Equal(t, b2.USN, 0, "b2 USN should be reset to 0")
3260 assert.Equal(t, b2.Dirty, true, "b2 should be marked dirty")
3261 assert.Equal(t, b2.Deleted, false, "b2 should not be deleted")
3262
3263 assert.Equal(t, b3.USN, 6, "b3 USN should remain unchanged (deleted item)")
3264 assert.Equal(t, b3.Dirty, false, "b3 should not be marked dirty (deleted item)")
3265 assert.Equal(t, b3.Deleted, true, "b3 should remain deleted")
3266
3267 var n1, n2, n3 database.Note
3268 database.MustScan(t, "getting n1", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n1-uuid"), &n1.USN, &n1.Dirty, &n1.Deleted)
3269 database.MustScan(t, "getting n2", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n2-uuid"), &n2.USN, &n2.Dirty, &n2.Deleted)
3270 database.MustScan(t, "getting n3", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n3-uuid"), &n3.USN, &n3.Dirty, &n3.Deleted)
3271
3272 assert.Equal(t, n1.USN, 0, "n1 USN should be reset to 0")
3273 assert.Equal(t, n1.Dirty, true, "n1 should be marked dirty")
3274 assert.Equal(t, n1.Deleted, false, "n1 should not be deleted")
3275
3276 assert.Equal(t, n2.USN, 0, "n2 USN should be reset to 0")
3277 assert.Equal(t, n2.Dirty, true, "n2 should be marked dirty")
3278 assert.Equal(t, n2.Deleted, false, "n2 should not be deleted")
3279
3280 assert.Equal(t, n3.USN, 7, "n3 USN should remain unchanged (deleted item)")

Callers

nothing calls this directly

Calls 9

InitTestMemoryDBFunction · 0.92
MustExecFunction · 0.92
MustScanFunction · 0.92
EqualFunction · 0.92
prepareEmptyServerSyncFunction · 0.85
BeginMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
QueryRowMethod · 0.65

Tested by

no test coverage detected