Skip to content

Commit

Permalink
✨ client: try test
Browse files Browse the repository at this point in the history
  • Loading branch information
budougumi0617 committed Sep 25, 2020
1 parent 6aba196 commit 7e0db45
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
37 changes: 36 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -178,8 +179,42 @@ func (lc *LeetCode) fill(req *http.Request, q *Question) {
// Need to set referer
}

func (lc *LeetCode) Test(ctx context.Context, q *Question, ans string) (string, error) {
sr := &SolutionRequest{
Lang: "golang",
QuestionID: q.QuestionID,
TestMode: "false",
Name: q.Slug,
DataInput: q.SampleTestCase,
TypedCode: ans,
}
req.AddCookie(c)
b, err := json.Marshal(sr)
if err != nil {
log.Printf("failed Marshal %+v\n", err)
return "", err
}
url := lc.BaseURL + fmt.Sprintf("/problems/%s/interpret_solution/", q.Slug)
log.Printf("send to %q", url)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(b))
if err != nil {
return "", err
}
lc.fill(req, q)
req.Header.Set("Referer", url)
cli := http.Client{
Timeout: 3 * time.Second,
}
resp, err := cli.Do(req)
if err != nil {
log.Printf("failed Do %+v\n", err)
return "", err
}
var res SolutionResult
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return "", err
}
log.Printf("result %+v\n", res)
return res.InterpretID, nil
}

// TODO: testメソッドをつくる for test
Expand Down
43 changes: 43 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,46 @@ func TestLeetCode_GetQuestionByID(t *testing.T) {
})
}
}

func Test(t *testing.T) {
t.Parallel()
type args struct {
q *Question
ans string
}
tests := []struct {
name string
args args
want string
}{
{
name: "動作確認用ケース",
args: args{
q: &Question{
Slug: "two-sum",
QuestionID: "1",
SampleTestCase: "[2,7,11,15]\n9",
},
ans: "package main\n\n/*\n* @lc app=leetcode id=1 lang=golang\n*\n* [1] Two Sum\n*/\n// @lc code=start\nfunc twoSum(nums []int, target int) []int {\nl := make(map[int]int, len(nums))\n// answer - x = y\nfor i, n := range nums {\nif j, ok := l[n]; ok {\nreturn []int{j, i}\n}\nl[target-n] = i\n}\nreturn []int{}\n}\n\n// @lc code=end",
},
want: "",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
lc := &LeetCode{
BaseURL: "https://leetcode.com",
gqlEndpoint: "https://leetcode.com/graphql",
}
got, err := lc.Test(context.TODO(), tt.args.q, tt.args.ans)
if err != nil {
t.Fatalf("Test() error = %v", err)
}
if got != tt.want {
t.Errorf("Test() got = %v, want %v", got, tt.want)
}
})
}
}
7 changes: 4 additions & 3 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package leetgode
// SolutionRequest is the parameters for below URL.
// https://leetcode.com/problems/${title_slug}/interpret_solution/
type SolutionRequest struct {
Lang string `json:"lang"`
Lang string `json:"lang"` // golang
QuestionID string `json:"question_id"`
TestMode string `json:"test_mode"`
TestMode string `json:"test_mode"` // false
Name string `json:"name"`
TypedCode string `json:"typed_code"`
DataInput string `json:"data_input"` // ex: "[2,7,11,15]\n9", load from Question.SampleTestCase
TypedCode string `json:"typed_code"` // load from file
}

// SolutionResult is the response from below URL.
Expand Down

0 comments on commit 7e0db45

Please sign in to comment.