Skip to content

Commit

Permalink
Add action for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
EmadMokhtar committed Apr 11, 2024
1 parent be8f9e0 commit 8aa5a08
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/go-test.yaml
Original file line number Diff line number Diff line change
@@ -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
106 changes: 106 additions & 0 deletions service/quran_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 8aa5a08

Please sign in to comment.