init writing first integration test

This commit is contained in:
2025-04-18 20:31:24 +02:00
parent db6bc33e9d
commit 65e06b25ff

View File

@@ -1,6 +1,9 @@
package main
import (
"bytes"
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/require"
@@ -88,6 +91,52 @@ func TestIntegration(t *testing.T) {
t.Log("Memgraph Running: ", memgraphC.IsRunning())
t.Log(dbAdapterC.Logs(t.Context()))
}
require.NoError(t, err)
dbAdapterHost, err := dbAdapterC.Host(t.Context())
require.NoError(t, err)
dbAdapterPort, err := dbAdapterC.MappedPort(t.Context(), "8080/tcp")
require.NoError(t, err)
dbAdapterURI := "http://" + dbAdapterHost + ":" + dbAdapterPort.Port()
testClient := &http.Client{}
t.Run("TestCreatePersonByGoogleId", CreatePersonByGoogleIdTest(dbAdapterURI, testClient))
}
func CreatePersonByGoogleIdTest(dbAdapterUri string, client *http.Client) func(t *testing.T) {
return func(t *testing.T) {
url := dbAdapterUri + "/person/google/test-google-id"
requestBody := map[string]any{
"first_name": "John",
"last_name": "Doe",
"born": "1990-01-01",
"limit": 10,
"mothers_first_name": "Jane",
"mothers_last_name": "Doe",
}
body, err := json.Marshal(requestBody)
require.NoError(t, err)
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, url, bytes.NewBuffer(body))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
// Send the request
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
var responseBody map[string]any
err = json.NewDecoder(resp.Body).Decode(&responseBody)
require.NoError(t, err)
// Validate the response
t.Log("Response Status Code: ", responseBody)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, int(1), responseBody["id"])
require.Equal(t, "John", responseBody["first_name"])
require.Equal(t, "Doe", responseBody["last_name"])
}
}