-
Notifications
You must be signed in to change notification settings - Fork 8
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
1 parent
be8f9e0
commit 8aa5a08
Showing
2 changed files
with
131 additions
and
0 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,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 |
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,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) | ||
} |