|
| 1 | +// |
| 2 | +// Copyright (C) 2025 IOTech Ltd |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package postgres |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/edgexfoundry/edgex-go/internal/pkg/infrastructure/postgres/mocks" |
| 15 | + "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" |
| 16 | + |
| 17 | + "github.com/jackc/pgx/v5" |
| 18 | + "github.com/jackc/pgx/v5/pgconn" |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + "github.com/stretchr/testify/mock" |
| 21 | +) |
| 22 | + |
| 23 | +func TestDeleteEvents(t *testing.T) { |
| 24 | + ctx := context.Background() |
| 25 | + sqlStatement := "DELETE FROM event WHERE id = @id" |
| 26 | + args := pgx.NamedArgs{"id": "test-id"} |
| 27 | + |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + rowsAffected int64 |
| 31 | + execError error |
| 32 | + expectError bool |
| 33 | + expectedErrKind errors.ErrKind |
| 34 | + errorContains []string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "No rows affected - should return KindEntityDoesNotExist", |
| 38 | + rowsAffected: 0, |
| 39 | + execError: nil, |
| 40 | + expectError: true, |
| 41 | + expectedErrKind: errors.KindEntityDoesNotExist, |
| 42 | + errorContains: []string{"no event found", "SQL statement:", sqlStatement}, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Rows affected - should succeed", |
| 46 | + rowsAffected: 1, |
| 47 | + execError: nil, |
| 48 | + expectError: false, |
| 49 | + expectedErrKind: "", |
| 50 | + errorContains: nil, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Exec returns error - should return KindDatabaseError", |
| 54 | + rowsAffected: 0, |
| 55 | + execError: fmt.Errorf("database connection error"), |
| 56 | + expectError: true, |
| 57 | + expectedErrKind: errors.KindDatabaseError, |
| 58 | + errorContains: []string{"event(s) delete failed"}, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + for _, tt := range tests { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + tx := new(mocks.Tx) |
| 65 | + // On successful completion, a DELETE command returns a command tag of the form "DELETE count" |
| 66 | + // https://www.postgresql.org/docs/16/sql-delete.html |
| 67 | + commandTag := pgconn.NewCommandTag(fmt.Sprintf("DELETE %d", tt.rowsAffected)) |
| 68 | + tx.On("Exec", ctx, sqlStatement, args).Return(commandTag, tt.execError) |
| 69 | + |
| 70 | + err := deleteEvents(ctx, tx, sqlStatement, args) |
| 71 | + |
| 72 | + if tt.expectError { |
| 73 | + assert.Error(t, err) |
| 74 | + assert.Equal(t, tt.expectedErrKind, errors.Kind(err)) |
| 75 | + for _, contains := range tt.errorContains { |
| 76 | + assert.Contains(t, err.Error(), contains) |
| 77 | + } |
| 78 | + } else { |
| 79 | + assert.NoError(t, err) |
| 80 | + } |
| 81 | + tx.AssertExpectations(t) |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestDeleteReadingsBySubQuery(t *testing.T) { |
| 87 | + ctx := context.Background() |
| 88 | + subQuerySql := "SELECT id FROM event WHERE devicename = @devicename" |
| 89 | + args := pgx.NamedArgs{"devicename": "test-device"} |
| 90 | + |
| 91 | + tests := []struct { |
| 92 | + name string |
| 93 | + rowsAffected int64 |
| 94 | + execError error |
| 95 | + expectError bool |
| 96 | + expectedErrKind errors.ErrKind |
| 97 | + errorContains []string |
| 98 | + }{ |
| 99 | + { |
| 100 | + name: "No rows affected - should return KindEntityDoesNotExist", |
| 101 | + rowsAffected: 0, |
| 102 | + execError: nil, |
| 103 | + expectError: true, |
| 104 | + expectedErrKind: errors.KindEntityDoesNotExist, |
| 105 | + errorContains: []string{"no reading found", "SQL statement:"}, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "Rows affected - should succeed", |
| 109 | + rowsAffected: 1, |
| 110 | + execError: nil, |
| 111 | + expectError: false, |
| 112 | + expectedErrKind: "", |
| 113 | + errorContains: nil, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "Exec returns error - should return KindDatabaseError", |
| 117 | + rowsAffected: 1, // Use non-zero to avoid RowsAffected check before error check |
| 118 | + execError: fmt.Errorf("database connection error"), |
| 119 | + expectError: true, |
| 120 | + expectedErrKind: errors.KindDatabaseError, |
| 121 | + errorContains: []string{"reading(s) delete failed"}, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + for _, tt := range tests { |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + tx := new(mocks.Tx) |
| 128 | + // On successful completion, a DELETE command returns a command tag of the form "DELETE count" |
| 129 | + // https://www.postgresql.org/docs/16/sql-delete.html |
| 130 | + commandTag := pgconn.NewCommandTag(fmt.Sprintf("DELETE %d", tt.rowsAffected)) |
| 131 | + tx.On("Exec", ctx, mock.MatchedBy(func(sql string) bool { |
| 132 | + return strings.Contains(sql, subQuerySql) |
| 133 | + }), args).Return(commandTag, tt.execError) |
| 134 | + |
| 135 | + err := deleteReadingsBySubQuery(ctx, tx, subQuerySql, args) |
| 136 | + |
| 137 | + if tt.expectError { |
| 138 | + assert.Error(t, err) |
| 139 | + assert.Equal(t, tt.expectedErrKind, errors.Kind(err)) |
| 140 | + for _, contains := range tt.errorContains { |
| 141 | + assert.Contains(t, err.Error(), contains) |
| 142 | + } |
| 143 | + } else { |
| 144 | + assert.NoError(t, err) |
| 145 | + } |
| 146 | + tx.AssertExpectations(t) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments