105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
|
package test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/database"
|
||
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
||
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
|
||
|
"log"
|
||
|
"strconv"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/stretchr/testify/suite"
|
||
|
)
|
||
|
|
||
|
type CustomerRepoTestSuite struct {
|
||
|
suite.Suite
|
||
|
database database.OtterSpace
|
||
|
pgContainer *PostgresContainer
|
||
|
ctx context.Context
|
||
|
}
|
||
|
|
||
|
func (suite *CustomerRepoTestSuite) SetupSuite() {
|
||
|
suite.ctx = context.Background()
|
||
|
pgContainer, err := CreatePostgresContainer(suite.ctx)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
suite.pgContainer = pgContainer
|
||
|
|
||
|
databaseClient := database.NewPostgresqlConnection(true)
|
||
|
containerInspect, _ := suite.pgContainer.Inspect(suite.ctx)
|
||
|
dbPortMap := containerInspect.NetworkSettings.Ports
|
||
|
var dbPortStr string
|
||
|
for _, v := range dbPortMap {
|
||
|
for _, n := range v {
|
||
|
dbPortStr = n.HostPort
|
||
|
}
|
||
|
}
|
||
|
dbPort, err := strconv.Atoi(dbPortStr)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
err = databaseClient.Connect(suite.ctx, "localhost", suite.pgContainer.DatabaseUser, suite.pgContainer.DatabasePassword, suite.pgContainer.DatabaseName, dbPort, "disable", "Europe/Berlin")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
suite.database = databaseClient
|
||
|
|
||
|
}
|
||
|
|
||
|
func (suite *CustomerRepoTestSuite) TearDownSuite() {
|
||
|
if err := suite.pgContainer.Terminate(suite.ctx); err != nil {
|
||
|
log.Fatalf("error terminating postgres container: %s", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (suite *CustomerRepoTestSuite) TestCreateTagNodeWitRelation() {
|
||
|
t := suite.T()
|
||
|
post := graphModels.AnthrovePost{
|
||
|
PostID: "1234",
|
||
|
Rating: models.SFW,
|
||
|
}
|
||
|
|
||
|
tag := &graphModels.AnthroveTag{
|
||
|
Name: "test_tag",
|
||
|
Type: "general",
|
||
|
}
|
||
|
|
||
|
err := suite.database.AddPost(suite.ctx, &post)
|
||
|
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
err = suite.database.AddTagWithRelationToPost(suite.ctx, "1234", tag)
|
||
|
|
||
|
assert.NoError(t, err)
|
||
|
}
|
||
|
|
||
|
func (suite *CustomerRepoTestSuite) TestGetTags() {
|
||
|
t := suite.T()
|
||
|
|
||
|
expectedTag := []graphModels.TagsWithFrequency{
|
||
|
{
|
||
|
Frequency: 0,
|
||
|
Tags: graphModels.AnthroveTag{
|
||
|
Name: "test_tag",
|
||
|
Type: "general",
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
tag, err := suite.database.GetAllTags(suite.ctx)
|
||
|
|
||
|
assert.NoError(t, err)
|
||
|
assert.Equal(t, expectedTag, tag)
|
||
|
assert.Len(t, tag, 1)
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestCustomerRepoTestSuite(t *testing.T) {
|
||
|
suite.Run(t, new(CustomerRepoTestSuite))
|
||
|
}
|