package diyanet
import (
"context"
"strings"
"testing"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
"prayertimes/internal/scrapeutils"
"prayertimes/pkg/prayer"
)
type mockFetcher string
func (f mockFetcher) Fetch(_ context.Context, _ string) (*goquery.Document, error) {
return goquery.NewDocumentFromReader(strings.NewReader(string(f)))
}
const mockHtml = `
Monthly Prayer Times for Some Location
| Gregorian Calendar Date |
Fajr |
Sun |
Dhuhr |
Asr |
Maghrib |
Isha |
| 04.03.2023 |
05:48 |
07:11 |
13:05 |
16:15 |
18:49 |
20:06 |
| 05.03.2023 |
05:46 |
07:09 |
13:04 |
16:15 |
18:50 |
20:07 |
`
func TestDiyanet_Get(t *testing.T) {
t.Run("validates location", func(t *testing.T) {
d := Diyanet{}
_, err := d.Get(context.Background(), " not numeric ")
assert.ErrorIs(t, err, prayer.ErrInvalidLocation)
})
t.Run("extracts prayer times", func(t *testing.T) {
d := Diyanet{
FetcherFunc: mockFetcher(mockHtml).Fetch,
}
actual, err := d.Get(context.Background(), "1234")
assert.NoError(t, err)
expected := []prayer.Times{
{
Date: time.Date(2023, time.March, 4, 0, 0, 0, 0, time.UTC),
Fajr: "05:48",
Sunrise: "07:11",
Dhuhr: "13:05",
Asr: "16:15",
Maghrib: "18:49",
Isha: "20:06",
},
{
Date: time.Date(2023, time.March, 5, 0, 0, 0, 0, time.UTC),
Fajr: "05:46",
Sunrise: "07:09",
Dhuhr: "13:04",
Asr: "16:15",
Maghrib: "18:50",
Isha: "20:07",
},
}
assert.Equal(t, expected, actual)
})
t.Run("real endpoint", func(t *testing.T) {
if testing.Short() {
t.Skip()
}
d := Diyanet{
FetcherFunc: scrapeutils.GetParsed,
}
times, err := d.Get(context.Background(), "9205")
assert.NoError(t, err)
assert.Greater(t, len(times), 1)
assert.NotZero(t, times[0].Date)
})
}