|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "workspace-engine/pkg/oapi" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | +) |
| 11 | + |
| 12 | +func validateRetrievedSystems(t *testing.T, actualSystems []*oapi.System, expectedSystems []*oapi.System) { |
| 13 | + if len(actualSystems) != len(expectedSystems) { |
| 14 | + t.Fatalf("expected %d systems, got %d", len(expectedSystems), len(actualSystems)) |
| 15 | + } |
| 16 | + for _, expectedSystem := range expectedSystems { |
| 17 | + var actualSystem *oapi.System |
| 18 | + for _, as := range actualSystems { |
| 19 | + if as.Id == expectedSystem.Id { |
| 20 | + actualSystem = as |
| 21 | + break |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + if actualSystem == nil { |
| 26 | + t.Fatalf("expected system %v, got %v", expectedSystem, actualSystem) |
| 27 | + } |
| 28 | + if actualSystem.Id != expectedSystem.Id { |
| 29 | + t.Fatalf("expected system %v, got %v", expectedSystem, actualSystem) |
| 30 | + } |
| 31 | + if actualSystem.Name != expectedSystem.Name { |
| 32 | + t.Fatalf("expected system %v, got %v", expectedSystem, actualSystem) |
| 33 | + } |
| 34 | + compareStrPtr(t, actualSystem.Description, expectedSystem.Description) |
| 35 | + if actualSystem.WorkspaceId != expectedSystem.WorkspaceId { |
| 36 | + t.Fatalf("expected system %v, got %v", expectedSystem, actualSystem) |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestDBSystems_BasicWrite(t *testing.T) { |
| 42 | + workspaceID, conn := setupTestWithWorkspace(t) |
| 43 | + // Note: conn.Release() is handled by t.Cleanup in setupTestWithWorkspace |
| 44 | + |
| 45 | + tx, err := conn.Begin(t.Context()) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("failed to begin tx: %v", err) |
| 48 | + } |
| 49 | + defer tx.Rollback(t.Context()) |
| 50 | + |
| 51 | + id := uuid.New().String() |
| 52 | + name := fmt.Sprintf("test-system-%s", id[:8]) |
| 53 | + description := fmt.Sprintf("test-description-%s", id[:8]) |
| 54 | + sys := &oapi.System{ |
| 55 | + Id: id, |
| 56 | + WorkspaceId: workspaceID, |
| 57 | + Name: name, |
| 58 | + Description: &description, |
| 59 | + } |
| 60 | + |
| 61 | + err = writeSystem(t.Context(), sys, tx) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("expected no errors, got %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + err = tx.Commit(t.Context()) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("failed to commit: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + expectedSystems := []*oapi.System{sys} |
| 72 | + actualSystems, err := getSystems(t.Context(), workspaceID) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("expected no errors, got %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + validateRetrievedSystems(t, actualSystems, expectedSystems) |
| 78 | +} |
| 79 | + |
| 80 | +func TestDBSystems_BasicWriteAndDelete(t *testing.T) { |
| 81 | + workspaceID, conn := setupTestWithWorkspace(t) |
| 82 | + // Note: conn.Release() is handled by t.Cleanup in setupTestWithWorkspace |
| 83 | + |
| 84 | + tx, err := conn.Begin(t.Context()) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("failed to begin tx: %v", err) |
| 87 | + } |
| 88 | + defer tx.Rollback(t.Context()) |
| 89 | + |
| 90 | + id := uuid.New().String() |
| 91 | + name := fmt.Sprintf("test-system-%s", id[:8]) |
| 92 | + description := fmt.Sprintf("test-description-%s", id[:8]) |
| 93 | + sys := &oapi.System{ |
| 94 | + Id: id, |
| 95 | + WorkspaceId: workspaceID, |
| 96 | + Name: name, |
| 97 | + Description: &description, |
| 98 | + } |
| 99 | + |
| 100 | + err = writeSystem(t.Context(), sys, tx) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("expected no errors, got %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + err = tx.Commit(t.Context()) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("failed to commit: %v", err) |
| 108 | + } |
| 109 | + |
| 110 | + tx, err = conn.Begin(t.Context()) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("failed to begin tx: %v", err) |
| 113 | + } |
| 114 | + defer tx.Rollback(t.Context()) |
| 115 | + |
| 116 | + expectedSystems := []*oapi.System{sys} |
| 117 | + actualSystems, err := getSystems(t.Context(), workspaceID) |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("expected no errors, got %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + validateRetrievedSystems(t, actualSystems, expectedSystems) |
| 123 | + |
| 124 | + err = deleteSystem(t.Context(), id, tx) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("expected no errors, got %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + err = tx.Commit(t.Context()) |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("failed to commit: %v", err) |
| 132 | + } |
| 133 | + |
| 134 | + actualSystems, err = getSystems(t.Context(), workspaceID) |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("expected no errors, got %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + validateRetrievedSystems(t, actualSystems, []*oapi.System{}) |
| 140 | +} |
| 141 | + |
| 142 | +func TestDBSystems_BasicWriteAndUpdate(t *testing.T) { |
| 143 | + workspaceID, conn := setupTestWithWorkspace(t) |
| 144 | + // Note: conn.Release() is handled by t.Cleanup in setupTestWithWorkspace |
| 145 | + |
| 146 | + tx, err := conn.Begin(t.Context()) |
| 147 | + if err != nil { |
| 148 | + t.Fatalf("failed to begin tx: %v", err) |
| 149 | + } |
| 150 | + defer tx.Rollback(t.Context()) |
| 151 | + |
| 152 | + id := uuid.New().String() |
| 153 | + name := fmt.Sprintf("test-system-%s", id[:8]) |
| 154 | + description := fmt.Sprintf("test-description-%s", id[:8]) |
| 155 | + sys := &oapi.System{ |
| 156 | + Id: id, |
| 157 | + WorkspaceId: workspaceID, |
| 158 | + Name: name, |
| 159 | + Description: &description, |
| 160 | + } |
| 161 | + |
| 162 | + err = writeSystem(t.Context(), sys, tx) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("expected no errors, got %v", err) |
| 165 | + } |
| 166 | + |
| 167 | + err = tx.Commit(t.Context()) |
| 168 | + if err != nil { |
| 169 | + t.Fatalf("failed to commit: %v", err) |
| 170 | + } |
| 171 | + |
| 172 | + tx, err = conn.Begin(t.Context()) |
| 173 | + if err != nil { |
| 174 | + t.Fatalf("failed to begin tx: %v", err) |
| 175 | + } |
| 176 | + defer tx.Rollback(t.Context()) |
| 177 | + |
| 178 | + sys.Description = &description |
| 179 | + err = writeSystem(t.Context(), sys, tx) |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("expected no errors, got %v", err) |
| 182 | + } |
| 183 | + |
| 184 | + err = tx.Commit(t.Context()) |
| 185 | + if err != nil { |
| 186 | + t.Fatalf("failed to commit: %v", err) |
| 187 | + } |
| 188 | + |
| 189 | + actualSystems, err := getSystems(t.Context(), workspaceID) |
| 190 | + if err != nil { |
| 191 | + t.Fatalf("expected no errors, got %v", err) |
| 192 | + } |
| 193 | + |
| 194 | + validateRetrievedSystems(t, actualSystems, []*oapi.System{sys}) |
| 195 | +} |
| 196 | + |
| 197 | +func TestDBSystems_NonexistentWorkspaceThrowsError(t *testing.T) { |
| 198 | + _, conn := setupTestWithWorkspace(t) |
| 199 | + // Note: conn.Release() is handled by t.Cleanup in setupTestWithWorkspace |
| 200 | + |
| 201 | + tx, err := conn.Begin(t.Context()) |
| 202 | + if err != nil { |
| 203 | + t.Fatalf("failed to begin tx: %v", err) |
| 204 | + } |
| 205 | + defer tx.Rollback(t.Context()) |
| 206 | + |
| 207 | + description := "test-description" |
| 208 | + sys := &oapi.System{ |
| 209 | + Id: uuid.New().String(), |
| 210 | + WorkspaceId: uuid.New().String(), |
| 211 | + Name: "test-system", |
| 212 | + Description: &description, |
| 213 | + } |
| 214 | + |
| 215 | + err = writeSystem(t.Context(), sys, tx) |
| 216 | + // should throw fk constraint error |
| 217 | + if err == nil { |
| 218 | + t.Fatalf("expected FK violation error, got nil") |
| 219 | + } |
| 220 | + |
| 221 | + // Check for foreign key violation (SQLSTATE 23503) |
| 222 | + if !strings.Contains(err.Error(), "23503") && !strings.Contains(err.Error(), "foreign key") { |
| 223 | + t.Fatalf("expected FK violation error, got: %v", err) |
| 224 | + } |
| 225 | + |
| 226 | +} |
0 commit comments