-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
695 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
name: CI/CD Pipeline | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
test-and-build: | ||
name: Test and Build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: '1.22.4' | ||
|
||
- name: Cache dependencies | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/go/pkg/mod | ||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-go- | ||
- name: Install dependencies | ||
run: go mod download | ||
|
||
- name: Run tests | ||
run: go test -coverprofile=coverage.out ./... | ||
|
||
- name: Upload coverage to codecov | ||
uses: codecov/codecov-action@v2 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
|
||
- name: Build binary | ||
run: GOOS=linux GOARCH=amd64 go build -o api_scraper_lambda | ||
|
||
- name: Zip binary | ||
run: zip api_scraper_lambda.zip api_scraper_lambda | ||
|
||
- name: Upload artifact | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: api_scraper_lambda | ||
path: api_scraper_lambda.zip | ||
|
||
deploy: | ||
name: Deploy | ||
runs-on: ubuntu-latest | ||
needs: test-and-build | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Download artifact | ||
uses: actions/download-artifact@v2 | ||
with: | ||
name: api_scraper_lambda | ||
path: . | ||
|
||
- name: Set up Terraform | ||
uses: hashicorp/setup-terraform@v1 | ||
|
||
- name: Initialize Terraform | ||
working-directory: ./terraform | ||
run: terraform init | ||
|
||
- name: Apply Terraform | ||
working-directory: ./terraform | ||
env: | ||
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
run: terraform apply -auto-approve -var="aws_region=${{ secrets.AWS_REGION }}" -var="aws_account_id=${{ secrets.AWS_ACCOUNT_ID }}" | ||
|
||
- name: Get API Gateway URL | ||
working-directory: ./terraform | ||
run: echo "API Gateway URL => https://$(terraform output -json | jq -r '.api_gateway_invoke_url')" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package controller | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/dieg0code/serverles-api-scraper/api/data/request" | ||
"github.com/dieg0code/serverles-api-scraper/api/data/response" | ||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockProductService struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockProductService) GetAll() ([]response.ProductResponse, error) { | ||
args := m.Called() | ||
return args.Get(0).([]response.ProductResponse), args.Error(1) | ||
} | ||
func (m *MockProductService) GetByID(productID string) (response.ProductResponse, error) { | ||
args := m.Called(productID) | ||
return args.Get(0).(response.ProductResponse), args.Error(1) | ||
} | ||
func (m *MockProductService) UpdateData(updateData request.UpdateDataRequest) (bool, error) { | ||
args := m.Called(updateData) | ||
return args.Bool(0), args.Error(1) | ||
} | ||
|
||
func TestProductController_GetAll(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
mockService := new(MockProductService) | ||
productController := NewProductControllerImpl(mockService) | ||
|
||
router := gin.Default() | ||
router.GET("/products", productController.GetAll) | ||
|
||
mockService.On("GetAll").Return([]response.ProductResponse{ | ||
{ | ||
ProductID: "test-id", | ||
Name: "Test Product", | ||
Category: "Test Category", | ||
OriginalPrice: 100, | ||
DiscountedPrice: 90, | ||
}, | ||
{ | ||
ProductID: "test-id-2", | ||
Name: "Test Product 2", | ||
Category: "Test Category 2", | ||
OriginalPrice: 200, | ||
DiscountedPrice: 180, | ||
}, | ||
}, nil) | ||
|
||
req, err := http.NewRequest(http.MethodGet, "/products", nil) | ||
assert.NoError(t, err, "Expected no error creating request") | ||
|
||
rec := httptest.NewRecorder() | ||
router.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusOK, rec.Code, "Expected status code 200") | ||
|
||
var response response.BaseResponse | ||
err = json.Unmarshal(rec.Body.Bytes(), &response) | ||
assert.NoError(t, err, "Expected no error unmarshalling response") | ||
assert.Equal(t, 200, response.Code, "Response code should be 200") | ||
assert.Equal(t, "OK", response.Status, "Response status should be Success") | ||
} | ||
|
||
func TestProductController_GetByID(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
mockService := new(MockProductService) | ||
productController := NewProductControllerImpl(mockService) | ||
|
||
router := gin.Default() | ||
router.GET("/products/:productId", productController.GetByID) | ||
|
||
mockService.On("GetByID", "test-id").Return(response.ProductResponse{ | ||
ProductID: "test-id", | ||
Name: "Test Product", | ||
Category: "Test Category", | ||
OriginalPrice: 100, | ||
DiscountedPrice: 90, | ||
}, nil) | ||
|
||
req, err := http.NewRequest(http.MethodGet, "/products/test-id", nil) | ||
assert.NoError(t, err, "Expected no error creating request") | ||
|
||
rec := httptest.NewRecorder() | ||
router.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusOK, rec.Code, "Expected status code 200") | ||
|
||
var response response.BaseResponse | ||
err = json.Unmarshal(rec.Body.Bytes(), &response) | ||
assert.NoError(t, err, "Expected no error unmarshalling response") | ||
assert.Equal(t, 200, response.Code, "Response code should be 200") | ||
assert.Equal(t, "OK", response.Status, "Response status should be Success") | ||
} | ||
|
||
func TestProductController_UpdateData(t *testing.T) { | ||
gin.SetMode(gin.TestMode) | ||
mockService := new(MockProductService) | ||
productController := NewProductControllerImpl(mockService) | ||
|
||
router := gin.Default() | ||
router.PUT("/products", productController.UpdateData) | ||
|
||
mockService.On("UpdateData", request.UpdateDataRequest{ | ||
UpdateData: true, | ||
}).Return(true, nil) | ||
|
||
reqBody, err := json.Marshal(request.UpdateDataRequest{ | ||
UpdateData: true, | ||
}) | ||
assert.NoError(t, err, "Expected no error marshalling request") | ||
|
||
req, err := http.NewRequest(http.MethodPut, "/products", bytes.NewBuffer(reqBody)) | ||
assert.NoError(t, err, "Expected no error creating request") | ||
|
||
rec := httptest.NewRecorder() | ||
router.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusOK, rec.Code, "Expected status code 200") | ||
|
||
var response response.BaseResponse | ||
err = json.Unmarshal(rec.Body.Bytes(), &response) | ||
assert.NoError(t, err, "Expected no error unmarshalling response") | ||
assert.Equal(t, 200, response.Code, "Response code should be 200") | ||
assert.Equal(t, "OK", response.Status, "Response status should be Success") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.