Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/contests/[contestID] のクローラーのテストを追加 #84

Merged
merged 10 commits into from
Jul 26, 2024

Conversation

meian
Copy link
Owner

@meian meian commented Jul 26, 2024

GET /contests/[contestID] へのテストを追加しました。

  • crawler.Contestのテスト
    • request
      • コンテストIDが存在しない
    • response
      • 正常系
      • コンテストIDが存在しない(404)
      • HTMLが返って来なかった
      • タイムアウト

これと合わせて以下を修正しています。

  • テストで使う型を共通化した
    • requestWant
      • リクエストパラメータの期待値として欲しい内容
      • query, body は url.Values でもnilを表現できるので url.Values を利用するようにした
      • pathもキャプチャするようにした
    • mockHTTPResponse
      • レスポンスを評価する際のテストケース
  • HTML以外のレスポンスを受けた時にエラーを返すようにした
    • /login のテストではHTML以外のテストもログイン失敗の評価だけでエラーを返さなかったので合わせてテストを修正した
  • /loginのテストの修正
    • レスポンスのテストで期待値として responses の構造体全体を持つようにした
    • サンプルのレスポンスを日本語 => 英語に変更

@coderabbitai

上記方針のテストで何か懸念があるか教えてください
また、この指針に沿ったテスト実装がなされているかも合わせてレビューしてください
また、 crawler.Login.Do についてこれより他にテストするべき箇所があればそれも合わせて指摘してください

Summary by CodeRabbit

  • 新機能

    • HTMLレスポンスのバリデーション機能を追加し、無効なHTMLコンテンツの処理を強化しました。
    • Contest 機能に対する新しいユニットテストを追加し、HTTPリクエストとレスポンスの検証を行います。
    • requestWant型とmockHTTPResponse型を新たに追加し、HTTPリクエストおよびレスポンスのテストを改善しました。
    • 東京のタイムゾーン情報を管理する新しいパッケージを追加しました。
  • バグ修正

    • htmlMap 型の Get メソッドのロジックを修正し、特定のキーの処理を改善しました。
  • ドキュメント

    • AtCoder初心者コンテスト234のHTML構造を新たに追加し、参加者向けの情報を整理しました。
  • リファクタ

    • ログインテストの構造を改善し、テストの可読性を向上させました。

@meian meian added the test Add test(#7) label Jul 26, 2024
@meian meian self-assigned this Jul 26, 2024
@meian meian linked an issue Jul 26, 2024 that may be closed by this pull request
Copy link

coderabbitai bot commented Jul 26, 2024

Warning

Rate limit exceeded

@meian has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 3 minutes and 51 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 453358e and 6949c56.

Walkthrough

この変更は、crawlerパッケージ内のHTTPリクエストとレスポンスの処理を改善し、テストの信頼性を高めることを目指しています。新たな型やメソッドの追加、HTMLの検証機能の導入により、コンテストやログイン機能のテストが明確になり、開発者が理解しやすくなります。

Changes

ファイル 変更の要約
crawler/common_test.go htmlMapGetメソッドのロジック変更、新しいrequestWant型とmockHTTPResponse型の追加。captureFuncの戻り値の変更。
crawler/contest_test.go Contest機能に対するユニットテストの導入。HTTPリクエストとレスポンスの動作を検証。
crawler/crawler.go validHTMLメソッドを追加し、HTTPレスポンスの内容を検証するロジックを改良。
crawler/testdata/contest/success.html AtCoder Beginner Contest 234のHTML構造を表現する新しいファイルを追加。
io/read_action.go WithReadAction関数を追加し、データを読みながらバッファに保持する機能を実装。
io/types.go 新しいEOF変数を追加し、EOFを扱いやすくする機能を改善。
timezone/timezone.go 東京のタイムゾーン情報を管理する新しいtimezoneパッケージを追加。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Crawler
    participant HTTPClient
    participant ResponseHandler

    User->>Crawler: HTTP Request (e.g., Contest)
    Crawler->>HTTPClient: Send Request
    HTTPClient-->>Crawler: Return Response
    Crawler->>ResponseHandler: Validate Response
    ResponseHandler-->>Crawler: Valid/Invalid
    Crawler->>User: Return Result
Loading

🐰 うさぎの歌 🎶
変わる季節に喜びを、
すべてのテスト、今輝く。
HTMLの海を渡りゆく、
みんなの笑顔、心に宿る。
さあ、進もう、楽しい道!
コードを整えて、夢をつかもう! 🌟

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

@meian meian force-pushed the 78-crawler-contests_contestid-test branch from 042f45f to a8c5100 Compare July 26, 2024 12:05
@meian meian marked this pull request as ready for review July 26, 2024 12:15
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 3161fb6 and a8c5100.

Files selected for processing (6)
  • crawler/common_test.go (4 hunks)
  • crawler/contest_test.go (1 hunks)
  • crawler/crawler.go (3 hunks)
  • crawler/login_test.go (3 hunks)
  • crawler/testdata/contest/success.html (1 hunks)
  • crawler/testdata/login/success.html (1 hunks)
Additional context used
Learnings (1)
crawler/common_test.go (1)
Learnt from: meian
PR: meian/atgo#83
File: crawler/common_test.go:16-39
Timestamp: 2024-07-24T18:05:28.796Z
Learning: The `testHTMLMap` function in `crawler/common_test.go` reads HTML files from an embedded filesystem, caches the results in a map, and uses `require.NoError` to handle errors, which is appropriate for test code.
Additional comments not posted (18)
crawler/login_test.go (5)

23-26: 良い変更です!

requestWant 構造体を使用してリクエストパラメータをカプセル化することで、テストの可読性が向上しています。


32-33: 良い変更です!

リクエストメソッドとパスのアサーションが適切に行われています。


40-44: 良い変更です!

want 構造体に err フィールドと responses.Login ポインタを追加することで、期待されるレスポンスの表現が明確になっています。


47-75: 良い変更です!

テストケースが明確に定義されており、各シナリオに対する期待されるレスポンスが適切に設定されています。


Line range hint 78-91:
良い変更です!

テストケースの実行ロジックが適切に実装されており、エラーハンドリングも適切です。

crawler/contest_test.go (5)

20-24: 良い変更です!

requestWant 構造体を使用してリクエストパラメータをカプセル化することで、テストの可読性が向上しています。


30-33: 良い変更です!

リクエストメソッドとパスのアサーションが適切に行われています。


39-42: 良い変更です!

want 構造体に err フィールドと responses.Contest ポインタを追加することで、期待されるレスポンスの表現が明確になっています。


43-75: 良い変更です!

テストケースが明確に定義されており、各シナリオに対する期待されるレスポンスが適切に設定されています。


78-96: 良い変更です!

テストケースの実行ロジックが適切に実装されており、エラーハンドリングも適切です。

crawler/common_test.go (5)

20-21: 良い変更です!

"not-a-html" キーの条件と返り値が適切に変更されています。


51-55: 良い変更です!

requestWant 構造体を導入することで、HTTPリクエストの取り扱いが明確になり、テストの可読性が向上しています。


69-69: 良い変更です!

captureFunc の戻り値にパスを追加することで、テストと検証の用途において使いやすさが向上しています。


71-82: 良い変更です!

lastCaputure メソッドの戻り値にパスを追加することで、テストと検証の用途において使いやすさが向上しています。


93-97: 良い変更です!

mockHTTPResponse 構造体を導入することで、HTTPインタラクションのテスト機能が強化されています。

crawler/crawler.go (1)

164-177: LGTM!

新しい関数validHTMLは基本的なHTMLの検証を行います。コード変更は承認されます。

crawler/testdata/login/success.html (1)

1-383: LGTM!

新しいファイルsuccess.htmlは、ログイン成功ページの静的なHTMLコンテンツを含んでいます。コード変更は承認されます。

crawler/testdata/contest/success.html (1)

1-505: LGTM!

新しいファイルsuccess.htmlは、コンテストページの静的なHTMLコンテンツを含んでいます。コード変更は承認されます。

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between a8c5100 and 8d69aa9.

Files selected for processing (3)
  • crawler/crawler.go (3 hunks)
  • io/read_action.go (1 hunks)
  • io/types.go (1 hunks)
Additional context used
golangci-lint
io/read_action.go

14-14: Error return value of io.Copy is not checked

(errcheck)

Additional comments not posted (3)
io/types.go (1)

9-9: LGTM!

EOF変数の追加は、EOF条件の処理を容易にするための良い変更です。

crawler/crawler.go (2)

146-152: LGTM!

documentFromReaderメソッドの変更は、HTMLコンテンツの検証を強化し、エラーハンドリングを改善しています。


164-194: validHTMLメソッドの正確性を確認してください。

validHTMLメソッドは、レスポンスボディ内のHTMLタグを検証します。タグの順序が正しいことを確認する必要があります。

Verification successful

validHTMLメソッドの正確性を確認しました。

validHTMLメソッドは、レスポンスボディ内のHTMLタグを正しい順序で検証しています。タグ <html>, <head>, <body> が順番に存在することを確認し、正しい場合は nil を返し、順番が間違っている場合や見つからない場合はエラーを返します。

  • crawler/crawler.go の 164 行目から 194 行目に validHTML メソッドが定義されています。
  • 同ファイル内で validHTML メソッドが使用されています。
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of the `validHTML` method.

# Test: Search for the `validHTML` method usage. Expect: Correct order of HTML tags.
rg --type go -A 10 $'func (Crawler) validHTML'

Length of output: 47


Script:

#!/bin/bash
# Searching for the definition of the `validHTML` method in the codebase
ast-grep --lang go --pattern 'func (Crawler) validHTML($_, $_) $_' 

# Searching for the usage of the `validHTML` method in the codebase
rg 'validHTML'

Length of output: 1655

@meian meian force-pushed the 78-crawler-contests_contestid-test branch from 8d69aa9 to 0f43e16 Compare July 26, 2024 17:17
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: .coderabbit.yaml
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 8d69aa9 and 453358e.

Files selected for processing (8)
  • crawler/contest.go (2 hunks)
  • crawler/contest_archive.go (2 hunks)
  • crawler/contest_test.go (1 hunks)
  • crawler/crawler.go (3 hunks)
  • crawler/testdata/contest/success.html (1 hunks)
  • io/read_action.go (1 hunks)
  • io/types.go (1 hunks)
  • timezone/timezone.go (1 hunks)
Additional context used
Learnings (2)
io/read_action.go (1)
Learnt from: meian
PR: meian/atgo#84
File: io/read_action.go:0-0
Timestamp: 2024-07-26T17:20:17.948Z
Learning: In the `WithReadAction` function in `io/read_action.go`, ensure that the error from `io.Copy` is properly handled.
crawler/testdata/contest/success.html (1)
Learnt from: meian
PR: meian/atgo#84
File: crawler/testdata/contest/success.html:1-1
Timestamp: 2024-07-26T12:16:40.801Z
Learning: HTMLファイル内のスクリプトパスのハッシュ値とキーやトークン関連の文字列が固定化されている場合、テスト目的であれば特にセキュリティ上の懸念はない。ただし、本番環境で使用されないように注意が必要。
Additional comments not posted (12)
io/types.go (1)

9-9: EOF変数の追加は良い改善です。

EOF変数の追加により、EOFの管理が簡単になりました。

io/read_action.go (1)

1-18: 良い実装です。

WithReadAction関数は適切にエラーハンドリングを行っています。

crawler/contest_test.go (2)

17-35: テスト設定とアサーションの確認

テストはリクエストパラメータを正しく検証しています。モッククライアントの使用も適切です。


37-99: テストケースの網羅性の確認

テストケースは成功とエラーシナリオを網羅しており、モックレスポンスの使用も適切です。アサーションも意味があります。

crawler/contest.go (3)

Line range hint 23-45:
メソッドの実装確認

Doメソッドはエラーを適切に処理し、時間の解析に正しいタイムゾーンを使用しています。


90-97: タイムゾーンの使用確認

parseTimesメソッドは時間の解析にtimezone.Tokyoを使用しており、正確な時間の解釈を保証しています。


Line range hint 110-118:
ターゲットレートの抽出確認

parseTargetRateメソッドはターゲットレートを正しく抽出して返しています。

crawler/crawler.go (3)

145-153: HTML構造の検証確認

documentFromReaderメソッドはドキュメントを解析する前にHTML構造を正しく検証しています。


164-194: HTMLタグの検証確認

validHTMLメソッドはレスポンスボディ内の重要なHTMLタグの存在を正しく検証しています。


195-198: ログイン状態の確認

LoggedInメソッドは設定リンクの存在を確認することで、ユーザーがログインしているかどうかを正しく判断しています。

crawler/contest_archive.go (1)

134-134: LGTM! 変更を承認しますが、timezone.Tokyoの使用箇所を確認してください。

この変更は、東京タイムゾーンでの一貫した時間解釈を保証するために適切です。

ただし、timezone.Tokyoが正しく定義されていることを確認してください。

Verification successful

timezone.Tokyoは正しく定義されています。

timezone.Tokyotimezone/timezone.goファイルで以下のように定義されています:

  • var Tokyo *time.Location
  • Tokyo, _ = time.LoadLocation("Asia/Tokyo")

この定義により、東京タイムゾーンが適切に設定されています。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that `timezone.Tokyo` is correctly defined and used.

# Test: Search for the definition and usage of `timezone.Tokyo`. Expect: Correct definition and usage.
rg --type go 'timezone.Tokyo'

Length of output: 480


Script:

#!/bin/bash
# Description: Verify the definition of `timezone.Tokyo`.

# Test: Search for the definition of `timezone.Tokyo`. Expect: Correct definition.
rg --type go 'Tokyo ='

Length of output: 24


Script:

#!/bin/bash
# Description: Verify the definition or import of `timezone`.

# Test: Search for the term `timezone` to locate its package or definition.
rg --type go 'timezone'

Length of output: 684


Script:

#!/bin/bash
# Description: Verify the definition of `Tokyo` within the `timezone/timezone.go` file.

# Test: Search for the definition of `Tokyo` in the `timezone/timezone.go` file.
rg --type go 'Tokyo' timezone/timezone.go

Length of output: 110

crawler/testdata/contest/success.html (1)

1-504: 確認済み: 固定値の使用はテスト目的で適切です。

スクリプトパスのハッシュ値とキーやトークン関連の文字列が固定化されていることを確認しました。これらはテスト目的であり、特にセキュリティ上の懸念はありません。ただし、これらの固定値が本番環境で使用されないように注意が必要です。

@meian meian force-pushed the 78-crawler-contests_contestid-test branch from d6087b3 to 6949c56 Compare July 26, 2024 17:36
@meian
Copy link
Owner Author

meian commented Jul 26, 2024

fixupを解消

@meian
Copy link
Owner Author

meian commented Jul 26, 2024

@coderabbitai review

一応変なコミットとか繋がりがないか一通り再レビューお願いします

Copy link

coderabbitai bot commented Jul 26, 2024

@meian: The commit history looks coherent and connected, with each commit addressing specific changes or improvements as outlined in the PR summary. I will proceed with the review.

Analysis chain
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Fetch the commit history for the pull request
git log --oneline --graph --decorate

Length of output: 656

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@meian meian merged commit 6ddb9f1 into main Jul 26, 2024
2 checks passed
@meian meian deleted the 78-crawler-contests_contestid-test branch July 26, 2024 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Add test(#7)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

crawler - /contests/[contestID] のテスト
1 participant