Repository: kmyk/online-judge-tools
Pull request #549 (2019-10-14 14:19:53)

538: Add dispatch test.

fukatani: おっしゃるとおりで、submission/problemや他のサービスにも拡大するつもりですが、一旦この状態でいい感じか見せようとしてました。
AtCoderだけ入れて、Hacktoberfest向けに残りの難易度低めのイシューとして空けてもいいのかなあと思っています。

サービスのカバレッジも上がるので、数値だけで言えば全サービスのテストをした方がいいです。
若干サービスのテストなのかdispatchのテストなのかわからなくなりますが。
kmyk: そういうことならよさそうです。
しかし 1 種類だけなのと 2 種類以上なのは差があるので、やはりなにかもうひとつは追加しておいてほしいです。

Patch:
diff --git a/onlinejudge/service/atcoder.py b/onlinejudge/service/atcoder.py
index 8a09ea6..8dbb0e1 100644
--- a/onlinejudge/service/atcoder.py
+++ b/onlinejudge/service/atcoder.py
@@ -1256,3 +1256,4 @@ class AtCoderSubmissionTestCaseResult(object):
 onlinejudge.dispatch.services += [AtCoderService]
 onlinejudge.dispatch.problems += [AtCoderProblem]
 onlinejudge.dispatch.submissions += [AtCoderSubmission]
+onlinejudge.dispatch.contests += [AtCoderContest]


Test patch:
diff --git a/tests/dispatch.py b/tests/dispatch.py
new file mode 100644
index 0000000..fb67811
--- /dev/null
+++ b/tests/dispatch.py
@@ -0,0 +1,69 @@
+import unittest
+
+from onlinejudge import dispatch, service
+
+
+class DispatchAtCoderTest(unittest.TestCase):
+    def test_problem_from_url(self):
+        problem = dispatch.problem_from_url('https://atcoder.jp/contests/arc001/tasks/arc001_1')
+        self.assertTrue(isinstance(problem, service.atcoder.AtCoderProblem))
+        self.assertEqual(problem.get_url(), 'https://atcoder.jp/contests/arc001/tasks/arc001_1')
+        self.assertEqual(problem.get_url(lang='ja'), 'https://atcoder.jp/contests/arc001/tasks/arc001_1?lang=ja')
+        self.assertTrue(isinstance(problem.get_service(), service.atcoder.AtCoderService))
+        self.assertEqual(problem.download_input_format(), '\r\n<var>N</var>\r\n<var>c_1c_2c_3…c_N</var>\r\n')
+        self.assertEqual(problem.get_name(), 'センター採点')
+
+    def test_submission_from_url(self):
+        submission = dispatch.submission_from_url('https://atcoder.jp/contests/agc039/submissions/7874055')
+        self.assertTrue(isinstance(submission, service.atcoder.AtCoderSubmission))
+        self.assertTrue(isinstance(submission.get_service(), service.atcoder.AtCoderService))
+        with self.assertRaises(Exception):
+            submission.get_problem()
+        problem = submission.download_problem()
+        self.assertEqual(problem.contest_id, "agc039")
+        self.assertEqual(problem.problem_id, "agc039_b")
+
+    def test_contest_from_url(self):
+        contest = dispatch.contest_from_url('https://atcoder.jp/contests/agc030')
+        self.assertTrue(isinstance(contest, service.atcoder.AtCoderContest))
+        self.assertTrue(isinstance(contest.get_service(), service.atcoder.AtCoderService))
+
+    def test_service_from_url(self):
+        service1 = dispatch.service_from_url('https://atcoder.jp/')
+        self.assertTrue(isinstance(service1, service.atcoder.AtCoderService))
+        self.assertEqual(service1.get_name(), 'AtCoder')
+        self.assertEqual(service1.get_url_of_login_page(), 'https://atcoder.jp/login')
+        self.assertEqual(service1.get_user_history_url(22), 'https://atcoder.jp/users/22/history/json')
+
+
+class DispatchCodeForcesTest(unittest.TestCase):
+    def test_problem_from_url(self):
+        problem = dispatch.problem_from_url('https://codeforces.com/contest/1244/problem/C')
+        self.assertTrue(isinstance(problem, service.codeforces.CodeforcesProblem))
+        self.assertTrue(isinstance(problem.get_service(), service.codeforces.CodeforcesService))
+
+    def test_contest_from_url(self):
+        contest = dispatch.contest_from_url('https://codeforces.com/contest/1244')
+        self.assertTrue(isinstance(contest, service.codeforces.CodeforcesContest))
+        self.assertTrue(isinstance(contest.get_service(), service.codeforces.CodeforcesService))
+        self.assertEqual(contest.get_url(), 'https://codeforces.com/contest/1244')
+
+    def test_service_from_url(self):
+        service1 = dispatch.service_from_url('https://codeforces.com/')
+        self.assertEqual(service1.get_name(), 'Codeforces')
+        self.assertTrue(isinstance(service1, service.codeforces.CodeforcesService))
+        self.assertEqual(service1.get_url_of_login_page(), 'https://codeforces.com/enter')
+
+
+class InvalidDispatchTest(unittest.TestCase):
+    def test_problem_from_url(self):
+        self.assertIsNone(dispatch.problem_from_url('https://atcoder.jp/contests/agc039'))
+
+    def test_submission_from_url(self):
+        self.assertIsNone(dispatch.submission_from_url('https://atcoder.jp/contests/agc039'))
+
+    def test_contest_from_url(self):
+        self.assertIsNone(dispatch.contest_from_url('https://atcoder.jp/'))
+
+    def test_service_from_url(self):
+        self.assertIsNone(dispatch.service_from_url('https://www.yahoo.co.jp/'))