|
1 | 1 | package db_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "github.com/DATA-DOG/go-sqlmock" |
4 | 5 | "github.com/google/uuid"
|
5 | 6 | "github.com/pangolin-do-golang/tech-challenge-cart-api/internal/adapters/db"
|
6 | 7 | "github.com/pangolin-do-golang/tech-challenge-cart-api/internal/core/product"
|
7 | 8 | "github.com/pangolin-do-golang/tech-challenge-cart-api/mocks"
|
8 | 9 | "github.com/stretchr/testify/assert"
|
9 | 10 | "github.com/stretchr/testify/mock"
|
| 11 | + "gorm.io/driver/postgres" |
| 12 | + "gorm.io/gorm" |
10 | 13 | "testing"
|
11 | 14 | )
|
12 | 15 |
|
@@ -55,3 +58,36 @@ func TestGetByID_ReturnsError(t *testing.T) {
|
55 | 58 | assert.Nil(t, result)
|
56 | 59 | mockDB.AssertExpectations(t)
|
57 | 60 | }
|
| 61 | + |
| 62 | +func TestProductSearch(t *testing.T) { |
| 63 | + d, m, err := sqlmock.New() |
| 64 | + conn, err := gorm.Open(postgres.New(postgres.Config{Conn: d, DriverName: "postgres"})) |
| 65 | + |
| 66 | + m.ExpectQuery("SELECT .+").WillReturnRows( |
| 67 | + sqlmock.NewRows([]string{"id", "name", "description", "category", "price"}). |
| 68 | + AddRow(uuid.New(), "Test Product", "Test Description", "Test Category", 100.0). |
| 69 | + AddRow(uuid.New(), "Test Product 2", "Test Description 2", "Test Category", 200.0), |
| 70 | + ) |
| 71 | + repo := db.NewPostgresProductRepository(conn) |
| 72 | + _, err = repo.Search("search", "category") |
| 73 | + |
| 74 | + assert.NoError(t, err) |
| 75 | +} |
| 76 | + |
| 77 | +func TestProductDelete(t *testing.T) { |
| 78 | + d, m, err := sqlmock.New() |
| 79 | + conn, err := gorm.Open(postgres.New(postgres.Config{Conn: d, DriverName: "postgres"})) |
| 80 | + |
| 81 | + m.ExpectQuery("SELECT .+").WillReturnRows( |
| 82 | + sqlmock.NewRows([]string{"id", "name", "description", "category", "price"}). |
| 83 | + AddRow(uuid.New(), "Test Product", "Test Description", "Test Category", 100.0), |
| 84 | + ) |
| 85 | + |
| 86 | + m.ExpectBegin() |
| 87 | + m.ExpectExec("UPDATE .+").WillReturnResult(sqlmock.NewResult(0, 1)) |
| 88 | + m.ExpectCommit() |
| 89 | + repo := db.NewPostgresProductRepository(conn) |
| 90 | + err = repo.Delete(uuid.New()) |
| 91 | + |
| 92 | + assert.NoError(t, err) |
| 93 | +} |
0 commit comments