add person and relationship tests

This commit is contained in:
2025-04-22 12:23:12 +02:00
parent b307658c6c
commit 1471ed28f0
7 changed files with 151 additions and 7 deletions

View File

@@ -0,0 +1,17 @@
{
"person": {
"first_name": "Johannes",
"last_name": "Doe",
"born": "2000-07-01",
"limit": 1000,
"mothers_first_name": "Frances",
"mothers_last_name": "Soft",
"email": "dj@example.com"
},
"type": "child",
"relationship": {
"verified": true,
"notes": "Test notes",
"from": "2023-01-01"
}
}

View File

@@ -0,0 +1,15 @@
{
"person": {
"first_name": "Ferdinand",
"last_name": "Fritz",
"born": "1940-06-01",
"limit": 1000,
"mothers_first_name": "Feras",
"mothers_last_name": "Frea",
"email": "FFd@example.com"
},
"type": "parent",
"relationship": {
"verified": true
}
}

View File

@@ -0,0 +1,16 @@
{
"person": {
"first_name": "Sandra",
"last_name": "Doe",
"born": "1987-07-01",
"limit": 1000,
"mothers_first_name": "Mary",
"mothers_last_name": "Smith",
"email": "SD@example.com"
},
"type": "sibling",
"relationship": {
"verified": true,
"notes": "Good siblings"
}
}

View File

@@ -0,0 +1,15 @@
{
"person": {
"first_name": "Ferdinand",
"last_name": "Fritz",
"born": "1970-04-01",
"limit": 1000,
"mothers_first_name": "Seabruch",
"mothers_last_name": "Klein",
"email": "FF@example.com"
},
"type": "spouse",
"relationship": {
"verified": true
}
}

View File

@@ -1,4 +1,63 @@
package integration_tests
func CreatePersonAndRelationship() {
import (
"bytes"
_ "embed"
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
//go:embed payloads/create_person_and_relationship_child.json
var create_child []byte
//go:embed payloads/create_person_and_relationship_parent.json
var create_parent []byte
//go:embed payloads/create_person_and_relationship_spouse.json
var create_spouse []byte
//go:embed payloads/create_person_and_relationship_sibling.json
var create_sibling []byte
func CreateAFamilyTest(dbAdapterUri string, client *http.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Run("CreateChild", CreatePersonAndRelationshipTest(dbAdapterUri, &create_child, client))
t.Run("CreateParent", CreatePersonAndRelationshipTest(dbAdapterUri, &create_parent, client))
t.Run("CreateSpouse", CreatePersonAndRelationshipTest(dbAdapterUri, &create_spouse, client))
t.Run("CreateSibling", CreatePersonAndRelationshipTest(dbAdapterUri, &create_sibling, client))
}
}
func CreatePersonAndRelationshipTest(dbAdapterUri string, payload *[]byte, client *http.Client) func(t *testing.T) {
return func(t *testing.T) {
url := dbAdapterUri + "/person_and_relationship/1"
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, url, bytes.NewBuffer(create_other_person))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-User-ID", "0")
req.Header.Set("X-User-Name", "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
require.Equal(t, http.StatusOK, resp.StatusCode)
_, ok := responseBody["Id"]
require.True(t, ok)
_, ok = responseBody["person"]
require.True(t, ok)
_, ok = responseBody["relationship"]
require.True(t, ok)
}
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/dbtype"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/api/auth"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/internal/memgraph"
"github.com/vcscsvcscs/GenerationsHeritage/apps/db-adapter/pkg/api"
"go.uber.org/zap"
@@ -23,6 +24,12 @@ func (srv *server) CreatePersonAndRelationship(c *gin.Context, id int, params ap
session := srv.db.NewSession(c.Request.Context(), neo4j.SessionConfig{})
defer closeSession(c.Request.Context(), srv.logger, session, srv.dbOpTimeout)
if err := auth.CouldManagePersonUnknownAdmin(c.Request.Context(), session, id, params.XUserID); err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"msg": err.Error()})
return
}
trs, err := session.BeginTransaction(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
@@ -79,7 +86,7 @@ func (srv *server) CreatePersonAndRelationship(c *gin.Context, id int, params ap
convertedRelationship := memgraph.StructToMap(requestBody.Relationship)
var relationShipResultRaw any
var relationShipResultRaw neo4j.ResultWithContext
var relationshipError error
switch *requestBody.Type {
case api.CreatePersonAndRelationshipJSONBodyTypeChild:
@@ -126,8 +133,22 @@ func (srv *server) CreatePersonAndRelationship(c *gin.Context, id int, params ap
return
}
relationshipsSingle, err := relationShipResultRaw.Single(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"msg": "no relationship was created" + err.Error()})
return
}
relationships, ok := relationshipsSingle.Get("relationships")
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"msg": "no relationship was created"})
return
}
c.JSON(http.StatusOK, struct {
Person any `json:"person"`
Rel any `json:"relationship"`
}{Person: singleRes, Rel: relationShipResultRaw})
}{Person: singleRes, Rel: relationships})
}

View File

@@ -68,13 +68,13 @@ func TestIntegration(t *testing.T) {
Context: "./",
Dockerfile: "integration-test.dockerfile",
},
ExposedPorts: []string{"8080/tcp"},
ExposedPorts: []string{"5237/tcp"},
Env: map[string]string{
"MEMGRAPH_URI": memgraphURI,
"HTTP_PORT": ":8080",
"HTTP_PORT": ":5237",
},
Networks: []string{net.Name},
WaitingFor: wait.ForListeningPort("8080/tcp"),
WaitingFor: wait.ForListeningPort("5237/tcp"),
}
dbAdapterC, err := testcontainers.GenericContainer(t.Context(), testcontainers.GenericContainerRequest{
@@ -96,7 +96,7 @@ func TestIntegration(t *testing.T) {
dbAdapterHost, err := dbAdapterC.Host(t.Context())
require.NoError(t, err)
dbAdapterPort, err := dbAdapterC.MappedPort(t.Context(), "8080/tcp")
dbAdapterPort, err := dbAdapterC.MappedPort(t.Context(), "5237/tcp")
require.NoError(t, err)
dbAdapterURI := "http://" + dbAdapterHost + ":" + dbAdapterPort.Port()
@@ -111,6 +111,7 @@ func IntegrationTestFlow(dbAdapterURI string) func(t *testing.T) {
t.Run("UpdatePerson", integration_tests.UpdatePersonTest(dbAdapterURI, client))
t.Run("AddInviteCodeToPerson", integration_tests.UpdatePersonWithInviteCodeTest(dbAdapterURI, client))
t.Run("GetPersonById", integration_tests.GetPersonById(dbAdapterURI, client))
t.Run("CreateFamilyTest", integration_tests.CreateAFamilyTest(dbAdapterURI, client))
t.Run("SoftDeletePerson", integration_tests.SoftDeletePersonTest(dbAdapterURI, client))
t.Run("HardDeletePerson", integration_tests.HardDeletePersonTest(dbAdapterURI, client))
}