From 8aa5a0891c0e5c05187756c65ea9338e9468dbd7 Mon Sep 17 00:00:00 2001 From: Emad Mokhtar Date: Thu, 11 Apr 2024 10:16:37 +0200 Subject: [PATCH] Add action for testing --- .github/workflows/go-test.yaml | 25 ++++++++ service/quran_test.go | 106 +++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 .github/workflows/go-test.yaml create mode 100644 service/quran_test.go diff --git a/.github/workflows/go-test.yaml b/.github/workflows/go-test.yaml new file mode 100644 index 0000000..e2bb70c --- /dev/null +++ b/.github/workflows/go-test.yaml @@ -0,0 +1,25 @@ +name: go-test + +on: + push: + branches: + - master + - main + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + + - name: Build + run: go build -v ./... + + - name: Test + uses: robherley/go-test-action@v0 \ No newline at end of file diff --git a/service/quran_test.go b/service/quran_test.go new file mode 100644 index 0000000..7682bb4 --- /dev/null +++ b/service/quran_test.go @@ -0,0 +1,106 @@ +package service + +import ( + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" + "net/http/httptest" + "tafseer_api/domain" + "testing" +) + +type QuranDomainMock struct { + Surahs *domain.SurahListResponse + Ayah *domain.AyahDetailsResponse + Err error +} + +var _ domain.QuranDomainer = &QuranDomainMock{} + +func (m *QuranDomainMock) GetSurahs() (*domain.SurahListResponse, error) { + return m.Surahs, m.Err +} + +func (m *QuranDomainMock) GetAyah(surahIndex, ayahNumber string) (*domain.AyahDetailsResponse, error) { + // Mimic the behavior of the real GetAyah method where the ayah is not found (404) + if surahIndex != "1" || ayahNumber != "1" { + return nil, nil + } + + return m.Ayah, m.Err +} + +func TestQuranService_GetSurahs(t *testing.T) { + surahs := domain.SurahListResponse{ + domain.SurahResponse{ + Index: 1, + Name: "Al-Fatihah", + }, + domain.SurahResponse{ + Index: 2, + Name: "Al-Baqarah", + }, + } + quranDomainMock := &QuranDomainMock{ + Surahs: &surahs, + Err: nil, + } + quranService := NewQuranService(quranDomainMock) + + e := echo.New() + req := httptest.NewRequest(echo.GET, "/surahs", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + err := quranService.GetSurahs(c) + assert.NoError(t, err) + assert.Equal(t, 200, rec.Code) + +} + +func TestQuranService_GetAyah(t *testing.T) { + quranDomainMock := &QuranDomainMock{ + Ayah: &domain.AyahDetailsResponse{ + SuraIndex: 1, + AyahNumber: 1, + SuraName: "Al-Fatihah", + Text: "In the name of Allah, the Entirely Merciful, the Especially Merciful.", + }, + Err: nil, + } + quranService := NewQuranService(quranDomainMock) + + e := echo.New() + req := httptest.NewRequest(echo.GET, "/surahs/1/ayahs/1", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetParamNames("sura_number", "ayah_number") + c.SetParamValues("1", "1") + + err := quranService.GetAyah(c) + assert.NoError(t, err) + assert.Equal(t, 200, rec.Code) +} + +func TestQuranService_GetAyah_NotFound(t *testing.T) { + quranDomainMock := &QuranDomainMock{ + Ayah: &domain.AyahDetailsResponse{ + SuraIndex: 1, + AyahNumber: 1, + SuraName: "Al-Fatihah", + Text: "In the name of Allah, the Entirely Merciful, the Especially Merciful.", + }, + Err: nil, + } + quranService := NewQuranService(quranDomainMock) + + e := echo.New() + req := httptest.NewRequest(echo.GET, "/surahs/1/ayahs/1", nil) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + c.SetParamNames("sura_number", "ayah_number") + c.SetParamValues("2", "2") + + err := quranService.GetAyah(c) + assert.NoError(t, err) + assert.Equal(t, 404, rec.Code) +}