| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | crand "crypto/rand" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestSuiteImportIsTeamRandomAndAnnouncedOnce(t *testing.T) { |
| 10 | app, _ := sealedApp(t, Config{}) |
| 11 | owner := app.TestPerson(t, "owner@example.com") |
| 12 | ctx := context.Background() |
| 13 | imp, err := convertImport(ctx, app.store.core, nestedDoc(t)) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | t.Cleanup(func() { randomSource = crand.Reader }) |
| 18 | randomSource = draws(101, 102) |
| 19 | doc, err := app.store.CreateImported(ctx, owner, "Imported", "suite-import", imp) |
| 20 | randomSource = crand.Reader |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if doc.Visibility != VisibilityTeam || doc.Revision != 101 { |
| 25 | t.Errorf("import visibility/revision = %q/%d, want team/101", doc.Visibility, doc.Revision) |
| 26 | } |
| 27 | rows := feedRowsOf(t, app.store, doc.ID) |
| 28 | if len(rows) != 1 || rows[0].Revision != doc.Revision || rows[0].Stamp != 102 || rows[0].Gone { |
| 29 | t.Errorf("import feed = %+v, want one current revision with nonce 102", rows) |
| 30 | } |
| 31 | again, err := app.store.CreateImported(ctx, owner, "Retry", "suite-import", imp) |
| 32 | if err != nil || again.ID != doc.ID || again.Revision != doc.Revision { |
| 33 | t.Fatalf("retry changed document: %+v, %v", again, err) |
| 34 | } |
| 35 | if rows := feedRowsOf(t, app.store, doc.ID); len(rows) != 1 { |
| 36 | t.Errorf("retry announced %d rows, want 1", len(rows)) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestSuiteImportRollsBackWithoutItsFeedRow(t *testing.T) { |
| 41 | app, _ := sealedApp(t, Config{}) |
| 42 | owner := app.TestPerson(t, "owner@example.com") |
| 43 | ctx := context.Background() |
| 44 | imp, err := convertImport(ctx, app.store.core, nestedDoc(t)) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if err := app.store.db.Exec(`CREATE TRIGGER suite_feed_refuses BEFORE INSERT ON suite_feed BEGIN SELECT RAISE(ABORT, 'refused'); END`).Error; err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if _, err := app.store.CreateImported(ctx, owner, "Imported", "suite-import", imp); err == nil { |
| 52 | t.Error("import committed without its feed row") |
| 53 | } |
| 54 | for _, table := range []string{"documents", "doc_updates", "doc_import_reports", "suite_feed"} { |
| 55 | if n := countRows(t, app, table); n != 0 { |
| 56 | t.Errorf("failed import left %d rows in %s", n, table) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |