Skip to content

Commit c8fa6d6

Browse files
author
Andreas Wagner
committed
Fix tests.
1 parent ce9020c commit c8fa6d6

11 files changed

+64
-30
lines changed

internal/handlers/admin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,6 @@ func TestAdminFunc(t *testing.T) {
113113
shutDownServer()
114114
})
115115

116-
fmt.Printf("\n\n\n\n")
116+
fmt.Printf("\n")
117117

118118
}

internal/handlers/api_standards_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,5 @@ func TestAPIStandardFunc(t *testing.T) {
176176
shutDownServer()
177177
})
178178

179-
fmt.Printf("\n\n\n\n")
179+
fmt.Printf("\n")
180180
}

internal/handlers/embeddings_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,5 @@ func TestEmbeddingsFunc(t *testing.T) {
334334
shutDownServer()
335335
})
336336

337-
fmt.Printf("\n\n\n\n")
337+
fmt.Printf("\n")
338338
}

internal/handlers/llm_services_sharing_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,5 +376,5 @@ func TestDefinitionSharingFunc(t *testing.T) {
376376
shutDownServer()
377377
})
378378

379-
fmt.Printf("\n\n\n\n")
379+
fmt.Printf("\n")
380380
}

internal/handlers/llm_services_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
func TestInstancesFunc(t *testing.T) {
1616

17-
fmt.Printf("\n\n\n\n")
17+
fmt.Printf("\n")
1818

1919
// Get the database connection pool from package variable
2020
pool := connPool

internal/handlers/projects_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,6 @@ func TestProjectTransactionRollback(t *testing.T) {
417417
shutDownServer()
418418
})
419419
420-
fmt.Printf("\n\n\n\n")
420+
fmt.Printf("\n")
421421
}
422422
*/

internal/handlers/projects_transfer_ownership_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestProjectTransferOwnershipFunc(t *testing.T) {
183183

184184
// Build the request URL
185185
requestURL := fmt.Sprintf("http://%s:%d%s", options.Host, options.Port, tc.requestPath)
186-
186+
187187
// Create the request
188188
req, err := http.NewRequest(tc.method, requestURL, bodyReader)
189189
if err != nil {
@@ -319,8 +319,24 @@ func TestProjectTransferOwnershipFunc(t *testing.T) {
319319
assert.Equal(t, "owner", response["role"])
320320
})
321321

322-
// Cleanup - shutdown the server
323-
defer shutDownServer()
322+
// Cleanup
323+
t.Cleanup(func() {
324+
fmt.Print("\n\nRunning cleanup ...\n\n")
325+
326+
requestURL := fmt.Sprintf("http://%s:%d/v1/admin/footgun", options.Host, options.Port)
327+
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
328+
assert.NoError(t, err)
329+
req.Header.Set("Authorization", "Bearer "+options.AdminKey)
330+
_, err = http.DefaultClient.Do(req)
331+
if err != nil && err.Error() != "no rows in result set" {
332+
t.Fatalf("Error sending request: %v\n", err)
333+
}
334+
assert.NoError(t, err)
335+
336+
fmt.Print("Shutting down server\n\n")
337+
shutDownServer()
338+
})
339+
324340
}
325341

326342
// Helper function to share a project

internal/handlers/public_access_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func TestPublicAccess(t *testing.T) {
209209
shutDownServer()
210210
})
211211

212-
fmt.Printf("\n\n\n\n")
212+
fmt.Printf("\n")
213213
}
214214

215215
// Helper function to post embeddings

internal/handlers/similars_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestSimilarsFunc(t *testing.T) {
9191
requestPath: "/v1/similars/alice/test1/https%3A%2F%2Fid.salamanca.school%2Ftexts%2FW0001%3Avol1.1.1.1.1",
9292
bodyPath: "",
9393
apiKey: aliceAPIKey,
94-
expectBody: "", // Will validate structure programmatically
94+
expectBody: "", // Will validate structure programmatically
9595
expectStatus: http.StatusOK,
9696
},
9797
{
@@ -100,7 +100,7 @@ func TestSimilarsFunc(t *testing.T) {
100100
requestPath: "/v1/similars/alice/test1/https%3A%2F%2Fid.salamanca.school%2Ftexts%2FW0001%3Avol1.1.1.1.1?metadata_path=author&metadata_value=Immanuel%20Kant",
101101
bodyPath: "",
102102
apiKey: options.AdminKey,
103-
expectBody: "", // Will validate structure programmatically
103+
expectBody: "", // Will validate structure programmatically
104104
expectStatus: http.StatusOK,
105105
},
106106
}
@@ -145,14 +145,14 @@ func TestSimilarsFunc(t *testing.T) {
145145

146146
respBody, err := io.ReadAll(resp.Body) // response body is []byte
147147
assert.NoError(t, err)
148-
148+
149149
// Parse and validate JSON structure
150150
if v.expectBody == "" && resp.StatusCode == http.StatusOK {
151151
// Validate that response has correct structure with results array
152152
var result map[string]interface{}
153153
err = json.Unmarshal(respBody, &result)
154154
assert.NoError(t, err)
155-
155+
156156
// Check results field exists and is an array
157157
results, ok := result["results"].([]interface{})
158158
if !ok {
@@ -212,7 +212,7 @@ func TestSimilarsFunc(t *testing.T) {
212212
shutDownServer()
213213
})
214214

215-
fmt.Printf("\n\n\n\n")
215+
fmt.Printf("\n")
216216
}
217217

218218
// TestPostSimilar tests the POST similar functionality.
@@ -296,11 +296,11 @@ func TestPostSimilar(t *testing.T) {
296296
expectError bool
297297
}{
298298
{
299-
name: "POST similar with valid 5D vector",
300-
method: http.MethodPost,
301-
requestPath: "/v1/similars/alice/test1",
302-
body: `{"vector": [-0.02085085, 0.01852216, 0.05327000, 0.07138438, 0.02000308]}`,
303-
apiKey: aliceAPIKey,
299+
name: "POST similar with valid 5D vector",
300+
method: http.MethodPost,
301+
requestPath: "/v1/similars/alice/test1",
302+
body: `{"vector": [-0.02085085, 0.01852216, 0.05327000, 0.07138438, 0.02000308]}`,
303+
apiKey: aliceAPIKey,
304304
expectStatus: http.StatusOK,
305305
expectIDs: []string{
306306
"https%3A%2F%2Fid.salamanca.school%2Ftexts%2FW0001%3Avol1.1.1.1.1",
@@ -310,11 +310,11 @@ func TestPostSimilar(t *testing.T) {
310310
expectError: false,
311311
},
312312
{
313-
name: "POST similar with valid 5D vector and metadata filter",
314-
method: http.MethodPost,
315-
requestPath: "/v1/similars/alice/test1?metadata_path=author&metadata_value=Immanuel%20Kant",
316-
body: `{"vector": [-0.02085085, 0.01852216, 0.05327000, 0.07138438, 0.02000308]}`,
317-
apiKey: aliceAPIKey,
313+
name: "POST similar with valid 5D vector and metadata filter",
314+
method: http.MethodPost,
315+
requestPath: "/v1/similars/alice/test1?metadata_path=author&metadata_value=Immanuel%20Kant",
316+
body: `{"vector": [-0.02085085, 0.01852216, 0.05327000, 0.07138438, 0.02000308]}`,
317+
apiKey: aliceAPIKey,
318318
expectStatus: http.StatusOK,
319319
expectIDs: []string{
320320
"https%3A%2F%2Fid.salamanca.school%2Ftexts%2FW0001%3Avol2",
@@ -425,7 +425,7 @@ func TestPostSimilar(t *testing.T) {
425425
continue
426426
}
427427
actualIDs[i] = id.(string)
428-
428+
429429
// Verify similarity field exists and is a number
430430
similarity, hasSim := resultItem["similarity"]
431431
if !hasSim {
@@ -434,7 +434,7 @@ func TestPostSimilar(t *testing.T) {
434434
t.Errorf("Result item %d 'similarity' is not a number", i)
435435
}
436436
}
437-
437+
438438
// Check that all expected IDs are present (order doesn't matter for similar items)
439439
for _, expectedID := range v.expectIDs {
440440
found := false
@@ -474,5 +474,5 @@ func TestPostSimilar(t *testing.T) {
474474
shutDownServer()
475475
})
476476

477-
fmt.Printf("\n\n\n\n")
477+
fmt.Printf("\n")
478478
}

internal/handlers/system_user_restrictions_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func TestSystemUserRestrictions(t *testing.T) {
2424
// Start the server
2525
err, shutDownServer := startTestServer(t, pool, mockKeyGen)
2626
assert.NoError(t, err)
27-
defer shutDownServer()
2827

2928
// Create a regular user (alice) for testing
3029
aliceJSON := `{"user_handle": "alice", "name": "Alice Doe", "email": "alice@foo.bar"}`
@@ -195,4 +194,23 @@ func TestSystemUserRestrictions(t *testing.T) {
195194
}
196195
})
197196
}
197+
198+
// Cleanup
199+
t.Cleanup(func() {
200+
fmt.Print("\n\nRunning cleanup ...\n\n")
201+
202+
requestURL := fmt.Sprintf("http://%s:%d/v1/admin/footgun", options.Host, options.Port)
203+
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
204+
assert.NoError(t, err)
205+
req.Header.Set("Authorization", "Bearer "+options.AdminKey)
206+
_, err = http.DefaultClient.Do(req)
207+
if err != nil && err.Error() != "no rows in result set" {
208+
t.Fatalf("Error sending request: %v\n", err)
209+
}
210+
assert.NoError(t, err)
211+
212+
fmt.Print("Shutting down server\n\n")
213+
shutDownServer()
214+
})
215+
198216
}

0 commit comments

Comments
 (0)