-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobScraper_test.go
71 lines (57 loc) · 1.66 KB
/
jobScraper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"sync"
"testing"
"time"
)
type MockWorker struct {
mock.Mock
}
func (m *MockWorker) scrapeDetailPage(url string) {
m.Called(url)
}
func (m *MockWorker) sleepRandomly() {
m.Called()
}
func TestSleepRandomly(t *testing.T) {
start := time.Now()
sleepRandomly()
duration := time.Since(start)
// Check that the sleep duration was between 1s and 2s
assert.True(t, duration >= 1*time.Second, "sleep duration should be at least 1 second")
assert.True(t, duration <= 2*time.Second, "sleep duration should be no more than 2 seconds")
}
// Unite test for worker method
func TestWorker(t *testing.T) {
// Set up the mock worker
mockWorker := new(MockWorker)
mockWorker.On("scrapeDetailPage", mock.Anything).Return()
mockWorker.On("sleepRandomly").Return()
// Set up channels and wait group
detailPageChan := make(chan string, 1)
wg = sync.WaitGroup{}
detailPageChan <- "http://expected.com"
// Start the worker
wg.Add(1)
go func() {
defer wg.Done()
mockWorker.scrapeDetailPage("http://expected.com")
mockWorker.sleepRandomly()
}()
wg.Wait()
// Verify that the mock functions were called
mockWorker.AssertExpectations(t)
}
// Unit test for new collector method
func TestGetNewCollector(t *testing.T) {
collector, err := getNewCollector()
// Check for errors
assert.NoError(t, err)
assert.NotNil(t, collector)
// Check user agent is set correctly
userAgent := collector.UserAgent
expectedUserAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
assert.Equal(t, expectedUserAgent, userAgent)
}