|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/dieg0code/shared/mocks" |
| 7 | + "github.com/dieg0code/shared/models" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/mock" |
| 10 | +) |
| 11 | + |
| 12 | +func TestScraperService_GetProducts(t *testing.T) { |
| 13 | + t.Run("GetProducts_Success", func(t *testing.T) { |
| 14 | + repo := new(mocks.MockScraperRepository) |
| 15 | + scraper := new(mocks.MockScraper) |
| 16 | + |
| 17 | + scraperService := NewScraperServiceImpl(scraper, repo) |
| 18 | + |
| 19 | + // Configurar los mocks |
| 20 | + repo.On("DeleteAll").Return(nil) |
| 21 | + scraper.On("ScrapeData", "https", "cugat.cl/categoria-producto", mock.Anything, mock.Anything).Return([]models.Product{ |
| 22 | + { |
| 23 | + Name: "Product1", |
| 24 | + Category: "Category1", |
| 25 | + OriginalPrice: 100, |
| 26 | + DiscountedPrice: 80, |
| 27 | + }, |
| 28 | + }, nil) |
| 29 | + repo.On("Create", mock.Anything).Return(models.Product{}, nil) |
| 30 | + |
| 31 | + // Llamar a la función |
| 32 | + success, err := scraperService.GetProducts() |
| 33 | + |
| 34 | + // Verificar los resultados |
| 35 | + assert.NoError(t, err, "Expected no error, but got %v", err) |
| 36 | + assert.True(t, success, "Expected success to be true, but got %v", success) |
| 37 | + |
| 38 | + repo.AssertCalled(t, "DeleteAll") |
| 39 | + scraper.AssertCalled(t, "ScrapeData", "https", "cugat.cl/categoria-producto", mock.Anything, mock.Anything) |
| 40 | + repo.AssertCalled(t, "Create", mock.Anything) |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("GetProducts_ErrorDeletingAllProducts", func(t *testing.T) { |
| 44 | + repo := new(mocks.MockScraperRepository) |
| 45 | + scraper := new(mocks.MockScraper) |
| 46 | + |
| 47 | + scraperService := NewScraperServiceImpl(scraper, repo) |
| 48 | + |
| 49 | + // Configurar los mocks |
| 50 | + repo.On("DeleteAll").Return(assert.AnError) |
| 51 | + scraper.On("ScrapeData", "https", "cugat.cl/categoria-producto", mock.Anything, mock.Anything).Return([]models.Product{ |
| 52 | + { |
| 53 | + Name: "Product1", |
| 54 | + Category: "Category1", |
| 55 | + OriginalPrice: 100, |
| 56 | + DiscountedPrice: 80, |
| 57 | + }, |
| 58 | + }, nil) |
| 59 | + repo.On("Create", mock.Anything).Return(models.Product{}, nil) |
| 60 | + |
| 61 | + // Llamar a la función |
| 62 | + success, err := scraperService.GetProducts() |
| 63 | + |
| 64 | + // Verificar los resultados |
| 65 | + assert.Error(t, err, "Expected an error, but got nil") |
| 66 | + assert.False(t, success, "Expected success to be false, but got %v", success) |
| 67 | + |
| 68 | + repo.AssertCalled(t, "DeleteAll") |
| 69 | + repo.AssertNotCalled(t, "Create", mock.Anything) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("GetProducts_ErrorScrapingData", func(t *testing.T) { |
| 73 | + repo := new(mocks.MockScraperRepository) |
| 74 | + scraper := new(mocks.MockScraper) |
| 75 | + |
| 76 | + scraperService := NewScraperServiceImpl(scraper, repo) |
| 77 | + |
| 78 | + // Configurar los mocks |
| 79 | + repo.On("DeleteAll").Return(nil) |
| 80 | + scraper.On("ScrapeData", "https", "cugat.cl/categoria-producto", mock.Anything, mock.Anything).Return([]models.Product{}, assert.AnError) |
| 81 | + repo.On("Create", mock.Anything).Return(models.Product{}, nil) |
| 82 | + |
| 83 | + // Llamar a la función |
| 84 | + success, err := scraperService.GetProducts() |
| 85 | + |
| 86 | + // Verificar los resultados |
| 87 | + assert.Error(t, err, "Expected an error, but got nil") |
| 88 | + assert.False(t, success, "Expected success to be false, but got %v", success) |
| 89 | + |
| 90 | + repo.AssertCalled(t, "DeleteAll") |
| 91 | + scraper.AssertCalled(t, "ScrapeData", "https", "cugat.cl/categoria-producto", mock.Anything, mock.Anything) |
| 92 | + repo.AssertNotCalled(t, "Create", mock.Anything) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("GetProducts_ErrorCreatingProduct", func(t *testing.T) { |
| 96 | + repo := new(mocks.MockScraperRepository) |
| 97 | + scraper := new(mocks.MockScraper) |
| 98 | + |
| 99 | + scraperService := NewScraperServiceImpl(scraper, repo) |
| 100 | + |
| 101 | + // Configurar los mocks |
| 102 | + repo.On("DeleteAll").Return(nil) |
| 103 | + scraper.On("ScrapeData", "https", "cugat.cl/categoria-producto", mock.Anything, mock.Anything).Return([]models.Product{ |
| 104 | + { |
| 105 | + Name: "Product1", |
| 106 | + Category: "Category1", |
| 107 | + OriginalPrice: 100, |
| 108 | + DiscountedPrice: 80, |
| 109 | + }, |
| 110 | + }, nil) |
| 111 | + repo.On("Create", mock.Anything).Return(models.Product{}, assert.AnError) |
| 112 | + |
| 113 | + // Llamar a la función |
| 114 | + success, err := scraperService.GetProducts() |
| 115 | + |
| 116 | + // Verificar los resultados |
| 117 | + assert.Error(t, err, "Expected an error, but got nil") |
| 118 | + assert.False(t, success, "Expected success to be false, but got %v", success) |
| 119 | + |
| 120 | + repo.AssertCalled(t, "DeleteAll") |
| 121 | + scraper.AssertCalled(t, "ScrapeData", "https", "cugat.cl/categoria-producto", mock.Anything, mock.Anything) |
| 122 | + repo.AssertCalled(t, "Create", mock.Anything) |
| 123 | + }) |
| 124 | +} |
0 commit comments