Compare commits

..

No commits in common. "main" and "v2.2.0" have entirely different histories.
main ... v2.2.0

88 changed files with 9874 additions and 8675 deletions

View File

@ -3,7 +3,8 @@ run-name: ${{ gitea.actor }} is testing the build
on:
push:
branches:
- main
- ci/*
- dev/*
pull_request:
branches: [ "main" ]

View File

@ -1,4 +1,4 @@
![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v5/actions/workflows/build_check.yaml/badge.svg)
![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v2/actions/workflows/build_check.yaml/badge.svg)
[![Bugs](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=bugs&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK)
[![Code Smells](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=code_smells&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK)
[![Coverage](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=coverage&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK)
@ -22,7 +22,7 @@ The OtterSpace SDK is a Go package for interacting with the OtterSpace API. It p
To install the OtterSpace SDK, you can use `go get`:
```shell
go get git.anthrove.art/Anthrove/otter-space-sdk/v5
go get git.anthrove.art/Anthrove/otter-space-sdk/v2
````
## Usage
@ -33,33 +33,23 @@ package main
import (
"context"
"log"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/database"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
)
func main() {
ctx := context.Background()
var err error
cfg := models.DatabaseConfig{}
config := models.DatabaseConfig{
Endpoint: "",
Username: "",
Password: "",
Database: "",
Port: 5432,
SSL: false,
Timezone: "Europe/Berlin",
Debug: false,
}
err = database.Connect(ctx, config)
pgClient := database.NewPostgresqlConnection()
err := pgClient.Connect(ctx, cfg)
if err != nil {
panic(err)
log.Panic(err)
}
}
```
This example creates a new client, connects to the OtterSpace API, and then the client can be used to interact with the API.

79
go.mod
View File

@ -1,76 +1,73 @@
module git.anthrove.art/Anthrove/otter-space-sdk/v5
module git.anthrove.art/Anthrove/otter-space-sdk/v2
go 1.22.0
require (
github.com/davecgh/go-spew v1.1.1
github.com/lib/pq v1.10.9
github.com/matoous/go-nanoid/v2 v2.1.0
github.com/rubenv/sql-migrate v1.7.0
github.com/sirupsen/logrus v1.9.3
github.com/testcontainers/testcontainers-go v0.33.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.33.0
go.opentelemetry.io/contrib/bridges/otellogrus v0.5.0
go.opentelemetry.io/otel v1.31.0
go.opentelemetry.io/otel/trace v1.31.0
github.com/testcontainers/testcontainers-go v0.32.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.12
gorm.io/gorm v1.25.11
)
require (
dario.cat/mergo v1.0.1 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/Microsoft/hcsshim v0.11.5 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/platforms v0.2.1 // indirect
github.com/cpuguy83/dockercfg v0.3.2 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v27.3.1+incompatible // indirect
github.com/docker/docker v27.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.6.0 // indirect
github.com/moby/sys/user v0.3.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.9.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 // indirect
go.opentelemetry.io/otel/log v0.7.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.28.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

175
go.sum
View File

@ -1,19 +1,23 @@
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/Microsoft/hcsshim v0.11.5 h1:haEcLNpj9Ka1gd3B3tAEs9CpE0c+1IhoL59w/exYU38=
github.com/Microsoft/hcsshim v0.11.5/go.mod h1:MV8xMfmECjl5HdO7U/3/hFVnkmSBjAjmA09d4bExKcU=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/containerd v1.7.18 h1:jqjZTQNfXGoEaZdW1WwPU0RqSn1Bm2Ay/KJPUuO8nao=
github.com/containerd/containerd v1.7.18/go.mod h1:IYEk9/IO6wAPUz2bCMVUbsfXjzw5UNP5fLz4PsUygQ4=
github.com/containerd/errdefs v0.1.0 h1:m0wCRBiu1WJT/Fr+iOoQHMQS/eP5myQ8lCv4Dz5ZURM=
github.com/containerd/errdefs v0.1.0/go.mod h1:YgWiiHtLmSeBrvpw+UfPijzbLaB77mEG1WwJTDETIV0=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A=
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA=
github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E=
github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -21,8 +25,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/docker v27.3.1+incompatible h1:KttF0XoteNTicmUtBO0L2tP+J7FGRFTjaEF4k6WdhfI=
github.com/docker/docker v27.3.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v27.0.3+incompatible h1:aBGI9TeQ4MPlhquTQKq9XbK79rKFVwXNUAYz9aXyEBE=
github.com/docker/docker v27.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
@ -32,17 +36,20 @@ github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSw
github.com/go-gorp/gorp/v3 v3.1.0 h1:ItKF/Vbuj31dmV4jxA1qblpSwkl9g1typ24xoe70IGs=
github.com/go-gorp/gorp/v3 v3.1.0/go.mod h1:dLEjIyyRNiXvNZ8PSmzpt1GsWAUK8kjVhEpjH8TixEw=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@ -51,28 +58,28 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rH
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 h1:7UMa6KCCMjZEMDtTVdcGu0B1GmmC7QJKiCCjyTAWQy0=
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/matoous/go-nanoid/v2 v2.1.0 h1:P64+dmq21hhWdtvZfEAofnvJULaRR1Yib0+PnU669bE=
@ -83,12 +90,10 @@ github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3N
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
github.com/moby/sys/sequential v0.6.0 h1:qrx7XFUd/5DxtqcoH1h438hF5TmOvzC/lspjy7zgvCU=
github.com/moby/sys/sequential v0.6.0/go.mod h1:uyv8EUTrca5PnDsdMGXhZe6CCe8U/UiTWd+lL+7b/Ko=
github.com/moby/sys/user v0.3.0 h1:9ni5DlcW5an3SvRSx4MouotOygvzaXbaSrc/wGDFWPo=
github.com/moby/sys/user v0.3.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs=
github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g=
github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc=
github.com/moby/sys/sequential v0.5.0/go.mod h1:tH2cOOs5V9MlPiXcQzRC+eEyab644PWKGRYaaV5ZZlo=
github.com/moby/sys/user v0.1.0 h1:WmZ93f5Ux6het5iituh9x2zAG7NFY9Aqi49jjE1PaQg=
github.com/moby/sys/user v0.1.0/go.mod h1:fKJhFOnsCN6xZ5gSfbM6zaHGgDJMrqt9/reuj4T7MmU=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
@ -101,16 +106,16 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rubenv/sql-migrate v1.7.0 h1:HtQq1xyTN2ISmQDggnh0c9U3JlP8apWh8YO2jzlXpTI=
github.com/rubenv/sql-migrate v1.7.0/go.mod h1:S4wtDEG1CKn+0ShpTtzWhFpHHI5PvCUtiGI+C+Z2THE=
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4=
github.com/shirou/gopsutil/v3 v3.23.12/go.mod h1:1FrWgea594Jp7qmjHUUPlJDTPgcsb9mGnXDxavtikzM=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
@ -118,60 +123,61 @@ github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnj
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/testcontainers/testcontainers-go v0.33.0 h1:zJS9PfXYT5O0ZFXM2xxXfk4J5UMw/kRiISng037Gxdw=
github.com/testcontainers/testcontainers-go v0.33.0/go.mod h1:W80YpTa8D5C3Yy16icheD01UTDu+LmXIA2Keo+jWtT8=
github.com/testcontainers/testcontainers-go/modules/postgres v0.33.0 h1:c+Gt+XLJjqFAejgX4hSpnHIpC9eAhvgI/TFWL/PbrFI=
github.com/testcontainers/testcontainers-go/modules/postgres v0.33.0/go.mod h1:I4DazHBoWDyf69ByOIyt3OdNjefiUx372459txOpQ3o=
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.9.0 h1:lmyCHtANi8aRUgkckBgoDk1nHCux3n2cgkJLXdQGPDo=
github.com/tklauser/numcpus v0.9.0/go.mod h1:SN6Nq1O3VychhC1npsWostA+oW+VOQTxZrS604NSRyI=
github.com/testcontainers/testcontainers-go v0.32.0 h1:ug1aK08L3gCHdhknlTTwWjPHPS+/alvLJU/DRxTD/ME=
github.com/testcontainers/testcontainers-go v0.32.0/go.mod h1:CRHrzHLQhlXUsa5gXjTOfqIEJcrK5+xMDmBr/WMI88E=
github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0 h1:ZE4dTdswj3P0j71nL+pL0m2e5HTXJwPoIFr+DDgdPaU=
github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0/go.mod h1:njrNuyuoF2fjhVk6TG/R3Oeu82YwfYkbf5WVTyBXhV4=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.opentelemetry.io/contrib/bridges/otellogrus v0.5.0 h1:T9cxTlfBz5go7dFWejO5E4JfeglWgWL1MRTPq9p8da0=
go.opentelemetry.io/contrib/bridges/otellogrus v0.5.0/go.mod h1:f6xZHPJ3A+RFc3Lfz4SoGIpfmVr4PEF4XM1H3fl+6/4=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 h1:ZIg3ZT/aQ7AfKqdwp7ECpOK6vHqquXXuyTjIO8ZdmPs=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0/go.mod h1:DQAwmETtZV00skUwgD6+0U89g80NKsJE3DCKeLLPQMI=
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 h1:Mne5On7VWdx7omSrSSZvM4Kw7cS7NQkOOmLcgscI51U=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0/go.mod h1:IPtUMKL4O3tH5y+iXVyAXqpAwMuzC1IrxVS81rummfE=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU=
go.opentelemetry.io/otel/log v0.7.0 h1:d1abJc0b1QQZADKvfe9JqqrfmPYQCz2tUSO+0XZmuV4=
go.opentelemetry.io/otel/log v0.7.0/go.mod h1:2jf2z7uVfnzDNknKTO9G+ahcOAyWcp1fJmk/wJjULRo=
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE=
go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg=
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o=
go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A=
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -179,15 +185,17 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 h1:vVKdlvoWBphwdxWKrFZEuM0kGgGLxUOYcY4U/2Vjg44=
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@ -198,12 +206,13 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 h1:RFiFrvy37/mpSpdySBDrUdipW/dHwsRwh3J3+A9VgT4=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237/go.mod h1:Z5Iiy3jtmioajWHDGFk7CeugTyHtPvMHA4UTmUkyalE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA=
google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0=
google.golang.org/genproto v0.0.0-20231012201019-e917dd12ba7a h1:fwgW9j3vHirt4ObdHoYNwuO24BEZjSzbh+zPaNWoiY8=
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb h1:lK0oleSc7IQsUxO3U5TjL9DWlsxpEBemh+zpB7IqhWI=
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b h1:ZlWIi1wSK56/8hn4QcBp/j9M7Gt3U/3hZw3mC7vDICo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b/go.mod h1:swOH3j0KzcDDgGUWr+SNpyTen5YrXjS3eyPzFYKc6lc=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@ -214,7 +223,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=

131
internal/postgres/post.go Normal file
View File

@ -0,0 +1,131 @@
package postgres
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func CreatePost(ctx context.Context, db *gorm.DB, anthrovePost *models.Post) error {
if anthrovePost == nil {
return &otterError.EntityValidationFailed{Reason: "anthrovePost is nil"}
}
result := db.WithContext(ctx).Create(&anthrovePost)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"anthrove_post_id": anthrovePost.ID,
"anthrove_post_rating": anthrovePost.Rating,
}).Trace("database: created anthrove post")
return nil
}
func CreatePostInBatch(ctx context.Context, db *gorm.DB, anthrovePost []models.Post, batchSize int) error {
if anthrovePost == nil {
return &otterError.EntityValidationFailed{Reason: "anthrovePost cannot be nil"}
}
if len(anthrovePost) == 0 {
return &otterError.EntityValidationFailed{Reason: "anthrovePost cannot be empty"}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"}
}
result := db.WithContext(ctx).CreateInBatches(anthrovePost, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"tag_size": len(anthrovePost),
"batch_size": batchSize,
}).Trace("database: created tag node")
return nil
}
func GetPostByAnthroveID(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID) (*models.Post, error) {
if anthrovePostID == "" {
return nil, &otterError.EntityValidationFailed{Reason: "anthrovePostID is not set"}
}
if len(anthrovePostID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
}
var post models.Post
result := db.WithContext(ctx).First(&post, "id = ?", anthrovePostID)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
return &post, nil
}
func GetPostBySourceURL(ctx context.Context, db *gorm.DB, sourceURL string) (*models.Post, error) {
if sourceURL == "" {
return nil, &otterError.EntityValidationFailed{Reason: "sourceURL is not set"}
}
var post models.Post
result := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.url = $1 LIMIT 1`, sourceURL).First(&post)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
return &post, nil
}
func GetPostBySourceID(ctx context.Context, db *gorm.DB, sourceID models.AnthroveSourceID) (*models.Post, error) {
if sourceID == "" {
return nil, &otterError.EntityValidationFailed{Reason: "sourceID is not set"}
}
var post models.Post
result := db.WithContext(ctx).Raw(`SELECT p.id AS id, p.rating as rating FROM "Post" AS p INNER JOIN "PostReference" AS pr ON p.id = pr.post_id AND pr.source_id = $1 LIMIT 1`, sourceID).First(&post)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
return &post, nil
}

View File

@ -0,0 +1,469 @@
package postgres
import (
"context"
"fmt"
"testing"
_ "github.com/lib/pq"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
"gorm.io/gorm"
)
func TestCreateAnthrovePostNode(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Tests
validPost := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
},
Rating: "safe",
}
invalidPost := &models.Post{
Rating: "error",
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthrovePost *models.Post
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid AnthrovePostID and Rating",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePost: validPost,
},
wantErr: false,
},
{
name: "Test 2: Invalid Rating",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePost: invalidPost,
},
wantErr: true,
},
{
name: "Test 3: Nill",
args: args{
ctx: context.Background(),
db: gormDB,
anthrovePost: nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreatePost(tt.args.ctx, tt.args.db, tt.args.anthrovePost); (err != nil) != tt.wantErr {
t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetPostByAnthroveID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Tests
post := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal("Could not create post", err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthrovePostID models.AnthrovePostID
}
tests := []struct {
name string
args args
want *models.Post
wantErr bool
}{
{
name: "Test 1: Valid anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: post.ID,
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "1234",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: No anthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthrovePostID: "",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostByAnthroveID(tt.args.ctx, tt.args.db, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostByAnthroveID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPost(got, tt.want) {
t.Errorf("GetPostByAnthroveID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetPostBySourceURL(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Tests
post := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal("Could not create post", err)
}
source := models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "https://e621.net/icon.ico",
}
err = CreateSource(ctx, gormDB, &source)
if err != nil {
t.Fatal("Could not create source", err)
}
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.org", models.PostReferenceConfig{})
if err != nil {
t.Fatal("Could not create source reference", err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
sourceURL string
}
tests := []struct {
name string
args args
want *models.Post
wantErr bool
}{
{
name: "Test 1: Valid sourceURL",
args: args{
ctx: ctx,
db: gormDB,
sourceURL: "http://test.org",
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid sourceURL",
args: args{
ctx: ctx,
db: gormDB,
sourceURL: "1234",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: No sourceURL",
args: args{
ctx: ctx,
db: gormDB,
sourceURL: "",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostBySourceURL(tt.args.ctx, tt.args.db, tt.args.sourceURL)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostBySourceURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPost(got, tt.want) {
t.Errorf("GetPostBySourceURL() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetPostBySourceID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Tests
post := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal("Could not create post", err)
}
source := models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{
ID: models.AnthroveSourceID(fmt.Sprintf("%025s", "1")),
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "https://e621.net/icon.ico",
}
err = CreateSource(ctx, gormDB, &source)
if err != nil {
t.Fatal("Could not create source", err)
}
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.otg", models.PostReferenceConfig{})
if err != nil {
t.Fatal("Could not create source reference", err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
sourceID models.AnthroveSourceID
}
tests := []struct {
name string
args args
want *models.Post
wantErr bool
}{
{
name: "Test 1: Valid sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: source.ID,
},
want: post,
wantErr: false,
},
{
name: "Test 2: Invalid sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: "1234",
},
want: nil,
wantErr: true,
},
{
name: "Test 3: No sourceID",
args: args{
ctx: ctx,
db: gormDB,
sourceID: "",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostBySourceID(tt.args.ctx, tt.args.db, tt.args.sourceID)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostBySourceID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPost(got, tt.want) {
t.Errorf("GetPostBySourceID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreatePostInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Tests
validPosts := []models.Post{
{
Rating: models.SFW,
},
{
Rating: models.NSFW,
},
{
Rating: models.Questionable,
},
}
emptyPost := []models.Post{}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthrovePost []models.Post
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid Data",
args: args{
ctx: ctx,
db: gormDB,
anthrovePost: validPosts,
batchSize: len(validPosts),
},
wantErr: false,
},
{
name: "Test 2: Emtpy Data",
args: args{
ctx: ctx,
db: gormDB,
anthrovePost: emptyPost,
batchSize: 0,
},
wantErr: true,
},
{
name: "Test 3: Nil Data",
args: args{
ctx: ctx,
db: gormDB,
anthrovePost: nil,
batchSize: 0,
},
wantErr: true,
},
{
name: "Test 4: batchSize 0",
args: args{
ctx: ctx,
db: gormDB,
anthrovePost: validPosts,
batchSize: 0,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreatePostInBatch(tt.args.ctx, tt.args.db, tt.args.anthrovePost, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreatePostInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkPost(got *models.Post, want *models.Post) bool {
if got == nil && want == nil {
return true
} else if got == nil || want == nil {
return false
}
if got.ID != want.ID {
return false
}
if got.Rating != want.Rating {
return false
}
return true
}

View File

@ -0,0 +1,126 @@
package postgres
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func CreateReferenceBetweenPostAndSource(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error {
if anthrovePostID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthrovePostID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
if sourceDomain == "" {
return &otterError.EntityValidationFailed{Reason: "sourceDomain cannot be empty"}
}
result := db.WithContext(ctx).Exec(`INSERT INTO "PostReference" (post_id, source_id, url, full_file_url, preview_file_url, sample_file_url, source_post_id) SELECT $1, source.id, $2, $4, $5, $6, $7 FROM "Source" AS source WHERE domain = $3;`, anthrovePostID, postURL, sourceDomain, config.FullFileURL, config.PreviewFileURL, config.SampleFileURL, config.SourcePostID)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
}
if errors.Is(result.Error, gorm.ErrCheckConstraintViolated) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"anthrove_post_id": anthrovePostID,
"anthrove_source_domain": sourceDomain,
}).Trace("database: created anthrove post to source link")
return nil
}
func CreateReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
if anthrovePostID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthrovePostID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
if anthroveUserID == "" {
return &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
}
userFavorite := models.UserFavorites{
UserID: string(anthroveUserID),
PostID: string(anthrovePostID),
}
result := db.WithContext(ctx).Create(&userFavorite)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"anthrove_post_id": anthrovePostID,
}).Trace("database: created user to post link")
return nil
}
func CheckReferenceBetweenUserAndPost(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
var count int64
if anthrovePostID == "" {
return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthrovePostID) != 25 {
return false, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
if anthroveUserID == "" {
return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
}
if len(anthroveUserID) != 25 {
return false, &otterError.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
}
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ? AND post_id = ?", string(anthroveUserID), string(anthrovePostID)).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return false, &otterError.NoDataFound{}
}
return false, result.Error
}
exists := count > 0
log.WithFields(log.Fields{
"relationship_exists": exists,
"relationship_anthrove_user_id": anthroveUserID,
"relationship_anthrove_post_id": anthrovePostID,
}).Trace("database: checked user post relationship")
return exists, nil
}

View File

@ -0,0 +1,392 @@
package postgres
import (
"context"
"fmt"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
"gorm.io/gorm"
)
func TestCheckUserToPostLink(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Test
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX")
validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
err = CreateUser(ctx, gormDB, validUserID)
if err != nil {
t.Fatal(err)
}
post := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: validPostID,
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal(err)
}
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthroveUserID models.AnthroveUserID
anthrovePostID models.AnthrovePostID
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: post.ID,
},
want: true,
wantErr: false,
},
{
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: "qadw",
},
want: false,
wantErr: true,
},
{
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: post.ID,
},
want: false,
wantErr: true,
},
{
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "123456",
},
want: false,
wantErr: true,
},
{
name: "Test 5: No AnthrovePostID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthrovePostID: "123456",
},
want: false,
wantErr: true,
},
{
name: "Test 6: No anthrovePostID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "",
},
want: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCheckUserToPostLinkWithNoData(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Test
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX")
validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
err = CreateUser(ctx, gormDB, validUserID)
if err != nil {
t.Fatal(err)
}
post := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: validPostID,
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal(err)
}
err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthroveUserID models.AnthroveUserID
anthrovePostID models.AnthrovePostID
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: post.ID,
},
want: true,
wantErr: false,
},
{
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: "qadw",
},
want: false,
wantErr: true,
},
{
name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: post.ID,
},
want: false,
wantErr: true,
},
{
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "123456",
},
want: false,
wantErr: true,
},
{
name: "Test 5: No AnthrovePostID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthrovePostID: "123456",
},
want: false,
wantErr: true,
},
{
name: "Test 6: No anthrovePostID given",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "",
},
want: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID)
if (err != nil) != tt.wantErr {
t.Errorf("CheckIfUserHasPostAsFavorite() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckIfUserHasPostAsFavorite() got = %v, want %v", got, tt.want)
}
})
}
}
func TestEstablishUserToPostLink(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Test
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
invalidUserID := models.AnthroveUserID("XXX")
validPostID := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1"))
err = CreateUser(ctx, gormDB, validUserID)
if err != nil {
t.Fatal(err)
}
post := &models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: validPostID,
},
Rating: "safe",
}
err = CreatePost(ctx, gormDB, post)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthroveUserID models.AnthroveUserID
anthrovePostID models.AnthrovePostID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid AnthroveUserID and AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: post.ID,
},
wantErr: false,
},
{
name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: validUserID,
anthrovePostID: "123456",
},
wantErr: true,
},
{
name: "Test 3: invalid AnthroveUserID and valid AnthrovePostID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: post.ID,
},
wantErr: true,
},
{
name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "123456",
},
wantErr: true,
},
{
name: "Test 5: AnthrovePostID is empty",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: invalidUserID,
anthrovePostID: "",
},
wantErr: true,
},
{
name: "Test 6: anthroveUserID is empty",
args: args{
ctx: ctx,
db: gormDB,
anthroveUserID: "",
anthrovePostID: validPostID,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateReferenceBetweenUserAndPost(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr {
t.Errorf("CreateReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -0,0 +1,85 @@
package postgres
import (
"context"
"errors"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
// CreateSource creates a pgModels.Source
func CreateSource(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error {
if anthroveSource.Domain == "" {
return &otterError.EntityValidationFailed{Reason: "Domain is required"}
}
result := db.WithContext(ctx).Where(models.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"node_source_url": anthroveSource.Domain,
"node_source_displayName": anthroveSource.DisplayName,
"node_source_icon": anthroveSource.Icon,
}).Trace("database: created source node")
return nil
}
// GetAllSource returns a list of all pgModels.Source
func GetAllSource(ctx context.Context, db *gorm.DB) ([]models.Source, error) {
var sources []models.Source
result := db.WithContext(ctx).Find(&sources)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_amount": result.RowsAffected,
}).Trace("database: get all source nodes")
return sources, nil
}
// GetSourceByDomain returns the first source it finds based on the domain
func GetSourceByDomain(ctx context.Context, db *gorm.DB, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) {
var sources models.Source
if sourceDomain == "" {
return nil, &otterError.EntityValidationFailed{Reason: "AnthroveSourceDomain is not set"}
}
result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&sources)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_amount": result.RowsAffected,
}).Trace("database: get all source nodes")
return &sources, nil
}

View File

@ -0,0 +1,270 @@
package postgres
import (
"context"
"fmt"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
"gorm.io/gorm"
)
func TestCreateSourceNode(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Test
validPostID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Post1"))
validSource := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
DisplayName: "e621",
Domain: "e621.net",
Icon: "icon.e621.net",
}
invalidSource := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "notfound.intern",
}
invalidSourceDomain := &models.Source{
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validPostID},
Domain: "",
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
anthroveSource *models.Source
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 1: Valid anthroveSource",
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: validSource,
},
wantErr: false,
},
{
name: "Test 2: inValid anthroveSource",
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: invalidSourceDomain,
},
wantErr: true,
},
{
name: "Test 3: unique anthroveSource",
args: args{
ctx: ctx,
db: gormDB,
anthroveSource: invalidSource,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateSource(tt.args.ctx, tt.args.db, tt.args.anthroveSource); (err != nil) != tt.wantErr {
t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetAllSourceNodes(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Test
sources := []models.Source{
{
DisplayName: "e621",
Domain: "e621.net",
Icon: "icon.e621.net",
},
{
DisplayName: "furaffinity",
Domain: "furaffinity.net",
Icon: "icon.furaffinity.net",
},
{
DisplayName: "fenpaws",
Domain: "fenpa.ws",
Icon: "icon.fenpa.ws",
},
}
for _, source := range sources {
err = CreateSource(ctx, gormDB, &source)
if err != nil {
t.Fatal(err)
}
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
}
tests := []struct {
name string
args args
want []models.Source
wantErr bool
}{
{
name: "Test 1: Get all entries",
args: args{
ctx: ctx,
db: gormDB,
},
want: sources,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllSource(tt.args.ctx, tt.args.db)
if (err != nil) != tt.wantErr {
t.Errorf("GetAllSource() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkSourcesNode(got, tt.want) {
t.Errorf("GetAllSource() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetSourceNodesByURL(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// Setup Test
source := &models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "icon.e621.net",
}
err = CreateSource(ctx, gormDB, source)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
db *gorm.DB
domain models.AnthroveSourceDomain
}
tests := []struct {
name string
args args
want *models.Source
wantErr bool
}{
{
name: "Test 1: Valid URL",
args: args{
ctx: ctx,
db: gormDB,
domain: "e621.net",
},
want: source,
wantErr: false,
},
{
name: "Test 2: Invalid URL",
args: args{
ctx: ctx,
db: gormDB,
domain: "eeeee.net",
},
want: nil,
wantErr: true,
},
{
name: "Test 2: No URL",
args: args{
ctx: ctx,
db: gormDB,
domain: "",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetSourceByDomain(tt.args.ctx, tt.args.db, tt.args.domain)
if (err != nil) != tt.wantErr {
t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkSourceNode(got, tt.want) {
t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want)
}
})
}
}
func checkSourcesNode(got []models.Source, want []models.Source) bool {
for i, source := range want {
if source.DisplayName != got[i].DisplayName {
return false
}
if source.Domain != got[i].Domain {
return false
}
if source.Icon != got[i].Icon {
return false
}
}
return true
}
func checkSourceNode(got *models.Source, want *models.Source) bool {
if want == nil && got == nil {
return true
}
if got.Domain != want.Domain {
return false
}
return true
}

440
internal/postgres/tag.go Normal file
View File

@ -0,0 +1,440 @@
package postgres
import (
"context"
"errors"
"gorm.io/gorm/clause"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
func CreateTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName, tagType models.TagType) error {
if tagName == "" {
return &otterError.EntityValidationFailed{Reason: "tagName cannot be empty"}
}
if tagType == "" {
return &otterError.EntityValidationFailed{Reason: "tagType cannot be empty"}
}
result := db.WithContext(ctx).Create(&models.Tag{Name: string(tagName), Type: tagType})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"tag_name": tagName,
"tag_type": tagType,
}).Trace("database: created tag node")
return nil
}
func CreateTagInBatchAndUpdate(ctx context.Context, db *gorm.DB, tags []models.Tag, batchSize int) error {
if len(tags) == 0 {
return &otterError.EntityValidationFailed{Reason: "tags cannot be empty"}
}
if tags == nil {
return &otterError.EntityValidationFailed{Reason: "tags cannot be nil"}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"}
}
result := db.WithContext(ctx).
Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}},
DoUpdates: clause.AssignmentColumns([]string{"tag_type"}),
}).CreateInBatches(tags, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"tag_size": len(tags),
"batch_size": batchSize,
}).Trace("database: created tag node")
return nil
}
func DeleteTag(ctx context.Context, db *gorm.DB, tagName models.AnthroveTagName) error {
if tagName == "" {
return &otterError.EntityValidationFailed{Reason: "tagName cannot be empty"}
}
result := db.WithContext(ctx).Delete(&models.Tag{Name: string(tagName)})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
}
return result.Error
}
log.WithFields(log.Fields{
"tag_name": tagName,
}).Trace("database: deleted tag")
return nil
}
func GetAllTagByTagsType(ctx context.Context, db *gorm.DB, tagType models.TagType) ([]models.Tag, error) {
var tags []models.Tag
if tagType == "" {
return nil, &otterError.EntityValidationFailed{Reason: "tagType cannot be empty"}
}
result := db.WithContext(ctx).Model(&models.Tag{}).Where("tag_type = ?", tagType).Scan(&tags)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tags_length": len(tags),
}).Trace("database: got tag")
return tags, nil
}
func CreateTagAndReferenceToPost(ctx context.Context, db *gorm.DB, anthrovePostID models.AnthrovePostID, tag *models.Tag) error {
if anthrovePostID == "" {
return &otterError.EntityValidationFailed{Reason: "anthrovePostID cannot be empty"}
}
if len(anthrovePostID) != 25 {
return &otterError.EntityValidationFailed{Reason: "anthrovePostID needs to be 25 characters long"}
}
if tag == nil {
return &otterError.EntityValidationFailed{Reason: "Tag is nil"}
}
pgPost := models.Post{
BaseModel: models.BaseModel[models.AnthrovePostID]{
ID: anthrovePostID,
},
}
err := db.WithContext(ctx).Model(&pgPost).Association("Tags").Append(tag)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
}
return errors.Join(err, &otterError.NoRelationCreated{})
}
log.WithFields(log.Fields{
"anthrove_post_id": anthrovePostID,
"tag_name": tag.Name,
"tag_type": tag.Type,
}).Trace("database: created tag node")
return nil
}
func GetTags(ctx context.Context, db *gorm.DB) ([]models.Tag, error) {
var tags []models.Tag
result := db.WithContext(ctx).Find(&tags)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_amount": len(tags),
}).Trace("database: got tags")
return tags, nil
}
func CreateTagAlias(ctx context.Context, db *gorm.DB, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error {
if tagAliasName == "" {
return &otterError.EntityValidationFailed{Reason: "tagAliasName cannot be empty"}
}
if tagID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveTagIDEmpty}
}
result := db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}},
DoNothing: true,
}).Create(&models.TagAlias{
Name: string(tagAliasName),
TagID: string(tagID),
})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
log.WithFields(log.Fields{
"tag_alias_name": tagAliasName,
"tag_alias_tag_id": tagID,
}).Trace("database: created tagAlias")
return nil
}
func CreateTagAliasInBatch(ctx context.Context, db *gorm.DB, tagAliases []models.TagAlias, batchSize int) error {
if len(tagAliases) == 0 {
return &otterError.EntityValidationFailed{Reason: "tagAliases cannot be empty"}
}
if tagAliases == nil {
return &otterError.EntityValidationFailed{Reason: "tagAliases cannot be nil"}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"}
}
result := db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}},
DoNothing: true,
}).CreateInBatches(tagAliases, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"tag_size": len(tagAliases),
"batch_size": batchSize,
}).Trace("database: created tag node")
return nil
}
func GetAllTagAlias(ctx context.Context, db *gorm.DB) ([]models.TagAlias, error) {
var tagAliases []models.TagAlias
result := db.WithContext(ctx).Find(&tagAliases)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_alias_length": len(tagAliases),
}).Trace("database: created tagAlias")
return tagAliases, nil
}
func GetAllTagAliasByTag(ctx context.Context, db *gorm.DB, tagID models.AnthroveTagID) ([]models.TagAlias, error) {
var tagAliases []models.TagAlias
if tagID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveTagIDEmpty}
}
result := db.WithContext(ctx).Where("tag_id = ?", tagID).Find(&tagAliases)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_alias_length": len(tagAliases),
"tag_alias_tag_id": tagID,
}).Trace("database: get specific tagAlias")
return tagAliases, nil
}
func DeleteTagAlias(ctx context.Context, db *gorm.DB, tagAliasName models.AnthroveTagAliasName) error {
if tagAliasName == "" {
return &otterError.EntityValidationFailed{Reason: "tagAliasName cannot be empty"}
}
result := db.WithContext(ctx).Delete(&models.TagAlias{Name: string(tagAliasName)})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
}
return result.Error
}
log.WithFields(log.Fields{
"tag_alias_name": tagAliasName,
}).Trace("database: deleted tagAlias")
return nil
}
func CreateTagGroup(ctx context.Context, db *gorm.DB, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) error {
if tagGroupName == "" {
return &otterError.EntityValidationFailed{Reason: "tagGroupName cannot be empty"}
}
if tagID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveTagIDEmpty}
}
result := db.WithContext(ctx).Create(&models.TagGroup{
Name: string(tagGroupName),
TagID: string(tagID),
})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
log.WithFields(log.Fields{
"tag_group_name": tagGroupName,
"tag_group_tag_id": tagID,
}).Trace("database: created tagGroup")
return nil
}
func CreateTagGroupInBatch(ctx context.Context, db *gorm.DB, tagGroups []models.TagGroup, batchSize int) error {
if len(tagGroups) == 0 {
return &otterError.EntityValidationFailed{Reason: "tagAliases cannot be empty"}
}
if tagGroups == nil {
return &otterError.EntityValidationFailed{Reason: "tagAliases cannot be nil"}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: "batch size cannot be zero"}
}
result := db.WithContext(ctx).CreateInBatches(tagGroups, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"tag_size": len(tagGroups),
"batch_size": batchSize,
}).Trace("database: created tag node")
return nil
}
func GetAllTagGroup(ctx context.Context, db *gorm.DB) ([]models.TagGroup, error) {
var tagGroups []models.TagGroup
result := db.WithContext(ctx).Find(&tagGroups)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_alias_length": len(tagGroups),
}).Trace("database: created tagGroup")
return tagGroups, nil
}
func GetAllTagGroupByTag(ctx context.Context, db *gorm.DB, tagID models.AnthroveTagID) ([]models.TagGroup, error) {
var tagGroups []models.TagGroup
if tagID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveTagIDEmpty}
}
result := db.WithContext(ctx).Where("tag_id = ?", tagID).Find(&tagGroups)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_alias_length": len(tagGroups),
"tag_alias_tag_id": tagID,
}).Trace("database: get specific tagGroup")
return tagGroups, nil
}
func DeleteTagGroup(ctx context.Context, db *gorm.DB, tagGroupName models.AnthroveTagGroupName) error {
if tagGroupName == "" {
return &otterError.EntityValidationFailed{Reason: "tagGroupName cannot be empty"}
}
result := db.WithContext(ctx).Delete(&models.TagGroup{Name: string(tagGroupName)})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.NoDataFound{}
}
return result.Error
}
log.WithFields(log.Fields{
"tag_alias_name": tagGroupName,
}).Trace("database: deleted tagAlias")
return nil
}

File diff suppressed because it is too large Load Diff

398
internal/postgres/user.go Normal file
View File

@ -0,0 +1,398 @@
package postgres
import (
"context"
"errors"
"time"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
gonanoid "github.com/matoous/go-nanoid/v2"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
// Workaround, should be changed later maybe, but its not that bad right now
type selectFrequencyTag struct {
tagName string `gorm:"tag_name"`
count int64 `gorm:"count"`
tagType models.TagType `gorm:"tag_type"`
}
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error {
if anthroveUserID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
user := models.User{
BaseModel: models.BaseModel[models.AnthroveUserID]{
ID: anthroveUserID,
},
}
result := db.WithContext(ctx).FirstOrCreate(&user)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
return nil
}
func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error {
if anthroveUserID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
if accountId == "" {
return &otterError.EntityValidationFailed{Reason: "accountID cannot be empty"}
}
if accountUsername == "" {
return &otterError.EntityValidationFailed{Reason: "accountUsername cannot be empty"}
}
validationCode, err := gonanoid.New(25)
if err != nil {
return err
}
result := db.WithContext(ctx).Exec(`WITH userObj AS (
INSERT INTO "User" (id)
VALUES ($1)
ON CONFLICT (id) DO NOTHING
)
INSERT INTO "UserSource" (user_id, source_id, account_username, account_id, account_validate, account_validation_key)
SELECT $2, source.id, $3, $4, false, $5
FROM "Source" AS source
WHERE source.id = $6;`, anthroveUserID, anthroveUserID, accountUsername, accountId, validationCode, sourceID)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.EntityAlreadyExists{}
}
return result.Error
}
if result.RowsAffected == 0 {
return &otterError.NoDataWritten{}
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"source_id": sourceID,
"account_username": accountUsername,
"account_id": accountId,
}).Info("database: created user-source relationship")
return nil
}
func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (int64, error) {
var count int64
if anthroveUserID == "" {
return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return 0, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return 0, &otterError.NoDataFound{}
}
return 0, result.Error
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"anthrove_user_fav_count": count,
}).Trace("database: got user favorite count")
return count, nil
}
func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
var userSources []models.UserSource
userSourceMap := make(map[string]models.UserSource)
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
result := db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
for _, userSource := range userSources {
var source models.Source
result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
userSourceMap[source.DisplayName] = models.UserSource{
UserID: userSource.AccountID,
AccountUsername: userSource.AccountUsername,
Source: models.Source{
DisplayName: source.DisplayName,
Domain: source.Domain,
Icon: source.Icon,
},
}
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
}).Trace("database: got user source link")
return userSourceMap, nil
}
func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) {
var userSource models.UserSource
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
if sourceID == "" {
return nil, &otterError.EntityValidationFailed{Reason: "sourceID cannot be empty"}
}
if len(sourceID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"}
}
result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"source_id": sourceID,
}).Trace("database: got specified user source link")
return &userSource, nil
}
func GetAllUsers(ctx context.Context, db *gorm.DB) ([]models.User, error) {
var users []models.User
result := db.WithContext(ctx).Model(&models.User{}).Find(&users)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, result.Error
}
log.WithFields(log.Fields{
"anthrove_user_id_count": len(users),
}).Trace("database: got all anthrove user IDs")
return users, nil
}
// TODO: FIX THE TEST
func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
var favoritePosts []models.Post
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
db.WithContext(ctx).Joins("RIGHT JOIN \"UserFavorites\" AS of ON \"Post\".id = of.post_id AND of.user_id = ?", anthroveUserID).Preload("References").Offset(skip).Limit(limit).Find(&favoritePosts)
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"anthrove_user_fav_count": len(favoritePosts),
}).Trace("database: got all anthrove user favorites")
return &models.FavoriteList{Posts: favoritePosts}, nil
}
func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
var queryUserFavorites []selectFrequencyTag
if anthroveUserID == "" {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
rows, err := db.WithContext(ctx).Raw(
`WITH user_posts AS (
SELECT post_id FROM "UserFavorites" WHERE user_id = $1
)
SELECT post_tags.tag_name AS tag_name, count(*) AS count, (SELECT tag_type FROM "Tag" WHERE "Tag".name = post_tags.tag_name LIMIT 1) AS tag_type FROM post_tags, user_posts WHERE post_tags.post_id IN (user_posts.post_id) GROUP BY post_tags.tag_name ORDER BY tag_type DESC, tag_name DESC`, anthroveUserID).Rows()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, &otterError.NoDataFound{}
}
return nil, err
}
var userFavoritesFrequency = make([]models.TagsWithFrequency, 0)
defer rows.Close()
for rows.Next() {
var tagName string
var count int64
var tagType string
rows.Scan(&tagName, &count, &tagType)
userFavoritesFrequency = append(userFavoritesFrequency, models.TagsWithFrequency{
Frequency: count,
Tags: models.Tag{
Name: tagName,
Type: models.TagType(tagType),
},
})
}
log.WithFields(log.Fields{
"anthrove_user_id": anthroveUserID,
"tag_amount": len(queryUserFavorites),
}).Trace("database: got user tag node with relation to faved posts")
return userFavoritesFrequency, nil
}
func UpdateUserSourceScrapeTimeInterval(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error {
if anthroveUserID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
if sourceID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDEmpty}
}
if len(sourceID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDToShort}
}
if scrapeTime == 0 {
return &otterError.EntityValidationFailed{Reason: "ScrapeTimeInterval cannot be empty"}
}
userSource := &models.UserSource{
UserID: string(anthroveUserID),
}
result := db.WithContext(ctx).Model(&userSource).Update("scrape_time_interval", scrapeTime)
if result.Error != nil {
return result.Error
}
return nil
}
func UpdateUserSourceLastScrapeTime(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error {
if anthroveUserID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
if sourceID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDEmpty}
}
if len(sourceID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDToShort}
}
if time.Time.IsZero(time.Time(lastScrapeTime)) {
return &otterError.EntityValidationFailed{Reason: "LastScrapeTime cannot be empty"}
}
userSource := &models.UserSource{
UserID: string(anthroveUserID),
}
result := db.WithContext(ctx).Model(&userSource).Update("last_scrape_time", time.Time(lastScrapeTime))
if result.Error != nil {
return result.Error
}
return nil
}
func UpdateUserSourceValidation(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error {
if anthroveUserID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
}
if len(anthroveUserID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
}
if sourceID == "" {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDEmpty}
}
if len(sourceID) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.AnthroveSourceIDToShort}
}
userSource := &models.UserSource{
UserID: string(anthroveUserID),
}
result := db.WithContext(ctx).Model(&userSource).Update("account_validate", valid)
if result.Error != nil {
return result.Error
}
return nil
}

File diff suppressed because it is too large Load Diff

View File

@ -1,27 +0,0 @@
package utils
import (
"context"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// HandleError logs the provided error, records it in the given trace span,
// sets the span status to error, and returns the error.
//
// Parameters:
// - ctx: context.Context, the context in which the error occurred.
// - span: trace.Span, the trace span where the error will be recorded.
// - logger: *log.Entry, a log entry used to log the error message.
// - error: error, the error to be handled.
//
// Returns:
// - error: The same error that was passed in.
func HandleError(_ context.Context, span trace.Span, logger *log.Entry, error error) error {
logger.Error(error)
span.RecordError(error)
span.SetStatus(codes.Error, error.Error())
return error
}

11
internal/utils/slices.go Normal file
View File

@ -0,0 +1,11 @@
package utils
func GetOrDefault(data map[string]any, key string, defaultVal any) any {
val, ok := data[key]
if !ok {
return defaultVal
}
return val
}

View File

@ -0,0 +1,60 @@
package utils
import (
"reflect"
"testing"
)
func TestGetOrDefault(t *testing.T) {
type args struct {
data map[string]any
key string
defaultVal any
}
tests := []struct {
name string
args args
want any
}{
{
name: "Test 1: Nil map",
args: args{
data: nil,
key: "key1",
defaultVal: "default",
},
want: "default",
},
{
name: "Test 2: Existing key",
args: args{
data: map[string]interface{}{
"key1": "value1",
"key2": "value2",
},
key: "key1",
defaultVal: "default",
},
want: "value1",
},
{
name: "Test 3: Non-existing key",
args: args{
data: map[string]interface{}{
"key1": "value1",
"key2": "value2",
},
key: "key3",
defaultVal: "default",
},
want: "default",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetOrDefault(tt.args.data, tt.args.key, tt.args.defaultVal); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetOrDefault() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,22 +0,0 @@
package utils
import (
"context"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/trace"
)
// SetupTracing initializes a new trace span and logger for the given context and tracer.
func SetupTracing(ctx context.Context, tracer trace.Tracer, tracerName string) (context.Context, trace.Span, *log.Entry) {
ctx, span := tracer.Start(ctx, tracerName)
localLogger := log.WithContext(ctx)
return ctx, span, localLogger
}
// HandleEvent logs the provided event name and adds it to the given trace span.
func HandleEvent(span trace.Span, logger *log.Entry, eventName string) {
logger.Debug(eventName)
span.AddEvent(eventName)
}

View File

@ -1,136 +0,0 @@
package database
import (
"context"
"embed"
"fmt"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/database"
var (
//go:embed migrations/*.sql
embedMigrations embed.FS
client *gorm.DB
tracer = otel.Tracer(tracingName)
logger = log.New()
)
// Connect to the Database
func Connect(ctx context.Context, config models.DatabaseConfig) error {
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
// Debug enabled?
if config.Debug {
log.SetLevel(log.DebugLevel)
}
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "Connect")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"endpoint": config.Endpoint,
"port": config.Port,
"database": config.Database,
})
span.SetAttributes(
attribute.String("endpoint", config.Endpoint),
attribute.Int("port", config.Port),
attribute.String("database", config.Database),
)
utils.HandleEvent(span, localLogger, "Starting database connection")
var localSSL string
if config.SSL {
localSSL = "require"
} else {
localSSL = "disable"
}
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone)
sqlDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
TranslateError: true,
})
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
if config.Migrate {
err = migrateDatabase(ctx, sqlDB, config)
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
}
client = sqlDB
utils.HandleEvent(span, localLogger, "Database connected successfully")
return nil
}
// migrateDatabase handels the migration of ann SQL files in the migrations subfolder
func migrateDatabase(ctx context.Context, dbPool *gorm.DB, config models.DatabaseConfig) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "migrateDatabase")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"database": config.Database,
})
span.SetAttributes(
attribute.String("database", config.Database),
)
utils.HandleEvent(span, localLogger, "Starting database migration")
dialect := "postgres"
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
db, err := dbPool.DB()
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
n, err := migrate.Exec(db, dialect, migrations, migrate.Up)
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
if n != 0 {
localLogger.Debugf("applied %d migrations!", n)
} else {
localLogger.Debug("nothing to migrate")
}
utils.HandleEvent(span, localLogger, "Database migration completed successfully")
return nil
}
// GetGorm returns a ready to use gorm.DB client
func GetGorm(ctx context.Context) (*gorm.DB, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetGorm")
defer span.End()
utils.HandleEvent(span, localLogger, "Retrieving GORM client")
if client == nil {
return nil, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
utils.HandleEvent(span, localLogger, "GORM client retrieved successfully")
return client, nil
}

View File

@ -1,174 +0,0 @@
package database
import (
"context"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestConnect(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, _, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Database config to test with
validDatabaseConfig, err := test.DatabaseModesFromConnectionString(ctx, container)
if err != nil {
t.Fatalf("Could not get valid database config: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
config models.DatabaseConfig
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid DatabaseConfig",
args: args{
ctx: ctx,
config: validDatabaseConfig,
},
wantErr: false,
},
{
name: "Test 02: invalid DatabaseConfig",
args: args{
ctx: ctx,
config: models.DatabaseConfig{},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Connect(tt.args.ctx, tt.args.config); (err != nil) != tt.wantErr {
t.Errorf("Connect() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetGorm(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, _, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Database config to test with
validDatabaseConfig, err := test.DatabaseModesFromConnectionString(ctx, container)
if err != nil {
t.Fatalf("Could not get valid database config: %v", err)
}
err = Connect(ctx, validDatabaseConfig)
if err != nil {
t.Fatalf("Could not connect to valid database: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: GetGorm",
args: args{
ctx: ctx,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := GetGorm(tt.args.ctx)
if (err == nil) != !tt.wantErr {
t.Errorf("GetGorm() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_migrateDatabase(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Database config to test with
validDatabaseConfig, err := test.DatabaseModesFromConnectionString(ctx, container)
if err != nil {
t.Fatalf("Could not get valid database config: %v", err)
}
validDatabaseConfig.Debug = true
// --
// -- -- Tests
type args struct {
ctx context.Context
dbPool *gorm.DB
config models.DatabaseConfig
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: MigrateDatabase",
args: args{
ctx: ctx,
dbPool: gormDB,
config: validDatabaseConfig,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := migrateDatabase(tt.args.ctx, tt.args.dbPool, tt.args.config); (err != nil) != tt.wantErr {
t.Errorf("migrateDatabase() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

30
pkg/database/database.go Normal file
View File

@ -0,0 +1,30 @@
package database
import (
"context"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
)
type OtterSpace interface {
// Connect establishes a connection to the database.
Connect(ctx context.Context, config models.DatabaseConfig) error
// Post contains all function that are needed to manage Posts
Post
// User contains all function that are needed to manage the AnthroveUser
User
// Source contains all function that are needed to manage the Source
Source
// Tag contains all functions that are used to manage Tag
Tag
// TagAlias contains all function that are needed to manage the TagAlias
TagAlias
// TagGroup contains all function that are needed to manage the TagGroup
TagGroup
}

View File

@ -1,199 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (models.UserFavorite, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavorite")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_id": userFav.ID,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(userFav.ID)),
)
utils.HandleEvent(span, localLogger, "Starting user favorite creation")
if client == nil {
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&userFav)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User favorite created successfully")
return userFav, nil
}
func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorite, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavoriteInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_count": len(userFav),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("user_favorite_count", len(userFav)),
)
utils.HandleEvent(span, localLogger, "Starting batch user favorite creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if userFav == nil || len(userFav) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&userFav, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Batch user favorites created successfully")
return nil
}
// UpdateUserFavorite updates the user post information in the database.
// Only supports the undulation of userFavorites, for this set the DeletedAt.Valid to false
func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_id": userFav.ID,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(userFav.ID)),
)
utils.HandleEvent(span, localLogger, "Starting user favorite update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(userFav.ID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty})
}
if userFav.DeletedAt.Valid == true {
return nil
}
result := client.WithContext(ctx).Unscoped().Model(&models.UserFavorite{}).Where("id", userFav.ID).Update("deleted_at", nil)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User favorite updated successfully")
return nil
}
func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models.UserFavorite, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserFavoritesByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_id": id,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get user favorite by ID")
var userFavorites models.UserFavorite
if client == nil {
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty})
}
result := client.WithContext(ctx).First(&userFavorites, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User favorite retrieved successfully")
return userFavorites, nil
}
func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserFavorite")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_id": id,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete user favorite")
var userFavorite models.UserFavorite
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIsWrongLength})
}
result := client.WithContext(ctx).Delete(&userFavorite, id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User favorite deleted successfully")
return nil
}

View File

@ -1,761 +0,0 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestCreateUserFavorite(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorite to test with
validFavorite := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(fmt.Sprintf("%025s", "Favorite1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: validUser.ID,
PostID: validPost.ID,
UserSourceID: validUser.Sources[0].ID,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
userFav models.UserFavorite
}
tests := []struct {
name string
args args
want models.UserFavorite
wantErr bool
}{
{
name: "Test 01: Valid UserFavorite",
args: args{
ctx: ctx,
userFav: validFavorite,
},
want: validFavorite,
wantErr: false,
},
{
name: "Test 02: Duplicate UserFavorite",
args: args{
ctx: ctx,
userFav: validFavorite,
},
want: models.UserFavorite{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateUserFavorite(tt.args.ctx, tt.args.userFav)
if (err != nil) != tt.wantErr {
t.Errorf("CreateUserFavorite() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateUserFavorite() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateUserFavoriteInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorites to test with
validUserFavorite := test.GenerateRandomUserFavorites(validUser.ID, validPost.ID, validUser.Sources[0].ID, 10)
// --
// -- -- Tests
type args struct {
ctx context.Context
userFav []models.UserFavorite
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid UserFavorite",
args: args{
ctx: ctx,
userFav: validUserFavorite,
batchSize: len(validUserFavorite),
},
wantErr: false,
},
{
name: "Test 02: Duplicate UserFavorite",
args: args{
ctx: ctx,
userFav: validUserFavorite,
batchSize: len(validUserFavorite),
},
wantErr: true,
},
{
name: "Test 03: Nil UserFavorite",
args: args{
ctx: ctx,
userFav: nil,
batchSize: len(validUserFavorite),
},
wantErr: true,
},
{
name: "Test 04: Empty UserFavorite",
args: args{
ctx: ctx,
userFav: []models.UserFavorite{},
batchSize: len(validUserFavorite),
},
wantErr: true,
},
{
name: "Test 08: Empty Batch Size",
args: args{
ctx: ctx,
userFav: []models.UserFavorite{},
batchSize: 0,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateUserFavoriteInBatch(tt.args.ctx, tt.args.userFav, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateUserFavoriteInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestUpdateUserFavorite(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorite to test with
validFavorite := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(fmt.Sprintf("%025s", "Favorite1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: validUser.ID,
PostID: validPost.ID,
UserSourceID: validUser.Sources[0].ID,
}
validFavorite, err = CreateUserFavorite(ctx, validFavorite)
if err != nil {
t.Fatalf("CreateUserFavorite err: %v", err)
}
// --
// -- Update UserFavorite
validUpdateFavorite := validFavorite
validFavorite.DeletedAt = gorm.DeletedAt{
Time: time.Time{},
Valid: false,
}
// --
// -- Delete UserFavorite
err = DeleteUserFavorite(ctx, validFavorite.ID)
if err != nil {
t.Fatalf("CreateUserFavorite err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
userFav models.UserFavorite
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for UserFavorite",
args: args{
ctx: ctx,
userFav: validUpdateFavorite,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdateUserFavorite(tt.args.ctx, tt.args.userFav); (err != nil) != tt.wantErr {
t.Errorf("UpdateUserFavorite() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetUserFavoritesByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorite to test with
validFavorite := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(fmt.Sprintf("%025s", "Favorite1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: validUser.ID,
PostID: validPost.ID,
UserSourceID: validUser.Sources[0].ID,
}
validFavorite, err = CreateUserFavorite(ctx, validFavorite)
if err != nil {
t.Fatalf("CreateUserFavorite err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.UserFavoriteID
}
tests := []struct {
name string
args args
want models.UserFavorite
wantErr bool
}{
{
name: "Test 01: Valid UserFavoriteID",
args: args{
ctx: ctx,
id: validFavorite.ID,
},
want: validFavorite,
wantErr: false,
},
{
name: "Test 03: Empty UserFavoriteID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short UserFavoriteID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserFavoritesByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserFavoritesByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkUserFavoriteID(got, tt.want) {
t.Errorf("GetUserFavoritesByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeleteUserFavorite(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
t.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create User to test with
userID := models.UserID(models.UserID(fmt.Sprintf("%025s", "User1")))
validUser := models.User{
BaseModel: models.BaseModel[models.UserID]{
ID: userID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Favorites: nil,
Sources: []models.UserSource{
{
BaseModel: models.BaseModel[models.UserSourceID]{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
},
},
}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePost err: %v", err)
}
// --
// -- Create UserFavorite to test with
validFavorite := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(fmt.Sprintf("%025s", "Favorite1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: validUser.ID,
PostID: validPost.ID,
UserSourceID: validUser.Sources[0].ID,
}
validFavorite, err = CreateUserFavorite(ctx, validFavorite)
if err != nil {
t.Fatalf("CreateUserFavorite err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.UserFavoriteID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid UserFavorite",
args: args{
ctx: ctx,
id: validFavorite.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed UserFavorite",
args: args{
ctx: ctx,
id: validFavorite.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty UserFavoriteID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short UserFavoriteID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteUserFavorite(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeleteUserFavorite() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkUserFavoriteID(got models.UserFavorite, want models.UserFavorite) bool {
if got.ID != want.ID {
return false
}
return true
}

View File

@ -2,8 +2,7 @@
CREATE TYPE Rating AS ENUM (
'safe',
'questionable',
'explicit',
'unknown'
'explicit'
);
CREATE TYPE TagType AS ENUM (
@ -21,9 +20,9 @@ CREATE TABLE "Post"
(
id CHAR(25) PRIMARY KEY,
rating Rating,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE NULL NULL
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL NULL
);
CREATE TABLE "Source"
@ -32,26 +31,26 @@ CREATE TABLE "Source"
display_name TEXT NULL,
icon TEXT NULL,
domain TEXT NOT NULL UNIQUE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE NULL
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL
);
CREATE TABLE "Tag"
(
name TEXT PRIMARY KEY,
tag_type TagType,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE NULL
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL
);
CREATE TABLE "User"
(
id TEXT PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE NULL
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL
);
CREATE TABLE "PostReference"
@ -70,42 +69,36 @@ CREATE TABLE "TagAlias"
(
name TEXT PRIMARY KEY,
tag_id TEXT REFERENCES "Tag" (name),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE "TagGroup"
(
name TEXT PRIMARY KEY,
tag_id TEXT REFERENCES "Tag" (name),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE "UserSource"
(
id CHAR(25) PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE NULL NULL,
user_id TEXT REFERENCES "User" (id),
source_id TEXT REFERENCES "Source" (id),
scrape_time_interval TEXT,
account_username TEXT,
account_id TEXT,
last_scrape_time TIMESTAMP WITH TIME ZONE,
account_validate BOOL DEFAULT FALSE,
account_validation_key CHAR(25),
UNIQUE (source_id, account_username, account_id)
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE "UserFavorites"
(
id CHAR(25) PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE NULL NULL,
user_id TEXT REFERENCES "User" (id),
post_id TEXT REFERENCES "Post" (id),
user_source_id CHAR(25) REFERENCES "UserSource" (id)
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, post_id)
);
CREATE TABLE "UserSource"
(
user_id TEXT REFERENCES "User" (id),
source_id TEXT REFERENCES "Source" (id),
scrape_time_interval INT,
account_username TEXT,
account_id TEXT,
last_scrape_time TIMESTAMP,
account_validate BOOL DEFAULT FALSE,
account_validation_key CHAR(25),
PRIMARY KEY (user_id, source_id),
UNIQUE (source_id, account_username, account_id)
);
CREATE TABLE "post_tags"

View File

@ -1,3 +0,0 @@
-- +migrate Up
CREATE INDEX idx_user_post_deleted ON "UserFavorites"(user_id, deleted_at);
CREATE INDEX idx_created_at ON "UserFavorites"(created_at);

View File

@ -1,28 +0,0 @@
-- +migrate Up
CREATE TYPE pool_category AS ENUM ('series', 'collection');
CREATE TABLE "Pool"
(
id CHAR(25) PRIMARY KEY,
name VARCHAR(75),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL,
category POOL_CATEGORY NOT NULL
);
CREATE TABLE "PoolPost"
(
pool_id CHAR(25) REFERENCES "Pool" (id),
post_id CHAR(25) REFERENCES "Post" (id),
order_position INT NOT NULL DEFAULT 0,
PRIMARY KEY (pool_id, post_id)
);
CREATE TABLE "PoolReference"
(
pool_id CHAR(25) REFERENCES "Pool" (id),
source_id CHAR(25) REFERENCES "Source" (id),
url TEXT NOT NULL,
PRIMARY KEY (pool_id, source_id, url)
);

View File

@ -1,11 +0,0 @@
-- +migrate Up
CREATE TABLE "ScrapeHistory"
(
scrape_task_id CHAR(25) PRIMARY KEY,
user_source_id CHAR(25) NOT NULL REFERENCES "UserSource" (id),
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
finished_at TIMESTAMP NULL,
error TEXT NULL,
added_posts INT NOT NULL,
deleted_posts INT NOT NULL
);

View File

@ -1,13 +0,0 @@
-- +migrate Up
ALTER TABLE "PostReference"
ALTER COLUMN post_id TYPE CHAR(25),
ALTER COLUMN source_id TYPE CHAR(25);
ALTER TABLE post_tags
ALTER COLUMN post_id TYPE CHAR(25);
ALTER TABLE "UserFavorites"
ALTER COLUMN post_id TYPE CHAR(25);
ALTER TABLE "UserSource"
ALTER COLUMN source_id TYPE CHAR(25);

View File

@ -1,30 +0,0 @@
-- +migrate Up
CREATE TYPE ReportType AS ENUM (
'duplicate',
'missing_data',
'rating_abuse',
'illegal_content'
);
CREATE TYPE ReportState AS ENUM (
'pending_unclaimed',
'pending',
'approved',
'partial',
'rejected'
);
CREATE TABLE "PostReport"
(
id CHAR(25) NOT NULL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE NULL,
post_id CHAR(25) NOT NULL REFERENCES "Post" (id),
report_by TEXT NOT NULL REFERENCES "User" (id),
report_description TEXT NOT NULL,
audit_by TEXT NULL REFERENCES "User" (id),
audit_description TEXT NOT NULL,
report_type ReportType NOT NULL,
report_state ReportState NOT NULL
);

View File

@ -1,163 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreatePool(ctx context.Context, pool models.Pool) (models.Pool, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePool")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": pool.ID,
})
span.SetAttributes(
attribute.String("pool_id", string(pool.ID)),
)
utils.HandleEvent(span, localLogger, "Starting pool creation")
if client == nil {
return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&pool)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.Pool{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "pool created successfully")
return pool, nil
}
func GetPoolByID(ctx context.Context, id models.PoolID) (models.Pool, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPool")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": id,
})
span.SetAttributes(
attribute.String("pool_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get pool by ID")
var pool models.Pool
if client == nil {
return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
result := client.WithContext(ctx).First(&pool, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.Pool{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "pool retrieved successfully")
return pool, nil
}
// UpdatePool updates the pool information in the database.
// Only supports changing the Name and the Category of the Pool
func UpdatePool(ctx context.Context, pool models.Pool) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePool")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": pool.ID,
})
span.SetAttributes(
attribute.String("pool_id", string(pool.ID)),
)
utils.HandleEvent(span, localLogger, "Starting post update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(pool.ID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
updatePost := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: pool.ID,
},
Name: pool.Name,
Category: pool.Category,
}
result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "pool updated successfully")
return nil
}
func DeletePool(ctx context.Context, id models.PoolID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePool")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": id,
})
span.SetAttributes(
attribute.String("pool_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete pool")
var pool models.Pool
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&pool, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "pool deleted successfully")
return nil
}

View File

@ -1,117 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreatePoolPost(ctx context.Context, poolID models.PoolID, postID models.PostID, orderPosition int) (models.PoolPost, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePoolPost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": poolID,
"post_id": postID,
})
span.SetAttributes(
attribute.String("pool_id", string(poolID)),
attribute.String("post_id", string(postID)),
)
utils.HandleEvent(span, localLogger, "Starting poolPost creation")
if client == nil {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(poolID) == 0 {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
if len(poolID) != 25 {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
}
if len(postID) == 0 {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
if len(postID) != 25 {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
}
poolPost := models.PoolPost{
PoolID: poolID,
PostID: postID,
OrderPosition: orderPosition,
}
result := client.WithContext(ctx).Create(&poolPost)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "poolPost created successfully")
return poolPost, nil
}
func DeletePoolPost(ctx context.Context, poolID models.PoolID, postID models.PostID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePoolPost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": poolID,
"post_id": postID,
})
span.SetAttributes(
attribute.String("pool_id", string(poolID)),
attribute.String("post_id", string(postID)),
)
utils.HandleEvent(span, localLogger, "Starting delete poolPost")
var poolPost models.PoolPost
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(poolID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
if len(poolID) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
}
if len(postID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
if len(postID) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&poolPost, "pool_id = ? AND post_id = ?", poolID, postID)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "poolPost deleted successfully")
return nil
}

View File

@ -1,252 +0,0 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreatePoolPost(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
pool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
},
Name: "Test Pool 01",
Category: models.Collection,
}
pool, err = CreatePool(ctx, pool)
if err != nil {
logger.Error(err)
}
// -- Create Post to test with
post := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
},
Rating: models.SFW,
}
post, err = CreatePost(ctx, post)
if err != nil {
logger.Error(err)
}
// -- Create PoolPost to test with
validPoolPost := models.PoolPost{
PoolID: pool.ID,
PostID: post.ID,
OrderPosition: 0,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
poolID models.PoolID
postID models.PostID
orderPosition int
}
tests := []struct {
name string
args args
want models.PoolPost
wantErr bool
}{
{
name: "Test 01: Valid post & pool ID",
args: args{
ctx: ctx,
postID: post.ID,
poolID: pool.ID,
orderPosition: 0,
},
want: validPoolPost,
wantErr: false,
},
{
name: "Test 02: Duplicate",
args: args{
ctx: ctx,
postID: post.ID,
poolID: pool.ID,
orderPosition: 0,
},
want: models.PoolPost{},
wantErr: true,
},
{
name: "Test 03: poolID is empty",
args: args{
ctx: ctx,
postID: post.ID,
poolID: "",
orderPosition: 0,
},
want: models.PoolPost{},
wantErr: true,
},
{
name: "Test 04: postID name is empty",
args: args{
ctx: ctx,
postID: "",
poolID: pool.ID,
orderPosition: 0,
},
want: models.PoolPost{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreatePoolPost(tt.args.ctx, tt.args.poolID, tt.args.postID, tt.args.orderPosition)
if (err != nil) != tt.wantErr {
t.Errorf("CreatePoolPost() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreatePoolPost() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeletePoolPost(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
pool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
},
Name: "Test Pool 01",
Category: models.Collection,
}
pool, err = CreatePool(ctx, pool)
if err != nil {
logger.Fatal(err)
}
// -- Create Post to test with
post := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
},
Rating: models.SFW,
}
post, err = CreatePost(ctx, post)
if err != nil {
logger.Fatal(err)
}
// -- Create PoolPost to test with
validPoolPost := models.PoolPost{
PoolID: pool.ID,
PostID: post.ID,
OrderPosition: 0,
}
validPoolPost, err = CreatePoolPost(ctx, validPoolPost.PoolID, validPoolPost.PostID, validPoolPost.OrderPosition)
if err != nil {
logger.Fatal(err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
poolID models.PoolID
postID models.PostID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Post & pool id",
args: args{
ctx: ctx,
poolID: validPoolPost.PoolID,
postID: validPoolPost.PostID,
},
wantErr: false,
},
{
name: "Test 02: Not existing postID",
args: args{
ctx: ctx,
poolID: validPoolPost.PoolID,
postID: "",
},
wantErr: true,
},
{
name: "Test 03: Not existing poolID",
args: args{
ctx: ctx,
poolID: "",
postID: validPoolPost.PostID,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeletePoolPost(tt.args.ctx, tt.args.poolID, tt.args.postID); (err != nil) != tt.wantErr {
t.Errorf("DeletePoolPost() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -1,121 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreatePoolReference(ctx context.Context, poolID models.PoolID, sourceID models.SourceID, url string) (models.PoolReference, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePoolReference")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": poolID,
"source_id": sourceID,
})
span.SetAttributes(
attribute.String("pool_id", string(poolID)),
attribute.String("source_id", string(sourceID)),
)
utils.HandleEvent(span, localLogger, "Starting poolReference creation")
if client == nil {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(poolID) == 0 {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
if len(poolID) != 25 {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
}
if len(sourceID) == 0 {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
}
if len(sourceID) != 25 {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
}
if url == "" {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolURLIsEmpty})
}
poolReference := models.PoolReference{
PoolID: poolID,
SourceID: sourceID,
URL: url,
}
result := client.WithContext(ctx).Create(&poolReference)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "poolReference created successfully")
return poolReference, nil
}
func DeletePoolReference(ctx context.Context, poolID models.PoolID, sourceID models.SourceID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePoolReference")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": poolID,
"source_id": sourceID,
})
span.SetAttributes(
attribute.String("pool_id", string(poolID)),
attribute.String("source_id", string(sourceID)),
)
utils.HandleEvent(span, localLogger, "Starting delete poolReference")
var poolReference models.PoolReference
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(poolID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
if len(poolID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
}
if len(sourceID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
}
if len(sourceID) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&poolReference, "pool_id = ? AND source_id = ?", poolID, sourceID)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "poolReference deleted successfully")
return nil
}

View File

@ -1,239 +0,0 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreatePoolReference(t *testing.T) {
// Setup throwaway container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
pool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
},
Name: "Test Pool 01",
Category: models.Collection,
}
pool, err = CreatePool(ctx, pool)
if err != nil {
logger.Error(err)
}
// -- Create Source to test with
source := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
},
DisplayName: "test",
Domain: "aaa",
Icon: "bbb",
}
source, err = CreateSource(ctx, source)
if err != nil {
logger.Fatal(err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
poolID models.PoolID
sourceID models.SourceID
url string
}
tests := []struct {
name string
args args
want models.PoolReference
wantErr bool
}{
{
name: "Test 01: Valid pool & source ID",
args: args{
ctx: ctx,
poolID: pool.ID,
sourceID: source.ID,
url: "http://example.com",
},
want: models.PoolReference{
PoolID: pool.ID,
SourceID: source.ID,
URL: "http://example.com",
},
wantErr: false,
},
{
name: "Test 02: Duplicate",
args: args{
ctx: ctx,
poolID: pool.ID,
sourceID: source.ID,
url: "http://example.com",
},
want: models.PoolReference{},
wantErr: true,
},
{
name: "Test 03: poolID is empty",
args: args{
ctx: ctx,
poolID: "",
sourceID: source.ID,
url: "http://example.com",
},
want: models.PoolReference{},
wantErr: true,
},
{
name: "Test 04: sourceID is empty",
args: args{
ctx: ctx,
poolID: pool.ID,
sourceID: "",
url: "http://example.com",
},
want: models.PoolReference{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreatePoolReference(tt.args.ctx, tt.args.poolID, tt.args.sourceID, tt.args.url)
if (err != nil) != tt.wantErr {
t.Errorf("CreatePoolReference() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreatePoolReference() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeletePoolReference(t *testing.T) {
// Setup throwaway container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
pool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
},
Name: "Test Pool 01",
Category: models.Collection,
}
pool, err = CreatePool(ctx, pool)
if err != nil {
logger.Error(err)
}
// -- Create Source to test with
source := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
},
DisplayName: "test",
Domain: "aaa",
Icon: "bbb",
}
source, err = CreateSource(ctx, source)
if err != nil {
logger.Fatal(err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
poolID models.PoolID
sourceID models.SourceID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Reference & pool id",
args: args{
ctx: ctx,
poolID: pool.ID,
sourceID: source.ID,
},
wantErr: false,
},
{
name: "Test 02: Not existing sourceID",
args: args{
ctx: ctx,
poolID: pool.ID,
sourceID: "",
},
wantErr: true,
},
{
name: "Test 03: Not existing poolID",
args: args{
ctx: ctx,
poolID: "",
sourceID: source.ID,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeletePoolReference(tt.args.ctx, tt.args.poolID, tt.args.sourceID); (err != nil) != tt.wantErr {
t.Errorf("DeletePoolReference() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -1,369 +0,0 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestCreatePool(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PoolgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
validPool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Name: "Test Pool 01",
Category: models.Series,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
post models.Pool
}
tests := []struct {
name string
args args
want models.Pool
wantErr bool
}{
{
name: "Test 01: Valid Pool",
args: args{
ctx: ctx,
post: validPool,
},
want: validPool,
wantErr: false,
},
{
name: "Test 02: Duplicate Pool",
args: args{
ctx: ctx,
post: validPool,
},
want: models.Pool{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreatePool(tt.args.ctx, tt.args.post)
if (err != nil) != tt.wantErr {
t.Errorf("CreatePool() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreatePool() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetPoolByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PoolgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
validPool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Name: "Test Pool 01",
Category: models.Series,
}
validPool, err = CreatePool(ctx, validPool)
if err != nil {
logger.Fatal(err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.PoolID
}
tests := []struct {
name string
args args
want models.Pool
wantErr bool
}{
{
name: "Test 01: Valid PoolID",
args: args{
ctx: ctx,
id: validPool.ID,
},
want: validPool,
wantErr: false,
},
{
name: "Test 03: Empty PoolID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short PoolID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPoolByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetPoolByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPoolID(got, tt.want) {
t.Errorf("GetPoolByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestUpdatePool(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PoolgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
validPool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Name: "Test Pool 01",
Category: models.Series,
}
validPool, err = CreatePool(ctx, validPool)
if err != nil {
logger.Fatal(err)
}
// --
// -- Create Updates models for Pool
validUpdatePool := validPool
validUpdatePool.Name = "Updated Test Pool"
validUpdatePool.Category = models.Collection
invalidUpdatePool := models.Pool{}
// --
// -- -- Tests
type args struct {
ctx context.Context
anthrovePool models.Pool
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for Pool",
args: args{
ctx: ctx,
anthrovePool: validUpdatePool,
},
wantErr: false,
},
{
name: "Test 02: Invalid Update for Pool",
args: args{
ctx: ctx,
anthrovePool: invalidUpdatePool,
},
wantErr: true,
},
{
name: "Test 03: Empty ID for Update for Pool",
args: args{
ctx: ctx,
anthrovePool: models.Pool{BaseModel: models.BaseModel[models.PoolID]{ID: ""}},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdatePool(tt.args.ctx, tt.args.anthrovePool); (err != nil) != tt.wantErr {
t.Errorf("UpdatePool() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeletePool(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PoolgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Pool to test with
validPool := models.Pool{
BaseModel: models.BaseModel[models.PoolID]{
ID: models.PoolID(fmt.Sprintf("%025s", "Pool1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Name: "Test Pool 01",
Category: models.Series,
}
validPool, err = CreatePool(ctx, validPool)
if err != nil {
logger.Fatal(err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.PoolID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid Pool",
args: args{
ctx: ctx,
id: validPool.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed Pool",
args: args{
ctx: ctx,
id: validPool.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty PoolID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short PoolID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeletePool(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeletePool() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkPoolID(got models.Pool, want models.Pool) bool {
if got.ID != want.ID {
return false
}
return true
}

View File

@ -2,206 +2,30 @@ package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
)
func CreatePost(ctx context.Context, post models.Post) (models.Post, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePost")
defer span.End()
type Post interface {
localLogger = localLogger.WithFields(log.Fields{
"post_id": post.ID,
})
// CreatePost adds a new post to the database.
CreatePost(ctx context.Context, anthrovePost *models.Post) error
span.SetAttributes(
attribute.String("post_id", string(post.ID)),
)
// TODO: Everything
CreatePostInBatch(ctx context.Context, anthrovePost []models.Post, batchSize int) error
utils.HandleEvent(span, localLogger, "Starting post creation")
// GetPostByAnthroveID retrieves a post by its Anthrove ID.
GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error)
if client == nil {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&post)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Post created successfully")
return post, nil
}
func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_count": len(post),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("post_count", len(post)),
)
utils.HandleEvent(span, localLogger, "Starting batch post creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if post == nil || len(post) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&post, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Batch posts created successfully")
return nil
}
func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_id": id,
})
span.SetAttributes(
attribute.String("post_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get post by ID")
var post models.Post
if client == nil {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
result := client.WithContext(ctx).First(&post, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Post retrieved successfully")
return post, nil
}
// UpdatePost updates the user post information in the database.
// Only a few parameter can be updated:
// - Rating
// - Tags
// - References
func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_id": anthrovePost.ID,
})
span.SetAttributes(
attribute.String("post_id", string(anthrovePost.ID)),
)
utils.HandleEvent(span, localLogger, "Starting post update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(anthrovePost.ID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
updatePost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: anthrovePost.ID,
},
Rating: anthrovePost.Rating,
Tags: anthrovePost.Tags,
References: anthrovePost.References,
}
result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{})
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Post updated successfully")
return nil
}
func DeletePost(ctx context.Context, id models.PostID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_id": id,
})
span.SetAttributes(
attribute.String("post_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete post")
var userFavorite models.UserFavorite
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Post deleted successfully")
return nil
// GetPostByURL retrieves a post by its source URL.
GetPostByURL(ctx context.Context, postURL string) (*models.Post, error)
// GetPostBySourceID retrieves a post by its source ID.
GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error)
// CreatePostWithReferenceToTagAnd adds a tag with a relation to a post.
CreatePostWithReferenceToTagAnd(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error
// CreatePostReference links a post with a source.
CreatePostReference(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error
}

View File

@ -1,173 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreatePostReport(ctx context.Context, postReport models.PostReport) (models.PostReport, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostReport")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_report_id": postReport.ID,
})
span.SetAttributes(
attribute.String("post_report_id", string(postReport.ID)),
)
utils.HandleEvent(span, localLogger, "Starting postReport creation")
if client == nil {
return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&postReport)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.PostReport{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Post createdReport successfully")
return postReport, nil
}
func GetPostReportByID(ctx context.Context, id models.PostReportID) (models.PostReport, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostReportByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_report_id": id,
})
span.SetAttributes(
attribute.String("post_report_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get postReport by ID")
var postReport models.PostReport
if client == nil {
return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostReportIDIsEmpty})
}
if len(id) != 25 {
return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostReportIDIsWrongLength})
}
result := client.WithContext(ctx).First(&postReport, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.PostReport{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "PostReport retrieved successfully")
return postReport, nil
}
// UpdatePostReport updates the PostReport information in the database.
// Only a few parameter can be updated:
// - AuditBy
// - AuditDescription
// - ReportType
// - ReportState
func UpdatePostReport(ctx context.Context, postReport models.PostReport) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePostReport")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_report_id": postReport.ID,
})
span.SetAttributes(
attribute.String("post_report_id", string(postReport.ID)),
)
utils.HandleEvent(span, localLogger, "Starting postReport update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(postReport.ID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
updatePost := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: postReport.ID,
},
AuditBy: postReport.AuditBy,
AuditDescription: postReport.AuditDescription,
ReportType: postReport.ReportType,
ReportState: postReport.ReportState,
}
result := client.WithContext(ctx).Model(&updatePost).Updates(updatePost)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "PostReport updated successfully")
return nil
}
func DeletePostReport(ctx context.Context, id models.PostReportID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePostReport")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_report_id": id,
})
span.SetAttributes(
attribute.String("post_report_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete postReport")
var postReport models.PostReport
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostReportIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostReportIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&postReport, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "PostReport deleted successfully")
return nil
}

View File

@ -1,507 +0,0 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestCreatePostReport(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// Setup Tests
validUser01 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser01, err = CreateUser(ctx, validUser01)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
validUser02 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User2"))}}
validUser02, err = CreateUser(ctx, validUser02)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// -- Create PostReport to test with
validPostReport := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: models.PostReportID(fmt.Sprintf("%025s", "PostReport1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
PostID: validPost.ID,
ReportBy: validUser01.ID,
ReportDescription: "aaa",
AuditBy: &validUser02.ID,
AuditDescription: "ahaa",
ReportType: models.Duplicate,
ReportState: models.Rejected,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
source models.PostReport
}
tests := []struct {
name string
args args
want models.PostReport
wantErr bool
}{
{
name: "Test 01: Valid PostReport",
args: args{
ctx: ctx,
source: validPostReport,
},
want: validPostReport,
wantErr: false,
},
{
name: "Test 02: Duplicate PostReport",
args: args{
ctx: ctx,
source: validPostReport,
},
want: models.PostReport{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreatePostReport(tt.args.ctx, tt.args.source)
if (err != nil) != tt.wantErr {
t.Errorf("CreatePostReport() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreatePostReport() got = %v, want %v", got, tt.want)
}
})
}
}
func TestUpdatePostReport(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// Setup Tests
validUser01 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser01, err = CreateUser(ctx, validUser01)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
validUser02 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User2"))}}
validUser02, err = CreateUser(ctx, validUser02)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// -- Create PostReport to test with
validPostReport := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: models.PostReportID(fmt.Sprintf("%025s", "PostReport1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
PostID: validPost.ID,
ReportBy: validUser01.ID,
ReportDescription: "aaa",
AuditBy: nil,
AuditDescription: "",
ReportType: models.Duplicate,
ReportState: models.Rejected,
}
// --
validPostReport, err = CreatePostReport(ctx, validPostReport)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// --
// -- Create Updates models for UserPostReport
validUpdatePostReport := validPostReport
validUpdatePostReport.AuditBy = &validUser02.ID
validUpdatePostReport.AuditDescription = "aaaaa"
validUpdatePostReport.ReportState = models.Approved
invalidUpdatePostReport := models.PostReport{}
// --
// -- -- Tests
type args struct {
ctx context.Context
source models.PostReport
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for PostReport",
args: args{
ctx: ctx,
source: validUpdatePostReport,
},
wantErr: false,
},
{
name: "Test 02: Invalid Update for PostReport",
args: args{
ctx: ctx,
source: invalidUpdatePostReport,
},
wantErr: true,
},
{
name: "Test 03: Empty ID for Update for PostReport",
args: args{
ctx: ctx,
source: models.PostReport{BaseModel: models.BaseModel[models.PostReportID]{ID: ""}},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdatePostReport(tt.args.ctx, tt.args.source); (err != nil) != tt.wantErr {
t.Errorf("UpdatePostReport() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetPostReportByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// Setup Tests
validUser01 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser01, err = CreateUser(ctx, validUser01)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
validUser02 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User2"))}}
validUser02, err = CreateUser(ctx, validUser02)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// -- Create PostReport to test with
validPostReport := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: models.PostReportID(fmt.Sprintf("%025s", "PostReport1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
PostID: validPost.ID,
ReportBy: validUser01.ID,
ReportDescription: "aaa",
AuditBy: nil,
AuditDescription: "",
ReportType: models.Duplicate,
ReportState: models.Rejected,
}
// --
validPostReport, err = CreatePostReport(ctx, validPostReport)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// --
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.PostReportID
}
tests := []struct {
name string
args args
want models.PostReport
wantErr bool
}{
{
name: "Test 01: Valid PostReport ID",
args: args{
ctx: ctx,
id: validPostReport.ID,
},
want: validPostReport,
wantErr: false,
},
{
name: "Test 03: Empty PostReportID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short PostReportID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostReportByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostReportByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPostReportID(got, tt.want) {
t.Errorf("GetPostReportByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeletePostReport(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// Setup Tests
validUser01 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser01, err = CreateUser(ctx, validUser01)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
validUser02 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User2"))}}
validUser02, err = CreateUser(ctx, validUser02)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// -- Create PostReport to test with
validPostReport := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: models.PostReportID(fmt.Sprintf("%025s", "PostReport1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
PostID: validPost.ID,
ReportBy: validUser01.ID,
ReportDescription: "aaa",
AuditBy: nil,
AuditDescription: "",
ReportType: models.Duplicate,
ReportState: models.Rejected,
}
// --
validPostReport, err = CreatePostReport(ctx, validPostReport)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// --
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.PostReportID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid PostReport",
args: args{
ctx: ctx,
id: validPostReport.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed PostReport",
args: args{
ctx: ctx,
id: validPostReport.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty PostReportID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short PostReportID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeletePostReport(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeletePostReport() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkPostReportID(got models.PostReport, want models.PostReport) bool {
if got.ID != want.ID {
return false
}
return true
}

View File

@ -1,454 +0,0 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestCreatePost(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
post models.Post
}
tests := []struct {
name string
args args
want models.Post
wantErr bool
}{
{
name: "Test 01: Valid Post",
args: args{
ctx: ctx,
post: validPost,
},
want: validPost,
wantErr: false,
},
{
name: "Test 02: Duplicate Post",
args: args{
ctx: ctx,
post: validPost,
},
want: models.Post{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreatePost(tt.args.ctx, tt.args.post)
if (err != nil) != tt.wantErr {
t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreatePost() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreatePostInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Posts to test with
validPosts := test.GenerateRandomPosts(20)
// --
// -- -- Tests
type args struct {
ctx context.Context
post []models.Post
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Posts",
args: args{
ctx: ctx,
post: validPosts,
batchSize: len(validPosts),
},
wantErr: false,
},
{
name: "Test 02: Duplicate Posts",
args: args{
ctx: ctx,
post: validPosts,
batchSize: len(validPosts),
},
wantErr: true,
},
{
name: "Test 03: Nil Posts",
args: args{
ctx: ctx,
post: nil,
batchSize: len(validPosts),
},
wantErr: true,
},
{
name: "Test 04: Empty Posts",
args: args{
ctx: ctx,
post: []models.Post{},
batchSize: len(validPosts),
},
wantErr: true,
},
{
name: "Test 08: Empty Batch Size",
args: args{
ctx: ctx,
post: []models.Post{},
batchSize: 0,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreatePostInBatch(tt.args.ctx, tt.args.post, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreatePostInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetPostByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
logger.Fatal(err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.PostID
}
tests := []struct {
name string
args args
want models.Post
wantErr bool
}{
{
name: "Test 01: Valid PostID",
args: args{
ctx: ctx,
id: validPost.ID,
},
want: validPost,
wantErr: false,
},
{
name: "Test 03: Empty PostID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short PostID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPostID(got, tt.want) {
t.Errorf("GetPostByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestUpdatePost(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
logger.Fatal(err)
}
// --
// -- Create Updates models for Post
validUpdatePost := validPost
validUpdatePost.Rating = models.NSFW
invalidUpdatePost := models.Post{}
// --
// -- -- Tests
type args struct {
ctx context.Context
anthrovePost models.Post
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for Post",
args: args{
ctx: ctx,
anthrovePost: validUpdatePost,
},
wantErr: false,
},
{
name: "Test 02: Invalid Update for Post",
args: args{
ctx: ctx,
anthrovePost: invalidUpdatePost,
},
wantErr: true,
},
{
name: "Test 03: Empty ID for Update for Post",
args: args{
ctx: ctx,
anthrovePost: models.Post{BaseModel: models.BaseModel[models.PostID]{ID: ""}},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdatePost(tt.args.ctx, tt.args.anthrovePost); (err != nil) != tt.wantErr {
t.Errorf("UpdatePost() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeletePost(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
logger.Fatal(err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.PostID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid Post",
args: args{
ctx: ctx,
id: validPost.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed Post",
args: args{
ctx: ctx,
id: validPost.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty PostID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short PostID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeletePost(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeletePost() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkPostID(got models.Post, want models.Post) bool {
if got.ID != want.ID {
return false
}
return true
}

261
pkg/database/postgres.go Normal file
View File

@ -0,0 +1,261 @@
package database
import (
"context"
"embed"
"fmt"
log2 "log"
"os"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/postgres"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
_ "github.com/lib/pq"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
gormPostgres "gorm.io/driver/postgres"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
)
//go:embed migrations/*.sql
var embedMigrations embed.FS
type postgresqlConnection struct {
db *gorm.DB
debug bool
}
func NewPostgresqlConnection() OtterSpace {
return &postgresqlConnection{
db: nil,
}
}
func (p *postgresqlConnection) Connect(_ context.Context, config models.DatabaseConfig) error {
var localSSL string
var logLevel gormLogger.LogLevel
if config.SSL {
localSSL = "require"
} else {
localSSL = "disable"
}
p.debug = config.Debug
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s", config.Endpoint, config.Username, config.Password, config.Database, config.Port, localSSL, config.Timezone)
var err error
if p.debug {
logLevel = gormLogger.Info
} else {
logLevel = gormLogger.Silent
}
dbLogger := gormLogger.New(log2.New(os.Stdout, "\r\n", log2.LstdFlags), gormLogger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: logLevel,
IgnoreRecordNotFoundError: true,
Colorful: true,
})
db, err := gorm.Open(gormPostgres.Open(dsn), &gorm.Config{
Logger: dbLogger,
})
p.db = db
if err != nil {
return err
}
log.Infof("OtterSpace: database connection established")
err = p.migrateDatabase(db)
if err != nil {
return err
}
log.Infof("OtterSpace: migration compleate")
return nil
}
func (p *postgresqlConnection) CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error {
return postgres.CreateUserWithRelationToSource(ctx, p.db, anthroveUserID, sourceID, accountId, accountUsername)
}
func (p *postgresqlConnection) CreateSource(ctx context.Context, anthroveSource *models.Source) error {
return postgres.CreateSource(ctx, p.db, anthroveSource)
}
func (p *postgresqlConnection) CreatePost(ctx context.Context, anthrovePost *models.Post) error {
return postgres.CreatePost(ctx, p.db, anthrovePost)
}
func (p *postgresqlConnection) CreatePostInBatch(ctx context.Context, anthrovePost []models.Post, batchSize int) error {
return postgres.CreatePostInBatch(ctx, p.db, anthrovePost, batchSize)
}
func (p *postgresqlConnection) CreatePostWithReferenceToTagAnd(ctx context.Context, anthrovePostID models.AnthrovePostID, anthroveTag *models.Tag) error {
return postgres.CreateTagAndReferenceToPost(ctx, p.db, anthrovePostID, anthroveTag)
}
func (p *postgresqlConnection) CreatePostReference(ctx context.Context, anthrovePostID models.AnthrovePostID, sourceDomain models.AnthroveSourceDomain, postURL models.AnthrovePostURL, config models.PostReferenceConfig) error {
return postgres.CreateReferenceBetweenPostAndSource(ctx, p.db, anthrovePostID, sourceDomain, postURL, config)
}
func (p *postgresqlConnection) CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error {
return postgres.CreateReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
}
func (p *postgresqlConnection) CheckIfUserHasPostAsFavorite(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) (bool, error) {
return postgres.CheckReferenceBetweenUserAndPost(ctx, p.db, anthroveUserID, anthrovePostID)
}
func (p *postgresqlConnection) GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error) {
return postgres.GetPostByAnthroveID(ctx, p.db, anthrovePostID)
}
func (p *postgresqlConnection) GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error) {
return postgres.GetPostBySourceURL(ctx, p.db, sourceUrl)
}
func (p *postgresqlConnection) GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error) {
return postgres.GetPostBySourceID(ctx, p.db, sourceID)
}
func (p *postgresqlConnection) GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error) {
return postgres.GetUserFavoritesCount(ctx, p.db, anthroveUserID)
}
func (p *postgresqlConnection) GetAllUserSources(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
return postgres.GetUserSourceLinks(ctx, p.db, anthroveUserID)
}
func (p *postgresqlConnection) GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error) {
return postgres.GetUserSourceBySourceID(ctx, p.db, anthroveUserID, sourceID)
}
func (p *postgresqlConnection) GetAllUsers(ctx context.Context) ([]models.User, error) {
return postgres.GetAllUsers(ctx, p.db)
}
func (p *postgresqlConnection) GetAllUserFavoritesWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
return postgres.GetUserFavoriteWithPagination(ctx, p.db, anthroveUserID, skip, limit)
}
func (p *postgresqlConnection) GetAllTagsFromUser(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
return postgres.GetUserTagWitRelationToFavedPosts(ctx, p.db, anthroveUserID)
}
func (p *postgresqlConnection) GetAllTags(ctx context.Context) ([]models.Tag, error) {
return postgres.GetTags(ctx, p.db)
}
func (p *postgresqlConnection) GetAllSources(ctx context.Context) ([]models.Source, error) {
return postgres.GetAllSource(ctx, p.db)
}
func (p *postgresqlConnection) GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) {
return postgres.GetSourceByDomain(ctx, p.db, sourceDomain)
}
// NEW FUNCTIONS
func (p *postgresqlConnection) UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error {
return postgres.UpdateUserSourceScrapeTimeInterval(ctx, p.db, anthroveUserID, sourceID, scrapeTime)
}
func (p *postgresqlConnection) UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error {
return postgres.UpdateUserSourceLastScrapeTime(ctx, p.db, anthroveUserID, sourceID, lastScrapeTime)
}
func (p *postgresqlConnection) UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error {
return postgres.UpdateUserSourceValidation(ctx, p.db, anthroveUserID, sourceID, valid)
}
func (p *postgresqlConnection) CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error {
return postgres.CreateTagAlias(ctx, p.db, tagAliasName, tagID)
}
func (p *postgresqlConnection) GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error) {
return postgres.GetAllTagAlias(ctx, p.db)
}
func (p *postgresqlConnection) GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error) {
return postgres.GetAllTagAliasByTag(ctx, p.db, tagID)
}
func (p *postgresqlConnection) DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error {
return postgres.DeleteTagAlias(ctx, p.db, tagAliasName)
}
func (p *postgresqlConnection) CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) error {
return postgres.CreateTagGroup(ctx, p.db, tagGroupName, tagID)
}
func (p *postgresqlConnection) GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error) {
return postgres.GetAllTagGroup(ctx, p.db)
}
func (p *postgresqlConnection) GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error) {
return postgres.GetAllTagGroupByTag(ctx, p.db, tagID)
}
func (p *postgresqlConnection) DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error {
return postgres.DeleteTagGroup(ctx, p.db, tagGroupName)
}
func (p *postgresqlConnection) CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error {
return postgres.CreateTag(ctx, p.db, tagName, tagType)
}
func (p *postgresqlConnection) GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) {
return postgres.GetAllTagByTagsType(ctx, p.db, tagType)
}
func (p *postgresqlConnection) DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error {
return postgres.DeleteTag(ctx, p.db, tagName)
}
func (p *postgresqlConnection) CreateTagInBatchAndUpdate(ctx context.Context, tags []models.Tag, batchSize int) error {
return postgres.CreateTagInBatchAndUpdate(ctx, p.db, tags, batchSize)
}
func (p *postgresqlConnection) CreateTagAliasInBatch(ctx context.Context, tagAliases []models.TagAlias, batchSize int) error {
return postgres.CreateTagAliasInBatch(ctx, p.db, tagAliases, batchSize)
}
func (p *postgresqlConnection) CreateTagGroupInBatch(ctx context.Context, tagGroups []models.TagGroup, batchSize int) error {
return postgres.CreateTagGroupInBatch(ctx, p.db, tagGroups, batchSize)
}
// HELPER
func (p *postgresqlConnection) migrateDatabase(dbPool *gorm.DB) error {
dialect := "postgres"
migrations := &migrate.EmbedFileSystemMigrationSource{FileSystem: embedMigrations, Root: "migrations"}
db, err := dbPool.DB()
if err != nil {
return fmt.Errorf("postgres migration: %v", err)
}
n, err := migrate.Exec(db, dialect, migrations, migrate.Up)
if err != nil {
return fmt.Errorf("postgres migration: %v", err)
}
if p.debug {
if n != 0 {
log.Infof("postgres migration: applied %d migrations!", n)
} else {
log.Info("postgres migration: nothing to migrate")
}
}
return nil
}

File diff suppressed because it is too large Load Diff

View File

@ -1,91 +0,0 @@
package database
import (
"math"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"gorm.io/gorm"
)
type Pagination struct {
Limit int `json:"limit,omitempty;query:limit"`
Page int `json:"page,omitempty;query:page"`
Sort string `json:"sort,omitempty;query:sort"`
TotalRows int64 `json:"total_rows"`
TotalPages int `json:"total_pages"`
}
// Paginate applies pagination to a GORM query.
// It takes two parameters:
//
// - page: the current page number (1-based index). If the page is less than or equal to 0, it defaults to 1.
//
// - pageSize: the number of records per page. If the pageSize is greater than the MaxPageSizeLimit defined in models, it defaults to MaxPageSizeLimit. If the pageSize is less than or equal to 0, it defaults to DefaultPageSize defined in models.
//
// The function calculates the offset based on the page and pageSize, and applies the offset and limit to the GORM DB instance.
func Paginate(page int, pageSize int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if page <= 0 {
page = 1
}
switch {
case pageSize > models.MaxPageSizeLimit:
pageSize = models.MaxPageSizeLimit
case pageSize <= 0:
pageSize = models.DefaultPageSize
}
offset := (page - 1) * pageSize
return db.Offset(offset).Limit(pageSize)
}
}
// AdvancedPagination applies pagination, sorting, and counting to a GORM query.
// Parameters:
//
// - value: The model or table to query.
//
// - pagination: A pointer to a Pagination struct containing page, limit, sort, total rows, and total pages information.
//
// - db: A pointer to the GORM database instance.
//
// The function calculates the offset based on the Page and Limit of the Pagination struct, and applies the offset and limit to the GORM DB instance.
func AdvancedPagination(value any, pagination *Pagination, db *gorm.DB) func(db *gorm.DB) *gorm.DB {
var totalRows int64
if pagination.Page <= 0 {
pagination.Page = 1
}
switch {
case pagination.Limit > models.MaxPageSizeLimit:
pagination.Limit = models.MaxPageSizeLimit
case pagination.Limit <= 0:
pagination.Limit = models.DefaultPageSize
}
db.Model(value).Count(&totalRows)
pagination.TotalRows = totalRows
totalPages := int(math.Ceil(float64(totalRows) / float64(pagination.Limit)))
pagination.TotalPages = totalPages
offset := (pagination.Page - 1) * pagination.Limit
return func(db *gorm.DB) *gorm.DB {
return db.Offset(offset).Limit(pagination.Limit)
}
}
// OrderBy applies an order operation to a GORM query.
// Parameters:
//
// - sort: a SQL order query like "id desc".
func OrderBy(sort string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Order(sort)
}
}

View File

@ -1,328 +0,0 @@
package database
import (
"context"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestPaginate(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tags to test with
validTags := test.GenerateRandomTags(500)
err = CreateTagInBatch(ctx, validTags, len(validTags))
if err != nil {
logger.Fatalf("Could not create tags: %v", err)
}
// --
// -- -- Tests
type args struct {
page int
pageSize int
}
tests := []struct {
name string
args args
want []models.Tag
}{
{
name: "Test 01: Valid Page & PageSize",
args: args{
page: 1,
pageSize: 5,
},
want: validTags[:5],
},
{
name: "Test 02: Second page with Valid Page & PageSize",
args: args{
page: 2,
pageSize: 5,
},
want: validTags[5:10],
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tags []models.Tag
result := client.WithContext(ctx).Scopes(Paginate(tt.args.page, tt.args.pageSize)).Find(&tags)
if result.Error != nil {
t.Errorf("Paginate() = %v", result.Error)
}
if !reflect.DeepEqual(tt.want, tags) {
t.Errorf("Length of tags: %d", len(tags))
t.Errorf("Length of want: %d", len(tt.want))
t.Errorf("Paginate() = %v, want %v", tags, tt.want)
}
})
}
}
func TestAdvancedPagination(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tags to test with
validTags := []models.Tag{
{
Name: "a",
Type: models.General,
},
{
Name: "b",
Type: models.General,
},
{
Name: "c",
Type: models.Lore,
},
{
Name: "d",
Type: models.Artist,
},
{
Name: "e",
Type: models.Artist,
},
{
Name: "f",
Type: models.Copyright,
},
{
Name: "g",
Type: models.Meta,
},
{
Name: "h",
Type: models.Species,
},
{
Name: "i",
Type: models.Invalid,
},
{
Name: "j",
Type: models.General,
},
}
err = CreateTagInBatch(ctx, validTags, len(validTags))
if err != nil {
logger.Fatalf("Could not create tags: %v", err)
}
// --
// -- -- Tests
type args struct {
value any
pagination *Pagination
db *gorm.DB
}
tests := []struct {
name string
args args
want []models.Tag
}{
{
name: "Test 01: Valid Data",
args: args{
value: validTags,
pagination: &Pagination{
Limit: 5,
Page: 1,
Sort: "name asc",
},
db: client,
},
want: validTags[:5],
},
{
name: "Test 02: Second page with Valid Data",
args: args{
value: validTags,
pagination: &Pagination{
Limit: 5,
Page: 2,
Sort: "name asc",
},
db: client,
},
want: validTags[5:10],
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tags []models.Tag
result := client.WithContext(ctx).Scopes(AdvancedPagination(tt.args.value, tt.args.pagination, tt.args.db)).Find(&tags)
if result.Error != nil {
t.Errorf("Paginate() = %v", result.Error)
}
if !reflect.DeepEqual(tt.want, tags) {
t.Errorf("Length of tags: %d", len(tags))
t.Errorf("Length of want: %d", len(tt.want))
t.Errorf("Paginate() = %v, want %v", tags, tt.want)
}
})
}
}
func TestOrderBy(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tags to test with
validTags := []models.Tag{
{
Name: "a",
Type: models.General,
},
{
Name: "b",
Type: models.General,
},
{
Name: "c",
Type: models.Lore,
},
{
Name: "d",
Type: models.Artist,
},
{
Name: "e",
Type: models.Artist,
},
{
Name: "f",
Type: models.Copyright,
},
{
Name: "g",
Type: models.Meta,
},
{
Name: "h",
Type: models.Species,
},
{
Name: "i",
Type: models.Invalid,
},
{
Name: "j",
Type: models.General,
},
}
err = CreateTagInBatch(ctx, validTags, len(validTags))
if err != nil {
logger.Fatalf("Could not create tags: %v", err)
}
// --
// -- -- Tests
type args struct {
sort string
}
tests := []struct {
name string
args args
want []models.Tag
wantErr bool
}{
{
name: "Test 01: Valid Data",
args: args{
sort: "name asc",
},
want: validTags,
wantErr: false,
},
{
name: "Test 01: Invalid Data",
args: args{
sort: "name desc",
},
want: validTags,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tags []models.Tag
result := client.WithContext(ctx).Scopes(OrderBy(tt.args.sort)).Find(&tags)
if result.Error != nil {
t.Errorf("Paginate() = %v", result.Error)
}
if !reflect.DeepEqual(tt.want, tags) != tt.wantErr {
t.Errorf("Length of tags: %d", len(tags))
t.Errorf("Length of want: %d", len(tt.want))
t.Errorf("Paginate() = %v, want %v", tags, tt.want)
}
})
}
}

View File

@ -1,182 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreateScrapeHistory(ctx context.Context, scrapeHistory models.ScrapeHistory) (models.ScrapeHistory, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateScrapeHistory")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"scrape_task_id": scrapeHistory.ScrapeTaskID,
"user_source_id": scrapeHistory.UserSourceID,
})
span.SetAttributes(
attribute.String("scrape_task_id", string(scrapeHistory.ScrapeTaskID)),
attribute.String("user_source_id", string(scrapeHistory.UserSourceID)),
)
utils.HandleEvent(span, localLogger, "Starting scrapeHistory creation")
if client == nil {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(scrapeHistory.ScrapeTaskID) == 0 {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
}
if len(scrapeHistory.ScrapeTaskID) != 25 {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength})
}
if len(scrapeHistory.UserSourceID) == 0 {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty})
}
if len(scrapeHistory.UserSourceID) != 25 {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIsWrongLength})
}
result := client.WithContext(ctx).Create(&scrapeHistory)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "scrapeHistory created successfully")
return scrapeHistory, nil
}
func GetScrapeHistoryByID(ctx context.Context, id models.ScrapeTaskID) (models.ScrapeHistory, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetScrapeHistoryByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"scrape_task_id": id,
})
span.SetAttributes(
attribute.String("scrape_task_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get scrapeHistory by ID")
var post models.ScrapeHistory
if len(id) == 0 {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
}
if len(id) != 25 {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength})
}
result := client.WithContext(ctx).First(&post, "scrape_task_id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "scrapeHistory retrieved successfully")
return post, nil
}
// UpdateScrapeHistory updates the scrape history information in the database.
// Only a few parameter can be updated:
// - FinishedAt
// - Error
// - AddedPosts
// - DeletedPosts
func UpdateScrapeHistory(ctx context.Context, scrapeHistory models.ScrapeHistory) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateScrapeHistory")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"scrape_task_id": scrapeHistory.ScrapeTaskID,
})
span.SetAttributes(
attribute.String("scrape_task_id", string(scrapeHistory.ScrapeTaskID)),
)
utils.HandleEvent(span, localLogger, "Starting scrapeHistory update")
if len(scrapeHistory.ScrapeTaskID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
}
if len(scrapeHistory.ScrapeTaskID) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength})
}
updateScrapeHistory := models.ScrapeHistory{
ScrapeTaskID: scrapeHistory.ScrapeTaskID,
UserSourceID: scrapeHistory.UserSourceID,
FinishedAt: scrapeHistory.FinishedAt,
Error: scrapeHistory.Error,
AddedPosts: scrapeHistory.AddedPosts,
DeletedPosts: scrapeHistory.DeletedPosts,
}
result := client.WithContext(ctx).Model(&updateScrapeHistory).Updates(updateScrapeHistory)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "scrapeHistory updated successfully")
return nil
}
func DeleteScrapeHistory(ctx context.Context, id models.ScrapeTaskID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteScrapeHistory")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"scrape_task_id": id,
})
span.SetAttributes(
attribute.String("scrape_task_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete scrapeHistory")
var scrapeHistory models.ScrapeHistory
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&scrapeHistory, "scrape_task_id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "scrapeHistory deleted successfully")
return nil
}

View File

@ -1,411 +0,0 @@
package database
import (
"context"
"fmt"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestCreateScrapeHistory(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUSerSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUSerSource, err = CreateUserSource(ctx, validUSerSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
validScrapeHistory := models.ScrapeHistory{
ScrapeTaskID: "000000000000valid_task_id",
UserSourceID: validUSerSource.ID,
}
// --
tests := []struct {
name string
args models.ScrapeHistory
wantErr bool
}{
{
name: "Valid ScrapeHistory",
args: validScrapeHistory,
wantErr: false,
},
{
name: "Duplicate ScrapeHistory",
args: validScrapeHistory,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := CreateScrapeHistory(ctx, tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("CreateScrapeHistory() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetScrapeHistoryByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUSerSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUSerSource, err = CreateUserSource(ctx, validUSerSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
validScrapeHistory := models.ScrapeHistory{
ScrapeTaskID: "000000000000valid_task_id",
UserSourceID: validUSerSource.ID,
}
validScrapeHistory, err = CreateScrapeHistory(ctx, validScrapeHistory)
if err != nil {
t.Fatalf("CreateScrapeHistory err: %v", err)
}
// --
tests := []struct {
name string
id models.ScrapeTaskID
wantErr bool
}{
{"Valid ID", "000000000000valid_task_id", false},
{"Empty ID", "", true},
{"Non-existent ID", "non_existent_id", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := GetScrapeHistoryByID(ctx, tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetScrapeHistoryByID() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestUpdateScrapeHistory(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUSerSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUSerSource, err = CreateUserSource(ctx, validUSerSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
validScrapeHistory := models.ScrapeHistory{
ScrapeTaskID: "000000000000valid_task_id",
UserSourceID: validUSerSource.ID,
}
validScrapeHistory, err = CreateScrapeHistory(ctx, validScrapeHistory)
if err != nil {
t.Fatalf("CreateScrapeHistory err: %v", err)
}
updatedScrapeHistory := validScrapeHistory
updatedScrapeHistory.FinishedAt = time.Now()
// --
tests := []struct {
name string
args models.ScrapeHistory
wantErr bool
}{
{"Valid Update", updatedScrapeHistory, false},
{"Empty ID", models.ScrapeHistory{ScrapeTaskID: ""}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := UpdateScrapeHistory(ctx, tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("UpdateScrapeHistory() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeleteScrapeHistory(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUSerSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUSerSource, err = CreateUserSource(ctx, validUSerSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
validScrapeHistory := models.ScrapeHistory{
ScrapeTaskID: "000000000000valid_task_id",
UserSourceID: validUSerSource.ID,
}
validScrapeHistory, err = CreateScrapeHistory(ctx, validScrapeHistory)
if err != nil {
t.Fatalf("CreateScrapeHistory err: %v", err)
}
// --
tests := []struct {
name string
id models.ScrapeTaskID
wantErr bool
}{
{"Valid ID", "000000000000valid_task_id", false},
{"Empty ID", "", true},
{"Short ID", "short_id", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := DeleteScrapeHistory(ctx, tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("DeleteScrapeHistory() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -2,246 +2,18 @@ package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
)
func CreateSource(ctx context.Context, source models.Source) (models.Source, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource")
defer span.End()
type Source interface {
utils.HandleEvent(span, localLogger, "Starting source creation")
// CreateSource adds a new source to the database.
CreateSource(ctx context.Context, anthroveSource *models.Source) error
if client == nil {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&source)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
localLogger = localLogger.WithFields(log.Fields{
"source_id": source.ID,
})
span.SetAttributes(
attribute.String("source_id", string(source.ID)),
)
utils.HandleEvent(span, localLogger, "Source created successfully")
return source, nil
}
func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSourceInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"source_count": len(source),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("source_count", len(source)),
)
utils.HandleEvent(span, localLogger, "Starting batch source creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if source == nil || len(source) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&source, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Batch sources created successfully")
return nil
}
// UpdateSource updates th source information in the database.
// Only a few parameter can be updated:
// - DisplayName
// - Domain
// - Icon
func UpdateSource(ctx context.Context, source models.Source) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"source_id": source.ID,
})
span.SetAttributes(
attribute.String("source_id", string(source.ID)),
)
utils.HandleEvent(span, localLogger, "Starting source update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(source.ID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
}
updateSource := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: source.ID,
},
DisplayName: source.DisplayName,
Domain: source.Domain,
Icon: source.Icon,
}
result := client.WithContext(ctx).Updates(&updateSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Source updated successfully")
return nil
}
func GetSourceByID(ctx context.Context, id models.SourceID) (models.Source, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"source_id": id,
})
span.SetAttributes(
attribute.String("source_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get source by ID")
var source models.Source
if client == nil {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
}
if len(id) != 25 {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
}
result := client.WithContext(ctx).First(&source, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Source retrieved successfully")
return source, nil
}
func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (models.Source, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByDomain")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"source_domain": sourceDomain,
})
span.SetAttributes(
attribute.String("source_domain", string(sourceDomain)),
)
utils.HandleEvent(span, localLogger, "Starting get source by domain")
var source models.Source
if client == nil {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(sourceDomain) == 0 {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceDomainIsEmpty})
}
result := client.WithContext(ctx).First(&source, "domain = ?", sourceDomain)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Source retrieved successfully")
return source, nil
}
func DeleteSource(ctx context.Context, id models.SourceID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteSource")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"source_id": id,
})
span.SetAttributes(
attribute.String("source_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete source")
var source models.Source
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&source, id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Source deleted successfully")
return nil
// GetAllSources retrieves all sources.
GetAllSources(ctx context.Context) ([]models.Source, error)
// GetSourceByDomain retrieves a source by its URL.
GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error)
}

View File

@ -1,546 +0,0 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestCreateSource(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
// --
// -- -- Tests
type args struct {
ctx context.Context
source models.Source
}
tests := []struct {
name string
args args
want models.Source
wantErr bool
}{
{
name: "Test 01: Valid Source",
args: args{
ctx: ctx,
source: validSource,
},
want: validSource,
wantErr: false,
},
{
name: "Test 02: Duplicate Source",
args: args{
ctx: ctx,
source: validSource,
},
want: models.Source{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateSource(tt.args.ctx, tt.args.source)
if (err != nil) != tt.wantErr {
t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateSource() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateSourceInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Sources to test with
validSources := test.GenerateRandomSources(5)
// --
// -- -- Tests
type args struct {
ctx context.Context
source []models.Source
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Sources",
args: args{
ctx: ctx,
source: validSources,
batchSize: len(validSources),
},
wantErr: false,
},
{
name: "Test 02: Duplicate Sources",
args: args{
ctx: ctx,
source: validSources,
batchSize: len(validSources),
},
wantErr: true,
},
{
name: "Test 03: Nil Sources",
args: args{
ctx: ctx,
source: nil,
batchSize: len(validSources),
},
wantErr: true,
},
{
name: "Test 04: Empty Sources",
args: args{
ctx: ctx,
source: []models.Source{},
batchSize: len(validSources),
},
wantErr: true,
},
{
name: "Test 08: Empty Batch Size",
args: args{
ctx: ctx,
source: []models.Source{},
batchSize: 0,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateSourceInBatch(tt.args.ctx, tt.args.source, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateSourceInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestUpdateSource(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create Updates models for UserSource
validUpdateSource := validSource
validUpdateSource.DisplayName = "eeeee"
validUpdateSource.Domain = "aaaaa"
validUpdateSource.Icon = "nnnn"
invalidUpdateSource := models.Source{}
// --
// -- -- Tests
type args struct {
ctx context.Context
source models.Source
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for Source",
args: args{
ctx: ctx,
source: validUpdateSource,
},
wantErr: false,
},
{
name: "Test 02: Invalid Update for Source",
args: args{
ctx: ctx,
source: invalidUpdateSource,
},
wantErr: true,
},
{
name: "Test 03: Empty ID for Update for Source",
args: args{
ctx: ctx,
source: models.Source{BaseModel: models.BaseModel[models.SourceID]{ID: ""}},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdateSource(tt.args.ctx, tt.args.source); (err != nil) != tt.wantErr {
t.Errorf("UpdateSource() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetSourceByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.SourceID
}
tests := []struct {
name string
args args
want models.Source
wantErr bool
}{
{
name: "Test 01: Valid Source ID",
args: args{
ctx: ctx,
id: validSource.ID,
},
want: validSource,
wantErr: false,
},
{
name: "Test 03: Empty SourceID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short SourceID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetSourceByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetSourceByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkSourceID(got, tt.want) {
t.Errorf("GetSourceByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetSourceByDomain(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
sourceDomain models.SourceDomain
}
tests := []struct {
name string
args args
want models.Source
wantErr bool
}{
{
name: "Test 01: Valid SourceURL",
args: args{
ctx: ctx,
sourceDomain: validSource.Domain,
},
want: validSource,
wantErr: false,
},
{
name: "Test 02: Empty SourceURL",
args: args{
ctx: ctx,
sourceDomain: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetSourceByDomain(tt.args.ctx, tt.args.sourceDomain)
if (err != nil) != tt.wantErr {
t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkSourceID(got, tt.want) {
t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeleteSource(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Source to test with
validSource := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: models.SourceID(fmt.Sprintf("%025s", "Source1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.SourceID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid Source",
args: args{
ctx: ctx,
id: validSource.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed Source",
args: args{
ctx: ctx,
id: validSource.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty SourceID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short SourceID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteSource(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeleteSource() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkSourceID(got models.Source, want models.Source) bool {
if got.ID != want.ID {
return false
}
return true
}

View File

@ -2,133 +2,19 @@ package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
)
func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) (models.Tag, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTag")
defer span.End()
type Tag interface {
CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error
localLogger = localLogger.WithFields(log.Fields{
"tag_name": tagName,
"tag_type": tagType,
})
CreateTagInBatchAndUpdate(ctx context.Context, tags []models.Tag, batchSize int) error
span.SetAttributes(
attribute.String("tag_name", string(tagName)),
attribute.String("tag_type", string(tagType)),
)
// GetAllTags retrieves all tags.
GetAllTags(ctx context.Context) ([]models.Tag, error)
utils.HandleEvent(span, localLogger, "Starting tag creation")
GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error)
if client == nil {
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagName == "" {
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
}
if tagType == "" {
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagTypeIsEmpty})
}
tag := models.Tag{
Name: tagName,
Type: tagType,
}
result := client.WithContext(ctx).Create(&tag)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.Tag{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag created successfully")
return tag, nil
}
func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_count": len(tags),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("tag_count", len(tags)),
)
utils.HandleEvent(span, localLogger, "Starting batch tag creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tags == nil || len(tags) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&tags, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Batch tags created successfully")
return nil
}
func DeleteTag(ctx context.Context, tagName models.TagName) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_name": tagName,
})
span.SetAttributes(
attribute.String("tag_name", string(tagName)),
)
utils.HandleEvent(span, localLogger, "Starting tag deletion")
var tag models.Tag
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagName) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
}
result := client.WithContext(ctx).Delete(&tag, tagName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag deleted successfully")
return nil
DeleteTag(ctx context.Context, tagName models.AnthroveTagName) error
}

View File

@ -1,134 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAlias")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_alias_name": tagAliasName,
"tag_name": tagName,
})
span.SetAttributes(
attribute.String("tag_alias_name", string(tagAliasName)),
attribute.String("tag_name", string(tagName)),
)
utils.HandleEvent(span, localLogger, "Starting tag alias creation")
if client == nil {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagAliasName == "" {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty})
}
if tagName == "" {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
}
tagAlias := models.TagAlias{
Name: tagAliasName,
TagID: tagName,
}
result := client.WithContext(ctx).Create(&tagAlias)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag alias created successfully")
return tagAlias, nil
}
func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_aliases_count": len(tagsAliases),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("tag_aliases_count", len(tagsAliases)),
)
utils.HandleEvent(span, localLogger, "Starting batch tag alias creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagsAliases == nil || len(tagsAliases) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Batch tags aliases created successfully")
return nil
}
func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagAlias")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_alias_name": tagAliasName,
})
span.SetAttributes(
attribute.String("tag_alias_name", string(tagAliasName)),
)
utils.HandleEvent(span, localLogger, "Starting tag alias deletion")
var tagAlias models.TagAlias
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagAliasName) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
}
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag alias deleted successfully")
return nil
}

View File

@ -1,292 +0,0 @@
package database
import (
"context"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreateTagAlias(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tag to test with
validTag := models.Tag{
Name: "valid_tag",
Type: models.General,
}
validTag, err = CreateTag(ctx, validTag.Name, validTag.Type)
if err != nil {
t.Fatalf("CreateTag err: %v", err)
}
// --
// -- Create TagAlias to test with
validTagAlias := models.TagAlias{
Name: "valid_tag_alias_name",
TagID: validTag.Name,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagAliasName models.TagAliasName
tagName models.TagName
}
tests := []struct {
name string
args args
want models.TagAlias
wantErr bool
}{
{
name: "Test 01: Valid tagAlias",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
tagName: validTag.Name,
},
want: validTagAlias,
wantErr: false,
},
{
name: "Test 02: Duplicate tagAlias",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
tagName: validTag.Name,
},
want: models.TagAlias{},
wantErr: true,
},
{
name: "Test 03: tagAlias name is empty",
args: args{
ctx: ctx,
tagAliasName: "",
tagName: validTag.Name,
},
want: models.TagAlias{},
wantErr: true,
},
{
name: "Test 04: tagName name is empty",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
tagName: "",
},
want: models.TagAlias{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateTagAlias(tt.args.ctx, tt.args.tagAliasName, tt.args.tagName)
if (err != nil) != tt.wantErr {
t.Errorf("CreateTagAlias() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateTagAlias() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateTagAliasInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tags to test with
validTags := test.GenerateRandomTags(5)
err = CreateTagInBatch(ctx, validTags, len(validTags))
if err != nil {
t.Fatalf("CreateTags err: %v", err)
}
// --
// -- Create TagAlias to test with
validTagGroup := test.GenerateRandomTagAlias(validTags, 5)
// --
// -- -- Tests
type args struct {
ctx context.Context
tagsAliases []models.TagAlias
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid TagAliases",
args: args{
ctx: ctx,
tagsAliases: validTagGroup,
batchSize: len(validTags),
},
wantErr: false,
},
{
name: "Test 02: Duplicate TagAliases",
args: args{
ctx: ctx,
tagsAliases: validTagGroup,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 03: Nil TagAliases",
args: args{
ctx: ctx,
tagsAliases: nil,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 04: Empty TagAliases",
args: args{
ctx: ctx,
tagsAliases: []models.TagAlias{},
batchSize: len(validTags),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTagAliasInBatch(tt.args.ctx, tt.args.tagsAliases, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateTagAliasInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeleteTagAlias(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tag to test with
validTag := models.Tag{
Name: "valid_tag",
Type: models.General,
}
validTag, err = CreateTag(ctx, validTag.Name, validTag.Type)
if err != nil {
t.Fatalf("CreateTag err: %v", err)
}
// --
// -- Create TagAlias to test with
validTagAlias := models.TagAlias{
Name: "valid_tag_group_name",
TagID: validTag.Name,
}
validTagAlias, err = CreateTagAlias(ctx, validTagAlias.Name, validTagAlias.TagID)
if err != nil {
t.Fatalf("CreateTagGroup err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagAliasName models.TagAliasName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid TagAlias",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
},
wantErr: false,
},
{
name: "Test 02: Not existing TagAlias",
args: args{
ctx: ctx,
tagAliasName: validTagAlias.Name,
},
wantErr: false,
},
{
name: "Test 03: Empty TagAliasName ",
args: args{
ctx: ctx,
tagAliasName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteTagAlias(tt.args.ctx, tt.args.tagAliasName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -1,137 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) (models.TagGroup, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagGroup")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
})
span.SetAttributes(
attribute.String("tag_group_name", string(tagGroupName)),
attribute.String("tag_name", string(tagName)),
)
utils.HandleEvent(span, localLogger, "Starting tag group creation")
if client == nil {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagGroupName == "" {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty})
}
if tagName == "" {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
}
tagGroup := models.TagGroup{
Name: tagGroupName,
TagID: tagName,
}
result := client.WithContext(ctx).Create(&tagGroup)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag group created successfully")
return tagGroup, nil
}
func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_groups_count": len(tagsGroups),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("tag_group_count", len(tagsGroups)),
)
utils.HandleEvent(span, localLogger, "Starting batch tag group creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagsGroups == nil || len(tagsGroups) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&tagsGroups, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag group created successfully")
return nil
}
func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagGroup")
defer span.End()
span.SetAttributes(
attribute.String("tag_group_name", string(tagGroupName)),
)
localLogger = localLogger.WithFields(log.Fields{
"tag_group_name": tagGroupName,
})
utils.HandleEvent(span, localLogger, "Starting tag group deletion")
var tagGroup models.TagGroup
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagGroupName) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagGroupNameIsEmpty})
}
result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag group deleted successfully")
return nil
}

View File

@ -1,292 +0,0 @@
package database
import (
"context"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreateTagGroup(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tag to test with
validTag := models.Tag{
Name: "valid_tag",
Type: models.General,
}
validTag, err = CreateTag(ctx, validTag.Name, validTag.Type)
if err != nil {
t.Fatalf("CreateTag err: %v", err)
}
// --
// -- Create TagGroup to test with
validTagGroup := models.TagGroup{
Name: "valid_tag_group_name",
TagID: validTag.Name,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagGroupName models.TagGroupName
tagName models.TagName
}
var tests = []struct {
name string
args args
want models.TagGroup
wantErr bool
}{
{
name: "Test 01: Valid TagGroup",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
tagName: validTag.Name,
},
want: validTagGroup,
wantErr: false,
},
{
name: "Test 02: Duplicate TagGroup",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
tagName: validTag.Name,
},
want: models.TagGroup{},
wantErr: true,
},
{
name: "Test 03: TagGroup name is empty",
args: args{
ctx: ctx,
tagGroupName: "",
tagName: validTag.Name,
},
want: models.TagGroup{},
wantErr: true,
},
{
name: "Test 04: tagName name is empty",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
tagName: "",
},
want: models.TagGroup{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateTagGroup(tt.args.ctx, tt.args.tagGroupName, tt.args.tagName)
if (err != nil) != tt.wantErr {
t.Errorf("CreateTagGroup() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateTagGroup() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateTagGroupInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tags to test with
validTags := test.GenerateRandomTags(5)
err = CreateTagInBatch(ctx, validTags, len(validTags))
if err != nil {
t.Fatalf("CreateTags err: %v", err)
}
// --
// -- Create TagGroup to test with
validTagGroup := test.GenerateRandomTagGroups(validTags, 5)
// --
// -- -- Tests
type args struct {
ctx context.Context
tagsGroups []models.TagGroup
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid TagGroups",
args: args{
ctx: ctx,
tagsGroups: validTagGroup,
batchSize: len(validTags),
},
wantErr: false,
},
{
name: "Test 02: Duplicate TagGroups",
args: args{
ctx: ctx,
tagsGroups: validTagGroup,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 03: Nil TagGroups",
args: args{
ctx: ctx,
tagsGroups: nil,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 04: Empty TagGroups",
args: args{
ctx: ctx,
tagsGroups: []models.TagGroup{},
batchSize: len(validTags),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTagGroupInBatch(tt.args.ctx, tt.args.tagsGroups, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateTagGroupInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeleteTagGroup(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tag to test with
validTag := models.Tag{
Name: "valid_tag",
Type: models.General,
}
validTag, err = CreateTag(ctx, validTag.Name, validTag.Type)
if err != nil {
t.Fatalf("CreateTag err: %v", err)
}
// --
// -- Create TagGroup to test with
validTagGroup := models.TagGroup{
Name: "valid_tag_group_name",
TagID: validTag.Name,
}
validTagGroup, err = CreateTagGroup(ctx, validTagGroup.Name, validTagGroup.TagID)
if err != nil {
t.Fatalf("CreateTagGroup err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagGroupName models.TagGroupName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid TagGroup",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
},
wantErr: false,
},
{
name: "Test 02: Not existing TagGroup",
args: args{
ctx: ctx,
tagGroupName: validTagGroup.Name,
},
wantErr: false,
},
{
name: "Test 03: Empty TagGroupName ",
args: args{
ctx: ctx,
tagGroupName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteTagGroup(tt.args.ctx, tt.args.tagGroupName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTagGroup() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -1,262 +0,0 @@
package database
import (
"context"
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreateTag(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tag to test with
validTag := models.Tag{
Name: "valid_tag",
Type: models.General,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagName models.TagName
tagType models.TagType
}
tests := []struct {
name string
args args
want models.Tag
wantErr bool
}{
{
name: "Test 01: Valid tag",
args: args{
ctx: ctx,
tagType: validTag.Type,
tagName: validTag.Name,
},
want: validTag,
wantErr: false,
},
{
name: "Test 02: Duplicate tag",
args: args{
ctx: ctx,
tagType: validTag.Type,
tagName: validTag.Name,
},
want: models.Tag{},
wantErr: true,
},
{
name: "Test 03: tagName is empty",
args: args{
ctx: ctx,
tagType: "",
tagName: validTag.Name,
},
want: models.Tag{},
wantErr: true,
},
{
name: "Test 04: tagName name is empty",
args: args{
ctx: ctx,
tagType: validTag.Type,
tagName: "",
},
want: models.Tag{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateTag(tt.args.ctx, tt.args.tagName, tt.args.tagType)
if (err != nil) != tt.wantErr {
t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateTag() got = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateTagInBatch(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tags to test with
validTags := test.GenerateRandomTags(5)
// --
// -- -- Tests
type args struct {
ctx context.Context
tags []models.Tag
batchSize int
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Tags",
args: args{
ctx: ctx,
tags: validTags,
batchSize: len(validTags),
},
wantErr: false,
},
{
name: "Test 02: Duplicate Tags",
args: args{
ctx: ctx,
tags: validTags,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 03: Nil Tags",
args: args{
ctx: ctx,
tags: nil,
batchSize: len(validTags),
},
wantErr: true,
},
{
name: "Test 04: Empty Tags",
args: args{
ctx: ctx,
tags: []models.Tag{},
batchSize: len(validTags),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := CreateTagInBatch(tt.args.ctx, tt.args.tags, tt.args.batchSize); (err != nil) != tt.wantErr {
t.Errorf("CreateTagInBatch() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDeleteTag(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Tag to test with
validTag := models.Tag{
Name: "valid_tag",
Type: models.General,
}
validTag, err = CreateTag(ctx, validTag.Name, validTag.Type)
if err != nil {
t.Fatalf("CreateTag err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
tagName models.TagName
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Tag",
args: args{
ctx: ctx,
tagName: validTag.Name,
},
wantErr: false,
},
{
name: "Test 02: Not existing Tag",
args: args{
ctx: ctx,
tagName: validTag.Name,
},
wantErr: false,
},
{
name: "Test 03: Empty TagName ",
args: args{
ctx: ctx,
tagName: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteTag(tt.args.ctx, tt.args.tagName); (err != nil) != tt.wantErr {
t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

19
pkg/database/tagalias.go Normal file
View File

@ -0,0 +1,19 @@
package database
import (
"context"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
)
type TagAlias interface {
CreateTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName, tagID models.AnthroveTagID) error
CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error
GetAllTagAlias(ctx context.Context) ([]models.TagAlias, error)
GetAllTagAliasByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagAlias, error)
DeleteTagAlias(ctx context.Context, tagAliasName models.AnthroveTagAliasName) error
}

19
pkg/database/taggroup.go Normal file
View File

@ -0,0 +1,19 @@
package database
import (
"context"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
)
type TagGroup interface {
CreateTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName, tagID models.AnthroveTagID) error
CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error
GetAllTagGroup(ctx context.Context) ([]models.TagGroup, error)
GetAllTagGroupByTag(ctx context.Context, tagID models.AnthroveTagID) ([]models.TagGroup, error)
DeleteTagGroup(ctx context.Context, tagGroupName models.AnthroveTagGroupName) error
}

View File

@ -2,107 +2,41 @@ package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
)
func CreateUser(ctx context.Context, user models.User) (models.User, error) {
type User interface {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUser")
defer span.End()
// CreateUserWithRelationToSource adds a user with a relation to a source.
CreateUserWithRelationToSource(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error
localLogger = localLogger.WithFields(log.Fields{
"user_id": user.ID,
})
// CreateReferenceBetweenUserAndPost links a user with a post.
CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error
span.SetAttributes(
attribute.String("user_id", string(user.ID)),
)
UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error
utils.HandleEvent(span, localLogger, "Starting user creation")
UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error
if client == nil {
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&user)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User created successfully")
return user, nil
}
func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserByID")
defer span.End()
span.SetAttributes(
attribute.String("user_id", string(id)),
)
localLogger = localLogger.WithFields(log.Fields{
"user_id": id,
})
utils.HandleEvent(span, localLogger, "Starting user retrieval")
var user models.User
if client == nil {
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).First(&user, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User retrieved successfully")
return user, nil
}
func DeleteUser(ctx context.Context, id models.UserID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUser")
defer span.End()
span.SetAttributes(
attribute.String("user_id", string(id)),
)
localLogger = localLogger.WithFields(log.Fields{
"user_id": id,
})
utils.HandleEvent(span, localLogger, "Starting user deletion")
var user models.User
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Delete(&user, id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User deleted successfully")
return nil
UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error
GetAllUsers(ctx context.Context) ([]models.User, error)
// GetUserFavoritesCount retrieves the count of a user's favorites.
GetUserFavoritesCount(ctx context.Context, anthroveUserID models.AnthroveUserID) (int64, error)
// GetAllUserSources retrieves the source links of a user.
GetAllUserSources(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error)
// GetUserSourceBySourceID retrieves a specified source link of a user.
GetUserSourceBySourceID(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (*models.UserSource, error)
// GetAllUserFavoritesWithPagination retrieves a user's favorite posts with pagination.
GetAllUserFavoritesWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error)
// GetAllTagsFromUser retrieves a user's tags through their favorite posts.
GetAllTagsFromUser(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error)
// CheckIfUserHasPostAsFavorite checks if a user-post link exists.
CheckIfUserHasPostAsFavorite(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID models.AnthrovePostID) (bool, error)
}

View File

@ -1,173 +0,0 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserSource")
defer span.End()
utils.HandleEvent(span, localLogger, "Starting user source creation")
if client == nil {
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&userSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
localLogger = localLogger.WithFields(log.Fields{
"user_source_id": userSource.ID,
})
span.SetAttributes(
attribute.String("user_source_id", string(userSource.SourceID)),
)
utils.HandleEvent(span, localLogger, "User source created successfully")
return userSource, nil
}
// UpdateUserSource updates the user source information in the database.
// Only a few parameter can be updated:
// - AccountID
// - ScrapeTimeInterval
// - AccountUsername
// - LastScrapeTime
// - AccountValidate
func UpdateUserSource(ctx context.Context, userSource models.UserSource) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserSource")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_source_id": userSource.ID,
})
span.SetAttributes(
attribute.String("user_source_id", string(userSource.ID)),
)
utils.HandleEvent(span, localLogger, "Starting user source update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(userSource.ID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.UserSourceIDIsEmpty})
}
updatedUserSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: userSource.ID,
},
ScrapeTimeInterval: userSource.ScrapeTimeInterval,
AccountUsername: userSource.AccountUsername,
AccountID: userSource.AccountID,
LastScrapeTime: userSource.LastScrapeTime,
AccountValidate: userSource.AccountValidate,
}
result := client.WithContext(ctx).Updates(&updatedUserSource)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User source updated successfully")
return nil
}
func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserSourceByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_source_id": id,
})
span.SetAttributes(
attribute.String("user_source_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get user source by ID")
var user models.UserSource
if client == nil {
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty})
}
if len(id) != 25 {
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIsWrongLength})
}
result := client.WithContext(ctx).First(&user, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, otterError.Database{Reason: otterError.NoDataFound})
}
return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User source retrieved successfully")
return user, nil
}
func DeleteUserSource(ctx context.Context, id models.UserSourceID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserSource")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_source_id": id,
})
span.SetAttributes(
attribute.String("user_source_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete user source")
var user models.UserSource
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIsWrongLength})
}
result := client.WithContext(ctx).Delete(&user, id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return result.Error
}
utils.HandleEvent(span, localLogger, "User source deleted successfully")
return nil
}

View File

@ -1,498 +0,0 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestCreateUserSource(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUSerSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
// --
// -- -- Tests
type args struct {
ctx context.Context
userSource models.UserSource
}
tests := []struct {
name string
args args
want models.UserSource
wantErr bool
}{
{
name: "Test 01: Valid User Source",
args: args{
ctx: ctx,
userSource: validUSerSource,
},
want: validUSerSource,
wantErr: false,
},
{
name: "Test 02: Invalid User Source",
args: args{
ctx: ctx,
userSource: models.UserSource{},
},
want: models.UserSource{},
wantErr: true,
},
{
name: "Test 03: Duplicate User Source",
args: args{
ctx: ctx,
userSource: validUSerSource,
},
want: models.UserSource{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateUserSource(tt.args.ctx, tt.args.userSource)
if (err != nil) != tt.wantErr {
t.Errorf("CreateUserSource() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateUserSource() got = %v, want %v", got, tt.want)
}
})
}
}
func TestUpdateUserSource(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUserSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUserSource, err = CreateUserSource(ctx, validUserSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
// --
// -- Create Updates models for UserSource
validUpdateSourceUser := validUserSource
validUpdateSourceUser.AccountID = "1234"
validUpdateSourceUser.ScrapeTimeInterval = "P2D"
validUpdateSourceUser.AccountUsername = "Update_Username"
validUpdateSourceUser.LastScrapeTime = time.Now()
validUpdateSourceUser.AccountValidate = true
invalidUpdateSourceUser := models.UserSource{}
// --
// -- -- Tests
type args struct {
ctx context.Context
userSource models.UserSource
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for UserSource",
args: args{
ctx: ctx,
userSource: validUpdateSourceUser,
},
wantErr: false,
},
{
name: "Test 02: Invalid Update for UserSource",
args: args{
ctx: ctx,
userSource: invalidUpdateSourceUser,
},
wantErr: true,
},
{
name: "Test 03: Empty ID for Update for UserSource",
args: args{
ctx: ctx,
userSource: models.UserSource{BaseModel: models.BaseModel[models.UserSourceID]{ID: ""}},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdateUserSource(tt.args.ctx, tt.args.userSource); (err != nil) != tt.wantErr {
t.Errorf("UpdateUserSource() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetUserSourceByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUserSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUserSource, err = CreateUserSource(ctx, validUserSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.UserSourceID
}
tests := []struct {
name string
args args
want models.UserSource
wantErr bool
}{
{
name: "Test 01: Valid UserSource ID",
args: args{
ctx: ctx,
id: validUserSource.ID,
},
want: validUserSource,
wantErr: false,
},
{
name: "Test 03: Empty UserSourceID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short UserSourceID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserSourceByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserSourceByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkUserSourceID(got, tt.want) {
t.Errorf("GetUserSourceByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeleteUserSource(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create User ot test with
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatalf("CreateUser err: %v", err)
}
// --
// -- Create Source to test with
validSource := models.Source{
DisplayName: "e621",
Domain: "e621.net",
Icon: "e621.net/icon.png",
}
validSource, err = CreateSource(ctx, validSource)
if err != nil {
t.Fatalf("CreateSource err: %v", err)
}
// --
// -- Create UserSource model
validUSerSource := models.UserSource{
BaseModel: models.BaseModel[models.UserSourceID]{
ID: models.UserSourceID(fmt.Sprintf("%025s", "UserSourceId1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
User: models.User{},
UserID: validUser.ID,
Source: models.Source{},
SourceID: validSource.ID,
ScrapeTimeInterval: "P1D",
AccountUsername: "marry",
AccountID: "poppens",
LastScrapeTime: time.Now(),
AccountValidate: false,
AccountValidationKey: "im-a-key",
}
validUSerSource, err = CreateUserSource(ctx, validUSerSource)
if err != nil {
t.Fatalf("CreateUserSource err: %v", err)
}
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.UserSourceID
}
var tests = []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid UserSource",
args: args{
ctx: ctx,
id: validUSerSource.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed UserSource",
args: args{
ctx: ctx,
id: validUSerSource.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty UserSourceID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short UserSourceID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteUserSource(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeleteUserSource() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkUserSourceID(got models.UserSource, want models.UserSource) bool {
if got.ID != want.ID {
return false
}
return true
}

View File

@ -1,219 +0,0 @@
package database
import (
"context"
"fmt"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)
func TestCreateUser(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// Setup Tests
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
// Tests
type args struct {
ctx context.Context
user models.User
}
tests := []struct {
name string
args args
want models.User
wantErr bool
}{
{
name: "Test 01: Create Valid User",
args: args{
ctx: ctx,
user: validUser,
},
want: validUser,
wantErr: false,
},
{
name: "Test 02: Duplicate User",
args: args{
ctx: ctx,
user: validUser,
},
want: models.User{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateUser(tt.args.ctx, tt.args.user)
if (err != nil) != tt.wantErr {
t.Errorf("CreateUser() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkUserID(got, tt.want) {
t.Errorf("CreateUser() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetUserByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// Setup Tests
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
invalidUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: "invalid"}}
validUser, err = CreateUser(ctx, validUser)
if err != nil {
logger.Fatal(err)
}
// Tests
type args struct {
ctx context.Context
id models.UserID
}
tests := []struct {
name string
args args
want models.User
wantErr bool
}{
{
name: "Test 01: Get Valid User",
args: args{
ctx: ctx,
id: validUser.ID,
},
want: validUser,
wantErr: false,
},
{
name: "Test 02: Get not existing User",
args: args{
ctx: ctx,
id: invalidUser.ID,
},
want: models.User{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetUserByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetUserByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkUserID(got, tt.want) {
t.Errorf("GetUserByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeleteUser(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// Setup Tests
validUser := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
_, err = CreateUser(ctx, validUser)
if err != nil {
t.Fatal(err)
}
// Test
type args struct {
ctx context.Context
id models.UserID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Existing User",
args: args{
ctx: ctx,
id: validUser.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existing User",
args: args{
ctx: ctx,
id: validUser.ID,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeleteUser(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeleteUser() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkUserID(got models.User, want models.User) bool {
if got.ID != want.ID {
return false
}
return true
}

View File

@ -1,19 +1,25 @@
package error
import "fmt"
type EntityAlreadyExists struct{}
const (
EntityAlreadyExists = "EntityAlreadyExists"
NoDataWritten = "NoDataWritten"
NoDataFound = "NoDataFound"
DatabaseIsNotConnected = "database is not connected"
DuplicateKey = "DuplicateKey"
)
type Database struct {
Reason string
func (e *EntityAlreadyExists) Error() string {
return "EntityAlreadyExists error"
}
func (e Database) Error() string {
return fmt.Sprintf("Database error: %s", e.Reason)
type NoDataWritten struct{}
func (e *NoDataWritten) Error() string {
return "NoDataWritten error"
}
type NoDataFound struct{}
func (e *NoDataFound) Error() string {
return "NoDataFound error"
}
type NoRelationCreated struct{}
func (e *NoRelationCreated) Error() string {
return "relationship creation error"
}

View File

@ -2,26 +2,79 @@ package error
import "testing"
func TestDatabase_Error(t *testing.T) {
type fields struct {
Reason string
}
func TestEntityAlreadyExists_Error(t *testing.T) {
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Reason",
fields: fields{Reason: "TEST ERROR"},
want: "Database error: TEST ERROR",
name: "Test : Valid error String",
want: "EntityAlreadyExists error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Database{
Reason: tt.fields.Reason,
}
e := &EntityAlreadyExists{}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}
func TestNoDataFound_Error(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "Test : Valid error String",
want: "NoDataFound error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &NoDataFound{}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}
func TestNoDataWritten_Error(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "Test : Valid error String",
want: "NoDataWritten error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &NoDataWritten{}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}
func TestNoRelationCreated_Error(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "Test : Valid error String",
want: "relationship creation error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &NoRelationCreated{}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}

View File

@ -3,44 +3,11 @@ package error
import "fmt"
const (
SourceListIsEmpty = "sourceList cannot be empty"
SourceIDIsEmpty = "SourceID cannot be empty"
SourceIDIsWrongLength = "sourceID has the wrong length"
SourceDomainIsEmpty = "source Domain cannot be empty"
UserSourceIDIsEmpty = "userSourceID cannot be empty"
UserSourceIsWrongLength = "userSourceID has the wrong length"
TagNameIsEmpty = "tagName cannot be empty"
TagTypeIsEmpty = "tagType cannot be empty"
TagListIsEmpty = "tagList cannot be empty"
TagAliasNameIsEmpty = "tagAliasName cannot be empty"
TagAliasListIsEmpty = "tagAliasList cannot be empty"
TagGroupListIsEmpty = "tagGroupList cannot be empty"
TagGroupNameIsEmpty = "tagGroupName cannot be empty"
UserFavoriteListIsEmpty = "userFavoriteList cannot be empty"
UserFavoriteIDIsEmpty = "userFavoriteID cannot be empty"
UserFavoriteIsWrongLength = "userFavoriteID has the wrong length"
PostListIsEmpty = "userFavoriteList cannot be empty"
PostIDIsEmpty = "PostID cannot be empty"
PostIDIsWrongLength = "PostID has the wrong length"
BatchSizeIsEmpty = "batchSize cannot be empty"
PoolIDIsEmpty = "PoolID cannot be empty"
PoolIDIsWrongLength = "PoolID has the wrong length"
PoolURLIsEmpty = "PoolURL cannot be empty"
ScrapeTaskIDIsEmpty = "ScrapeTaskID cannot be empty"
ScrapeTaskIDIsWrongLength = "ScrapeTaskID has the wrong length, needs to be 25 characters long"
PostReportIDIsEmpty = "PostReportID cannot be empty"
PostReportIDIsWrongLength = "PostReportID has the wrong length"
AnthroveUserIDIsEmpty = "anthrovePostID cannot be empty"
AnthroveUserIDToShort = "anthrovePostID needs to be 25 characters long"
AnthroveSourceIDEmpty = "anthroveSourceID cannot be empty"
AnthroveSourceIDToShort = "anthroveSourceID needs to be 25 characters long"
AnthroveTagIDEmpty = "tagID cannot be empty"
)
type EntityValidationFailed struct {

5
pkg/models/api.go Normal file
View File

@ -0,0 +1,5 @@
package models
type FavoriteList struct {
Posts []Post `json:"posts,omitempty"`
}

View File

@ -9,5 +9,4 @@ type DatabaseConfig struct {
SSL bool `env:"DB_SSL,required" envDefault:"true"`
Timezone string `env:"DB_TIMEZONE,required" envDefault:"Europe/Berlin"`
Debug bool `env:"DB_DEBUG" envDefault:"false"`
Migrate bool
}

View File

@ -2,39 +2,20 @@ package models
import "time"
type (
UserID string
PostID string
PostURL string
PoolID string
type AnthroveUserID string
type AnthrovePostID string
type AnthroveSourceID string
type AnthroveSourceDomain string
type AnthrovePostURL string
type AnthroveTagGroupName string
type AnthroveTagAliasName string
type AnthroveTagID string
type AnthroveScrapeTimeInterval int
type AnthroveUserLastScrapeTime time.Time
type AnthroveTagName string
SourceID string
SourceDomain string
TagName string
TagGroupName string
TagAliasName string
ScrapeTimeInterval int
UserLastScrapeTime time.Time
Rating string
TagType string
UserSourceID string
UserFavoriteID string
ScrapeTaskID string
PostReportID string
ReportType string
ReportState string
)
const (
MaxPageSizeLimit = 100
DefaultPageSize = 50
)
type Rating string
type TagType string
const (
SFW Rating = "safe"
@ -54,21 +35,6 @@ const (
Copyright TagType = "copyright"
)
const (
Duplicate ReportType = "duplicate"
MissingData ReportType = "missing_data"
RatingAbuse ReportType = "rating_abuse"
IllegalContent ReportType = "illegal_content"
)
const (
PendingUnclaimed ReportState = "pending_unclaimed"
Pending ReportState = "pending"
Approved ReportState = "approved"
Partial ReportState = "partial"
Rejected ReportState = "rejected"
)
func (r *Rating) Convert(e621Rating string) {
switch e621Rating {
case "e":

View File

@ -8,13 +8,13 @@ import (
)
type ID interface {
UserID | SourceID | PostID | UserSourceID | UserFavoriteID | PoolID | PostReportID
AnthroveUserID | AnthroveSourceID | AnthrovePostID
}
type BaseModel[T ID] struct {
ID T `json:"id" gorm:"primaryKey"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}

View File

@ -42,8 +42,8 @@ func TestBaseModel_BeforeCreate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
base := &BaseModel[PostID]{
ID: PostID(tt.fields.ID),
base := &BaseModel[AnthrovePostID]{
ID: AnthrovePostID(tt.fields.ID),
CreatedAt: tt.fields.CreatedAt,
UpdatedAt: tt.fields.UpdatedAt,
DeletedAt: tt.fields.DeletedAt,

View File

@ -1,42 +0,0 @@
package models
type PoolCategory string
const (
Series PoolCategory = "series"
Collection PoolCategory = "collection"
)
type Pool struct {
BaseModel[PoolID]
Name string `json:"name" gorm:"column:name;type:varchar(25)"`
Category PoolCategory `json:"category" gorm:"column:category;type:pool_category;type:enum('series', 'collection')"`
Posts []PoolPost `json:"posts" gorm:"foreignKey:PoolID"`
Sources []PoolReference `json:"sources" gorm:"foreignKey:PoolID"`
}
func (Pool) TableName() string {
return "Pool"
}
type PoolPost struct {
PoolID PoolID `json:"pool_id" gorm:"column:pool_id;primaryKey"`
Pool Pool `json:"-"`
PostID PostID `json:"post_id" gorm:"column:post_id;primaryKey"`
OrderPosition int `json:"order_position" gorm:"column:order_position;default:0"`
}
func (PoolPost) TableName() string {
return "PoolPost"
}
type PoolReference struct {
PoolID PoolID `json:"pool_id" gorm:"column:pool_id;primaryKey"`
Pool Pool `json:"-"`
SourceID SourceID `json:"source_id" gorm:"column:source_id;primaryKey"`
URL string `json:"url" gorm:"column:url;primaryKey"`
}
func (PoolReference) TableName() string {
return "PoolReference"
}

View File

@ -1,27 +0,0 @@
package models
import "testing"
func TestPool_TableName(t *testing.T) {
post := Pool{}
expectedTableName := "Pool"
if tableName := post.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
}
}
func TestPoolPost_TableName(t *testing.T) {
post := PoolPost{}
expectedTableName := "PoolPost"
if tableName := post.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
}
}
func TestPoolReference_TableName(t *testing.T) {
post := PoolReference{}
expectedTableName := "PoolReference"
if tableName := post.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
}
}

View File

@ -2,10 +2,10 @@ package models
// Post model
type Post struct {
BaseModel[PostID]
BaseModel[AnthrovePostID]
Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"`
Tags []Tag `json:"tags,omitempty" gorm:"many2many:post_tags;"`
Favorites []UserFavorite `json:"-" gorm:"foreignKey:PostID"`
Tags []Tag `json:"-" gorm:"many2many:post_tags;"`
Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"`
References []PostReference `json:"references" gorm:"foreignKey:PostID"`
}

View File

@ -3,9 +3,42 @@ package models
import "testing"
func TestPostReference_TableName(t *testing.T) {
postReference := PostReference{}
expectedTableName := "PostReference"
if tableName := postReference.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
type fields struct {
PostID string
SourceID string
URL string
SourcePostID string
FullFileURL string
PreviewFileURL string
SampleFileURL string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: PostReference",
fields: fields{},
want: "PostReference",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
po := PostReference{
PostID: tt.fields.PostID,
SourceID: tt.fields.SourceID,
URL: tt.fields.URL,
PostReferenceConfig: PostReferenceConfig{
SourcePostID: tt.fields.SourcePostID,
FullFileURL: tt.fields.FullFileURL,
PreviewFileURL: tt.fields.PreviewFileURL,
SampleFileURL: tt.fields.SampleFileURL,
},
}
if got := po.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,16 +0,0 @@
package models
type PostReport struct {
BaseModel[PostReportID]
PostID PostID `json:"post_id"`
ReportBy UserID `json:"report_by"`
ReportDescription string `json:"report_description"`
AuditBy *UserID `json:"audit_by"`
AuditDescription string `json:"audit_description"`
ReportType ReportType `json:"report_type" gorm:"type:enum('duplicate','missing_data','rating_abuse','illegal_content')"`
ReportState ReportState `json:"report_state" gorm:"type:enum('pending_unclaimed', 'pending', 'approved', 'partial', 'rejected')"`
}
func (PostReport) TableName() string {
return "PostReport"
}

View File

@ -1,11 +0,0 @@
package models
import "testing"
func TestPostReport_TableName(t *testing.T) {
postReport := PostReport{}
expectedTableName := "PostReport"
if tableName := postReport.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
}
}

View File

@ -3,9 +3,36 @@ package models
import "testing"
func TestPost_TableName(t *testing.T) {
post := Post{}
expectedTableName := "Post"
if tableName := post.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
type fields struct {
BaseModel BaseModel[AnthrovePostID]
Rating Rating
Tags []Tag
Favorites []UserFavorites
References []PostReference
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name Post",
fields: fields{},
want: "Post",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
po := Post{
BaseModel: tt.fields.BaseModel,
Rating: tt.fields.Rating,
Tags: tt.fields.Tags,
Favorites: tt.fields.Favorites,
References: tt.fields.References,
}
if got := po.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,19 +0,0 @@
package models
import (
"time"
)
type ScrapeHistory struct {
ScrapeTaskID ScrapeTaskID `json:"scrape_task_id" gorm:"primaryKey"`
UserSourceID UserSourceID `json:"user_source_id" gorm:""`
CreatedAt time.Time `json:"created_at" gorm:""`
FinishedAt time.Time `json:"finished_at" gorm:""`
Error string `json:"error" gorm:"null"`
AddedPosts int `json:"added_posts" gorm:"not null"`
DeletedPosts int `json:"deleted_posts" gorm:"not null"`
}
func (ScrapeHistory) TableName() string {
return "ScrapeHistory"
}

View File

@ -1,11 +0,0 @@
package models
import "testing"
func TestScrapeHistory_TableName(t *testing.T) {
scrapeHistory := ScrapeHistory{}
expectedTableName := "ScrapeHistory"
if tableName := scrapeHistory.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
}
}

View File

@ -2,9 +2,9 @@ package models
// Source model
type Source struct {
BaseModel[SourceID]
BaseModel[AnthroveSourceID]
DisplayName string `json:"display_name" `
Domain SourceDomain `json:"domain" gorm:"not null;unique"`
Domain string `json:"domain" gorm:"not null;unique"`
Icon string `json:"icon" gorm:"not null"`
UserSources []UserSource `json:"-" gorm:"foreignKey:SourceID"`
References []PostReference `json:"references" gorm:"foreignKey:SourceID"`

View File

@ -3,9 +3,38 @@ package models
import "testing"
func TestSource_TableName(t *testing.T) {
source := Source{}
expectedTableName := "Source"
if tableName := source.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
type fields struct {
BaseModel BaseModel[AnthroveSourceID]
DisplayName string
Domain string
Icon string
UserSources []UserSource
References []PostReference
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name Source",
fields: fields{},
want: "Source",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
so := Source{
BaseModel: tt.fields.BaseModel,
DisplayName: tt.fields.DisplayName,
Domain: tt.fields.Domain,
Icon: tt.fields.Icon,
UserSources: tt.fields.UserSources,
References: tt.fields.References,
}
if got := so.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -2,11 +2,11 @@ package models
// Tag models
type Tag struct {
Name TagName `json:"name" gorm:"primaryKey"`
Name string `json:"name" gorm:"primaryKey"`
Type TagType `json:"type" gorm:"column:tag_type"`
Aliases []TagAlias `json:"aliases,omitempty" gorm:"foreignKey:TagID"`
Groups []TagGroup `json:"groups,omitempty" gorm:"foreignKey:TagID"`
Posts []Post `json:"posts,omitempty" gorm:"many2many:post_tags;"`
Aliases []TagAlias `json:"aliases" gorm:"foreignKey:TagID"`
Groups []TagGroup `json:"groups" gorm:"foreignKey:TagID"`
Posts []Post `json:"posts" gorm:"many2many:post_tags;"`
}
func (Tag) TableName() string {
@ -15,8 +15,8 @@ func (Tag) TableName() string {
// TagAlias model
type TagAlias struct {
Name TagAliasName `json:"name" gorm:"primaryKey"`
TagID TagName `json:"tag_id"`
Name string `json:"name" gorm:"primaryKey"`
TagID string `json:"tag_id"`
}
func (TagAlias) TableName() string {
@ -25,8 +25,8 @@ func (TagAlias) TableName() string {
// TagGroup model
type TagGroup struct {
Name TagGroupName `json:"name" gorm:"primaryKey"`
TagID TagName `json:"tag_id"`
Name string `json:"name" gorm:"primaryKey"`
TagID string `json:"tag_id"`
}
func (TagGroup) TableName() string {

View File

@ -3,25 +3,94 @@ package models
import "testing"
func TestTagAlias_TableName(t *testing.T) {
tagAlias := TagAlias{}
expectedTableName := "TagAlias"
if tableName := tagAlias.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
type fields struct {
Name string
TagID string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is Name TagAlias",
fields: fields{},
want: "TagAlias",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ta := TagAlias{
Name: tt.fields.Name,
TagID: tt.fields.TagID,
}
if got := ta.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}
func TestTagGroup_TableName(t *testing.T) {
tagGroup := TagGroup{}
expectedTableName := "TagGroup"
if tableName := tagGroup.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
type fields struct {
Name string
TagID string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name TagGroup",
fields: fields{},
want: "TagGroup",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ta := TagGroup{
Name: tt.fields.Name,
TagID: tt.fields.TagID,
}
if got := ta.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}
func TestTags_TableName(t *testing.T) {
tag := Tag{}
expectedTableName := "Tag"
if tableName := tag.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
func TestTag_TableName(t *testing.T) {
type fields struct {
Name string
Type TagType
Aliases []TagAlias
Groups []TagGroup
Posts []Post
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name Tag",
fields: fields{},
want: "Tag",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ta := Tag{
Name: tt.fields.Name,
Type: tt.fields.Type,
Aliases: tt.fields.Aliases,
Groups: tt.fields.Groups,
Posts: tt.fields.Posts,
}
if got := ta.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -2,8 +2,8 @@ package models
// User model
type User struct {
BaseModel[UserID]
Favorites []UserFavorite `json:"-" gorm:"foreignKey:UserID"`
BaseModel[AnthroveUserID]
Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"`
Sources []UserSource `json:"-" gorm:"foreignKey:UserID"`
}

View File

@ -1,13 +1,13 @@
package models
type UserFavorite struct {
BaseModel[UserFavoriteID]
UserID UserID `json:"user_id"`
PostID PostID `json:"post_id"`
UserSourceID UserSourceID `json:"user_source_id"`
UserSource UserSource `json:"-" gorm:"foreignKey:ID;references:UserSourceID"`
import "time"
type UserFavorites struct {
UserID string `json:"user_id" gorm:"primaryKey"`
PostID string `json:"post_id" gorm:"primaryKey"`
CreatedAt time.Time `json:"-"`
}
func (UserFavorite) TableName() string {
func (UserFavorites) TableName() string {
return "UserFavorites"
}

View File

@ -1,11 +1,37 @@
package models
import "testing"
import (
"testing"
"time"
)
func TestUserFavorite_TableName(t *testing.T) {
userFavorite := UserFavorite{}
expectedTableName := "UserFavorites"
if tableName := userFavorite.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
type fields struct {
UserID string
PostID string
CreatedAt time.Time
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name UserFavorites",
fields: fields{},
want: "UserFavorites",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
us := UserFavorites{
UserID: tt.fields.UserID,
PostID: tt.fields.PostID,
CreatedAt: tt.fields.CreatedAt,
}
if got := us.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -3,17 +3,16 @@ package models
import "time"
type UserSource struct {
BaseModel[UserSourceID]
User User `json:"user" gorm:"foreignKey:ID;references:UserID"`
UserID UserID `json:"user_id"`
UserID string `json:"user_id" gorm:"primaryKey"`
Source Source `json:"source" gorm:"foreignKey:ID;references:SourceID"`
SourceID SourceID `json:"source_id"`
SourceID string `json:"source_id" gorm:"primaryKey"`
ScrapeTimeInterval string `json:"scrape_time_interval"`
AccountUsername string `json:"account_username"`
AccountID string `json:"account_id"`
LastScrapeTime time.Time `json:"last_scrape_time"`
AccountValidate bool `json:"account_validate"`
AccountValidationKey string `json:"account_validation_key"`
AccountValidationKey string `json:"-"`
}
func (UserSource) TableName() string {

View File

@ -3,9 +3,40 @@ package models
import "testing"
func TestUserSource_TableName(t *testing.T) {
userSource := UserSource{}
expectedTableName := "UserSource"
if tableName := userSource.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
type fields struct {
User User
UserID string
Source Source
SourceID string
ScrapeTimeInterval string
AccountUsername string
AccountID string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name UserSource",
fields: fields{},
want: "UserSource",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
us := UserSource{
User: tt.fields.User,
UserID: tt.fields.UserID,
Source: tt.fields.Source,
SourceID: tt.fields.SourceID,
ScrapeTimeInterval: tt.fields.ScrapeTimeInterval,
AccountUsername: tt.fields.AccountUsername,
AccountID: tt.fields.AccountID,
}
if got := us.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -3,9 +3,32 @@ package models
import "testing"
func TestUser_TableName(t *testing.T) {
user := User{}
expectedTableName := "User"
if tableName := user.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
type fields struct {
BaseModel BaseModel[AnthroveUserID]
Favorites []UserFavorites
Sources []UserSource
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1: Is name User",
fields: fields{},
want: "User",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
us := User{
BaseModel: tt.fields.BaseModel,
Favorites: tt.fields.Favorites,
Sources: tt.fields.Sources,
}
if got := us.TableName(); got != tt.want {
t.Errorf("TableName() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,151 +0,0 @@
package test
import (
"fmt"
"math/rand"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"github.com/davecgh/go-spew/spew"
gonanoid "github.com/matoous/go-nanoid/v2"
"gorm.io/gorm"
)
func GenerateRandomTags(num int) []models.Tag {
var tags []models.Tag
tagTypes := []models.TagType{"general", "species", "character", "artist", "lore", "meta", "invalid", "copyright"}
for i := 0; i < num; i++ {
id, _ := gonanoid.New(10)
tagName := spew.Sprintf("tag_name_%s", id)
tagType := tagTypes[rand.Intn(len(tagTypes))]
tag := models.Tag{
Name: models.TagName(tagName),
Type: tagType,
}
tags = append(tags, tag)
}
return tags
}
func GenerateRandomTagGroups(tags []models.Tag, num int) []models.TagGroup {
var tagGroups []models.TagGroup
for i := 0; i < num; i++ {
id, _ := gonanoid.New(10)
groupName := fmt.Sprintf("tag_group_%s", id)
randomTag := tags[rand.Intn(len(tags))]
tagGroup := models.TagGroup{
Name: models.TagGroupName(groupName),
TagID: randomTag.Name,
}
tagGroups = append(tagGroups, tagGroup)
}
return tagGroups
}
func GenerateRandomTagAlias(tags []models.Tag, num int) []models.TagAlias {
var tagAliases []models.TagAlias
for i := 0; i < num; i++ {
id, _ := gonanoid.New(10)
groupName := fmt.Sprintf("tag_alias_%s", id)
randomTag := tags[rand.Intn(len(tags))]
tagAlias := models.TagAlias{
Name: models.TagAliasName(groupName),
TagID: randomTag.Name,
}
tagAliases = append(tagAliases, tagAlias)
}
return tagAliases
}
func GenerateRandomSources(num int) []models.Source {
var sources []models.Source
for i := 0; i < num; i++ {
id, _ := gonanoid.New(10)
displayName, _ := gonanoid.New(10)
domain, _ := gonanoid.New(10)
icon, _ := gonanoid.New(10)
id = spew.Sprintf("source_name_%s", id)
source := models.Source{
BaseModel: models.BaseModel[models.SourceID]{
ID: models.SourceID(id),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
DisplayName: displayName,
Domain: models.SourceDomain(domain),
Icon: icon,
UserSources: nil,
References: nil,
}
sources = append(sources, source)
}
return sources
}
func GenerateRandomPosts(num int) []models.Post {
var sources []models.Post
ratings := []models.Rating{"safe", "explicit", "questionable", "unknown"}
for i := 0; i < num; i++ {
id, _ := gonanoid.New(10)
id = spew.Sprintf("source_name_%s", id)
rating := ratings[rand.Intn(len(ratings))]
source := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(id),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: rating,
}
sources = append(sources, source)
}
return sources
}
func GenerateRandomUserFavorites(userID models.UserID, postID models.PostID, userSourceID models.UserSourceID, num int) []models.UserFavorite {
var userFavorites []models.UserFavorite
for i := 0; i < num; i++ {
id, _ := gonanoid.New(6)
id = spew.Sprintf("user_favorite_name_%s", id)
source := models.UserFavorite{
BaseModel: models.BaseModel[models.UserFavoriteID]{
ID: models.UserFavoriteID(id),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
UserID: userID,
PostID: postID,
UserSourceID: userSourceID,
}
userFavorites = append(userFavorites, source)
}
return userFavorites
}

View File

@ -8,14 +8,13 @@ import (
"strings"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
migrate "github.com/rubenv/sql-migrate"
postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
_ "github.com/lib/pq"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
@ -29,7 +28,8 @@ const (
func StartPostgresContainer(ctx context.Context) (*postgrescontainer.PostgresContainer, *gorm.DB, error) {
pgContainer, err := postgrescontainer.Run(ctx, "postgres:alpine",
pgContainer, err := postgrescontainer.RunContainer(ctx,
testcontainers.WithImage("postgres:alpine"),
postgrescontainer.WithDatabase(databaseName),
postgrescontainer.WithUsername(databaseUser),
postgrescontainer.WithPassword(databasePassword),
@ -81,22 +81,20 @@ func migrateDatabase(connectionString string) error {
func getGormDB(connectionString string) (*gorm.DB, error) {
return gorm.Open(postgres.Open(connectionString), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
TranslateError: true,
})
}
func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgrescontainer.PostgresContainer) (models.DatabaseConfig, error) {
func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgrescontainer.PostgresContainer) (*models.DatabaseConfig, error) {
var err error
var databaseConfig models.DatabaseConfig
connectionString, err := pgContainer.ConnectionString(ctx)
if err != nil {
return databaseConfig, err
return nil, err
}
connectionStringUrl, err := url.Parse(connectionString)
if err != nil {
return databaseConfig, err
return nil, err
}
split := strings.Split(connectionStringUrl.Host, ":")
@ -104,7 +102,7 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre
port, err := strconv.Atoi(split[1])
if err != nil {
return databaseConfig, err
return nil, err
}
database := strings.TrimPrefix(connectionStringUrl.Path, "/")
@ -112,7 +110,7 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre
username := connectionStringUrl.User.Username()
password, _ := connectionStringUrl.User.Password()
databaseConfig = models.DatabaseConfig{
return &models.DatabaseConfig{
Endpoint: host,
Username: username,
Password: password,
@ -121,8 +119,5 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre
SSL: false,
Timezone: "Europe/Berlin",
Debug: true,
Migrate: true,
}
return databaseConfig, nil
}, nil
}