Compare commits
No commits in common. "975e084b166930a9ae52d250d07cdbff4c681d35" and "c2ab455d3e653b8d7cab9e2df812a45d6e033116" have entirely different histories.
975e084b16
...
c2ab455d3e
34
README.md
34
README.md
@ -1,4 +1,4 @@
|
||||
![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v3/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/v3
|
||||
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/v3/pkg/database"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
|
||||
config := models.DatabaseConfig{
|
||||
Endpoint: "",
|
||||
Username: "",
|
||||
Password: "",
|
||||
Database: "",
|
||||
Port: 5432,
|
||||
SSL: false,
|
||||
Timezone: "Europe/Berlin",
|
||||
Debug: false,
|
||||
}
|
||||
|
||||
err = database.Connect(ctx, config)
|
||||
ctx := context.Background()
|
||||
cfg := models.DatabaseConfig{}
|
||||
|
||||
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.
|
||||
|
21
go.mod
21
go.mod
@ -1,18 +1,14 @@
|
||||
module git.anthrove.art/Anthrove/otter-space-sdk/v3
|
||||
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.32.0
|
||||
github.com/testcontainers/testcontainers-go/modules/postgres v0.32.0
|
||||
go.opentelemetry.io/contrib/bridges/otellogrus v0.3.0
|
||||
go.opentelemetry.io/otel v1.28.0
|
||||
go.opentelemetry.io/otel/trace v1.28.0
|
||||
gorm.io/driver/postgres v1.5.9
|
||||
gorm.io/gorm v1.25.11
|
||||
)
|
||||
@ -33,7 +29,7 @@ require (
|
||||
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.2.6 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
@ -58,20 +54,19 @@ require (
|
||||
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-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/rogpeppe/go-internal v1.10.0 // 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.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/log v0.4.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.28.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk v1.28.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.8.0 // indirect
|
||||
golang.org/x/sys v0.21.0 // indirect
|
||||
golang.org/x/text v0.17.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
|
||||
|
40
go.sum
40
go.sum
@ -36,8 +36,8 @@ 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=
|
||||
@ -110,8 +110,8 @@ github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF
|
||||
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.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4=
|
||||
@ -144,24 +144,20 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
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/bridges/otellogrus v0.3.0 h1:QHEj9AK6bEiEA9S5OdDUE9KAx4xp6pRkYMnybHDmjZU=
|
||||
go.opentelemetry.io/contrib/bridges/otellogrus v0.3.0/go.mod h1:HRlW/1YWrBrbzB6FvHU7jUuz33F74PEvQVBL+b+wUhM=
|
||||
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.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
|
||||
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
|
||||
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.4.0 h1:/vZ+3Utqh18e8TPjuc3ecg284078KWrR8BRz+PQAj3o=
|
||||
go.opentelemetry.io/otel/log v0.4.0/go.mod h1:DhGnQvky7pHy82MIRV43iXh3FlKN8UUKftn0KbLOq6I=
|
||||
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
|
||||
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
|
||||
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.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
|
||||
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
|
||||
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=
|
||||
@ -180,8 +176,8 @@ 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=
|
||||
@ -192,14 +188,14 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
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.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
|
||||
golang.org/x/sys v0.21.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.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.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=
|
||||
|
42
internal/postgres/custom.go
Normal file
42
internal/postgres/custom.go
Normal file
@ -0,0 +1,42 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func ExecuteRawStatement(ctx context.Context, db *gorm.DB, query string, args ...any) error {
|
||||
if query == "" {
|
||||
return errors.New("query can not be empty")
|
||||
}
|
||||
|
||||
if args == nil {
|
||||
return errors.New("arguments can not be nil")
|
||||
}
|
||||
|
||||
result := db.WithContext(ctx).Exec(query, args...)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func QueryRawStatement(ctx context.Context, db *gorm.DB, query string, args ...any) (*sql.Rows, error) {
|
||||
if query == "" {
|
||||
return nil, errors.New("query can not be empty")
|
||||
}
|
||||
|
||||
if args == nil {
|
||||
return nil, errors.New("arguments can not be nil")
|
||||
}
|
||||
|
||||
result, err := db.WithContext(ctx).Raw(query, args...).Rows()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
125
internal/postgres/custom_test.go
Normal file
125
internal/postgres/custom_test.go
Normal file
@ -0,0 +1,125 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v2/test"
|
||||
"gorm.io/gorm"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExecuteRawStatement(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)
|
||||
|
||||
// Test
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
query string
|
||||
args []any
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *sql.Rows
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 01: Empty Query",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
query: "",
|
||||
args: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 02: Nil Query",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
query: "aasd",
|
||||
args: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ExecuteRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ExecuteRawStatement() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryRawStatement(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)
|
||||
|
||||
// Test
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
query string
|
||||
args []any
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *sql.Rows
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Test 01: Empty Query",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
query: "",
|
||||
args: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 02: Nil Query",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
query: "aasd",
|
||||
args: nil,
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := QueryRawStatement(tt.args.ctx, tt.args.db, tt.args.query, tt.args.args...)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("QueryRawStatement() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("QueryRawStatement() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
131
internal/postgres/post.go
Normal file
131
internal/postgres/post.go
Normal 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
|
||||
}
|
469
internal/postgres/post_test.go
Normal file
469
internal/postgres/post_test.go
Normal 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
|
||||
}
|
126
internal/postgres/relationships.go
Normal file
126
internal/postgres/relationships.go
Normal 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
|
||||
}
|
392
internal/postgres/relationships_test.go
Normal file
392
internal/postgres/relationships_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
85
internal/postgres/source.go
Normal file
85
internal/postgres/source.go
Normal 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
|
||||
}
|
270
internal/postgres/source_test.go
Normal file
270
internal/postgres/source_test.go
Normal 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
440
internal/postgres/tag.go
Normal 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
|
||||
}
|
1452
internal/postgres/tag_test.go
Normal file
1452
internal/postgres/tag_test.go
Normal file
File diff suppressed because it is too large
Load Diff
398
internal/postgres/user.go
Normal file
398
internal/postgres/user.go
Normal 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
|
||||
}
|
1370
internal/postgres/user_test.go
Normal file
1370
internal/postgres/user_test.go
Normal file
File diff suppressed because it is too large
Load Diff
@ -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
11
internal/utils/slices.go
Normal 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
|
||||
}
|
60
internal/utils/slices_test.go
Normal file
60
internal/utils/slices_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
@ -1,138 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database"
|
||||
|
||||
var (
|
||||
//go:embed migrations/*.sql
|
||||
embedMigrations embed.FS
|
||||
client *gorm.DB
|
||||
tracer trace.Tracer
|
||||
logger = log.New()
|
||||
)
|
||||
|
||||
// Connect to the Database
|
||||
func Connect(ctx context.Context, config models.DatabaseConfig) error {
|
||||
|
||||
// Setup open telemetry
|
||||
tracer = otel.Tracer(tracingName)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
@ -1,174 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
36
pkg/database/database.go
Normal file
36
pkg/database/database.go
Normal file
@ -0,0 +1,36 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"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
|
||||
|
||||
// ExecuteRawStatement run a custom query.
|
||||
ExecuteRawStatement(ctx context.Context, query string, args ...any) error
|
||||
|
||||
// QueryRawStatement runs a custom query and returns the table
|
||||
QueryRawStatement(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
||||
}
|
@ -1,199 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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.UserFavoriteIDToShort})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
@ -1,761 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
@ -2,8 +2,7 @@
|
||||
CREATE TYPE Rating AS ENUM (
|
||||
'safe',
|
||||
'questionable',
|
||||
'explicit',
|
||||
'unknown'
|
||||
'explicit'
|
||||
);
|
||||
|
||||
CREATE TYPE TagType AS ENUM (
|
||||
@ -19,7 +18,7 @@ CREATE TYPE TagType AS ENUM (
|
||||
|
||||
CREATE TABLE "Post"
|
||||
(
|
||||
id CHAR(25) PRIMARY KEY,
|
||||
id CHAR(25) PRIMARY KEY,
|
||||
rating Rating,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
@ -29,9 +28,9 @@ CREATE TABLE "Post"
|
||||
CREATE TABLE "Source"
|
||||
(
|
||||
id CHAR(25) PRIMARY KEY,
|
||||
display_name TEXT NULL,
|
||||
icon TEXT NULL,
|
||||
domain TEXT NOT NULL UNIQUE,
|
||||
display_name TEXT NULL,
|
||||
icon TEXT NULL,
|
||||
domain TEXT NOT NULL UNIQUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP NULL
|
||||
@ -48,7 +47,7 @@ CREATE TABLE "Tag"
|
||||
|
||||
CREATE TABLE "User"
|
||||
(
|
||||
id TEXT PRIMARY KEY,
|
||||
id TEXT PRIMARY KEY,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP NULL
|
||||
@ -58,7 +57,7 @@ CREATE TABLE "PostReference"
|
||||
(
|
||||
post_id TEXT REFERENCES "Post" (id),
|
||||
source_id TEXT REFERENCES "Source" (id),
|
||||
url TEXT NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
full_file_url TEXT,
|
||||
preview_file_url TEXT,
|
||||
sample_file_url TEXT,
|
||||
@ -80,34 +79,28 @@ CREATE TABLE "TagGroup"
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE "UserFavorites"
|
||||
(
|
||||
user_id TEXT REFERENCES "User" (id),
|
||||
post_id TEXT REFERENCES "Post" (id),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (user_id, post_id)
|
||||
);
|
||||
|
||||
CREATE TABLE "UserSource"
|
||||
(
|
||||
id CHAR(25) PRIMARY KEY,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP NULL NULL,
|
||||
user_id TEXT REFERENCES "User" (id),
|
||||
source_id TEXT REFERENCES "Source" (id),
|
||||
scrape_time_interval TEXT,
|
||||
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 "UserFavorites"
|
||||
(
|
||||
id CHAR(25) PRIMARY KEY,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at TIMESTAMP NULL NULL,
|
||||
user_id TEXT REFERENCES "User" (id),
|
||||
post_id TEXT REFERENCES "Post" (id),
|
||||
user_source_id CHAR(25) REFERENCES "UserSource" (id)
|
||||
);
|
||||
|
||||
CREATE TABLE "post_tags"
|
||||
(
|
||||
post_id TEXT REFERENCES "Post" (id),
|
||||
|
@ -2,206 +2,30 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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})
|
||||
}
|
||||
// GetPostByURL retrieves a post by its source URL.
|
||||
GetPostByURL(ctx context.Context, postURL string) (*models.Post, error)
|
||||
|
||||
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)
|
||||
}
|
||||
// GetPostBySourceID retrieves a post by its source ID.
|
||||
GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, 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.PostIDToShort})
|
||||
}
|
||||
|
||||
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
|
||||
// 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
|
||||
}
|
||||
|
@ -1,454 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
268
pkg/database/postgres.go
Normal file
268
pkg/database/postgres.go
Normal file
@ -0,0 +1,268 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"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)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
func (p *postgresqlConnection) ExecuteRawStatement(ctx context.Context, query string, args ...any) error {
|
||||
return postgres.ExecuteRawStatement(ctx, p.db, query, args...)
|
||||
}
|
||||
|
||||
func (p *postgresqlConnection) QueryRawStatement(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
|
||||
return postgres.QueryRawStatement(ctx, p.db, query, args...)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
3791
pkg/database/postgres_test.go
Normal file
3791
pkg/database/postgres_test.go
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,91 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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)
|
||||
}
|
||||
|
||||
}
|
@ -1,328 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
@ -2,246 +2,18 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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})
|
||||
}
|
||||
// GetAllSources retrieves all sources.
|
||||
GetAllSources(ctx context.Context) ([]models.Source, error)
|
||||
|
||||
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.SourceIDToShort})
|
||||
}
|
||||
|
||||
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.SourceIDToShort})
|
||||
}
|
||||
|
||||
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
|
||||
// GetSourceByDomain retrieves a source by its URL.
|
||||
GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*models.Source, error)
|
||||
}
|
||||
|
@ -1,546 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
@ -2,133 +2,19 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
||||
|
@ -1,134 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
@ -1,292 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -1,137 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
@ -1,292 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -1,262 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
19
pkg/database/tagalias.go
Normal 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
19
pkg/database/taggroup.go
Normal 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
|
||||
}
|
@ -2,107 +2,41 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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})
|
||||
}
|
||||
UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error
|
||||
|
||||
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)
|
||||
}
|
||||
GetAllUsers(ctx context.Context) ([]models.User, 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
|
||||
// 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)
|
||||
}
|
||||
|
@ -1,173 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/internal/utils"
|
||||
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/error"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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.UserSourceIDToShort})
|
||||
}
|
||||
|
||||
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.UserSourceIDToShort})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
@ -1,498 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
@ -1,219 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
@ -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"
|
||||
}
|
||||
|
@ -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 string
|
||||
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)
|
||||
}
|
||||
|
@ -3,33 +3,11 @@ package error
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
SourceListIsEmpty = "sourceList cannot be empty"
|
||||
SourceIDIsEmpty = "SourceID cannot be empty"
|
||||
SourceIDToShort = "sourceID needs to be 25 characters long"
|
||||
SourceDomainIsEmpty = "source Domain cannot be empty"
|
||||
|
||||
UserSourceIDIsEmpty = "userSourceID cannot be empty"
|
||||
UserSourceIDToShort = "userSourceID needs to be 25 characters long"
|
||||
|
||||
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"
|
||||
UserFavoriteIDToShort = "userFavoriteID needs to be 25 characters long"
|
||||
|
||||
PostListIsEmpty = "userFavoriteList cannot be empty"
|
||||
PostIDIsEmpty = "userFavoriteID cannot be empty"
|
||||
PostIDToShort = "PostID needs to be 25 characters long"
|
||||
|
||||
BatchSizeIsEmpty = "batchSize cannot be empty"
|
||||
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
5
pkg/models/api.go
Normal file
@ -0,0 +1,5 @@
|
||||
package models
|
||||
|
||||
type FavoriteList struct {
|
||||
Posts []Post `json:"posts,omitempty"`
|
||||
}
|
@ -2,32 +2,20 @@ package models
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
UserID string
|
||||
PostID string
|
||||
PostURL 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
|
||||
)
|
||||
|
||||
const (
|
||||
MaxPageSizeLimit = 100
|
||||
DefaultPageSize = 50
|
||||
)
|
||||
type Rating string
|
||||
type TagType string
|
||||
|
||||
const (
|
||||
SFW Rating = "safe"
|
||||
|
@ -8,13 +8,13 @@ import (
|
||||
)
|
||||
|
||||
type ID interface {
|
||||
UserID | SourceID | PostID | UserSourceID | UserFavoriteID
|
||||
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"`
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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"`
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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"`
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,9 @@ package models
|
||||
|
||||
// User model
|
||||
type User struct {
|
||||
BaseModel[UserID]
|
||||
Favorites []UserFavorite `json:"-" gorm:"foreignKey:UserID"`
|
||||
Sources []UserSource `json:"-" gorm:"foreignKey:UserID"`
|
||||
BaseModel[AnthroveUserID]
|
||||
Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"`
|
||||
Sources []UserSource `json:"-" gorm:"foreignKey:UserID"`
|
||||
}
|
||||
|
||||
func (User) TableName() string {
|
||||
|
@ -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"
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,151 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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
|
||||
}
|
@ -8,14 +8,13 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/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),
|
||||
@ -80,23 +80,21 @@ 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,
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
})
|
||||
}
|
||||
|
||||
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,7 +119,5 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre
|
||||
SSL: false,
|
||||
Timezone: "Europe/Berlin",
|
||||
Debug: true,
|
||||
}
|
||||
|
||||
return databaseConfig, nil
|
||||
}, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user