Skip to content

Commit

Permalink
Merge pull request #86 from meian/79-crawler-contests_contestid_tasks…
Browse files Browse the repository at this point in the history
…-test
  • Loading branch information
meian authored Jul 28, 2024
2 parents ab6cb67 + f2da76a commit a2526c3
Show file tree
Hide file tree
Showing 2 changed files with 623 additions and 0 deletions.
154 changes: 154 additions & 0 deletions crawler/contest_task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package crawler_test

import (
"context"
"net/http"
"net/url"
"testing"
"time"

"github.com/meian/atgo/crawler"
"github.com/meian/atgo/crawler/requests"
"github.com/meian/atgo/crawler/responses"
"github.com/stretchr/testify/assert"
)

func TestContestTask_Do_Request(t *testing.T) {
req := &requests.ContestTask{
ContestID: "abc123",
}
want := requestWant{
path: "/contests/abc123/tasks",
query: url.Values{},
body: url.Values{},
}

assert := assert.New(t)
client, cFunc := mockRequestClient()
_, _ = crawler.NewContestTask(client).Do(context.Background(), req)
method, path, query, body := cFunc()
assert.Equal(http.MethodGet, method)
assert.Equal(want.path, path)
assert.Equal(want.query, query)
assert.Equal(want.body, body)
}

func TestContestTask_Do_Response(t *testing.T) {
m := testHTMLMap(t, "contest_task")

mem := 1024 * 1024 * 1024

type want struct {
err bool
res *responses.ContestTask
}
tests := []struct {
name string
httpRes mockHTTPResponse
want want
}{
{
name: "success",
httpRes: mockHTTPResponse{status: http.StatusOK, bodyFile: "success.html"},
want: want{
res: &responses.ContestTask{
ContestID: "abc234",
Tasks: []responses.ContestTask_Task{
{
ID: "abc234_a",
Title: "Weird Function",
Index: "A",
TimeLimit: 2 * time.Second,
Memory: mem,
},
{
ID: "abc234_b",
Title: "Longest Segment",
Index: "B",
TimeLimit: 2 * time.Second,
Memory: mem,
},
{
ID: "abc234_c",
Title: "Happy New Year!",
Index: "C",
TimeLimit: 2 * time.Second,
Memory: mem,
},
{
ID: "abc234_d",
Title: "Prefix K-th Max",
Index: "D",
TimeLimit: 2 * time.Second,
Memory: mem,
},
{
ID: "abc234_e",
Title: "Arithmetic Number",
Index: "E",
TimeLimit: 2 * time.Second,
Memory: mem,
},
{
ID: "abc234_f",
Title: "Reordering",
Index: "F",
TimeLimit: 2 * time.Second,
Memory: mem,
},
{
ID: "abc234_g",
Title: "Divide a Sequence",
Index: "G",
TimeLimit: 2 * time.Second,
Memory: mem,
},
{
ID: "abc234_h",
Title: "Enumerate Pairs",
Index: "Ex",
TimeLimit: 4 * time.Second,
Memory: mem,
},
},
},
},
},
{
name: "not found",
httpRes: mockHTTPResponse{status: http.StatusNotFound},
want: want{err: true},
},
{
name: "not a html response",
httpRes: mockHTTPResponse{status: http.StatusOK, bodyFile: "not-a-html"},
want: want{err: true},
},
{
name: "timeout",
httpRes: mockHTTPResponse{timeout: true},
want: want{err: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
client := mockResponseClient(tt.httpRes.status, m.Get(tt.httpRes.bodyFile), tt.httpRes.timeout)
req := &requests.ContestTask{ContestID: "abc234"}
res, err := crawler.NewContestTask(client).Do(ctx, req)
if tt.want.err {
if assert.Error(err) {
t.Logf("error: %v", err)
}
return
}
assert.NoError(err)
if !assert.NotNil(res) {
return
}
assert.Equal(tt.want.res, res)
})
}
}
Loading

0 comments on commit a2526c3

Please sign in to comment.