MCPcopy
hub / github.com/dnote/dnote / TestLocalMigration1

Function TestLocalMigration1

pkg/cli/migrate/migrate_test.go:314–387  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

312}
313
314func TestLocalMigration1(t *testing.T) {
315 // set up
316 db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql")
317 ctx := context.InitTestCtxWithDB(t, db)
318 data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"})
319 a1UUID := testutils.MustGenerateUUID(t)
320 database.MustExec(t, "inserting action", db,
321 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 1, "add_book", string(data), 1537829463)
322
323 data = testutils.MustMarshalJSON(t, actions.EditNoteDataV1{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: "", Content: "note 1"})
324 a2UUID := testutils.MustGenerateUUID(t)
325 database.MustExec(t, "inserting action", db,
326 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a2UUID, 1, "edit_note", string(data), 1537829463)
327
328 data = testutils.MustMarshalJSON(t, actions.EditNoteDataV1{NoteUUID: "note-2-uuid", FromBook: "js", ToBook: "", Content: "note 2"})
329 a3UUID := testutils.MustGenerateUUID(t)
330 database.MustExec(t, "inserting action", db,
331 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a3UUID, 1, "edit_note", string(data), 1537829463)
332
333 // Execute
334 tx, err := db.Begin()
335 if err != nil {
336 t.Fatal(errors.Wrap(err, "beginning a transaction"))
337 }
338
339 err = lm1.run(ctx, tx)
340 if err != nil {
341 tx.Rollback()
342 t.Fatal(errors.Wrap(err, "failed to run"))
343 }
344
345 tx.Commit()
346
347 // Test
348 var actionCount int
349 database.MustScan(t, "counting actions", db.QueryRow("SELECT count(*) FROM actions"), &actionCount)
350 assert.Equal(t, actionCount, 3, "action count mismatch")
351
352 var a1, a2, a3 actions.Action
353 var a1DataRaw, a2DataRaw, a3DataRaw string
354 database.MustScan(t, "getting action 1", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a1UUID),
355 &a1.Schema, &a1.Type, &a1DataRaw, &a1.Timestamp)
356 database.MustScan(t, "getting action 2", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a2UUID),
357 &a2.Schema, &a2.Type, &a2DataRaw, &a2.Timestamp)
358 database.MustScan(t, "getting action 3", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a3UUID),
359 &a3.Schema, &a3.Type, &a3DataRaw, &a3.Timestamp)
360
361 var a1Data actions.AddBookDataV1
362 var a2Data, a3Data actions.EditNoteDataV3
363 testutils.MustUnmarshalJSON(t, []byte(a1DataRaw), &a1Data)
364 testutils.MustUnmarshalJSON(t, []byte(a2DataRaw), &a2Data)
365 testutils.MustUnmarshalJSON(t, []byte(a3DataRaw), &a3Data)
366
367 assert.Equal(t, a1.Schema, 1, "a1 schema mismatch")
368 assert.Equal(t, a1.Type, "add_book", "a1 type mismatch")
369 assert.Equal(t, a1.Timestamp, int64(1537829463), "a1 timestamp mismatch")
370 assert.Equal(t, a1Data.BookName, "js", "a1 data book_name mismatch")
371

Callers

nothing calls this directly

Calls 12

InitTestMemoryDBRawFunction · 0.92
InitTestCtxWithDBFunction · 0.92
MustMarshalJSONFunction · 0.92
MustGenerateUUIDFunction · 0.92
MustExecFunction · 0.92
MustScanFunction · 0.92
EqualFunction · 0.92
MustUnmarshalJSONFunction · 0.92
BeginMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
QueryRowMethod · 0.65

Tested by

no test coverage detected