|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/git-calendar/core/pkg/core" |
| 7 | + "github.com/google/uuid" |
| 8 | +) |
| 9 | + |
| 10 | +const TestTagName = "test" |
| 11 | + |
| 12 | +func TestTag_CreateTag_CreatesTag(t *testing.T) { |
| 13 | + c := newTestCore(t) |
| 14 | + |
| 15 | + tag := newTestTag("work", "blue") |
| 16 | + |
| 17 | + created, err := c.CreateTag(TestTagName, tag) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("CreateTag failed: %v", err) |
| 20 | + } |
| 21 | + if created == nil { |
| 22 | + t.Fatalf("expected created tag") |
| 23 | + } |
| 24 | + |
| 25 | + assertTagEqual(t, tag, *created) |
| 26 | +} |
| 27 | + |
| 28 | +func TestTag_CreateTag_DuplicateReturnsError(t *testing.T) { |
| 29 | + c := newTestCore(t) |
| 30 | + |
| 31 | + tag := newTestTag("work", "blue") |
| 32 | + |
| 33 | + if _, err := c.CreateTag(TestTagName, tag); err != nil { |
| 34 | + t.Fatalf("CreateTag failed: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + created, err := c.CreateTag(TestTagName, tag) |
| 38 | + if err == nil { |
| 39 | + t.Fatalf("expected duplicate tag error") |
| 40 | + } |
| 41 | + if created != nil { |
| 42 | + t.Fatalf("expected nil created tag, got %+v", created) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestTag_CreateTag_InvalidTagReturnsError(t *testing.T) { |
| 47 | + c := newTestCore(t) |
| 48 | + |
| 49 | + tag := core.Tag{ |
| 50 | + Name: "", |
| 51 | + Color: "blue", |
| 52 | + } |
| 53 | + |
| 54 | + created, err := c.CreateTag(TestTagName, tag) |
| 55 | + if err == nil { |
| 56 | + t.Fatalf("expected invalid tag error") |
| 57 | + } |
| 58 | + if created != nil { |
| 59 | + t.Fatalf("expected nil created tag, got %+v", created) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestTag_UpdateTag_UpdatesExistingTag(t *testing.T) { |
| 64 | + c := newTestCore(t) |
| 65 | + |
| 66 | + tag := newTestTag("work", "blue") |
| 67 | + |
| 68 | + if _, err := c.CreateTag(TestTagName, tag); err != nil { |
| 69 | + t.Fatalf("CreateTag failed: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + updated := core.Tag{ |
| 73 | + Id: tag.Id, |
| 74 | + Name: "personal", |
| 75 | + Color: "blue", |
| 76 | + } |
| 77 | + |
| 78 | + got, err := c.UpdateTag(TestTagName, updated) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("UpdateTag failed: %v", err) |
| 81 | + } |
| 82 | + if got == nil { |
| 83 | + t.Fatalf("expected updated tag") |
| 84 | + } |
| 85 | + |
| 86 | + assertTagEqual(t, updated, *got) |
| 87 | +} |
| 88 | + |
| 89 | +func TestTag_UpdateTag_InvalidTagReturnsError(t *testing.T) { |
| 90 | + c := newTestCore(t) |
| 91 | + |
| 92 | + tag := core.Tag{ |
| 93 | + Name: "work", |
| 94 | + Color: "red", |
| 95 | + } |
| 96 | + |
| 97 | + updated, err := c.UpdateTag(TestTagName, tag) |
| 98 | + if err == nil { |
| 99 | + t.Fatalf("expected invalid tag error") |
| 100 | + } |
| 101 | + if updated != nil { |
| 102 | + t.Fatalf("expected nil updated tag, got %+v", updated) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestTag_UpdateTag_MissingTagReturnsErrorInsteadOfPanicking(t *testing.T) { |
| 107 | + c := newTestCore(t) |
| 108 | + |
| 109 | + tag := newTestTag("missing", "blue") |
| 110 | + |
| 111 | + defer func() { |
| 112 | + if r := recover(); r != nil { |
| 113 | + t.Fatalf("expected error for missing tag, got panic: %v", r) |
| 114 | + } |
| 115 | + }() |
| 116 | + |
| 117 | + updated, err := c.UpdateTag(TestTagName, tag) |
| 118 | + if err == nil { |
| 119 | + t.Fatalf("expected missing tag error") |
| 120 | + } |
| 121 | + if updated != nil { |
| 122 | + t.Fatalf("expected nil updated tag, got %+v", updated) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestTag_RemoveTag_RemovesTag(t *testing.T) { |
| 127 | + c := newTestCore(t) |
| 128 | + |
| 129 | + tag := newTestTag("work", "blue") |
| 130 | + |
| 131 | + if _, err := c.CreateTag(TestTagName, tag); err != nil { |
| 132 | + t.Fatalf("CreateTag failed: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + if err := c.RemoveTag(TestTagName, tag.Id); err != nil { |
| 136 | + t.Fatalf("RemoveTag failed: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + // creating the same id again proves it was removed from the in-memory tag list |
| 140 | + recreated := core.Tag{ |
| 141 | + Id: tag.Id, |
| 142 | + Name: "recreated", |
| 143 | + Color: "blue", |
| 144 | + } |
| 145 | + |
| 146 | + got, err := c.CreateTag(TestTagName, recreated) |
| 147 | + if err != nil { |
| 148 | + t.Fatalf("CreateTag after RemoveTag failed: %v", err) |
| 149 | + } |
| 150 | + if got == nil { |
| 151 | + t.Fatalf("expected recreated tag") |
| 152 | + } |
| 153 | + |
| 154 | + assertTagEqual(t, recreated, *got) |
| 155 | +} |
| 156 | + |
| 157 | +func TestTag_RemoveTag_InvalidIdReturnsError(t *testing.T) { |
| 158 | + c := newTestCore(t) |
| 159 | + |
| 160 | + err := c.RemoveTag(TestTagName, uuid.Nil) |
| 161 | + if err == nil { |
| 162 | + t.Fatalf("expected invalid tag id error") |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func TestTag_InvalidCalendarReturnsError(t *testing.T) { |
| 167 | + c := newTestCore(t) |
| 168 | + |
| 169 | + tag := newTestTag("work", "green") |
| 170 | + |
| 171 | + if _, err := c.CreateTag("missing", tag); err == nil { |
| 172 | + t.Fatalf("expected CreateTag invalid calendar error") |
| 173 | + } |
| 174 | + |
| 175 | + if _, err := c.UpdateTag("missing", tag); err == nil { |
| 176 | + t.Fatalf("expected UpdateTag invalid calendar error") |
| 177 | + } |
| 178 | + |
| 179 | + if err := c.RemoveTag("missing", tag.Id); err == nil { |
| 180 | + t.Fatalf("expected RemoveTag invalid calendar error") |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func newTestTag(name string, color string) core.Tag { |
| 185 | + return core.Tag{ |
| 186 | + Id: uuid.New(), |
| 187 | + Name: name, |
| 188 | + Color: color, |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +func assertTagEqual(t *testing.T, want core.Tag, got core.Tag) { |
| 193 | + t.Helper() |
| 194 | + |
| 195 | + if got.Id != want.Id { |
| 196 | + t.Fatalf("tag id mismatch: expected %s, got %s", want.Id, got.Id) |
| 197 | + } |
| 198 | + if got.Name != want.Name { |
| 199 | + t.Fatalf("tag name mismatch: expected %q, got %q", want.Name, got.Name) |
| 200 | + } |
| 201 | + if got.Color != want.Color { |
| 202 | + t.Fatalf("tag color mismatch: expected %q, got %q", want.Color, got.Color) |
| 203 | + } |
| 204 | +} |
0 commit comments