From 615a6b1a25e8eddeabdfbd915a53adc256fa74a7 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 26 Jun 2024 11:31:42 +0200 Subject: [PATCH] fix(errors): fix unneeded error handling Signed-off-by: SoXX --- internal/postgres/source_test.go | 44 -------------------------------- internal/postgres/user.go | 5 +--- internal/postgres/user_test.go | 8 +++--- 3 files changed, 5 insertions(+), 52 deletions(-) diff --git a/internal/postgres/source_test.go b/internal/postgres/source_test.go index bcb38bd..dc5c58e 100644 --- a/internal/postgres/source_test.go +++ b/internal/postgres/source_test.go @@ -159,50 +159,6 @@ func TestGetAllSourceNodes(t *testing.T) { } } -func TestGetAllSourceWithNoData(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 - } - tests := []struct { - name string - args args - want []models.Source - wantErr bool - }{ - { - name: "Test 1: Get all entries", - args: args{ - ctx: ctx, - db: gormDB, - }, - want: nil, - wantErr: true, - }, - } - 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() diff --git a/internal/postgres/user.go b/internal/postgres/user.go index e10495e..9346cb5 100644 --- a/internal/postgres/user.go +++ b/internal/postgres/user.go @@ -28,16 +28,13 @@ func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.Anthrove result := db.WithContext(ctx).FirstOrCreate(&user) if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return &otterError.EntityAlreadyExists{} } return result.Error } - if result.RowsAffected == 0 { - return &otterError.EntityAlreadyExists{} - } - return nil } diff --git a/internal/postgres/user_test.go b/internal/postgres/user_test.go index 4477bea..dd0932b 100644 --- a/internal/postgres/user_test.go +++ b/internal/postgres/user_test.go @@ -707,7 +707,7 @@ func TestGetUserSourceLinks(t *testing.T) { } err = CreateSource(ctx, gormDB, eSource) if err != nil { - t.Fatal(err) + t.Fatal("Create Source e621:", err) } faSource := &models.Source{ @@ -717,7 +717,7 @@ func TestGetUserSourceLinks(t *testing.T) { } err = CreateSource(ctx, gormDB, faSource) if err != nil { - t.Fatal(err) + t.Fatal("Create Source fa:", err) } expectedResult := make(map[string]models.UserSource) @@ -740,11 +740,11 @@ func TestGetUserSourceLinks(t *testing.T) { err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, eSource.ID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername) if err != nil { - t.Fatal(err) + t.Fatal("CreateUserWithRelationToSource e621:", err) } err = CreateUserWithRelationToSource(ctx, gormDB, validAnthroveUserID, faSource.ID, expectedResult["fa"].UserID, expectedResult["fa"].AccountUsername) if err != nil { - t.Fatal(err) + t.Fatal("CreateUserWithRelationToSource fa:", err) } // Test