Monorepo for Tangled
tangled.org
1package db
2
3import (
4 "context"
5 "path/filepath"
6 "testing"
7
8 "github.com/bluesky-social/indigo/atproto/syntax"
9 "tangled.org/core/appview/models"
10)
11
12func TestRemoveReposByKnotCascadesEntities(t *testing.T) {
13 d := newTestDB(t)
14
15 knot := "kelp.example"
16 repo := seedRepo(t, d, "did:plc:akshay", knot, "anemone", "anemone", "did:plc:anemone")
17
18 starNotif := &models.Notification{
19 RecipientDid: "did:plc:akshay",
20 ActorDid: "did:plc:boltless",
21 Type: models.NotificationTypeRepoStarred,
22 EntityType: "repo",
23 EntityId: repo.RepoAt().String(),
24 RepoId: &repo.Id,
25 }
26 if err := CreateNotification(d, starNotif); err != nil {
27 t.Fatalf("CreateNotification repo: %v", err)
28 }
29
30 tx, err := d.Begin()
31 if err != nil {
32 t.Fatalf("Begin: %v", err)
33 }
34 issue := &models.Issue{
35 Did: "did:plc:akshay",
36 Rkey: "issue1",
37 RepoDid: syntax.DID(repo.RepoDid),
38 Title: "title",
39 Body: "body",
40 Open: true,
41 }
42 if err := PutIssue(tx, issue); err != nil {
43 t.Fatalf("PutIssue: %v", err)
44 }
45 if err := tx.Commit(); err != nil {
46 t.Fatalf("Commit: %v", err)
47 }
48
49 issueNotif := &models.Notification{
50 RecipientDid: "did:plc:akshay",
51 ActorDid: "did:plc:boltless",
52 Type: models.NotificationTypeIssueCommented,
53 EntityType: "issue",
54 EntityId: issue.AtUri().String(),
55 IssueId: &issue.Id,
56 }
57 if err := CreateNotification(d, issueNotif); err != nil {
58 t.Fatalf("CreateNotification issue: %v", err)
59 }
60
61 if err := RemoveReposByKnot(d, knot); err != nil {
62 t.Fatalf("RemoveReposByKnot: %v", err)
63 }
64
65 if got := countRows(t, d, "select count(*) from repos where knot = ?", knot); got != 0 {
66 t.Errorf("repos remaining: got %d, want 0", got)
67 }
68 if got := countRows(t, d, "select count(*) from issues where repo_did = ?", repo.RepoDid); got != 0 {
69 t.Errorf("issues remaining: got %d, want 0", got)
70 }
71 if got := countRows(t, d, "select count(*) from notifications"); got != 0 {
72 t.Errorf("notifications remaining: got %d, want 0", got)
73 }
74}
75
76func TestMakeReopenPreservesOrphanData(t *testing.T) {
77 path := filepath.Join(t.TempDir(), "reopen.db")
78
79 d, err := Make(context.Background(), path)
80 if err != nil {
81 t.Fatalf("first Make: %v", err)
82 }
83
84 repo := seedRepo(t, d, "did:plc:akshay", "ghost.example", "anemone", "anemone", "did:plc:anemone")
85 notif := &models.Notification{
86 RecipientDid: "did:plc:akshay",
87 ActorDid: "did:plc:boltless",
88 Type: models.NotificationTypeRepoStarred,
89 EntityType: "repo",
90 EntityId: repo.RepoAt().String(),
91 RepoId: &repo.Id,
92 }
93 if err := CreateNotification(d, notif); err != nil {
94 t.Fatalf("CreateNotification: %v", err)
95 }
96 if err := d.Close(); err != nil {
97 t.Fatalf("Close: %v", err)
98 }
99
100 d2, err := Make(context.Background(), path)
101 if err != nil {
102 t.Fatalf("second Make: %v", err)
103 }
104 t.Cleanup(func() { d2.Close() })
105
106 if got := countRows(t, d2, "select count(*) from repos where knot = ?", "ghost.example"); got != 1 {
107 t.Errorf("orphan repo lost across reopen: got %d, want 1", got)
108 }
109 if got := countRows(t, d2, "select count(*) from notifications"); got != 1 {
110 t.Errorf("notification lost across reopen: got %d, want 1", got)
111 }
112}