MCPcopy
hub / github.com/dnote/dnote / TestLocalMigration2

Function TestLocalMigration2

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

Source from the content-addressed store, hash-verified

387}
388
389func TestLocalMigration2(t *testing.T) {
390 // set up
391 db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql")
392 ctx := context.InitTestCtxWithDB(t, db)
393 c1 := "note 1 - v1"
394 c2 := "note 1 - v2"
395 css := "css"
396
397 b1UUID := testutils.MustGenerateUUID(t)
398 database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css")
399
400 data := testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: "note-1-uuid", BookName: "js", Content: "note 1", Public: false})
401 a1UUID := testutils.MustGenerateUUID(t)
402 database.MustExec(t, "inserting action", db,
403 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 2, "add_note", string(data), 1537829463)
404
405 data = testutils.MustMarshalJSON(t, actions.EditNoteDataV2{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: nil, Content: &c1, Public: nil})
406 a2UUID := testutils.MustGenerateUUID(t)
407 database.MustExec(t, "inserting action", db,
408 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a2UUID, 2, "edit_note", string(data), 1537829463)
409
410 data = testutils.MustMarshalJSON(t, actions.EditNoteDataV2{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: &css, Content: &c2, Public: nil})
411 a3UUID := testutils.MustGenerateUUID(t)
412 database.MustExec(t, "inserting action", db,
413 "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a3UUID, 2, "edit_note", string(data), 1537829463)
414
415 // Execute
416 tx, err := db.Begin()
417 if err != nil {
418 t.Fatal(errors.Wrap(err, "beginning a transaction"))
419 }
420
421 err = lm2.run(ctx, tx)
422 if err != nil {
423 tx.Rollback()
424 t.Fatal(errors.Wrap(err, "failed to run"))
425 }
426
427 tx.Commit()
428
429 // Test
430 var actionCount int
431 database.MustScan(t, "counting actions", db.QueryRow("SELECT count(*) FROM actions"), &actionCount)
432 assert.Equal(t, actionCount, 3, "action count mismatch")
433
434 var a1, a2, a3 actions.Action
435 var a1DataRaw, a2DataRaw, a3DataRaw string
436 database.MustScan(t, "getting action 1", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a1UUID),
437 &a1.Schema, &a1.Type, &a1DataRaw, &a1.Timestamp)
438 database.MustScan(t, "getting action 2", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a2UUID),
439 &a2.Schema, &a2.Type, &a2DataRaw, &a2.Timestamp)
440 database.MustScan(t, "getting action 3", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a3UUID),
441 &a3.Schema, &a3.Type, &a3DataRaw, &a3.Timestamp)
442
443 var a1Data actions.AddNoteDataV2
444 var a2Data, a3Data actions.EditNoteDataV3
445 testutils.MustUnmarshalJSON(t, []byte(a1DataRaw), &a1Data)
446 testutils.MustUnmarshalJSON(t, []byte(a2DataRaw), &a2Data)

Callers

nothing calls this directly

Calls 12

InitTestMemoryDBRawFunction · 0.92
InitTestCtxWithDBFunction · 0.92
MustGenerateUUIDFunction · 0.92
MustExecFunction · 0.92
MustMarshalJSONFunction · 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