diff --git a/README.md b/README.md index 7360a56..5096c0a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v2/actions/workflows/build_check.yaml/badge.svg) +![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v3/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/v2 +go get git.anthrove.art/Anthrove/otter-space-sdk/v3 ```` ## Usage @@ -33,23 +33,33 @@ package main import ( "context" - "log" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/database" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" ) func main() { - ctx := context.Background() - cfg := models.DatabaseConfig{} - - pgClient := database.NewPostgresqlConnection() - err := pgClient.Connect(ctx, cfg) - if err != nil { - log.Panic(err) + 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) + if err != nil { + 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. diff --git a/go.mod b/go.mod index 0f5fce6..2206750 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,18 @@ -module git.anthrove.art/Anthrove/otter-space-sdk/v2 +module git.anthrove.art/Anthrove/otter-space-sdk/v3 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 ) @@ -29,7 +33,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.1 // indirect + github.com/go-logr/logr v1.4.2 // 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 @@ -54,19 +58,20 @@ 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 v1.24.0 // indirect - go.opentelemetry.io/otel/metric v1.24.0 // indirect - go.opentelemetry.io/otel/trace v1.24.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 golang.org/x/crypto v0.22.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.19.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.17.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 diff --git a/go.sum b/go.sum index e1cb8d0..73406fa 100644 --- a/go.sum +++ b/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.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= -github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +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/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.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= -github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +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/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,20 +144,24 @@ 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.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= -go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo= +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/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/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/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/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= @@ -176,8 +180,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.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.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= @@ -188,14 +192,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.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.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/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.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +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/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= diff --git a/internal/postgres/custom.go b/internal/postgres/custom.go deleted file mode 100644 index c360669..0000000 --- a/internal/postgres/custom.go +++ /dev/null @@ -1,42 +0,0 @@ -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 -} diff --git a/internal/postgres/custom_test.go b/internal/postgres/custom_test.go deleted file mode 100644 index 22c176b..0000000 --- a/internal/postgres/custom_test.go +++ /dev/null @@ -1,125 +0,0 @@ -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) - } - }) - } -} diff --git a/internal/postgres/post.go b/internal/postgres/post.go deleted file mode 100644 index 8b36e51..0000000 --- a/internal/postgres/post.go +++ /dev/null @@ -1,131 +0,0 @@ -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 -} diff --git a/internal/postgres/post_test.go b/internal/postgres/post_test.go deleted file mode 100644 index 6fd63a4..0000000 --- a/internal/postgres/post_test.go +++ /dev/null @@ -1,469 +0,0 @@ -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 -} diff --git a/internal/postgres/relationships.go b/internal/postgres/relationships.go deleted file mode 100644 index 5e03a12..0000000 --- a/internal/postgres/relationships.go +++ /dev/null @@ -1,126 +0,0 @@ -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 -} diff --git a/internal/postgres/relationships_test.go b/internal/postgres/relationships_test.go deleted file mode 100644 index f55c1ca..0000000 --- a/internal/postgres/relationships_test.go +++ /dev/null @@ -1,392 +0,0 @@ -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) - } - }) - } -} diff --git a/internal/postgres/source.go b/internal/postgres/source.go deleted file mode 100644 index bae1244..0000000 --- a/internal/postgres/source.go +++ /dev/null @@ -1,85 +0,0 @@ -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 -} diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go deleted file mode 100644 index c29a1e7..0000000 --- a/internal/postgres/source_test.go +++ /dev/null @@ -1,270 +0,0 @@ -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 - -} diff --git a/internal/postgres/tag.go b/internal/postgres/tag.go deleted file mode 100644 index 2c17a87..0000000 --- a/internal/postgres/tag.go +++ /dev/null @@ -1,440 +0,0 @@ -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 -} diff --git a/internal/postgres/tag_test.go b/internal/postgres/tag_test.go deleted file mode 100644 index 7c6f15b..0000000 --- a/internal/postgres/tag_test.go +++ /dev/null @@ -1,1452 +0,0 @@ -package postgres - -import ( - "context" - "fmt" - "reflect" - "testing" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" -) - -func TestCreateTagNodeWitRelation(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 - - 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(err) - } - - tag := &models.Tag{ - Name: "JayTheFerret", - Type: "artist", - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - PostID models.AnthrovePostID - tag *models.Tag - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid PostID and Tag", - args: args{ - ctx: ctx, - db: gormDB, - PostID: post.ID, - tag: tag, - }, - wantErr: false, - }, - { - name: "Test 2: Valid PostID and no Tag", - args: args{ - ctx: ctx, - db: gormDB, - PostID: post.ID, - tag: nil, - }, - wantErr: true, - }, - { - name: "Test 3: Invalid PostID and valid Tag", - args: args{ - ctx: ctx, - db: gormDB, - PostID: "123456", - tag: tag, - }, - wantErr: true, - }, - { - name: "Test 4: No PostID and valid Tag", - args: args{ - ctx: ctx, - db: gormDB, - PostID: "", - tag: tag, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagAndReferenceToPost(tt.args.ctx, tt.args.db, tt.args.PostID, tt.args.tag); (err != nil) != tt.wantErr { - t.Errorf("CreatePostWithReferenceToTagAnd() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetTags(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 - - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: "artist", - }, - { - Name: "anthro", - Type: "general", - }, - { - Name: "soxx", - Type: "character", - }, - } - - for _, tag := range tags { - err = CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.Tag - wantErr bool - }{ - { - name: "Test 1: Get Tags", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: tags, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetTags(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetTags() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkTag(got, tt.want) { - t.Errorf("GetTags() got = %v, want %v", got, tt.want) - } - }) - } -} - -func checkTag(got []models.Tag, want []models.Tag) bool { - for i, tag := range want { - if tag.Type != got[i].Type { - return false - } - if tag.Name != got[i].Name { - return false - } - } - - return true - -} - -func TestCreateTag(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 - validTag := models.Tag{ - Name: "JayTheFerret", - Type: "artist", - } - - invalidTag := models.Tag{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagName models.AnthroveTagName - tagType models.TagType - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tag", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName(validTag.Name), - tagType: validTag.Type, - }, - wantErr: false, - }, - { - name: "Test 2: Duplicate Tag", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName(validTag.Name), - tagType: validTag.Type, - }, - wantErr: true, - }, - { - name: "Test 3: Invalid Tag", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName(invalidTag.Name), - tagType: invalidTag.Type, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTag(tt.args.ctx, tt.args.db, tt.args.tagName, tt.args.tagType); (err != nil) != tt.wantErr { - t.Errorf("CreateTag() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateTagAlias(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 - - validTagAliasName01 := models.AnthroveTagAliasName("httyd") - validTagAliasName02 := models.AnthroveTagAliasName("dragon") - - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagAliasName models.AnthroveTagAliasName - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliasName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 2: No TagAliasName", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: "", - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 4: No tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliasName01, - tagID: "", - }, - wantErr: true, - }, - { - name: "Test 5: Duplicate tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliasName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 6: Invalide tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliasName02, - tagID: "aaa", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagAlias(tt.args.ctx, tt.args.db, tt.args.tagAliasName, tt.args.tagID); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetAllTagAlias(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 - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagAlias{ - { - Name: string(validTagAliases[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[2]), - TagID: string(validTagID), - }, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.TagAlias - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagAlias(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagAlias() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagAlias() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetAllTagAliasByTag(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 - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagAlias{ - { - Name: string(validTagAliases[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[2]), - TagID: string(validTagID), - }, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - want []models.TagAlias - wantErr bool - }{ - { - name: "Test 1: Valid TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: validTagID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Invalid TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: "adads", - }, - want: []models.TagAlias{}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagAliasByTag(tt.args.ctx, tt.args.db, tt.args.tagID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestDeleteTagAlias(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 - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagAliasName models.AnthroveTagAliasName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveTagAliasName", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: validTagAliases[0], - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveTagAliasName", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: "asdad", - }, - wantErr: false, - }, - { - name: "Test 3: No AnthroveTagAliasName", - args: args{ - ctx: ctx, - db: gormDB, - tagAliasName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := DeleteTagAlias(tt.args.ctx, tt.args.db, tt.args.tagAliasName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateTagGroup(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 - - validTagGroupName01 := models.AnthroveTagGroupName("httyd") - validTagGroupName02 := models.AnthroveTagGroupName("dragon") - - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagGroupName models.AnthroveTagGroupName - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 2: No TagGroupName", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: "", - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 4: No tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupName01, - tagID: "", - }, - wantErr: true, - }, - { - name: "Test 5: Duplicate tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupName01, - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 6: Invalide tagID", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupName02, - tagID: "aaa", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagGroup(tt.args.ctx, tt.args.db, tt.args.tagGroupName, tt.args.tagID); (err != nil) != tt.wantErr { - t.Errorf("CreateTagGroup() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetAllTagGroup(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 - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagGroup{ - { - Name: string(validTagGroupes[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[2]), - TagID: string(validTagID), - }, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.TagGroup - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagGroup(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagGroup() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagGroup() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetAllTagGroupByTag(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 - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagGroup{ - { - Name: string(validTagGroupes[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[2]), - TagID: string(validTagID), - }, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - want []models.TagGroup - wantErr bool - }{ - { - name: "Test 1: Valid TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: validTagID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Invalid TagID", - args: args{ - ctx: ctx, - db: gormDB, - tagID: "adads", - }, - want: []models.TagGroup{}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagGroupByTag(tt.args.ctx, tt.args.db, tt.args.tagID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagGroupByTag() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagGroupByTag() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestDeleteTagGroup(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 - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagGroupName models.AnthroveTagGroupName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveTagGroupName", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: validTagGroupes[0], - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveTagGroupName", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: "asdad", - }, - wantErr: false, - }, - { - name: "Test 3: No AnthroveTagGroupName", - args: args{ - ctx: ctx, - db: gormDB, - tagGroupName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := DeleteTagGroup(tt.args.ctx, tt.args.db, tt.args.tagGroupName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTagAlias() 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 { - t.Fatalf("Could not start PostgreSQL container: %v", err) - } - defer container.Terminate(ctx) - - // Setup Test - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagName models.AnthroveTagName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid TagName", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName(validTagID), - }, - wantErr: false, - }, - { - name: "Test 2: Invalid TagName", - args: args{ - ctx: ctx, - db: gormDB, - tagName: models.AnthroveTagName("aaa"), - }, - wantErr: false, - }, - { - name: "Test 3: No TagName", - args: args{ - ctx: ctx, - db: gormDB, - tagName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := DeleteTag(tt.args.ctx, tt.args.db, tt.args.tagName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetAllTagByTagType(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 - - validTags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Character, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Alphyron", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - } - - expectetResult := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Character, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Alphyron", - Type: models.Character, - }, - } - - for _, tag := range validTags { - err = CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagType models.TagType - } - tests := []struct { - name string - args args - want []models.Tag - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - tagType: models.Character, - }, - want: expectetResult, - wantErr: false, - }, - { - name: "Test 2: invalid Tag Type", - args: args{ - ctx: ctx, - db: gormDB, - tagType: "aa", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No Tag Type", - args: args{ - ctx: ctx, - db: gormDB, - tagType: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllTagByTagsType(tt.args.ctx, tt.args.db, tt.args.tagType) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagByTagsType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkTag(got, tt.want) { - t.Errorf("GetAllTagByTagsType() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestCreateTagInBatchAndUpdate(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 - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - emptyTags := []models.Tag{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tags []models.Tag - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - db: gormDB, - tags: tags, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - db: gormDB, - tags: emptyTags, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - db: gormDB, - tags: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - db: gormDB, - tags: nil, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagInBatchAndUpdate(tt.args.ctx, tt.args.db, tt.args.tags, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagInBatchAndUpdate() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateTagAliasInBatch(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 - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - err = CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags)) - if err != nil { - t.Fatal(err) - } - - tagAlias := []models.TagAlias{ - { - Name: "test1", - TagID: tags[0].Name, - }, - { - Name: "test2", - TagID: tags[1].Name, - }, - { - Name: "test3", - TagID: tags[2].Name, - }, - { - Name: "test4", - TagID: tags[3].Name, - }, - } - emptyTagAlias := []models.TagAlias{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagAliases []models.TagAlias - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagAliases: tagAlias, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagAliases: emptyTagAlias, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagAliases: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - db: gormDB, - tagAliases: tagAlias, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagAliasInBatch(tt.args.ctx, tt.args.db, tt.args.tagAliases, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAliasInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateTagGroupInBatch(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 - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - err = CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags)) - if err != nil { - t.Fatal(err) - } - - tagGroup := []models.TagGroup{ - { - Name: "test1", - TagID: tags[0].Name, - }, - { - Name: "test2", - TagID: tags[1].Name, - }, - { - Name: "test3", - TagID: tags[2].Name, - }, - { - Name: "test4", - TagID: tags[3].Name, - }, - } - emptyTagGroup := []models.TagGroup{} - - // Test - type args struct { - ctx context.Context - db *gorm.DB - tagGroups []models.TagGroup - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagGroups: tagGroup, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagGroups: emptyTagGroup, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - db: gormDB, - tagGroups: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - db: gormDB, - tagGroups: tagGroup, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateTagGroupInBatch(tt.args.ctx, tt.args.db, tt.args.tagGroups, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagGroupInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} diff --git a/internal/postgres/user.go b/internal/postgres/user.go deleted file mode 100644 index 45a847a..0000000 --- a/internal/postgres/user.go +++ /dev/null @@ -1,398 +0,0 @@ -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 -} diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go deleted file mode 100644 index aa89e05..0000000 --- a/internal/postgres/user_test.go +++ /dev/null @@ -1,1370 +0,0 @@ -package postgres - -import ( - "context" - "fmt" - "reflect" - "testing" - "time" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" -) - -func TestCreateUser(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") - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - }, - wantErr: true, - }, - { - name: "Test 3: No anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateUser(tt.args.ctx, tt.args.db, tt.args.anthroveUserID); (err != nil) != tt.wantErr { - t.Errorf("CreateUser() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestCreateUserNodeWithSourceRelation(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - 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 - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - userID string - username string - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid anthroveUserID, sourceID, userID, username", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: source.ID, - userID: "e1", - username: "marius", - }, - wantErr: false, - }, - { - name: "Test 2: Invalid anthroveUserID, valid sourceID, userID, username", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: source.ID, - userID: "e1", - username: "marius", - }, - wantErr: true, - }, - { - name: "Test 3: Empty anthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: source.ID, - userID: "e1", - username: "marius", - }, - wantErr: true, - }, - { - name: "Test 4: invalid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "fa.net", - userID: "e1", - username: "marius", - }, - wantErr: true, - }, - { - name: "Test 5: no userID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: source.ID, - userID: "", - username: "marius", - }, - wantErr: true, - }, - { - name: "Test 6: no username", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: source.ID, - userID: "aa", - username: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := CreateUserWithRelationToSource(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.userID, tt.args.username); (err != nil) != tt.wantErr { - t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestGetAllUsers(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 - validUserID01 := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2")) - validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3")) - - users := []models.User{ - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID01}, - }, - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID02}, - }, - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID03}, - }, - } - - for _, user := range users { - err = CreateUser(ctx, gormDB, user.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - } - tests := []struct { - name string - args args - want []models.User - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: users, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetAllUsers(tt.args.ctx, tt.args.db) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkUser(got, tt.want) { - t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetUserSourceBySourceID(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - expectedResult := &models.UserSource{ - UserID: string(validUserID), - AccountUsername: "euser", - Source: models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: source.ID}, - DisplayName: source.DisplayName, - Domain: source.Domain, - Icon: source.Icon, - }, - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult.UserID, expectedResult.AccountUsername) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - } - tests := []struct { - name string - args args - want *models.UserSource - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: source.ID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveUserID and valid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: source.ID, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Valid AnthroveUserID and invalid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "fa", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 4: No AnthroveUserID and Valid sourceID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: source.ID, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 5: Valid AnthroveUserID and No anthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "1", - sourceID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 6: No AnthroveUserID and No anthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 7: No anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "", - }, - want: nil, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserSourceBySourceID(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID) - if (err != nil) != tt.wantErr { - t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkUserSource(got, tt.want) { - t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetUserFavoriteNodeWithPagination(t *testing.T) { - // Setup trow away containert - 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 - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) - validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) - validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) - - expectedResultPosts := []models.Post{ - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, - Rating: "safe", - }, - { - - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, - Rating: "safe", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, - Rating: "questionable", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, - Rating: "safe", - }, - } - expectedResult := &models.FavoriteList{ - Posts: expectedResultPosts, - } - expectedResult2 := &models.FavoriteList{ - Posts: expectedResultPosts[2:], - } - expectedResult3 := &models.FavoriteList{ - Posts: expectedResultPosts[:3], - } - - err = CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - for _, expectedResultPost := range expectedResultPosts { - err = CreatePost(ctx, gormDB, &expectedResultPost) - if err != nil { - t.Fatal(err) - } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, expectedResultPost.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - skip int - limit int - } - tests := []struct { - name string - args args - want *models.FavoriteList - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - skip: 0, - limit: 2000, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: Skip first two", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - skip: 2, - limit: 2000, - }, - want: expectedResult2, - wantErr: false, - }, - { - name: "Test 3: Limit of 3", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - skip: 0, - limit: 3, - }, - want: expectedResult3, - wantErr: false, - }, - { - name: "Test 4: No anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - skip: 0, - limit: 3, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 5: Short anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "aaa", - skip: 0, - limit: 3, - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserFavoriteWithPagination(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.skip, tt.args.limit) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkFavoritePosts(got, tt.want) { - t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want) - } - }) - } -} - -func checkFavoritePosts(got *models.FavoriteList, want *models.FavoriteList) bool { - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - for i, post := range got.Posts { - if post.ID == want.Posts[i].ID { - } else { - return false - } - } - - return true -} - -func TestGetUserFavoritesCount(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 - - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) - validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) - validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) - - expectedResultPosts := []models.Post{ - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, - Rating: "safe", - }, - { - - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, - Rating: "safe", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, - Rating: "questionable", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, - Rating: "safe", - }, - } - - err = CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - for _, post := range expectedResultPosts { - err = CreatePost(ctx, gormDB, &post) - if err != nil { - t.Fatal(err) - } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want int64 - wantErr bool - }{ - { - name: "Test 1: Valid anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - }, - want: 6, - wantErr: false, - }, - { - name: "Test 2: Invalid anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "2", - }, - want: 0, - wantErr: true, - }, - { - name: "Test 3: no anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - }, - want: 0, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserFavoritesCount(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetUserFavoritesCount() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("GetUserFavoritesCount() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetUserSourceLinks(t *testing.T) { - // Setup trow away containert - 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 - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - validSourceID2 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source2")) - - eSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, - DisplayName: "e621", - Domain: "e621.net", - } - err = CreateSource(ctx, gormDB, eSource) - if err != nil { - t.Fatal("Create Source e621:", err) - } - - faSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID2}, - DisplayName: "fa", - Domain: "fa.net", - } - err = CreateSource(ctx, gormDB, faSource) - if err != nil { - t.Fatal("Create Source fa:", err) - } - - expectedResult := make(map[string]models.UserSource) - expectedResult["e621"] = models.UserSource{ - UserID: "e1", - AccountUsername: "e621-user", - Source: models.Source{ - DisplayName: eSource.DisplayName, - Domain: eSource.Domain, - }, - } - expectedResult["fa"] = models.UserSource{ - UserID: "fa1", - AccountUsername: "fa-user", - Source: models.Source{ - DisplayName: faSource.DisplayName, - Domain: faSource.Domain, - }, - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) - if err != nil { - t.Fatal("CreateUserWithRelationToSource e621:", err) - } - err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) - if err != nil { - t.Fatal("CreateUserWithRelationToSource fa:", err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want map[string]models.UserSource - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 3: No AnthroveID", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 1: AnthroveID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "aaa", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserSourceLinks(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUserSources() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllUserSources() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) { - // Setup trow away containert - 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 - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - - err = CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - posts := []models.Post{ - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit"}, - } - - for _, post := range posts { - err = CreatePost(ctx, gormDB, &post) - if err != nil { - t.Fatal(err) - } - err = CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID) - if err != nil { - t.Fatal(err) - } - } - - tags := []models.Tag{ - {Name: "JayTheFerret", Type: "artist"}, - {Name: "Ferret", Type: "species"}, - {Name: "Jay", Type: "character"}, - } - - for i, tag := range tags { - err = CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag) - if err != nil { - t.Fatal(err) - } - } - - expectedResult := []models.TagsWithFrequency{ - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[0].Name, - Type: tags[0].Type, - }, - }, - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[2].Name, - Type: tags[2].Type, - }, - }, - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[1].Name, - Type: tags[1].Type, - }, - }, - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want []models.TagsWithFrequency - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validAnthroveUserID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: short anthroveUserID given", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "aaa", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetUserTagWitRelationToFavedPosts(tt.args.ctx, tt.args.db, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagsFromUser() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagsFromUser() got = %v, want %v", got, tt.want) - } - }) - } -} - -func checkUser(got []models.User, want []models.User) bool { - for i, user := range want { - if user.ID != got[i].ID { - return false - } - } - return true -} - -func checkUserSource(got *models.UserSource, want *models.UserSource) bool { - - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - if got.UserID != want.UserID { - return false - } - if got.AccountUsername != want.AccountUsername { - return false - } - if got.Source.DisplayName != want.Source.DisplayName { - return false - } - if got.Source.Domain != want.Source.Domain { - return false - } - if got.Source.Icon != want.Source.Icon { - return false - } - - return true -} - -func TestUpdateUserSourceScrapeTimeInterval(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - scrapeTime models.AnthroveScrapeTimeInterval - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "111", - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "", - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 5: scrapeTime is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - scrapeTime: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := UpdateUserSourceScrapeTimeInterval(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.scrapeTime); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceScrapeTimeInterval() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestUpdateUserSourceLastScrapeTime(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - validScrapeTime := models.AnthroveUserLastScrapeTime(time.Now()) - inValidScrapeTime := models.AnthroveUserLastScrapeTime{} - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - lastScrapeTime models.AnthroveUserLastScrapeTime - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "111", - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "", - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 5: scrapeTime is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - lastScrapeTime: inValidScrapeTime, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := UpdateUserSourceLastScrapeTime(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.lastScrapeTime); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceLastScrapeTime() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestUpdateUserSourceValidation(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - db *gorm.DB - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - valid bool - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: validSourceID, - valid: true, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - valid: true, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: "", - sourceID: validSourceID, - valid: true, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "111", - valid: true, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - db: gormDB, - anthroveUserID: validUserID, - sourceID: "", - valid: true, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if err := UpdateUserSourceValidation(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID, tt.args.valid); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceValidation() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} diff --git a/internal/utils/error.go b/internal/utils/error.go new file mode 100644 index 0000000..aa80866 --- /dev/null +++ b/internal/utils/error.go @@ -0,0 +1,27 @@ +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 +} diff --git a/internal/utils/slices.go b/internal/utils/slices.go deleted file mode 100644 index dfb5aed..0000000 --- a/internal/utils/slices.go +++ /dev/null @@ -1,11 +0,0 @@ -package utils - -func GetOrDefault(data map[string]any, key string, defaultVal any) any { - val, ok := data[key] - - if !ok { - return defaultVal - } - - return val -} diff --git a/internal/utils/slices_test.go b/internal/utils/slices_test.go deleted file mode 100644 index 9f8cdc6..0000000 --- a/internal/utils/slices_test.go +++ /dev/null @@ -1,60 +0,0 @@ -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) - } - }) - } -} diff --git a/internal/utils/tracing.go b/internal/utils/tracing.go new file mode 100644 index 0000000..e71be80 --- /dev/null +++ b/internal/utils/tracing.go @@ -0,0 +1,22 @@ +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) +} diff --git a/pkg/database/client.go b/pkg/database/client.go new file mode 100644 index 0000000..e45cdd7 --- /dev/null +++ b/pkg/database/client.go @@ -0,0 +1,138 @@ +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 +} diff --git a/pkg/database/client_test.go b/pkg/database/client_test.go new file mode 100644 index 0000000..3f63553 --- /dev/null +++ b/pkg/database/client_test.go @@ -0,0 +1,174 @@ +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) + } + }) + } +} diff --git a/pkg/database/database.go b/pkg/database/database.go deleted file mode 100644 index 3a6beeb..0000000 --- a/pkg/database/database.go +++ /dev/null @@ -1,36 +0,0 @@ -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) -} diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go new file mode 100644 index 0000000..661f842 --- /dev/null +++ b/pkg/database/favorite.go @@ -0,0 +1,199 @@ +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 +} diff --git a/pkg/database/favorite_test.go b/pkg/database/favorite_test.go new file mode 100644 index 0000000..d6885a9 --- /dev/null +++ b/pkg/database/favorite_test.go @@ -0,0 +1,761 @@ +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 +} diff --git a/pkg/database/migrations/001_inital_database.sql b/pkg/database/migrations/001_inital_database.sql index 03932c9..95bde1d 100644 --- a/pkg/database/migrations/001_inital_database.sql +++ b/pkg/database/migrations/001_inital_database.sql @@ -2,7 +2,8 @@ CREATE TYPE Rating AS ENUM ( 'safe', 'questionable', - 'explicit' + 'explicit', + 'unknown' ); CREATE TYPE TagType AS ENUM ( @@ -18,7 +19,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, @@ -28,9 +29,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 @@ -47,7 +48,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 @@ -57,7 +58,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, @@ -79,28 +80,34 @@ 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 INT, + scrape_time_interval TEXT, 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), diff --git a/pkg/database/post.go b/pkg/database/post.go index f19e70d..c2873f9 100644 --- a/pkg/database/post.go +++ b/pkg/database/post.go @@ -2,30 +2,206 @@ package database import ( "context" + "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "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" ) -type Post interface { +func CreatePost(ctx context.Context, post models.Post) (models.Post, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePost") + defer span.End() - // CreatePost adds a new post to the database. - CreatePost(ctx context.Context, anthrovePost *models.Post) error + localLogger = localLogger.WithFields(log.Fields{ + "post_id": post.ID, + }) - // TODO: Everything - CreatePostInBatch(ctx context.Context, anthrovePost []models.Post, batchSize int) error + span.SetAttributes( + attribute.String("post_id", string(post.ID)), + ) - // GetPostByAnthroveID retrieves a post by its Anthrove ID. - GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error) + utils.HandleEvent(span, localLogger, "Starting post creation") - // GetPostByURL retrieves a post by its source URL. - GetPostByURL(ctx context.Context, postURL string) (*models.Post, error) + if client == nil { + return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } - // GetPostBySourceID retrieves a post by its source ID. - GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*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) + } - // 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 + 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 } diff --git a/pkg/database/post_test.go b/pkg/database/post_test.go new file mode 100644 index 0000000..68977e6 --- /dev/null +++ b/pkg/database/post_test.go @@ -0,0 +1,454 @@ +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 +} diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go deleted file mode 100644 index 001d7b0..0000000 --- a/pkg/database/postgres.go +++ /dev/null @@ -1,268 +0,0 @@ -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 -} diff --git a/pkg/database/postgres_test.go b/pkg/database/postgres_test.go deleted file mode 100644 index 221b882..0000000 --- a/pkg/database/postgres_test.go +++ /dev/null @@ -1,3791 +0,0 @@ -package database - -import ( - "context" - "database/sql" - "fmt" - "reflect" - "testing" - "time" - - "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/postgres" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/test" - "gorm.io/gorm" -) - -func TestNewPostgresqlConnection(t *testing.T) { - // Test - tests := []struct { - name string - want OtterSpace - }{ - { - name: "Test 1: Create new postgresql connection", - want: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if tt.want != NewPostgresqlConnection() { - } else { - t.Errorf("NewPostgresqlConnection() = %s", tt.want) - } - }) - } -} - -func Test_postgresqlConnection_Connect(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 - - dbConfig, err := test.DatabaseModesFromConnectionString(ctx, container) - if err != nil { - t.Fatalf("Could not parse database config: %v", err) - } - - // Test - type args struct { - in0 context.Context - config models.DatabaseConfig - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Connect to postgresql connection", - args: args{ - in0: ctx, - config: *dbConfig, - }, - wantErr: false, - }, - { - name: "Test 1: Empty connection config", - args: args{ - in0: ctx, - config: models.DatabaseConfig{}, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{} - if err := p.Connect(tt.args.in0, tt.args.config); (err != nil) != tt.wantErr { - t.Errorf("Connect() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateUserWithRelationToSource(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")) - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - accountId string - accountUsername string - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid anthroveUserID, sourceID, accountId, accountUsername", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: source.ID, - accountId: "e1", - accountUsername: "marius", - }, - wantErr: false, - }, - { - name: "Test 2: Invalid anthroveUserID, valid sourceID, accountId, accountUsername", - args: args{ - ctx: ctx, - anthroveUserID: "2", - sourceID: source.ID, - accountId: "e1", - accountUsername: "marius", - }, - wantErr: true, - }, - { - name: "Test 3: Empty anthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: source.ID, - accountId: "e1", - accountUsername: "marius", - }, - wantErr: true, - }, - { - name: "Test 4: invalid sourceID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "fa.net", - accountId: "e1", - accountUsername: "marius", - }, - wantErr: true, - }, - { - name: "Test 5: no accountId", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: source.ID, - accountId: "", - accountUsername: "marius", - }, - wantErr: true, - }, - { - name: "Test 6: no accountUsername", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: source.ID, - accountId: "aa", - accountUsername: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateUserWithRelationToSource(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.accountId, tt.args.accountUsername); (err != nil) != tt.wantErr { - t.Errorf("CreateUserWithRelationToSource() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateSource(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 - - validSource := &models.Source{ - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - - invalidSource := &models.Source{ - Domain: "", - } - - // Test - type args struct { - ctx context.Context - anthroveSource *models.Source - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid anthroveSource", - args: args{ - ctx: ctx, - anthroveSource: validSource, - }, - wantErr: false, - }, - { - name: "Test 2: inValid anthroveSource", - args: args{ - ctx: ctx, - anthroveSource: invalidSource, - }, - wantErr: true, - }, - { - name: "Test 3: unique anthroveSource", - args: args{ - ctx: ctx, - anthroveSource: validSource, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateSource(tt.args.ctx, tt.args.anthroveSource); (err != nil) != tt.wantErr { - t.Errorf("CreateSource() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreatePost(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 - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - validPost := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - invalidPost := &models.Post{ - Rating: "error", - } - - // Test - type args struct { - ctx context.Context - anthrovePost *models.Post - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthrovePostID and Rating", - args: args{ - ctx: context.Background(), - anthrovePost: validPost, - }, - wantErr: false, - }, - { - name: "Test 2: Invalid Rating", - args: args{ - ctx: context.Background(), - anthrovePost: invalidPost, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreatePost(tt.args.ctx, tt.args.anthrovePost); (err != nil) != tt.wantErr { - t.Errorf("CreatePost() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateTagAndReferenceToPost(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 - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - tag := &models.Tag{ - Name: "JayTheFerret", - Type: "artist", - } - - // Test - type args struct { - ctx context.Context - anthrovePostID models.AnthrovePostID - anthroveTag *models.Tag - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid PostID and Tag", - args: args{ - ctx: ctx, - anthrovePostID: post.ID, - anthroveTag: tag, - }, - wantErr: false, - }, - { - name: "Test 2: Valid PostID and no Tag", - args: args{ - ctx: ctx, - anthrovePostID: post.ID, - anthroveTag: nil, - }, - wantErr: true, - }, - { - name: "Test 3: Invalid PostID and valid Tag", - args: args{ - ctx: ctx, - anthrovePostID: "123456", - anthroveTag: tag, - }, - wantErr: true, - }, - { - name: "Test 4: No PostID and valid Tag", - args: args{ - ctx: ctx, - anthrovePostID: "", - anthroveTag: tag, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreatePostWithReferenceToTagAnd(tt.args.ctx, tt.args.anthrovePostID, tt.args.anthroveTag); (err != nil) != tt.wantErr { - t.Errorf("CreatePostWithReferenceToTagAnd() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(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 - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, - DisplayName: "e621", - Domain: "e621.net", - Icon: "icon.e621.net", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthrovePostID models.AnthrovePostID - sourceDomain models.AnthroveSourceDomain - postURl models.AnthrovePostURL - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthrovePostID and anthroveSourceDomain", - args: args{ - ctx: ctx, - anthrovePostID: post.ID, - sourceDomain: "e621.net", - postURl: "http://e621.net/post/eeasd", - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthrovePostID and Valid anthroveSourceDomain", - args: args{ - ctx: ctx, - anthrovePostID: "123456", - sourceDomain: "e621.net", - postURl: "", - }, - wantErr: true, - }, - { - name: "Test 3: Invalid anthroveSourceDomain and Valid AnthrovePostID", - args: args{ - ctx: ctx, - anthrovePostID: "1234", - sourceDomain: "fa.banana", - postURl: "", - }, - wantErr: true, - }, - { - name: "Test 4: Invalid anthroveSourceDomain and Invalid AnthrovePostID", - args: args{ - ctx: ctx, - anthrovePostID: "696969", - postURl: "", - }, - wantErr: true, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreatePostReference(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl, models.PostReferenceConfig{}); (err != nil) != tt.wantErr { - t.Errorf("CreatePostReference() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateReferenceBetweenUserAndPost(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")) - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - err = postgres.CreateUser(ctx, gormDB, validUserID) - if err != nil { - t.Fatal(err) - } - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - 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, - anthroveUserID: validUserID, - anthrovePostID: post.ID, - }, - wantErr: false, - }, - { - name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - anthrovePostID: "123456", - }, - wantErr: true, - }, - { - name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "123", - anthrovePostID: "1234", - }, - wantErr: true, - }, - { - name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "123", - anthrovePostID: "123456", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateReferenceBetweenUserAndPost(tt.args.ctx, tt.args.anthroveUserID, tt.args.anthrovePostID); (err != nil) != tt.wantErr { - t.Errorf("CreateReferenceBetweenUserAndPost() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CheckReferenceBetweenUserAndPost(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")) - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - err = postgres.CreateUser(ctx, gormDB, validUserID) - if err != nil { - t.Fatal(err) - } - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validUserID, post.ID) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - 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, - anthroveUserID: validUserID, - anthrovePostID: post.ID, - }, - want: true, - wantErr: false, - }, - { - name: "Test 2: Valid AnthroveUserID and invalid AnthrovePostID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - anthrovePostID: "qadw", - }, - want: false, - wantErr: true, - }, - { - name: "Test 3: Valid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "123", - anthrovePostID: post.ID, - }, - want: false, - wantErr: true, - }, - { - name: "Test 4: Invalid AnthrovePostID and invalid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: "123", - anthrovePostID: "123456", - }, - want: false, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.CheckIfUserHasPostAsFavorite(tt.args.ctx, 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 Test_postgresqlConnection_GetPostByAnthroveID(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 - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal("Could not create post", err) - } - - // Test - type args struct { - ctx context.Context - anthrovePost models.AnthrovePostID - } - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid anthrovePostID", - args: args{ - ctx: ctx, - anthrovePost: post.ID, - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: No anthrovePostID", - args: args{ - ctx: ctx, - anthrovePost: "nil", - }, - want: nil, - wantErr: true, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetPostByAnthroveID(tt.args.ctx, tt.args.anthrovePost) - 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 Test_postgresqlConnection_GetPostByURL(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 - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - err = postgres.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 = postgres.CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal("Could not create source", err) - } - - err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://e62asdwad.com/asdas", models.PostReferenceConfig{}) - if err != nil { - t.Fatal("Could not create source reference", err) - } - - // Test - type args struct { - ctx context.Context - sourceUrl string - } - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid sourceUrl", - args: args{ - ctx: ctx, - sourceUrl: "https://e62asdwad.com/asdas", - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: Invalid sourceUrl", - args: args{ - ctx: ctx, - sourceUrl: "1234", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No sourceUrl", - args: args{ - ctx: ctx, - sourceUrl: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetPostByURL(tt.args.ctx, 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 Test_postgresqlConnection_GetPostBySourceID(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 - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - post := &models.Post{ - BaseModel: models.BaseModel[models.AnthrovePostID]{ - ID: validPostID1, - }, - Rating: "safe", - } - - source := models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID1, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.net/icon.ico", - } - - err = postgres.CreatePost(ctx, gormDB, post) - if err != nil { - t.Fatal("Could not create post", err) - } - - err = postgres.CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal("Could not create source", err) - } - - err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://easd15aed.de/asd", models.PostReferenceConfig{}) - if err != nil { - t.Fatal("Could not create source reference", err) - } - - // Test - type args struct { - ctx context.Context - sourceID models.AnthroveSourceID - } - tests := []struct { - name string - args args - want *models.Post - wantErr bool - }{ - { - name: "Test 1: Valid sourceID", - args: args{ - ctx: ctx, - sourceID: source.ID, - }, - want: post, - wantErr: false, - }, - { - name: "Test 2: Invalid sourceID", - args: args{ - ctx: ctx, - sourceID: "1234", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No sourceID", - args: args{ - ctx: ctx, - sourceID: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetPostBySourceID(tt.args.ctx, 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 Test_postgresqlConnection_GetUserFavoritesCount(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 - - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) - validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) - validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) - - expectedResultPosts := []models.Post{ - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, - Rating: "safe", - }, - { - - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, - Rating: "safe", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, - Rating: "questionable", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, - Rating: "safe", - }, - } - - err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - for _, post := range expectedResultPosts { - err = postgres.CreatePost(ctx, gormDB, &post) - if err != nil { - t.Fatal(err) - } - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, post.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want int64 - wantErr bool - }{ - { - name: "Test 1: Valid anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - }, - want: 6, - wantErr: false, - }, - { - name: "Test 2: Invalid anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - anthroveUserID: "2", - }, - want: 0, - wantErr: true, - }, - { - name: "Test 3: no anthroveUserID and 6 favorite posts", - args: args{ - ctx: ctx, - anthroveUserID: "", - }, - want: 0, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetUserFavoritesCount(tt.args.ctx, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetUserFavoritesCount() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got != tt.want { - t.Errorf("GetUserFavoritesCount() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserSourceLinks(t *testing.T) { - // Setup trow away containert - 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 - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validSourceID1 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - validSourceID2 := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source2")) - - eSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID1}, - DisplayName: "e621", - Domain: "e621.net", - } - err = postgres.CreateSource(ctx, gormDB, eSource) - if err != nil { - t.Fatal(err) - } - - faSource := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: validSourceID2}, - DisplayName: "fa", - Domain: "fa.net", - } - err = postgres.CreateSource(ctx, gormDB, faSource) - if err != nil { - t.Fatal(err) - } - - expectedResult := make(map[string]models.UserSource) - expectedResult["e621"] = models.UserSource{ - UserID: "e1", - AccountUsername: "e621-user", - Source: models.Source{ - DisplayName: eSource.DisplayName, - Domain: eSource.Domain, - }, - } - expectedResult["fa"] = models.UserSource{ - UserID: "fa1", - AccountUsername: "fa-user", - Source: models.Source{ - DisplayName: faSource.DisplayName, - Domain: faSource.Domain, - }, - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) - if err != nil { - t.Fatal(err) - } - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want map[string]models.UserSource - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - }, - want: expectedResult, - wantErr: false, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllUserSources(tt.args.ctx, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUserSources() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllUserSources() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserSourceBySourceID(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - expectedResult := &models.UserSource{ - UserID: string(validUserID), - AccountUsername: "euser", - Source: models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: source.ID}, - DisplayName: source.DisplayName, - Domain: source.Domain, - Icon: source.Icon, - }, - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult.UserID, expectedResult.AccountUsername) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - } - - tests := []struct { - name string - args args - want *models.UserSource - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID and sourceID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: source.ID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveUserID and valid sourceID", - args: args{ - ctx: ctx, - anthroveUserID: invalidUserID, - sourceID: source.ID, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Valid AnthroveUserID and invalid sourceID", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "fa", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 4: No AnthroveUserID and Valid sourceID", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: source.ID, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 5: Valid AnthroveUserID and No SourceDisplayName", - args: args{ - ctx: ctx, - anthroveUserID: "1", - sourceID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 6: No AnthroveUserID and No SourceDisplayName", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetUserSourceBySourceID(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID) - if (err != nil) != tt.wantErr { - t.Errorf("GetUserSourceBySourceID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkUserSource(got, tt.want) { - t.Errorf("GetUserSourceBySourceID() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllUsers(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 - validUserID01 := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - validUserID02 := models.AnthroveUserID(fmt.Sprintf("%025s", "User2")) - validUserID03 := models.AnthroveUserID(fmt.Sprintf("%025s", "User3")) - - users := []models.User{ - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID01}, - }, - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID02}, - }, - { - BaseModel: models.BaseModel[models.AnthroveUserID]{ID: validUserID03}, - }, - } - - for _, user := range users { - err = postgres.CreateUser(ctx, gormDB, user.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.User - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - }, - want: users, - wantErr: false, - }} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllUsers(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkUser(got, tt.want) { - t.Errorf("GetAllUsers() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserFavoriteWithPagination(t *testing.T) { - // Setup trow away containert - 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 - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - validPostID4 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post4")) - validPostID5 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post5")) - validPostID6 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post6")) - - expectedResultPosts := []models.Post{ - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, - Rating: "safe", - }, - { - - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, - Rating: "safe", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID4}, - Rating: "explicit", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID5}, - Rating: "questionable", - }, - { - BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID6}, - Rating: "safe", - }, - } - expectedResult := &models.FavoriteList{ - Posts: expectedResultPosts, - } - expectedResult2 := &models.FavoriteList{ - Posts: expectedResultPosts[2:], - } - expectedResult3 := &models.FavoriteList{ - Posts: expectedResultPosts[:3], - } - - err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - for _, expectedResultPost := range expectedResultPosts { - err = postgres.CreatePost(ctx, gormDB, &expectedResultPost) - if err != nil { - t.Fatal(err) - } - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, expectedResultPost.ID) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - skip int - limit int - } - tests := []struct { - name string - args args - want *models.FavoriteList - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveUserID", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - skip: 0, - limit: 2000, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: Skip first two", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - skip: 2, - limit: 2000, - }, - want: expectedResult2, - wantErr: false, - }, - { - name: "Test 3: Limit of 3", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - skip: 0, - limit: 3, - }, - want: expectedResult3, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllUserFavoritesWithPagination(tt.args.ctx, tt.args.anthroveUserID, tt.args.skip, tt.args.limit) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllUserFavoritesWithPagination() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkFavoritePosts(got, tt.want) { - t.Errorf("GetAllUserFavoritesWithPagination() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetUserTagWitRelationToFavedPosts(t *testing.T) { - // Setup trow away containert - 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 - validAnthroveUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1")) - - validPostID1 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post1")) - validPostID2 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post2")) - validPostID3 := models.AnthrovePostID(fmt.Sprintf("%025s", "Post3")) - - err = postgres.CreateUser(ctx, gormDB, validAnthroveUserID) - if err != nil { - t.Fatal(err) - } - - posts := []models.Post{ - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID1}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID2}, Rating: "safe"}, - {BaseModel: models.BaseModel[models.AnthrovePostID]{ID: validPostID3}, Rating: "explicit"}, - } - - for _, post := range posts { - err = postgres.CreatePost(ctx, gormDB, &post) - if err != nil { - t.Fatal(err) - } - err = postgres.CreateReferenceBetweenUserAndPost(ctx, gormDB, validAnthroveUserID, models.AnthrovePostID(post.ID)) - if err != nil { - t.Fatal(err) - } - } - - tags := []models.Tag{ - {Name: "JayTheFerret", Type: "artist"}, - {Name: "Ferret", Type: "species"}, - {Name: "Jay", Type: "character"}, - } - - for i, tag := range tags { - err = postgres.CreateTagAndReferenceToPost(ctx, gormDB, posts[i].ID, &tag) - if err != nil { - t.Fatal(err) - } - } - - expectedResult := []models.TagsWithFrequency{ - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[0].Name, - Type: tags[0].Type, - }, - }, - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[2].Name, - Type: tags[2].Type, - }, - }, - { - Frequency: 1, - Tags: models.Tag{ - Name: tags[1].Name, - Type: tags[1].Type, - }, - }, - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - } - tests := []struct { - name string - args args - want []models.TagsWithFrequency - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - anthroveUserID: validAnthroveUserID, - }, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagsFromUser(tt.args.ctx, tt.args.anthroveUserID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagsFromUser() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagsFromUser() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTags(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 - - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: "artist", - }, - { - Name: "anthro", - Type: "general", - }, - { - Name: "soxx", - Type: "character", - }, - } - - for _, tag := range tags { - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.Tag - wantErr bool - }{ - { - name: "Test 1: Get Tags", - args: args{ - ctx: ctx, - }, - want: tags, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTags(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTags() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTags() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllSources(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 = postgres.CreateSource(ctx, gormDB, &source) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.Source - wantErr bool - }{ - { - name: "Test 1: Get all entries", - args: args{ - ctx: ctx, - }, - want: sources, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllSources(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllSources() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkSources(got, tt.want) { - t.Errorf("GetAllSources() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetSourceByDomain(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 = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - sourceDomain models.AnthroveSourceDomain - } - tests := []struct { - name string - args args - want *models.Source - wantErr bool - }{ - { - name: "Test 1: Valid URL", - args: args{ - ctx: ctx, - sourceDomain: "e621.net", - }, - want: source, - wantErr: false, - }, - { - name: "Test 2: Invalid URL", - args: args{ - ctx: ctx, - sourceDomain: "eeeee.net", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 2: No URL", - args: args{ - ctx: ctx, - sourceDomain: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetSourceByDomain(tt.args.ctx, tt.args.sourceDomain) - if (err != nil) != tt.wantErr { - t.Errorf("GetSourceByDomain() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !checkSource(got, tt.want) { - t.Errorf("GetSourceByDomain() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_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) - } - defer container.Terminate(ctx) - - // Setup Test - - // Test - type args struct { - dbPool *gorm.DB - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Migrate Databases", - args: args{dbPool: gormDB}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.migrateDatabase(tt.args.dbPool); (err != nil) != tt.wantErr { - t.Errorf("migrateDatabase() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -// Helper functions for validation purposes -func checkSource(got *models.Source, want *models.Source) bool { - - if want == nil && got == nil { - return true - } - - if got.Domain != want.Domain { - return false - } - - return true - -} -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 -} -func checkSources(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 checkUser(got []models.User, want []models.User) bool { - for i, user := range want { - if user.ID != got[i].ID { - return false - } - } - return true -} -func checkUserSource(got *models.UserSource, want *models.UserSource) bool { - - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - if got.UserID != want.UserID { - return false - } - if got.AccountUsername != want.AccountUsername { - return false - } - if got.Source.DisplayName != want.Source.DisplayName { - return false - } - if got.Source.Domain != want.Source.Domain { - return false - } - if got.Source.Icon != want.Source.Icon { - return false - } - - return true -} - -//------------------------- - -func Test_postgresqlConnection_CreateTagAlias(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 - - validTagAliasName01 := models.AnthroveTagAliasName("httyd") - validTagAliasName02 := models.AnthroveTagAliasName("dragon") - - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - tagAliasName models.AnthroveTagAliasName - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - tagAliasName: validTagAliasName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 2: No TagAliasName", - args: args{ - ctx: ctx, - tagAliasName: "", - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 4: No tagID", - args: args{ - ctx: ctx, - tagAliasName: validTagAliasName01, - tagID: "", - }, - wantErr: true, - }, - { - name: "Test 6: Invalide tagID", - args: args{ - ctx: ctx, - tagAliasName: validTagAliasName02, - tagID: "aaa", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagAlias(tt.args.ctx, tt.args.tagAliasName, tt.args.tagID); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateTagAliasInBatch(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 - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - err = postgres.CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags)) - if err != nil { - t.Fatal(err) - } - - tagAlias := []models.TagAlias{ - { - Name: "test1", - TagID: tags[0].Name, - }, - { - Name: "test2", - TagID: tags[1].Name, - }, - { - Name: "test3", - TagID: tags[2].Name, - }, - { - Name: "test4", - TagID: tags[3].Name, - }, - } - emptyTagAlias := []models.TagAlias{} - - // Test - type args struct { - ctx context.Context - tagAliases []models.TagAlias - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - tagAliases: tagAlias, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - tagAliases: emptyTagAlias, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - tagAliases: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - tagAliases: tagAlias, - batchSize: 0, - }, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagAliasInBatch(tt.args.ctx, tt.args.tagAliases, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagAliasInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagAlias(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 - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagAlias{ - { - Name: string(validTagAliases[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[2]), - TagID: string(validTagID), - }, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.TagAlias - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ctx: ctx}, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagAlias(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagAlias() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagAlias() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagAliasByTag(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 - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagAlias{ - { - Name: string(validTagAliases[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagAliases[2]), - TagID: string(validTagID), - }, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - want []models.TagAlias - wantErr bool - }{ - { - name: "Test 1: Valid TagID", - args: args{ - ctx: ctx, - tagID: validTagID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No TagID", - args: args{ - ctx: ctx, - tagID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Invalid TagID", - args: args{ - ctx: ctx, - tagID: "adads", - }, - want: []models.TagAlias{}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagAliasByTag(tt.args.ctx, tt.args.tagID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagAliasByTag() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagAliasByTag() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_DeleteTagAlias(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 - validTagID := models.AnthroveTagID("toothless") - validTagAliases := []models.AnthroveTagAliasName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagAliasName := range validTagAliases { - err = postgres.CreateTagAlias(ctx, gormDB, tagAliasName, validTagID) - } - - // Test - type args struct { - ctx context.Context - tagAliasName models.AnthroveTagAliasName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveTagAliasName", - args: args{ - ctx: ctx, - tagAliasName: validTagAliases[0], - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveTagAliasName", - args: args{ - ctx: ctx, - tagAliasName: "asdad", - }, - wantErr: false, - }, - { - name: "Test 3: No AnthroveTagAliasName", - args: args{ - ctx: ctx, - tagAliasName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.DeleteTagAlias(tt.args.ctx, tt.args.tagAliasName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -//-------------------------- - -func Test_postgresqlConnection_CreateTagGroup(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 - - validTagGroupName01 := models.AnthroveTagGroupName("httyd") - validTagGroupName02 := models.AnthroveTagGroupName("dragon") - - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - tagGroupName models.AnthroveTagGroupName - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupName01, - tagID: validTagID, - }, - wantErr: false, - }, - { - name: "Test 2: No TagGroupName", - args: args{ - ctx: ctx, - tagGroupName: "", - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 4: No tagID", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupName01, - tagID: "", - }, - wantErr: true, - }, - { - name: "Test 5: Duplicate tagID", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupName01, - tagID: validTagID, - }, - wantErr: true, - }, - { - name: "Test 6: Invalide tagID", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupName02, - tagID: "aaa", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagGroup(tt.args.ctx, tt.args.tagGroupName, tt.args.tagID); (err != nil) != tt.wantErr { - t.Errorf("CreateTagGroup() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreateTagGroupInBatch(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 - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - err = postgres.CreateTagInBatchAndUpdate(ctx, gormDB, tags, len(tags)) - if err != nil { - t.Fatal(err) - } - - tagGroup := []models.TagGroup{ - { - Name: "test1", - TagID: tags[0].Name, - }, - { - Name: "test2", - TagID: tags[1].Name, - }, - { - Name: "test3", - TagID: tags[2].Name, - }, - { - Name: "test4", - TagID: tags[3].Name, - }, - } - emptyTagGroup := []models.TagGroup{} - - // Test - type args struct { - ctx context.Context - tagGroups []models.TagGroup - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - tagGroups: tagGroup, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - tagGroups: emptyTagGroup, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - tagGroups: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - tagGroups: tagGroup, - batchSize: 0, - }, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagGroupInBatch(tt.args.ctx, tt.args.tagGroups, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagGroupInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagGroup(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 - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagGroup{ - { - Name: string(validTagGroupes[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[2]), - TagID: string(validTagID), - }, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want []models.TagGroup - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ctx: ctx}, - want: expectedResult, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagGroup(tt.args.ctx) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagGroup() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagGroup() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagGroupByTag(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 - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - expectedResult := []models.TagGroup{ - { - Name: string(validTagGroupes[0]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[1]), - TagID: string(validTagID), - }, - { - Name: string(validTagGroupes[2]), - TagID: string(validTagID), - }, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - tagID models.AnthroveTagID - } - tests := []struct { - name string - args args - want []models.TagGroup - wantErr bool - }{ - { - name: "Test 1: Valid TagID", - args: args{ - ctx: ctx, - tagID: validTagID, - }, - want: expectedResult, - wantErr: false, - }, - { - name: "Test 2: No TagID", - args: args{ - ctx: ctx, - tagID: "", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: Invalid TagID", - args: args{ - ctx: ctx, - tagID: "adads", - }, - want: []models.TagGroup{}, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagGroupByTag(tt.args.ctx, tt.args.tagID) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagGroupByTag() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagGroupByTag() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_DeleteTagGroup(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 - validTagID := models.AnthroveTagID("toothless") - validTagGroupes := []models.AnthroveTagGroupName{"httyd", "dragon", "scaly"} - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - for _, tagGroupName := range validTagGroupes { - err = postgres.CreateTagGroup(ctx, gormDB, tagGroupName, validTagID) - } - - // Test - type args struct { - ctx context.Context - tagGroupName models.AnthroveTagGroupName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid AnthroveTagGroupName", - args: args{ - ctx: ctx, - tagGroupName: validTagGroupes[0], - }, - wantErr: false, - }, - { - name: "Test 2: Invalid AnthroveTagGroupName", - args: args{ - ctx: ctx, - tagGroupName: "asdad", - }, - wantErr: false, - }, - { - name: "Test 3: No AnthroveTagGroupName", - args: args{ - ctx: ctx, - tagGroupName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.DeleteTagGroup(tt.args.ctx, tt.args.tagGroupName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTagAlias() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -//-------------------------- - -func Test_postgresqlConnection_UpdateUserSourceScrapeTimeInterval(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - scrapeTime models.AnthroveScrapeTimeInterval - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: validSourceID, - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "111", - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "", - scrapeTime: 10, - }, - wantErr: true, - }, - { - name: "Test 5: scrapeTime is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - scrapeTime: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.UpdateUserSourceScrapeTimeInterval(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.scrapeTime); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceScrapeTimeInterval() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_UpdateUserSourceLastScrapeTime(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - validScrapeTime := models.AnthroveUserLastScrapeTime(time.Now()) - inValidScrapeTime := models.AnthroveUserLastScrapeTime{} - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - lastScrapeTime models.AnthroveUserLastScrapeTime - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: validSourceID, - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "111", - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "", - lastScrapeTime: validScrapeTime, - }, - wantErr: true, - }, - { - name: "Test 5: scrapeTime is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - lastScrapeTime: inValidScrapeTime, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.UpdateUserSourceLastScrapeTime(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.lastScrapeTime); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceLastScrapeTime() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_UpdateUserSourceValidation(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") - - validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1")) - - source := &models.Source{ - BaseModel: models.BaseModel[models.AnthroveSourceID]{ - ID: validSourceID, - }, - DisplayName: "e621", - Domain: "e621.net", - Icon: "https://e621.icon", - } - - err = postgres.CreateSource(ctx, gormDB, source) - if err != nil { - t.Fatal(err) - } - - err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, "e66e6e6e6", "euser") - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - anthroveUserID models.AnthroveUserID - sourceID models.AnthroveSourceID - valid bool - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: validSourceID, - valid: true, - }, - wantErr: false, - }, - { - name: "Test 2: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: invalidUserID, - sourceID: validSourceID, - valid: true, - }, - wantErr: true, - }, - { - name: "Test 3: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: "", - sourceID: validSourceID, - valid: true, - }, - wantErr: true, - }, - { - name: "Test 4: anthroveUserID to short", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "111", - valid: true, - }, - wantErr: true, - }, - { - name: "Test 5: anthroveUserID is empty", - args: args{ - ctx: ctx, - anthroveUserID: validUserID, - sourceID: "", - valid: true, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.UpdateUserSourceValidation(tt.args.ctx, tt.args.anthroveUserID, tt.args.sourceID, tt.args.valid); (err != nil) != tt.wantErr { - t.Errorf("UpdateUserSourceValidation() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -//-------------------------- - -func Test_postgresqlConnection_DeleteTag(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 - validTagID := models.AnthroveTagID("toothless") - - validTag := &models.Tag{ - Name: string(validTagID), - Type: models.Character, - } - - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(validTag.Name), validTag.Type) - if err != nil { - t.Fatal(err) - } - - // Test - type args struct { - ctx context.Context - tagName models.AnthroveTagName - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid TagName", - args: args{ - ctx: ctx, - tagName: models.AnthroveTagName(validTagID), - }, - wantErr: false, - }, - { - name: "Test 2: Invalid TagName", - args: args{ - ctx: ctx, - tagName: models.AnthroveTagName("aaa"), - }, - wantErr: false, - }, - { - name: "Test 3: No TagName", - args: args{ - ctx: ctx, - tagName: "", - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.DeleteTag(tt.args.ctx, tt.args.tagName); (err != nil) != tt.wantErr { - t.Errorf("DeleteTag() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_GetAllTagsByTagType(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 - - validTags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Character, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Alphyron", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - } - - expectetResult := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Character, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Alphyron", - Type: models.Character, - }, - } - - for _, tag := range validTags { - err = postgres.CreateTag(ctx, gormDB, models.AnthroveTagName(tag.Name), tag.Type) - if err != nil { - t.Fatal(err) - } - } - - // Test - type args struct { - ctx context.Context - tagType models.TagType - } - tests := []struct { - name string - args args - want []models.Tag - wantErr bool - }{ - { - name: "Test 1: Get Data", - args: args{ - ctx: ctx, - tagType: models.Character, - }, - want: expectetResult, - wantErr: false, - }, - { - name: "Test 2: invalid Tag Type", - args: args{ - ctx: ctx, - tagType: "aa", - }, - want: nil, - wantErr: true, - }, - { - name: "Test 3: No Tag Type", - args: args{ - ctx: ctx, - tagType: "", - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.GetAllTagsByTagType(tt.args.ctx, tt.args.tagType) - if (err != nil) != tt.wantErr { - t.Errorf("GetAllTagsByTagType() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetAllTagsByTagType() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_postgresqlConnection_CreateTagInBatchAndUpdate(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 - tags := []models.Tag{ - { - Name: "JayTheFerret", - Type: models.Artist, - }, - { - Name: "SoXX", - Type: models.Character, - }, - { - Name: "Dragon", - Type: models.Species, - }, - { - Name: "Fennec", - Type: models.Species, - }, - } - emptyTags := []models.Tag{} - - // Test - type args struct { - ctx context.Context - tags []models.Tag - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Tags", - args: args{ - ctx: ctx, - tags: tags, - batchSize: 10, - }, - wantErr: false, - }, - { - name: "Test 2: Empty Tags", - args: args{ - ctx: ctx, - tags: emptyTags, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Tags", - args: args{ - ctx: ctx, - tags: nil, - batchSize: 10, - }, - wantErr: true, - }, - { - name: "Test 4: No batchSize", - args: args{ - ctx: ctx, - tags: nil, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreateTagInBatchAndUpdate(tt.args.ctx, tt.args.tags, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreateTagInBatchAndUpdate() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_postgresqlConnection_CreatePostInBatch(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 - anthrovePost []models.Post - batchSize int - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Test 1: Valid Data", - args: args{ - ctx: ctx, - anthrovePost: validPosts, - batchSize: len(validPosts), - }, - wantErr: false, - }, - { - name: "Test 2: Emtpy Data", - args: args{ - ctx: ctx, - anthrovePost: emptyPost, - batchSize: 0, - }, - wantErr: true, - }, - { - name: "Test 3: Nil Data", - args: args{ - ctx: ctx, - anthrovePost: nil, - batchSize: 0, - }, - wantErr: true, - }, - { - name: "Test 4: batchSize 0", - args: args{ - ctx: ctx, - anthrovePost: validPosts, - batchSize: 0, - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - if err := p.CreatePostInBatch(tt.args.ctx, tt.args.anthrovePost, tt.args.batchSize); (err != nil) != tt.wantErr { - t.Errorf("CreatePostInBatch() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func checkFavoritePosts(got *models.FavoriteList, want *models.FavoriteList) bool { - if got == nil && want == nil { - return true - } else if got == nil || want == nil { - return false - } - - for i, post := range got.Posts { - if post.ID == want.Posts[i].ID { - } else { - return false - } - } - - return true -} - -func Test_postgresqlConnection_ExecuteRawStatement(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 - query string - args []any - } - tests := []struct { - name string - args args - want *sql.Rows - wantErr bool - }{ - { - name: "Test 01: Empty Query", - args: args{ - ctx: ctx, - query: "", - args: nil, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 02: Nil Query", - args: args{ - ctx: ctx, - query: "aasd", - args: nil, - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - err := p.ExecuteRawStatement(tt.args.ctx, tt.args.query, tt.args.args...) - if (err != nil) != tt.wantErr { - t.Errorf("ExecuteRawStatement() error = %v, wantErr %v", err, tt.wantErr) - return - } - }) - } -} - -func Test_postgresqlConnection_QueryRawStatement(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 - query string - args []any - } - tests := []struct { - name string - args args - want *sql.Rows - wantErr bool - }{ - { - name: "Test 01: Empty Query", - args: args{ - ctx: ctx, - query: "", - args: nil, - }, - want: nil, - wantErr: true, - }, - { - name: "Test 02: Nil Query", - args: args{ - ctx: ctx, - query: "aasd", - args: nil, - }, - want: nil, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &postgresqlConnection{ - db: gormDB, - debug: true, - } - got, err := p.QueryRawStatement(tt.args.ctx, 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) - } - }) - } -} diff --git a/pkg/database/scopes.go b/pkg/database/scopes.go new file mode 100644 index 0000000..958d1a3 --- /dev/null +++ b/pkg/database/scopes.go @@ -0,0 +1,91 @@ +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) + } + +} diff --git a/pkg/database/scopes_test.go b/pkg/database/scopes_test.go new file mode 100644 index 0000000..8a42d9d --- /dev/null +++ b/pkg/database/scopes_test.go @@ -0,0 +1,328 @@ +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) + } + + }) + } +} diff --git a/pkg/database/source.go b/pkg/database/source.go index 544bfcc..5dda285 100644 --- a/pkg/database/source.go +++ b/pkg/database/source.go @@ -2,18 +2,246 @@ package database import ( "context" + "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "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" ) -type Source interface { +func CreateSource(ctx context.Context, source models.Source) (models.Source, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource") + defer span.End() - // CreateSource adds a new source to the database. - CreateSource(ctx context.Context, anthroveSource *models.Source) error + utils.HandleEvent(span, localLogger, "Starting source creation") - // GetAllSources retrieves all sources. - GetAllSources(ctx context.Context) ([]models.Source, error) + if client == nil { + return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } - // GetSourceByDomain retrieves a source by its URL. - GetSourceByDomain(ctx context.Context, sourceDomain models.AnthroveSourceDomain) (*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 } diff --git a/pkg/database/source_test.go b/pkg/database/source_test.go new file mode 100644 index 0000000..d99f036 --- /dev/null +++ b/pkg/database/source_test.go @@ -0,0 +1,546 @@ +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 +} diff --git a/pkg/database/tag.go b/pkg/database/tag.go index c7fe822..25d13fd 100644 --- a/pkg/database/tag.go +++ b/pkg/database/tag.go @@ -2,19 +2,133 @@ package database import ( "context" + "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "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" ) -type Tag interface { - CreateTag(ctx context.Context, tagName models.AnthroveTagName, tagType models.TagType) error +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() - CreateTagInBatchAndUpdate(ctx context.Context, tags []models.Tag, batchSize int) error + localLogger = localLogger.WithFields(log.Fields{ + "tag_name": tagName, + "tag_type": tagType, + }) - // GetAllTags retrieves all tags. - GetAllTags(ctx context.Context) ([]models.Tag, error) + span.SetAttributes( + attribute.String("tag_name", string(tagName)), + attribute.String("tag_type", string(tagType)), + ) - GetAllTagsByTagType(ctx context.Context, tagType models.TagType) ([]models.Tag, error) + utils.HandleEvent(span, localLogger, "Starting tag creation") - DeleteTag(ctx context.Context, tagName models.AnthroveTagName) 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 } diff --git a/pkg/database/tagAlias.go b/pkg/database/tagAlias.go new file mode 100644 index 0000000..10f643f --- /dev/null +++ b/pkg/database/tagAlias.go @@ -0,0 +1,134 @@ +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 +} diff --git a/pkg/database/tagAlias_test.go b/pkg/database/tagAlias_test.go new file mode 100644 index 0000000..e5bd1eb --- /dev/null +++ b/pkg/database/tagAlias_test.go @@ -0,0 +1,292 @@ +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) + } + }) + } +} diff --git a/pkg/database/tagGroup.go b/pkg/database/tagGroup.go new file mode 100644 index 0000000..ef4dca2 --- /dev/null +++ b/pkg/database/tagGroup.go @@ -0,0 +1,137 @@ +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 +} diff --git a/pkg/database/tagGroup_test.go b/pkg/database/tagGroup_test.go new file mode 100644 index 0000000..8d087a4 --- /dev/null +++ b/pkg/database/tagGroup_test.go @@ -0,0 +1,292 @@ +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) + } + }) + } +} diff --git a/pkg/database/tag_test.go b/pkg/database/tag_test.go new file mode 100644 index 0000000..0c36ff6 --- /dev/null +++ b/pkg/database/tag_test.go @@ -0,0 +1,262 @@ +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) + } + }) + } +} diff --git a/pkg/database/tagalias.go b/pkg/database/tagalias.go deleted file mode 100644 index 0f26efb..0000000 --- a/pkg/database/tagalias.go +++ /dev/null @@ -1,19 +0,0 @@ -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 -} diff --git a/pkg/database/taggroup.go b/pkg/database/taggroup.go deleted file mode 100644 index e293c68..0000000 --- a/pkg/database/taggroup.go +++ /dev/null @@ -1,19 +0,0 @@ -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 -} diff --git a/pkg/database/user.go b/pkg/database/user.go index ecf5900..b6e2b69 100644 --- a/pkg/database/user.go +++ b/pkg/database/user.go @@ -2,41 +2,107 @@ package database import ( "context" + "errors" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "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" ) -type User interface { +func CreateUser(ctx context.Context, user models.User) (models.User, error) { - // 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 + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUser") + defer span.End() - // CreateReferenceBetweenUserAndPost links a user with a post. - CreateReferenceBetweenUserAndPost(ctx context.Context, anthroveUserID models.AnthroveUserID, anthrovePostID models.AnthrovePostID) error + localLogger = localLogger.WithFields(log.Fields{ + "user_id": user.ID, + }) - UpdateUserSourceScrapeTimeInterval(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, scrapeTime models.AnthroveScrapeTimeInterval) error + span.SetAttributes( + attribute.String("user_id", string(user.ID)), + ) - UpdateUserSourceLastScrapeTime(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, lastScrapeTime models.AnthroveUserLastScrapeTime) error + utils.HandleEvent(span, localLogger, "Starting user creation") - UpdateUserSourceValidation(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, valid bool) error + if client == nil { + return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } - GetAllUsers(ctx context.Context) ([]models.User, 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) + } - // 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) + 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 } diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go new file mode 100644 index 0000000..c21c712 --- /dev/null +++ b/pkg/database/userSource.go @@ -0,0 +1,173 @@ +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 +} diff --git a/pkg/database/userSource_test.go b/pkg/database/userSource_test.go new file mode 100644 index 0000000..4a4b192 --- /dev/null +++ b/pkg/database/userSource_test.go @@ -0,0 +1,498 @@ +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 +} diff --git a/pkg/database/user_test.go b/pkg/database/user_test.go new file mode 100644 index 0000000..2affc6b --- /dev/null +++ b/pkg/database/user_test.go @@ -0,0 +1,219 @@ +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 +} diff --git a/pkg/error/database.go b/pkg/error/database.go index 278a043..c372d14 100644 --- a/pkg/error/database.go +++ b/pkg/error/database.go @@ -1,25 +1,19 @@ package error -type EntityAlreadyExists struct{} +import "fmt" -func (e *EntityAlreadyExists) Error() string { - return "EntityAlreadyExists error" +const ( + EntityAlreadyExists = "EntityAlreadyExists" + NoDataWritten = "NoDataWritten" + NoDataFound = "NoDataFound" + DatabaseIsNotConnected = "database is not connected" + DuplicateKey = "DuplicateKey" +) + +type Database struct { + Reason string } -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" +func (e Database) Error() string { + return fmt.Sprintf("Database error: %s", e.Reason) } diff --git a/pkg/error/database_test.go b/pkg/error/database_test.go index 5fd2e1f..96143cd 100644 --- a/pkg/error/database_test.go +++ b/pkg/error/database_test.go @@ -2,79 +2,26 @@ package error import "testing" -func TestEntityAlreadyExists_Error(t *testing.T) { +func TestDatabase_Error(t *testing.T) { + type fields struct { + Reason string + } tests := []struct { - name string - want string + name string + fields fields + want string }{ { - name: "Test : Valid error String", - want: "EntityAlreadyExists error", + name: "Test 1: Reason", + fields: fields{Reason: "TEST ERROR"}, + want: "Database error: TEST ERROR", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - 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{} + e := Database{ + Reason: tt.fields.Reason, + } if got := e.Error(); got != tt.want { t.Errorf("Error() = %v, want %v", got, tt.want) } diff --git a/pkg/error/validation.go b/pkg/error/validation.go index 27ec3c4..889744b 100644 --- a/pkg/error/validation.go +++ b/pkg/error/validation.go @@ -3,11 +3,33 @@ package error import "fmt" const ( - 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" + 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" ) type EntityValidationFailed struct { diff --git a/pkg/models/api.go b/pkg/models/api.go deleted file mode 100644 index 37adbd0..0000000 --- a/pkg/models/api.go +++ /dev/null @@ -1,5 +0,0 @@ -package models - -type FavoriteList struct { - Posts []Post `json:"posts,omitempty"` -} diff --git a/pkg/models/const.go b/pkg/models/const.go index 7e84b0c..092a803 100644 --- a/pkg/models/const.go +++ b/pkg/models/const.go @@ -2,20 +2,32 @@ package models import "time" -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 +type ( + UserID string + PostID string + PostURL string -type Rating string -type TagType 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 +) const ( SFW Rating = "safe" diff --git a/pkg/models/orm.go b/pkg/models/orm.go index f9209d9..2cf6cf5 100644 --- a/pkg/models/orm.go +++ b/pkg/models/orm.go @@ -8,13 +8,13 @@ import ( ) type ID interface { - AnthroveUserID | AnthroveSourceID | AnthrovePostID + UserID | SourceID | PostID | UserSourceID | UserFavoriteID } type BaseModel[T ID] struct { ID T `json:"id" gorm:"primaryKey"` - CreatedAt time.Time `json:"-"` - UpdatedAt time.Time `json:"-"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` DeletedAt gorm.DeletedAt `json:"-" gorm:"index"` } diff --git a/pkg/models/orm_test.go b/pkg/models/orm_test.go index cf9e1a4..780b67d 100644 --- a/pkg/models/orm_test.go +++ b/pkg/models/orm_test.go @@ -42,8 +42,8 @@ func TestBaseModel_BeforeCreate(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - base := &BaseModel[AnthrovePostID]{ - ID: AnthrovePostID(tt.fields.ID), + base := &BaseModel[PostID]{ + ID: PostID(tt.fields.ID), CreatedAt: tt.fields.CreatedAt, UpdatedAt: tt.fields.UpdatedAt, DeletedAt: tt.fields.DeletedAt, diff --git a/pkg/models/post.go b/pkg/models/post.go index d272387..479e4cf 100644 --- a/pkg/models/post.go +++ b/pkg/models/post.go @@ -2,10 +2,10 @@ package models // Post model type Post struct { - BaseModel[AnthrovePostID] + BaseModel[PostID] Rating Rating `json:"rating" gorm:"type:enum('safe','questionable','explicit')"` - Tags []Tag `json:"-" gorm:"many2many:post_tags;"` - Favorites []UserFavorites `json:"-" gorm:"foreignKey:PostID"` + Tags []Tag `json:"tags,omitempty" gorm:"many2many:post_tags;"` + Favorites []UserFavorite `json:"-" gorm:"foreignKey:PostID"` References []PostReference `json:"references" gorm:"foreignKey:PostID"` } diff --git a/pkg/models/postReference_test.go b/pkg/models/postReference_test.go index d203024..5353383 100644 --- a/pkg/models/postReference_test.go +++ b/pkg/models/postReference_test.go @@ -3,42 +3,9 @@ package models import "testing" func TestPostReference_TableName(t *testing.T) { - 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) - } - }) + postReference := PostReference{} + expectedTableName := "PostReference" + if tableName := postReference.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/post_test.go b/pkg/models/post_test.go index 2e7228d..aadf104 100644 --- a/pkg/models/post_test.go +++ b/pkg/models/post_test.go @@ -3,36 +3,9 @@ package models import "testing" func TestPost_TableName(t *testing.T) { - 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) - } - }) + post := Post{} + expectedTableName := "Post" + if tableName := post.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/source.go b/pkg/models/source.go index 9027b55..be62f2e 100644 --- a/pkg/models/source.go +++ b/pkg/models/source.go @@ -2,9 +2,9 @@ package models // Source model type Source struct { - BaseModel[AnthroveSourceID] + BaseModel[SourceID] DisplayName string `json:"display_name" ` - Domain string `json:"domain" gorm:"not null;unique"` + Domain SourceDomain `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"` diff --git a/pkg/models/source_test.go b/pkg/models/source_test.go index e65b2c8..50b04c8 100644 --- a/pkg/models/source_test.go +++ b/pkg/models/source_test.go @@ -3,38 +3,9 @@ package models import "testing" func TestSource_TableName(t *testing.T) { - 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) - } - }) + source := Source{} + expectedTableName := "Source" + if tableName := source.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/tag.go b/pkg/models/tag.go index 09fafb9..4e87361 100644 --- a/pkg/models/tag.go +++ b/pkg/models/tag.go @@ -2,11 +2,11 @@ package models // Tag models type Tag struct { - Name string `json:"name" gorm:"primaryKey"` + Name TagName `json:"name" gorm:"primaryKey"` Type TagType `json:"type" gorm:"column:tag_type"` - Aliases []TagAlias `json:"aliases" gorm:"foreignKey:TagID"` - Groups []TagGroup `json:"groups" gorm:"foreignKey:TagID"` - Posts []Post `json:"posts" gorm:"many2many:post_tags;"` + 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;"` } func (Tag) TableName() string { @@ -15,8 +15,8 @@ func (Tag) TableName() string { // TagAlias model type TagAlias struct { - Name string `json:"name" gorm:"primaryKey"` - TagID string `json:"tag_id"` + Name TagAliasName `json:"name" gorm:"primaryKey"` + TagID TagName `json:"tag_id"` } func (TagAlias) TableName() string { @@ -25,8 +25,8 @@ func (TagAlias) TableName() string { // TagGroup model type TagGroup struct { - Name string `json:"name" gorm:"primaryKey"` - TagID string `json:"tag_id"` + Name TagGroupName `json:"name" gorm:"primaryKey"` + TagID TagName `json:"tag_id"` } func (TagGroup) TableName() string { diff --git a/pkg/models/tag_test.go b/pkg/models/tag_test.go index 6f66b18..eb2d729 100644 --- a/pkg/models/tag_test.go +++ b/pkg/models/tag_test.go @@ -3,94 +3,25 @@ package models import "testing" func TestTagAlias_TableName(t *testing.T) { - 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) - } - }) + tagAlias := TagAlias{} + expectedTableName := "TagAlias" + if tableName := tagAlias.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } func TestTagGroup_TableName(t *testing.T) { - 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) - } - }) + tagGroup := TagGroup{} + expectedTableName := "TagGroup" + if tableName := tagGroup.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) - } - }) +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) } } diff --git a/pkg/models/user.go b/pkg/models/user.go index bc2d575..bf544ce 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -2,9 +2,9 @@ package models // User model type User struct { - BaseModel[AnthroveUserID] - Favorites []UserFavorites `json:"-" gorm:"foreignKey:UserID"` - Sources []UserSource `json:"-" gorm:"foreignKey:UserID"` + BaseModel[UserID] + Favorites []UserFavorite `json:"-" gorm:"foreignKey:UserID"` + Sources []UserSource `json:"-" gorm:"foreignKey:UserID"` } func (User) TableName() string { diff --git a/pkg/models/userFavorite.go b/pkg/models/userFavorite.go index 182af8f..16fc87d 100644 --- a/pkg/models/userFavorite.go +++ b/pkg/models/userFavorite.go @@ -1,13 +1,13 @@ package models -import "time" - -type UserFavorites struct { - UserID string `json:"user_id" gorm:"primaryKey"` - PostID string `json:"post_id" gorm:"primaryKey"` - CreatedAt time.Time `json:"-"` +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"` } -func (UserFavorites) TableName() string { +func (UserFavorite) TableName() string { return "UserFavorites" } diff --git a/pkg/models/userFavorite_test.go b/pkg/models/userFavorite_test.go index 3b69143..84fb0b2 100644 --- a/pkg/models/userFavorite_test.go +++ b/pkg/models/userFavorite_test.go @@ -1,37 +1,11 @@ package models -import ( - "testing" - "time" -) +import "testing" func TestUserFavorite_TableName(t *testing.T) { - 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) - } - }) + userFavorite := UserFavorite{} + expectedTableName := "UserFavorites" + if tableName := userFavorite.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/userSource.go b/pkg/models/userSource.go index 9ee0f87..171c95e 100644 --- a/pkg/models/userSource.go +++ b/pkg/models/userSource.go @@ -3,16 +3,17 @@ package models import "time" type UserSource struct { + BaseModel[UserSourceID] User User `json:"user" gorm:"foreignKey:ID;references:UserID"` - UserID string `json:"user_id" gorm:"primaryKey"` + UserID UserID `json:"user_id"` Source Source `json:"source" gorm:"foreignKey:ID;references:SourceID"` - SourceID string `json:"source_id" gorm:"primaryKey"` + SourceID SourceID `json:"source_id"` 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:"-"` + AccountValidationKey string `json:"account_validation_key"` } func (UserSource) TableName() string { diff --git a/pkg/models/userSource_test.go b/pkg/models/userSource_test.go index ff8cd95..c68fc89 100644 --- a/pkg/models/userSource_test.go +++ b/pkg/models/userSource_test.go @@ -3,40 +3,9 @@ package models import "testing" func TestUserSource_TableName(t *testing.T) { - 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) - } - }) + userSource := UserSource{} + expectedTableName := "UserSource" + if tableName := userSource.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/pkg/models/user_test.go b/pkg/models/user_test.go index c31745b..505ea43 100644 --- a/pkg/models/user_test.go +++ b/pkg/models/user_test.go @@ -3,32 +3,9 @@ package models import "testing" func TestUser_TableName(t *testing.T) { - 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) - } - }) + user := User{} + expectedTableName := "User" + if tableName := user.TableName(); tableName != expectedTableName { + t.Fatalf("expected %s, but got %s", expectedTableName, tableName) } } diff --git a/test/generator.go b/test/generator.go new file mode 100644 index 0000000..d6aee26 --- /dev/null +++ b/test/generator.go @@ -0,0 +1,151 @@ +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 +} diff --git a/test/helper.go b/test/helper.go index 8cd3a9e..4d31273 100644 --- a/test/helper.go +++ b/test/helper.go @@ -8,13 +8,14 @@ import ( "strings" "time" - "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "git.anthrove.art/Anthrove/otter-space-sdk/v3/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" ) @@ -28,8 +29,7 @@ const ( func StartPostgresContainer(ctx context.Context) (*postgrescontainer.PostgresContainer, *gorm.DB, error) { - pgContainer, err := postgrescontainer.RunContainer(ctx, - testcontainers.WithImage("postgres:alpine"), + pgContainer, err := postgrescontainer.Run(ctx, "postgres:alpine", postgrescontainer.WithDatabase(databaseName), postgrescontainer.WithUsername(databaseUser), postgrescontainer.WithPassword(databasePassword), @@ -80,21 +80,23 @@ 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), + Logger: logger.Default.LogMode(logger.Info), + TranslateError: true, }) } -func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgrescontainer.PostgresContainer) (*models.DatabaseConfig, error) { +func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgrescontainer.PostgresContainer) (models.DatabaseConfig, error) { var err error + var databaseConfig models.DatabaseConfig connectionString, err := pgContainer.ConnectionString(ctx) if err != nil { - return nil, err + return databaseConfig, err } connectionStringUrl, err := url.Parse(connectionString) if err != nil { - return nil, err + return databaseConfig, err } split := strings.Split(connectionStringUrl.Host, ":") @@ -102,7 +104,7 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre port, err := strconv.Atoi(split[1]) if err != nil { - return nil, err + return databaseConfig, err } database := strings.TrimPrefix(connectionStringUrl.Path, "/") @@ -110,7 +112,7 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre username := connectionStringUrl.User.Username() password, _ := connectionStringUrl.User.Password() - return &models.DatabaseConfig{ + databaseConfig = models.DatabaseConfig{ Endpoint: host, Username: username, Password: password, @@ -119,5 +121,7 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre SSL: false, Timezone: "Europe/Berlin", Debug: true, - }, nil + } + + return databaseConfig, nil }