Skip to content

Commit 815c42a

Browse files
committed
Added real tests
1 parent b0997c0 commit 815c42a

File tree

4 files changed

+237
-1
lines changed

4 files changed

+237
-1
lines changed

.github/workflows/pytest.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ jobs:
1717
run: pip install -r test-requirements.txt
1818
- name: Install pytest
1919
run: pip install pytest
20+
- name: pip freeze (for debug purposes)
21+
run: pip freeze
2022
- name: Run tests
21-
run: pytest
23+
run: pytest -vvvv -s
24+
env:
25+
LEETCODE_SESSION_ID: ${{ secrets.LEETCODE_SESSION }}

test/base.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
3+
import leetcode
4+
import leetcode.auth
5+
6+
7+
class BaseTest:
8+
def setup(self) -> None:
9+
session_id = os.environ["LEETCODE_SESSION_ID"]
10+
csrftoken = leetcode.auth.get_csrf_cookie(session_id)
11+
12+
configuration = leetcode.Configuration()
13+
14+
configuration.api_key["x-csrftoken"] = csrftoken
15+
configuration.api_key["csrftoken"] = csrftoken
16+
configuration.api_key["LEETCODE_SESSION"] = session_id
17+
configuration.api_key["Referer"] = "https://leetcode.com"
18+
19+
configuration.debug = False
20+
21+
self._api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
22+
23+
def teardown(self) -> None:
24+
pass
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import json
2+
import test.base
3+
4+
import leetcode
5+
6+
7+
class TestGraphqlGetQuestionDetail(test.base.BaseTest):
8+
def test_request(self) -> None:
9+
graphql_request = leetcode.GraphqlQuery(
10+
query="""
11+
query getQuestionDetail($titleSlug: String!) {
12+
question(titleSlug: $titleSlug) {
13+
questionId
14+
questionFrontendId
15+
boundTopicId
16+
title
17+
content
18+
translatedTitle
19+
isPaidOnly
20+
difficulty
21+
likes
22+
dislikes
23+
isLiked
24+
similarQuestions
25+
contributors {
26+
username
27+
profileUrl
28+
avatarUrl
29+
__typename
30+
}
31+
langToValidPlayground
32+
topicTags {
33+
name
34+
slug
35+
translatedName
36+
__typename
37+
}
38+
companyTagStats
39+
codeSnippets {
40+
lang
41+
langSlug
42+
code
43+
__typename
44+
}
45+
stats
46+
codeDefinition
47+
hints
48+
solution {
49+
id
50+
canSeeDetail
51+
__typename
52+
}
53+
status
54+
sampleTestCase
55+
enableRunCode
56+
metaData
57+
translatedContent
58+
judgerAvailable
59+
judgeType
60+
mysqlSchemas
61+
enableTestMode
62+
envInfo
63+
__typename
64+
}
65+
}
66+
""",
67+
variables=leetcode.GraphqlQueryVariables(title_slug="two-sum"),
68+
operation_name="getQuestionDetail",
69+
)
70+
71+
response = self._api_instance.graphql_post(body=graphql_request)
72+
73+
data = response.data
74+
75+
assert data
76+
77+
question = data.question
78+
user = data.user
79+
80+
assert user is None
81+
82+
assert question.question_id == "1"
83+
assert question.question_frontend_id == "1"
84+
assert question.bound_topic_id is None
85+
assert question.title == "Two Sum"
86+
assert len(question.content) > 10
87+
assert question.translated_title is None
88+
assert question.is_paid_only is False
89+
assert question.difficulty == "Easy"
90+
assert question.likes > 0
91+
assert question.dislikes > 0
92+
assert question.is_liked is None
93+
assert json.loads(question.similar_questions)[0]["difficulty"] in (
94+
"Easy",
95+
"Medium",
96+
"Hard",
97+
)
98+
assert len(question.contributors) == 0
99+
assert "python" in list(json.loads(question.lang_to_valid_playground).keys())
100+
topic_tag = question.topic_tags[0]
101+
assert isinstance(topic_tag, leetcode.GraphqlQuestionTopicTag)
102+
assert len(topic_tag.name) > 0
103+
assert len(topic_tag.slug) > 0
104+
assert question.topic_tags[0].translated_name is None
105+
assert len(topic_tag.typename) > 0
106+
107+
tag_stat = list(json.loads(question.company_tag_stats).values())[0][0]
108+
109+
assert tag_stat["taggedByAdmin"] in (True, False)
110+
assert len(tag_stat["name"]) > 0
111+
assert len(tag_stat["slug"]) > 0
112+
assert tag_stat["timesEncountered"] > 0
113+
114+
code_snippet = question.code_snippets[0]
115+
116+
assert isinstance(code_snippet, leetcode.GraphqlQuestionCodeSnippet)
117+
assert len(code_snippet.code) > 0
118+
assert len(code_snippet.lang) > 0
119+
assert len(code_snippet.lang_slug) > 0
120+
assert code_snippet.typename == "CodeSnippetNode"
121+
122+
stats = json.loads(question.stats)
123+
124+
assert len(stats["totalAccepted"]) > 0
125+
assert len(stats["totalSubmission"]) > 0
126+
assert int(stats["totalAcceptedRaw"]) > 0
127+
assert int(stats["totalSubmissionRaw"]) > 0
128+
129+
code_definition = json.loads(question.code_definition)[0]
130+
131+
assert len(code_definition["value"]) > 0
132+
assert len(code_definition["text"]) > 0
133+
assert len(code_definition["defaultCode"]) > 0
134+
135+
assert [len(hint) > 0 for hint in question.hints]
136+
137+
solution = question.solution
138+
139+
# FIXME: this check doesn't work with swagger generated code
140+
# assert isinstance(solution, leetcode.GraphqlQuestionSolution)
141+
142+
# FIXME: swagger generates the code which returns dict
143+
assert solution["__typename"] == "ArticleNode"
144+
assert solution["canSeeDetail"] in (True, False)
145+
assert int(solution["id"]) > 0
146+
147+
assert question.status in ("ac", "not_started", "tried")
148+
149+
assert len(question.sample_test_case) > 0
150+
151+
assert question.enable_run_code in (True, False)
152+
153+
meta_data = json.loads(question.meta_data)
154+
155+
assert meta_data["name"] == "twoSum"
156+
assert meta_data["params"][0]["name"]
157+
assert meta_data["params"][0]["type"]
158+
assert meta_data["return"]["type"]
159+
assert meta_data["return"]["size"]
160+
assert meta_data["manual"] in (True, False)
161+
162+
assert question.translated_content is None
163+
164+
assert question.judger_available is True
165+
assert question.judge_type in ("large", "small")
166+
167+
assert question.mysql_schemas == []
168+
169+
assert question.enable_test_mode in (True, False)
170+
171+
env_info = json.loads(question.env_info)
172+
173+
assert env_info["cpp"][0] == "C++"

test/test_graphql_request_user.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import json
2+
import test.base
3+
4+
import leetcode
5+
6+
7+
class TestGraphqlGetUser(test.base.BaseTest):
8+
def test_request(self) -> None:
9+
graphql_request = leetcode.GraphqlQuery(
10+
query="""
11+
{
12+
user {
13+
username
14+
isCurrentUserPremium
15+
}
16+
}
17+
""",
18+
variables=leetcode.GraphqlQueryVariables(),
19+
operation_name="",
20+
)
21+
22+
response = self._api_instance.graphql_post(body=graphql_request)
23+
24+
data = response.data
25+
26+
assert data
27+
28+
question = data.question
29+
30+
assert question is None
31+
32+
user = data.user
33+
34+
assert user.username
35+
assert user.is_current_user_premium in (True, False)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy