You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
package diyanet
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"prayertimes/internal/net"
|
|
"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 = `
|
|
<div id='tab-1'>
|
|
<div class='table-responsive'>
|
|
<table class='table vakit-table'>
|
|
<caption>Monthly Prayer Times for Some Location</caption>
|
|
<thead>
|
|
<tr>
|
|
<th>Gregorian Calendar Date</th>
|
|
<th>Hijri Date</th>
|
|
<th>Fajr</th>
|
|
<th>Sun</th>
|
|
<th>Dhuhr</th>
|
|
<th>Asr</th>
|
|
<th>Maghrib</th>
|
|
<th>Isha</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td>04.03.2023</td>
|
|
<td>...</td>
|
|
<td>05:48</td>
|
|
<td>07:11</td>
|
|
<td>13:05</td>
|
|
<td>16:15</td>
|
|
<td>18:49</td>
|
|
<td>20:06</td>
|
|
</tr>
|
|
<tr>
|
|
<td>05.03.2023</td>
|
|
<td>...</td>
|
|
<td>05:46</td>
|
|
<td>07:09</td>
|
|
<td>13:04</td>
|
|
<td>16:15</td>
|
|
<td>18:50</td>
|
|
<td>20:07</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
`
|
|
|
|
func TestDiyanet_Get(t *testing.T) {
|
|
t.Run("validates location", func(t *testing.T) {
|
|
d := Provider{}
|
|
_, err := d.Get(context.Background(), " not numeric ")
|
|
assert.ErrorIs(t, err, prayer.ErrInvalidLocation)
|
|
})
|
|
|
|
t.Run("extracts prayer times", func(t *testing.T) {
|
|
d := Provider{
|
|
FetcherFunc: mockFetcher(mockHtml).Fetch,
|
|
}
|
|
actual, err := d.Get(context.Background(), "1234")
|
|
assert.NoError(t, err)
|
|
|
|
expected := []prayer.Times{
|
|
{
|
|
Date: "2023-03-04",
|
|
Fajr: "05:48",
|
|
Sunrise: "07:11",
|
|
Dhuhr: "13:05",
|
|
Asr: "16:15",
|
|
Maghrib: "18:49",
|
|
Isha: "20:06",
|
|
},
|
|
{
|
|
Date: "2023-03-05",
|
|
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 := Provider{
|
|
FetcherFunc: net.GetParsed,
|
|
}
|
|
times, err := d.Get(context.Background(), "11104")
|
|
if err != nil {
|
|
t.Skipf("skipping live endpoint test due to upstream/network error: %v", err)
|
|
}
|
|
if len(times) == 0 {
|
|
t.Skip("skipping live endpoint test because upstream returned no times")
|
|
}
|
|
|
|
assert.Greater(t, len(times), 0)
|
|
assert.NotZero(t, times[0].Date)
|
|
|
|
for _, it := range times {
|
|
t.Logf("%+v", it)
|
|
}
|
|
})
|
|
}
|