From ae879e159e04ff8ea0606b813221e07028f28a30 Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 00:48:30 +0900 Subject: [PATCH 01/31] =?UTF-8?q?=E3=81=8A=E3=81=BF=E3=81=8F=E3=81=98?= =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=83=96=E3=83=A9=E3=83=AA=E3=82=92=E6=9B=B8?= =?UTF-8?q?=E3=81=84=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 97 +++++++++++++++++++++++++++++++++++ tests/library/test_omikuji.py | 38 ++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 library/omikuji.py create mode 100644 tests/library/test_omikuji.py diff --git a/library/omikuji.py b/library/omikuji.py new file mode 100644 index 0000000000..62eeae90c7 --- /dev/null +++ b/library/omikuji.py @@ -0,0 +1,97 @@ +# coding: utf-8 + +""" +おみくじを返す +""" + +from abc import ABC +from enum import Enum +from dataclasses import dataclass +from functools import reduce +from random import choices + +class AbstractOmikujiResults(ABC) + """ + おみくじの結果一覧の抽象クラス + """ + pass + +""" +Enumを継承している +""" + +AbstractOmikujiResults.register(Enum) + + + +@dataclass +class OmikujiResult: + key: AbstractOmikujiResults + rate: float + message: str + + def test(self): + assert type(key).name == 'OmikujiResults' + assert rate < 1 + assert message != '' + + +class Omikuji: + """ + おみくじのメイン処理 + ガチャではないので排出率を公開するメソッドはあえて実装されていない + """ + + def __init__(self, entries: dict[AbstractOmikujiResults, OmikujiResult]): + self.entries = entries + + def test(self): + """ + 全体のテストを行う + + 各エントリーのテスト、 排出率の合計が1であること、エントリーのキーが不整合のテストをする + """ + for entry in values(self.entries): + entry.test + + assert reduce(lambda acc, cur: acc + cur.rate, self.entries, 0) == 1.0 + assert len(filter(lambda key, value: key != value.key, self.entries)) == 0 + + def draw(self): + """ + おみくじを引く + """ + return choices( + population=self.entries, + weights=map(lambda entry: entry.rate, self.entries) + k=1 + )[0] + + +""" +おみくじメイン処理 +""" +class OmikujiResults(AbstractOmikujiResults): + DaiKichi = auto() + ChuKichi = auto() + ShoKichi = auto() + HatoKichi = auto() + Kichi = auto() + Kyo = auto() + DaiKyo = auto() + +omikuji = Omikuji(entries={ + OmikujiResults.DaiKichi: OmikujiResult(OmikujiResults.DaiKichi, 0.02, ":tada: 大吉 何でもうまくいく!!気がする!!"), + OmikujiResults.ChuKichi: OmikujiResult(OmikujiResults.ChuKichi, 0.2, ":smile: 中吉 そこそこうまくいくかも!?"), + OmikujiResults.SyoKichi: OmikujiResult(OmikujiResults.ShoKichi, 0.38, ":smily: 小吉 なんとなくうまくいくかも!?"), + OmikujiResults.Kichi: OmikujiResult(OmikujiResults.Kichi, 0.3, ":smirk: 吉 まあうまくいくかも!?"), + OmikujiResults.HatoKichi: OmikujiResult(OmikujiResults.HatoKichi, 0.09, ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!"), + OmikujiResults.Kyo: OmikujiResult(OmikujiResults.Kyo, 0.0075, ":cry: 凶 ちょっと慎重にいったほうがいいかも……"), + OmikujiResults.DaiKyo: OmikujiResult(OmikujiResults.DaiKyo, 0.0025, ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう"), +}) + +def test(): + omikuji.test() + +def draw(): + return omikuji.draw().message diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py new file mode 100644 index 0000000000..81cd21981e --- /dev/null +++ b/tests/library/test_omikuji.py @@ -0,0 +1,38 @@ +""" +omikujiライブラリのテスト +""" + +import unittest +from library.omikuji import AbstractOmikujiResults, OmikujiResult, Omikuji, test + + +class TestOmikuji(unittest.TestCase): + """ + おみくじのテスト + """ + + def test_omikuji_config: + """ + おみくじの設定が正常か + """ + test() + + + def test_omikuji_minimum: + """ + おみくじの実装が正常か + """ + + class TestOmikujiResults(AbstractOmikujiResults): + Kichi = auto() + SueKichi = auto() + + testOmikuji = Omikuji(entries={ + TestOmikujiResults.Kichi: OmikujiResult(TestOmikujiResults.Kichi, 0.5, "吉"), + TestOmikujiResults.SueKichi: OmikujiResult(TestOmikujiResults.SueKichi, 0.5, "末吉"), + }) + + self.assertTrue( + testOmikuji.draw().message == "吉" + or testOmikuji.draw().message == "末吉" + ) From ebc515ad24c999b43642a98e6ce56ba12674ef8d Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 00:52:22 +0900 Subject: [PATCH 02/31] =?UTF-8?q?=E7=B6=99=E6=89=BF=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/omikuji.py b/library/omikuji.py index 62eeae90c7..582404ad98 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -31,7 +31,7 @@ class OmikujiResult: message: str def test(self): - assert type(key).name == 'OmikujiResults' + assert isinstance(key, AbstractOmikujiResults) assert rate < 1 assert message != '' From 43a1986e151380afda49f0770841fefa08b691c8 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 5 Mar 2022 15:53:08 +0000 Subject: [PATCH 03/31] =?UTF-8?q?=E9=B3=A9=E3=81=AF=E5=94=90=E6=8F=9A?= =?UTF-8?q?=E3=81=92=EF=BC=81(=E8=87=AA=E5=8B=95=E3=81=A7=E7=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 25 +++++++++++++++++-------- tests/library/test_omikuji.py | 3 +-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 582404ad98..bc5a489892 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -10,12 +10,16 @@ from functools import reduce from random import choices + class AbstractOmikujiResults(ABC) - """ + + +""" おみくじの結果一覧の抽象クラス """ - pass - +pass + + """ Enumを継承している """ @@ -23,7 +27,6 @@ class AbstractOmikujiResults(ABC) AbstractOmikujiResults.register(Enum) - @dataclass class OmikujiResult: key: AbstractOmikujiResults @@ -55,14 +58,15 @@ def test(self): entry.test assert reduce(lambda acc, cur: acc + cur.rate, self.entries, 0) == 1.0 - assert len(filter(lambda key, value: key != value.key, self.entries)) == 0 + assert len(filter(lambda key, value: key != + value.key, self.entries)) == 0 def draw(self): """ おみくじを引く """ return choices( - population=self.entries, + population=self.entries, weights=map(lambda entry: entry.rate, self.entries) k=1 )[0] @@ -71,6 +75,8 @@ def draw(self): """ おみくじメイン処理 """ + + class OmikujiResults(AbstractOmikujiResults): DaiKichi = auto() ChuKichi = auto() @@ -80,6 +86,7 @@ class OmikujiResults(AbstractOmikujiResults): Kyo = auto() DaiKyo = auto() + omikuji = Omikuji(entries={ OmikujiResults.DaiKichi: OmikujiResult(OmikujiResults.DaiKichi, 0.02, ":tada: 大吉 何でもうまくいく!!気がする!!"), OmikujiResults.ChuKichi: OmikujiResult(OmikujiResults.ChuKichi, 0.2, ":smile: 中吉 そこそこうまくいくかも!?"), @@ -90,8 +97,10 @@ class OmikujiResults(AbstractOmikujiResults): OmikujiResults.DaiKyo: OmikujiResult(OmikujiResults.DaiKyo, 0.0025, ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう"), }) + def test(): omikuji.test() - -def draw(): + + +def draw(): return omikuji.draw().message diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index 81cd21981e..f8fa00f39a 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -4,7 +4,7 @@ import unittest from library.omikuji import AbstractOmikujiResults, OmikujiResult, Omikuji, test - + class TestOmikuji(unittest.TestCase): """ @@ -17,7 +17,6 @@ def test_omikuji_config: """ test() - def test_omikuji_minimum: """ おみくじの実装が正常か From ce07d82d0529dbf56f0371244c6ec55b45ab140f Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 01:01:48 +0900 Subject: [PATCH 04/31] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=81=A8?= =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 11 +++++++++-- tests/library/test_omikuji.py | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index bc5a489892..910df2ae37 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -41,7 +41,7 @@ def test(self): class Omikuji: """ - おみくじのメイン処理 + おみくじのコアロジック ガチャではないので排出率を公開するメソッドはあえて実装されていない """ @@ -73,7 +73,7 @@ def draw(self): """ -おみくじメイン処理 +以下おみくじの設定 """ @@ -99,8 +99,15 @@ class OmikujiResults(AbstractOmikujiResults): def test(): + """ + おみくじの設定テスト + """ + omikuji.test() def draw(): + """ + おみくじ抽選 + """ return omikuji.draw().message diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index f8fa00f39a..b32202fd11 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -11,13 +11,13 @@ class TestOmikuji(unittest.TestCase): おみくじのテスト """ - def test_omikuji_config: + def test_omikuji_config(self): """ おみくじの設定が正常か """ test() - def test_omikuji_minimum: + def test_omikuji_minimum(self): """ おみくじの実装が正常か """ From 7a7d4a658b337bd1e97288c14e0027694df390ad Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 01:04:01 +0900 Subject: [PATCH 05/31] =?UTF-8?q?typo=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 910df2ae37..deee8d1e0d 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -11,19 +11,17 @@ from random import choices -class AbstractOmikujiResults(ABC) +class AbstractOmikujiResults(ABC): - -""" + """ おみくじの結果一覧の抽象クラス """ -pass + pass -""" -Enumを継承している -""" + +# Enumを継承している AbstractOmikujiResults.register(Enum) @@ -67,15 +65,12 @@ def draw(self): """ return choices( population=self.entries, - weights=map(lambda entry: entry.rate, self.entries) + weights=map(lambda entry: entry.rate, self.entries), k=1 )[0] -""" -以下おみくじの設定 -""" - +# 以下おみくじの設定 class OmikujiResults(AbstractOmikujiResults): DaiKichi = auto() From 5aef79acaf45fd1c1b89cf2f434ca7f4803e1e5a Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 01:07:10 +0900 Subject: [PATCH 06/31] =?UTF-8?q?auto=E5=BF=98=E3=82=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/omikuji.py b/library/omikuji.py index deee8d1e0d..1dea723270 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -5,7 +5,7 @@ """ from abc import ABC -from enum import Enum +from enum import Enum, auto from dataclasses import dataclass from functools import reduce from random import choices From f0bfa4a3387272d73f00cea85a244f7bcf89e6e1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 5 Mar 2022 16:05:55 +0000 Subject: [PATCH 07/31] =?UTF-8?q?=E9=B3=A9=E3=81=AF=E5=94=90=E6=8F=9A?= =?UTF-8?q?=E3=81=92=EF=BC=81(=E8=87=AA=E5=8B=95=E3=81=A7=E7=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 1 - 1 file changed, 1 deletion(-) diff --git a/library/omikuji.py b/library/omikuji.py index 1dea723270..fbeb3f2bb5 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -20,7 +20,6 @@ class AbstractOmikujiResults(ABC): pass - # Enumを継承している AbstractOmikujiResults.register(Enum) From c54f7bd0ac132f41c26cc356bba5849f029cb21e Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 01:08:33 +0900 Subject: [PATCH 08/31] =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E5=AE=9F=E8=A1=8C=E5=BF=98=E3=82=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/omikuji.py b/library/omikuji.py index fbeb3f2bb5..db10700bef 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -52,7 +52,7 @@ def test(self): 各エントリーのテスト、 排出率の合計が1であること、エントリーのキーが不整合のテストをする """ for entry in values(self.entries): - entry.test + entry.test() assert reduce(lambda acc, cur: acc + cur.rate, self.entries, 0) == 1.0 assert len(filter(lambda key, value: key != From 981a75f12d4758db5359f198908810dd021b856a Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 01:10:01 +0900 Subject: [PATCH 09/31] =?UTF-8?q?=E8=87=AA=E5=88=86=E3=81=A7=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=E3=81=97=E3=81=9F=E5=9E=8B=E3=82=92=E7=A0=B4=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/omikuji.py b/library/omikuji.py index db10700bef..c9e62fb489 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -84,7 +84,7 @@ class OmikujiResults(AbstractOmikujiResults): omikuji = Omikuji(entries={ OmikujiResults.DaiKichi: OmikujiResult(OmikujiResults.DaiKichi, 0.02, ":tada: 大吉 何でもうまくいく!!気がする!!"), OmikujiResults.ChuKichi: OmikujiResult(OmikujiResults.ChuKichi, 0.2, ":smile: 中吉 そこそこうまくいくかも!?"), - OmikujiResults.SyoKichi: OmikujiResult(OmikujiResults.ShoKichi, 0.38, ":smily: 小吉 なんとなくうまくいくかも!?"), + OmikujiResults.ShoKichi: OmikujiResult(OmikujiResults.ShoKichi, 0.38, ":smily: 小吉 なんとなくうまくいくかも!?"), OmikujiResults.Kichi: OmikujiResult(OmikujiResults.Kichi, 0.3, ":smirk: 吉 まあうまくいくかも!?"), OmikujiResults.HatoKichi: OmikujiResult(OmikujiResults.HatoKichi, 0.09, ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!"), OmikujiResults.Kyo: OmikujiResult(OmikujiResults.Kyo, 0.0075, ":cry: 凶 ちょっと慎重にいったほうがいいかも……"), From 8f4b00e1845f0366023c3948f18c52c50a635f94 Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 01:11:43 +0900 Subject: [PATCH 10/31] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=81=A7Enum?= =?UTF-8?q?=E3=81=AEauto=E3=82=92import=E6=BC=8F=E3=82=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/library/test_omikuji.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index b32202fd11..5c254a9f4b 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -4,6 +4,7 @@ import unittest from library.omikuji import AbstractOmikujiResults, OmikujiResult, Omikuji, test +from Enum import auto class TestOmikuji(unittest.TestCase): From 52f45774738140347014c34447dd21860a3e6824 Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 01:14:36 +0900 Subject: [PATCH 11/31] typo --- tests/library/test_omikuji.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index 5c254a9f4b..725029f4f7 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -4,7 +4,7 @@ import unittest from library.omikuji import AbstractOmikujiResults, OmikujiResult, Omikuji, test -from Enum import auto +from enum import auto class TestOmikuji(unittest.TestCase): From 79f4d34d7e731ad1ab49fbd2f5a3578e51a0e6a1 Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 01:47:17 +0900 Subject: [PATCH 12/31] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index c9e62fb489..969538399b 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -31,9 +31,8 @@ class OmikujiResult: message: str def test(self): - assert isinstance(key, AbstractOmikujiResults) - assert rate < 1 - assert message != '' + assert self.rate < 1 + assert self.message != '' class Omikuji: @@ -51,22 +50,21 @@ def test(self): 各エントリーのテスト、 排出率の合計が1であること、エントリーのキーが不整合のテストをする """ - for entry in values(self.entries): + for entry in self.entries.values(): entry.test() - assert reduce(lambda acc, cur: acc + cur.rate, self.entries, 0) == 1.0 - assert len(filter(lambda key, value: key != - value.key, self.entries)) == 0 + assert round(reduce(lambda acc, cur: acc + cur.rate, self.entries.values(), 0), 1) == 1 + assert len(list(filter(lambda item: (item[0] != item[1].key), self.entries.items()))) == 0 def draw(self): """ おみくじを引く """ return choices( - population=self.entries, - weights=map(lambda entry: entry.rate, self.entries), + population=list(self.entries.items()), + weights=map(lambda entry: entry.rate, self.entries.values()), k=1 - )[0] + )[0][1] # 以下おみくじの設定 From 89a3089a2faff7fc78bb6fa5c84fd03e3747848c Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 09:11:01 +0900 Subject: [PATCH 13/31] =?UTF-8?q?=E8=AC=8E=E3=81=AE=E6=8A=BD=E8=B1=A1?= =?UTF-8?q?=E3=82=AF=E3=83=A9=E3=82=B9=E3=81=A7=E3=81=AF=E3=81=AA=E3=81=8F?= =?UTF-8?q?=E3=82=B8=E3=82=A7=E3=83=8D=E3=83=AA=E3=82=AF=E3=82=B9=E3=81=A7?= =?UTF-8?q?=E8=89=AF=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 25 ++++++++----------------- tests/library/test_omikuji.py | 8 ++++---- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 969538399b..446acbb113 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -9,39 +9,30 @@ from dataclasses import dataclass from functools import reduce from random import choices +from typing import TypeVar, Generic -class AbstractOmikujiResults(ABC): - - """ - おみくじの結果一覧の抽象クラス - """ - - pass - - -# Enumを継承している -AbstractOmikujiResults.register(Enum) - +TOmikujiEnum = TypeVar('TOmikujiEnum') @dataclass class OmikujiResult: - key: AbstractOmikujiResults + key: TOmikujiEnum rate: float message: str def test(self): + assert isinstance(self.key, Enum) assert self.rate < 1 assert self.message != '' -class Omikuji: +class Omikuji(Generic[TOmikujiEnum]): """ おみくじのコアロジック ガチャではないので排出率を公開するメソッドはあえて実装されていない """ - def __init__(self, entries: dict[AbstractOmikujiResults, OmikujiResult]): + def __init__(self, entries: dict[TOmikujiEnum, OmikujiResult]): self.entries = entries def test(self): @@ -69,7 +60,7 @@ def draw(self): # 以下おみくじの設定 -class OmikujiResults(AbstractOmikujiResults): +class OmikujiResults(Enum): DaiKichi = auto() ChuKichi = auto() ShoKichi = auto() @@ -79,7 +70,7 @@ class OmikujiResults(AbstractOmikujiResults): DaiKyo = auto() -omikuji = Omikuji(entries={ +omikuji = Omikuji[OmikujiResults](entries={ OmikujiResults.DaiKichi: OmikujiResult(OmikujiResults.DaiKichi, 0.02, ":tada: 大吉 何でもうまくいく!!気がする!!"), OmikujiResults.ChuKichi: OmikujiResult(OmikujiResults.ChuKichi, 0.2, ":smile: 中吉 そこそこうまくいくかも!?"), OmikujiResults.ShoKichi: OmikujiResult(OmikujiResults.ShoKichi, 0.38, ":smily: 小吉 なんとなくうまくいくかも!?"), diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index 725029f4f7..49bf45c5c8 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -3,8 +3,8 @@ """ import unittest -from library.omikuji import AbstractOmikujiResults, OmikujiResult, Omikuji, test -from enum import auto +from library.omikuji import OmikujiResult, Omikuji, test +from enum import Enum, auto class TestOmikuji(unittest.TestCase): @@ -23,11 +23,11 @@ def test_omikuji_minimum(self): おみくじの実装が正常か """ - class TestOmikujiResults(AbstractOmikujiResults): + class TestOmikujiResults(Enum): Kichi = auto() SueKichi = auto() - testOmikuji = Omikuji(entries={ + testOmikuji = Omikuji[TestOmikujiResults](entries={ TestOmikujiResults.Kichi: OmikujiResult(TestOmikujiResults.Kichi, 0.5, "吉"), TestOmikujiResults.SueKichi: OmikujiResult(TestOmikujiResults.SueKichi, 0.5, "末吉"), }) From 45f270f7df3533561311d717b1bb041c2b74a28e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 5 Mar 2022 16:48:02 +0000 Subject: [PATCH 14/31] =?UTF-8?q?=E9=B3=A9=E3=81=AF=E5=94=90=E6=8F=9A?= =?UTF-8?q?=E3=81=92=EF=BC=81(=E8=87=AA=E5=8B=95=E3=81=A7=E7=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 446acbb113..430d78b682 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -44,8 +44,10 @@ def test(self): for entry in self.entries.values(): entry.test() - assert round(reduce(lambda acc, cur: acc + cur.rate, self.entries.values(), 0), 1) == 1 - assert len(list(filter(lambda item: (item[0] != item[1].key), self.entries.items()))) == 0 + assert round(reduce(lambda acc, cur: acc + cur.rate, + self.entries.values(), 0), 1) == 1 + assert len( + list(filter(lambda item: (item[0] != item[1].key), self.entries.items()))) == 0 def draw(self): """ From 59c68b77c357471999811628ea662436e0e44b40 Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 09:41:00 +0900 Subject: [PATCH 15/31] =?UTF-8?q?=E3=82=B8=E3=82=A7=E3=83=8D=E3=83=AA?= =?UTF-8?q?=E3=82=AF=E3=82=B9=E5=91=A8=E3=82=8A=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 5 ++--- tests/library/test_omikuji.py | 5 +---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 430d78b682..94fba64a87 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -21,7 +21,6 @@ class OmikujiResult: message: str def test(self): - assert isinstance(self.key, Enum) assert self.rate < 1 assert self.message != '' @@ -49,7 +48,7 @@ def test(self): assert len( list(filter(lambda item: (item[0] != item[1].key), self.entries.items()))) == 0 - def draw(self): + def draw(self) -> OmikujiResult: """ おみくじを引く """ @@ -91,7 +90,7 @@ def test(): omikuji.test() -def draw(): +def draw() -> str: """ おみくじ抽選 """ diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index 49bf45c5c8..a16fcad304 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -32,7 +32,4 @@ class TestOmikujiResults(Enum): TestOmikujiResults.SueKichi: OmikujiResult(TestOmikujiResults.SueKichi, 0.5, "末吉"), }) - self.assertTrue( - testOmikuji.draw().message == "吉" - or testOmikuji.draw().message == "末吉" - ) + self.assertIn(testOmikuji.draw().key, TestOmikujiResults) From 60db5a3cff005fffcda0784392b7097dbaa2baba Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 09:57:14 +0900 Subject: [PATCH 16/31] =?UTF-8?q?=E3=81=8A=E3=81=BF=E3=81=8F=E3=81=98?= =?UTF-8?q?=E3=81=AE=E7=B9=8B=E3=81=8E=E3=81=93=E3=81=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/analyze.py | 1 + plugins/hato.py | 9 +++++++++ tests/plugins/test_hato.py | 14 +++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/plugins/analyze.py b/plugins/analyze.py index 614a4bb090..f6c94ca3f0 100644 --- a/plugins/analyze.py +++ b/plugins/analyze.py @@ -24,6 +24,7 @@ def analyze_message(message: str) -> Callable[[BaseClient], None]: '標高': lambda m: hato.altitude((m[len('標高'):]).strip()), 'version': lambda m: hato.version, 'にゃーん': lambda m: hato.yoshiyoshi, + 'おみくじ': lambda m: hato.omikuji, } for key, method in conditions.items(): diff --git a/plugins/hato.py b/plugins/hato.py index 67d28d052b..65b929615a 100644 --- a/plugins/hato.py +++ b/plugins/hato.py @@ -22,6 +22,7 @@ from library.hatokaraage import hato_ha_karaage from library.clientclass import BaseClient from library.jma_amesh import jma_amesh +from library.omikuji import draw as omikuji_draw logger = getLogger(__name__) @@ -54,6 +55,7 @@ def help_message(client: BaseClient): 'text delete [int] ... 指定した番号[int]のパワーワードを削除する。 ', '>< [text] ... 文字列[text]を吹き出しで表示する。', 'にゃーん ... 「よしよし」と返す。', + 'おみくじ ... おみくじを引いて返す。', 'version ... バージョン情報を表示する。', '', '詳細はドキュメント(https://github.com/dev-hato/hato-bot/wiki)も見てくれっぽ!', @@ -273,3 +275,10 @@ def yoshiyoshi(client: BaseClient): logger.debug("%s called 'hato yoshiyoshi'", client.get_send_user()) logger.debug("%s app called 'hato yoshiyoshi'", client.get_type()) client.post('よしよし') + + +def omikuji(client: BaseClient): + """ + おみくじ結果を返す + """ + client.post(message=omikuji_draw()) diff --git a/tests/plugins/test_hato.py b/tests/plugins/test_hato.py index 1792c5e556..6891488249 100644 --- a/tests/plugins/test_hato.py +++ b/tests/plugins/test_hato.py @@ -10,10 +10,12 @@ import requests_mock import slackbot_settings as conf -from plugins.hato import split_command, amesh, altitude, yoshiyoshi +from plugins.hato import split_command, amesh, altitude, yoshiyoshi, omikuji from tests.library.test_geo import set_mock from tests.plugins import TestClient +from library.omikuji import omikuji + class TestSplitCommand(unittest.TestCase): """ @@ -232,5 +234,15 @@ def test_yoshiyoshi(self): self.assertEqual(client1.get_post_message(), 'よしよし') +class TestOmikuji(unittest.TestCase): + """ + omikujiのテスト + """ + + def test_omikuji(self): + client1 = TestClient() + omikuji(client1) + self.assertIn(client1.get_post_message(), map(lambda e: e.message, omikuji.entries.values)) + if __name__ == '__main__': unittest.main() From 9f2ab9c030590e30414a11e9d2b9b391b76e7e2a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 6 Mar 2022 00:41:44 +0000 Subject: [PATCH 17/31] =?UTF-8?q?=E9=B3=A9=E3=81=AF=E5=94=90=E6=8F=9A?= =?UTF-8?q?=E3=81=92=EF=BC=81(=E8=87=AA=E5=8B=95=E3=81=A7=E7=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 1 + 1 file changed, 1 insertion(+) diff --git a/library/omikuji.py b/library/omikuji.py index 94fba64a87..ec00ed6f63 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -14,6 +14,7 @@ TOmikujiEnum = TypeVar('TOmikujiEnum') + @dataclass class OmikujiResult: key: TOmikujiEnum From 68f2d7cdb10d7963f4b94a26b89d0c201be85551 Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 10:21:57 +0900 Subject: [PATCH 18/31] =?UTF-8?q?lint=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 69 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index ec00ed6f63..6fe0b92322 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -4,7 +4,6 @@ おみくじを返す """ -from abc import ABC from enum import Enum, auto from dataclasses import dataclass from functools import reduce @@ -12,11 +11,15 @@ from typing import TypeVar, Generic -TOmikujiEnum = TypeVar('TOmikujiEnum') +TOmikujiEnum = TypeVar('TOmikujiEnum') # type: ignore @dataclass class OmikujiResult: + """ + おみくじの引いた結果を示すデータクラス + 排出率の調整もここで行う + """ key: TOmikujiEnum rate: float message: str @@ -55,7 +58,7 @@ def draw(self) -> OmikujiResult: """ return choices( population=list(self.entries.items()), - weights=map(lambda entry: entry.rate, self.entries.values()), + weights=list(map(lambda entry: entry.rate, self.entries.values())), k=1 )[0][1] @@ -63,23 +66,55 @@ def draw(self) -> OmikujiResult: # 以下おみくじの設定 class OmikujiResults(Enum): - DaiKichi = auto() - ChuKichi = auto() - ShoKichi = auto() - HatoKichi = auto() - Kichi = auto() - Kyo = auto() - DaiKyo = auto() + DAI_KICHI = auto() + CHU_KICHI = auto() + SHO_KICHI = auto() + HATO_KICHI = auto() + KICHI = auto() + KYO = auto() + DAI_KYO = auto() omikuji = Omikuji[OmikujiResults](entries={ - OmikujiResults.DaiKichi: OmikujiResult(OmikujiResults.DaiKichi, 0.02, ":tada: 大吉 何でもうまくいく!!気がする!!"), - OmikujiResults.ChuKichi: OmikujiResult(OmikujiResults.ChuKichi, 0.2, ":smile: 中吉 そこそこうまくいくかも!?"), - OmikujiResults.ShoKichi: OmikujiResult(OmikujiResults.ShoKichi, 0.38, ":smily: 小吉 なんとなくうまくいくかも!?"), - OmikujiResults.Kichi: OmikujiResult(OmikujiResults.Kichi, 0.3, ":smirk: 吉 まあうまくいくかも!?"), - OmikujiResults.HatoKichi: OmikujiResult(OmikujiResults.HatoKichi, 0.09, ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!"), - OmikujiResults.Kyo: OmikujiResult(OmikujiResults.Kyo, 0.0075, ":cry: 凶 ちょっと慎重にいったほうがいいかも……"), - OmikujiResults.DaiKyo: OmikujiResult(OmikujiResults.DaiKyo, 0.0025, ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう"), + OmikujiResults.DAI_KICHI: OmikujiResult( + OmikujiResults.DAI_KICHI, + 0.02, + ":tada: 大吉 何でもうまくいく!!気がする!!" + ), + OmikujiResults.CHU_KICHI: OmikujiResult( + OmikujiResults.CHU_KICHI, + 0.2, + ":smile: 中吉 そこそこうまくいくかも!?" + ), + OmikujiResults.SHO_KICHI: OmikujiResult( + OmikujiResults.SHO_KICHI, + 0.38, + ":smily: 小吉 なんとなくうまくいくかも!?" + ), + + OmikujiResults.KICHI: OmikujiResult( + OmikujiResults.KICHI, + 0.3, + ":smirk: 吉 まあうまくいくかも!?" + ), + OmikujiResults.HATO_KICHI: OmikujiResult( + OmikujiResults.HATO_KICHI, + 0.09, + ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" + ), + + OmikujiResults.KYO: OmikujiResult( + OmikujiResults.KYO, + 0.0075, + ":cry: 凶 ちょっと慎重にいったほうがいいかも……" + ), + + OmikujiResults.DAI_KYO: OmikujiResult( + OmikujiResults.DAI_KYO, + 0.0025, + ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう" + ), + }) From 2388c77dc2a53c92d6540f17b36c0b47e819038b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 6 Mar 2022 01:26:02 +0000 Subject: [PATCH 19/31] =?UTF-8?q?=E9=B3=A9=E3=81=AF=E5=94=90=E6=8F=9A?= =?UTF-8?q?=E3=81=92=EF=BC=81(=E8=87=AA=E5=8B=95=E3=81=A7=E7=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 78 +++++++++++++++++++------------------- tests/plugins/test_hato.py | 4 +- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 6fe0b92322..4efcf77409 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -11,8 +11,8 @@ from typing import TypeVar, Generic +TOmikujiEnum = TypeVar('TOmikujiEnum') # type: ignore -TOmikujiEnum = TypeVar('TOmikujiEnum') # type: ignore @dataclass class OmikujiResult: @@ -76,44 +76,44 @@ class OmikujiResults(Enum): omikuji = Omikuji[OmikujiResults](entries={ - OmikujiResults.DAI_KICHI: OmikujiResult( - OmikujiResults.DAI_KICHI, - 0.02, - ":tada: 大吉 何でもうまくいく!!気がする!!" - ), - OmikujiResults.CHU_KICHI: OmikujiResult( - OmikujiResults.CHU_KICHI, - 0.2, - ":smile: 中吉 そこそこうまくいくかも!?" - ), - OmikujiResults.SHO_KICHI: OmikujiResult( - OmikujiResults.SHO_KICHI, - 0.38, - ":smily: 小吉 なんとなくうまくいくかも!?" - ), - - OmikujiResults.KICHI: OmikujiResult( - OmikujiResults.KICHI, - 0.3, - ":smirk: 吉 まあうまくいくかも!?" - ), - OmikujiResults.HATO_KICHI: OmikujiResult( - OmikujiResults.HATO_KICHI, - 0.09, - ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" - ), - - OmikujiResults.KYO: OmikujiResult( - OmikujiResults.KYO, - 0.0075, - ":cry: 凶 ちょっと慎重にいったほうがいいかも……" - ), - - OmikujiResults.DAI_KYO: OmikujiResult( - OmikujiResults.DAI_KYO, - 0.0025, - ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう" - ), + OmikujiResults.DAI_KICHI: OmikujiResult( + OmikujiResults.DAI_KICHI, + 0.02, + ":tada: 大吉 何でもうまくいく!!気がする!!" + ), + OmikujiResults.CHU_KICHI: OmikujiResult( + OmikujiResults.CHU_KICHI, + 0.2, + ":smile: 中吉 そこそこうまくいくかも!?" + ), + OmikujiResults.SHO_KICHI: OmikujiResult( + OmikujiResults.SHO_KICHI, + 0.38, + ":smily: 小吉 なんとなくうまくいくかも!?" + ), + + OmikujiResults.KICHI: OmikujiResult( + OmikujiResults.KICHI, + 0.3, + ":smirk: 吉 まあうまくいくかも!?" + ), + OmikujiResults.HATO_KICHI: OmikujiResult( + OmikujiResults.HATO_KICHI, + 0.09, + ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" + ), + + OmikujiResults.KYO: OmikujiResult( + OmikujiResults.KYO, + 0.0075, + ":cry: 凶 ちょっと慎重にいったほうがいいかも……" + ), + + OmikujiResults.DAI_KYO: OmikujiResult( + OmikujiResults.DAI_KYO, + 0.0025, + ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう" + ), }) diff --git a/tests/plugins/test_hato.py b/tests/plugins/test_hato.py index 6891488249..764f09081d 100644 --- a/tests/plugins/test_hato.py +++ b/tests/plugins/test_hato.py @@ -242,7 +242,9 @@ class TestOmikuji(unittest.TestCase): def test_omikuji(self): client1 = TestClient() omikuji(client1) - self.assertIn(client1.get_post_message(), map(lambda e: e.message, omikuji.entries.values)) + self.assertIn(client1.get_post_message(), map( + lambda e: e.message, omikuji.entries.values)) + if __name__ == '__main__': unittest.main() From dcd7d4d11a19a846ef36580749e232152c9f0cd4 Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 10:32:04 +0900 Subject: [PATCH 20/31] =?UTF-8?q?=E5=90=8D=E5=89=8D=E7=A9=BA=E9=96=93?= =?UTF-8?q?=E3=81=AE=E8=A1=9D=E7=AA=81=E3=81=AB=E5=AF=BE=E5=87=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/plugins/test_hato.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/plugins/test_hato.py b/tests/plugins/test_hato.py index 764f09081d..74c0fd49d3 100644 --- a/tests/plugins/test_hato.py +++ b/tests/plugins/test_hato.py @@ -14,7 +14,7 @@ from tests.library.test_geo import set_mock from tests.plugins import TestClient -from library.omikuji import omikuji +from library.omikuji import omikuji as omikuji_instance class TestSplitCommand(unittest.TestCase): @@ -243,7 +243,7 @@ def test_omikuji(self): client1 = TestClient() omikuji(client1) self.assertIn(client1.get_post_message(), map( - lambda e: e.message, omikuji.entries.values)) + lambda e: e.message, omikuji_instance.entries.values)) if __name__ == '__main__': From cbd53459f73c307d839533b831f2d48a7f50c18e Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 10:35:19 +0900 Subject: [PATCH 21/31] =?UTF-8?q?assertIn=E3=81=AEtest=E3=81=AE=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E3=83=9F=E3=82=B9=E3=81=AB=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/plugins/test_hato.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/plugins/test_hato.py b/tests/plugins/test_hato.py index 74c0fd49d3..66a27206e8 100644 --- a/tests/plugins/test_hato.py +++ b/tests/plugins/test_hato.py @@ -242,8 +242,13 @@ class TestOmikuji(unittest.TestCase): def test_omikuji(self): client1 = TestClient() omikuji(client1) - self.assertIn(client1.get_post_message(), map( - lambda e: e.message, omikuji_instance.entries.values)) + self.assertIn( + client1.get_post_message(), + list(map( + lambda e: e.message, + omikuji_instance.entries.values + )) + ) if __name__ == '__main__': From 01e8f10f4898906bd8394499f175feef5876c0de Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 10:42:16 +0900 Subject: [PATCH 22/31] =?UTF-8?q?=E5=87=A1=E3=83=9F=E3=82=B9=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/plugins/test_hato.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins/test_hato.py b/tests/plugins/test_hato.py index 66a27206e8..d52eafaa6b 100644 --- a/tests/plugins/test_hato.py +++ b/tests/plugins/test_hato.py @@ -246,7 +246,7 @@ def test_omikuji(self): client1.get_post_message(), list(map( lambda e: e.message, - omikuji_instance.entries.values + omikuji_instance.entries.values() )) ) From 410861e45c858eb5250d3afc73904fe0aa7362ce Mon Sep 17 00:00:00 2001 From: fono09 Date: Sun, 6 Mar 2022 11:03:03 +0900 Subject: [PATCH 23/31] =?UTF-8?q?lint=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 21 ++++++++++++--------- tests/library/test_omikuji.py | 22 +++++++++++++--------- tests/plugins/test_hato.py | 3 +++ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 4efcf77409..35c4f9c058 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -11,11 +11,11 @@ from typing import TypeVar, Generic -TOmikujiEnum = TypeVar('TOmikujiEnum') # type: ignore +TOmikujiEnum = TypeVar('TOmikujiEnum') @dataclass -class OmikujiResult: +class OmikujiResult(Generic[TOmikujiEnum]): """ おみくじの引いた結果を示すデータクラス 排出率の調整もここで行う @@ -25,6 +25,9 @@ class OmikujiResult: message: str def test(self): + """ + セルフテスト + """ assert self.rate < 1 assert self.message != '' @@ -76,40 +79,40 @@ class OmikujiResults(Enum): omikuji = Omikuji[OmikujiResults](entries={ - OmikujiResults.DAI_KICHI: OmikujiResult( + OmikujiResults.DAI_KICHI: OmikujiResult[OmikujiResults]( OmikujiResults.DAI_KICHI, 0.02, ":tada: 大吉 何でもうまくいく!!気がする!!" ), - OmikujiResults.CHU_KICHI: OmikujiResult( + OmikujiResults.CHU_KICHI: OmikujiResult[OmikujiResults]( OmikujiResults.CHU_KICHI, 0.2, ":smile: 中吉 そこそこうまくいくかも!?" ), - OmikujiResults.SHO_KICHI: OmikujiResult( + OmikujiResults.SHO_KICHI: OmikujiResult[OmikujiResults]( OmikujiResults.SHO_KICHI, 0.38, ":smily: 小吉 なんとなくうまくいくかも!?" ), - OmikujiResults.KICHI: OmikujiResult( + OmikujiResults.KICHI: OmikujiResult[OmikujiResults]( OmikujiResults.KICHI, 0.3, ":smirk: 吉 まあうまくいくかも!?" ), - OmikujiResults.HATO_KICHI: OmikujiResult( + OmikujiResults.HATO_KICHI: OmikujiResult[OmikujiResults]( OmikujiResults.HATO_KICHI, 0.09, ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" ), - OmikujiResults.KYO: OmikujiResult( + OmikujiResults.KYO: OmikujiResult[OmikujiResults]( OmikujiResults.KYO, 0.0075, ":cry: 凶 ちょっと慎重にいったほうがいいかも……" ), - OmikujiResults.DAI_KYO: OmikujiResult( + OmikujiResults.DAI_KYO: OmikujiResult[OmikujiResults]( OmikujiResults.DAI_KYO, 0.0025, ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう" diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index a16fcad304..6d3b4ef9c9 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -3,16 +3,17 @@ """ import unittest -from library.omikuji import OmikujiResult, Omikuji, test from enum import Enum, auto +from library.omikuji import OmikujiResult, Omikuji, test + class TestOmikuji(unittest.TestCase): """ おみくじのテスト """ - def test_omikuji_config(self): + def test_omikuji_config(): """ おみくじの設定が正常か """ @@ -23,13 +24,16 @@ def test_omikuji_minimum(self): おみくじの実装が正常か """ - class TestOmikujiResults(Enum): - Kichi = auto() - SueKichi = auto() + class DummyOmikujiResults(Enum): + """ + テスト用のおみくじ結果Enum + """ + KICHI = auto() + SUE_KICHI = auto() - testOmikuji = Omikuji[TestOmikujiResults](entries={ - TestOmikujiResults.Kichi: OmikujiResult(TestOmikujiResults.Kichi, 0.5, "吉"), - TestOmikujiResults.SueKichi: OmikujiResult(TestOmikujiResults.SueKichi, 0.5, "末吉"), + testOmikuji = Omikuji[DummyOmikujiResults](entries={ + DummyOmikujiResults.KICHI: OmikujiResult(DummyOmikujiResults.KICHI, 0.5, "吉"), + DummyOmikujiResults.SUE_KICHI: OmikujiResult(DummyOmikujiResults.SUE_KICHI, 0.5, "末吉"), }) - self.assertIn(testOmikuji.draw().key, TestOmikujiResults) + self.assertIn(testOmikuji.draw().key, DummyOmikujiResults) diff --git a/tests/plugins/test_hato.py b/tests/plugins/test_hato.py index d52eafaa6b..73830c2354 100644 --- a/tests/plugins/test_hato.py +++ b/tests/plugins/test_hato.py @@ -240,6 +240,9 @@ class TestOmikuji(unittest.TestCase): """ def test_omikuji(self): + """ + 登録されたメッセージのいずれかが返ってくることのみ確認 + """ client1 = TestClient() omikuji(client1) self.assertIn( From 8880b83a142a598e55eead77b584097f00fb1744 Mon Sep 17 00:00:00 2001 From: fono09 Date: Mon, 7 Mar 2022 20:34:55 +0900 Subject: [PATCH 24/31] =?UTF-8?q?lint/test=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 4 ++++ tests/library/test_omikuji.py | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 35c4f9c058..da1bde9f97 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -69,6 +69,10 @@ def draw(self) -> OmikujiResult: # 以下おみくじの設定 class OmikujiResults(Enum): + """ + おみくじ結果のEnum + """ + DAI_KICHI = auto() CHU_KICHI = auto() SHO_KICHI = auto() diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index 6d3b4ef9c9..b952dc8842 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -13,11 +13,12 @@ class TestOmikuji(unittest.TestCase): おみくじのテスト """ - def test_omikuji_config(): + def test_omikuji_config(self): """ おみくじの設定が正常か """ - test() + self.assertIsNone(test()) + def test_omikuji_minimum(self): """ @@ -31,9 +32,9 @@ class DummyOmikujiResults(Enum): KICHI = auto() SUE_KICHI = auto() - testOmikuji = Omikuji[DummyOmikujiResults](entries={ + dummy_omikuji = Omikuji[DummyOmikujiResults](entries={ DummyOmikujiResults.KICHI: OmikujiResult(DummyOmikujiResults.KICHI, 0.5, "吉"), DummyOmikujiResults.SUE_KICHI: OmikujiResult(DummyOmikujiResults.SUE_KICHI, 0.5, "末吉"), }) - self.assertIn(testOmikuji.draw().key, DummyOmikujiResults) + self.assertIn(dummy_omikuji.draw().key, DummyOmikujiResults) From b8e4036e740ee6e0ef2a3681262544c4225bc432 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 7 Mar 2022 11:35:45 +0000 Subject: [PATCH 25/31] =?UTF-8?q?=E9=B3=A9=E3=81=AF=E5=94=90=E6=8F=9A?= =?UTF-8?q?=E3=81=92=EF=BC=81(=E8=87=AA=E5=8B=95=E3=81=A7=E7=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/library/test_omikuji.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index b952dc8842..181cc0bca2 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -19,7 +19,6 @@ def test_omikuji_config(self): """ self.assertIsNone(test()) - def test_omikuji_minimum(self): """ おみくじの実装が正常か From cbe8113026c314b3b10d318aab3588fac2ecb505 Mon Sep 17 00:00:00 2001 From: fono09 Date: Tue, 8 Mar 2022 20:38:41 +0900 Subject: [PATCH 26/31] =?UTF-8?q?=E3=82=B7=E3=83=B3=E3=83=97=E3=83=AB?= =?UTF-8?q?=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 103 +++++++++------------------------- tests/library/test_omikuji.py | 25 ++------- 2 files changed, 33 insertions(+), 95 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index da1bde9f97..f009871a82 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -4,137 +4,90 @@ おみくじを返す """ +from typing import Hashable from enum import Enum, auto from dataclasses import dataclass from functools import reduce from random import choices -from typing import TypeVar, Generic - - -TOmikujiEnum = TypeVar('TOmikujiEnum') @dataclass -class OmikujiResult(Generic[TOmikujiEnum]): +class OmikujiResult(): """ おみくじの引いた結果を示すデータクラス - 排出率の調整もここで行う + 出やすさの調整もここで行う """ - key: TOmikujiEnum - rate: float + appearance: int message: str - def test(self): + def __post_init__(self): """ - セルフテスト + 初期化後のアサーション """ - assert self.rate < 1 + assert self.appearance > 0 assert self.message != '' -class Omikuji(Generic[TOmikujiEnum]): +class Omikuji: """ おみくじのコアロジック ガチャではないので排出率を公開するメソッドはあえて実装されていない """ - def __init__(self, entries: dict[TOmikujiEnum, OmikujiResult]): + def __init__(self, entries: dict[Hashable, OmikujiResult]): self.entries = entries - def test(self): - """ - 全体のテストを行う - - 各エントリーのテスト、 排出率の合計が1であること、エントリーのキーが不整合のテストをする - """ - for entry in self.entries.values(): - entry.test() - - assert round(reduce(lambda acc, cur: acc + cur.rate, - self.entries.values(), 0), 1) == 1 - assert len( - list(filter(lambda item: (item[0] != item[1].key), self.entries.items()))) == 0 - - def draw(self) -> OmikujiResult: + def draw(self) -> (Hashable, OmikujiResult): """ おみくじを引く """ return choices( population=list(self.entries.items()), - weights=list(map(lambda entry: entry.rate, self.entries.values())), + weights=list(map(lambda entry: entry.appearance, self.entries.values())), k=1 - )[0][1] + )[0] # 以下おみくじの設定 -class OmikujiResults(Enum): - """ - おみくじ結果のEnum - """ - - DAI_KICHI = auto() - CHU_KICHI = auto() - SHO_KICHI = auto() - HATO_KICHI = auto() - KICHI = auto() - KYO = auto() - DAI_KYO = auto() - - -omikuji = Omikuji[OmikujiResults](entries={ - OmikujiResults.DAI_KICHI: OmikujiResult[OmikujiResults]( - OmikujiResults.DAI_KICHI, - 0.02, +omikuji = Omikuji(entries={ + 'DAI_KICHI': OmikujiResult( + 200, ":tada: 大吉 何でもうまくいく!!気がする!!" ), - OmikujiResults.CHU_KICHI: OmikujiResult[OmikujiResults]( - OmikujiResults.CHU_KICHI, - 0.2, + 'CHU_KICHI': OmikujiResult( + 2000, ":smile: 中吉 そこそこうまくいくかも!?" ), - OmikujiResults.SHO_KICHI: OmikujiResult[OmikujiResults]( - OmikujiResults.SHO_KICHI, - 0.38, + 'SHO_KICHI': OmikujiResult( + 3800, ":smily: 小吉 なんとなくうまくいくかも!?" ), - OmikujiResults.KICHI: OmikujiResult[OmikujiResults]( - OmikujiResults.KICHI, - 0.3, + 'KICHI': OmikujiResult( + 3000, ":smirk: 吉 まあうまくいくかも!?" ), - OmikujiResults.HATO_KICHI: OmikujiResult[OmikujiResults]( - OmikujiResults.HATO_KICHI, - 0.09, + 'HATO_KICHI': OmikujiResult( + 900, ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" ), - OmikujiResults.KYO: OmikujiResult[OmikujiResults]( - OmikujiResults.KYO, - 0.0075, + 'KYO': OmikujiResult( + 75, ":cry: 凶 ちょっと慎重にいったほうがいいかも……" ), - OmikujiResults.DAI_KYO: OmikujiResult[OmikujiResults]( - OmikujiResults.DAI_KYO, - 0.0025, + 'DAI_KYO': OmikujiResult( + 25, ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう" ), }) -def test(): - """ - おみくじの設定テスト - """ - - omikuji.test() - - def draw() -> str: """ おみくじ抽選 """ - return omikuji.draw().message + return omikuji.draw()[1].message diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index b952dc8842..5988cbcb8e 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -5,7 +5,7 @@ import unittest from enum import Enum, auto -from library.omikuji import OmikujiResult, Omikuji, test +from library.omikuji import OmikujiResult, Omikuji class TestOmikuji(unittest.TestCase): @@ -13,28 +13,13 @@ class TestOmikuji(unittest.TestCase): おみくじのテスト """ - def test_omikuji_config(self): - """ - おみくじの設定が正常か - """ - self.assertIsNone(test()) - - def test_omikuji_minimum(self): """ おみくじの実装が正常か """ - - class DummyOmikujiResults(Enum): - """ - テスト用のおみくじ結果Enum - """ - KICHI = auto() - SUE_KICHI = auto() - - dummy_omikuji = Omikuji[DummyOmikujiResults](entries={ - DummyOmikujiResults.KICHI: OmikujiResult(DummyOmikujiResults.KICHI, 0.5, "吉"), - DummyOmikujiResults.SUE_KICHI: OmikujiResult(DummyOmikujiResults.SUE_KICHI, 0.5, "末吉"), + dummy_omikuji = Omikuji(entries={ + 'KICHI': OmikujiResult(1, "吉"), + 'SUE_KICHI': OmikujiResult(1, "末吉"), }) - self.assertIn(dummy_omikuji.draw().key, DummyOmikujiResults) + self.assertIn(dummy_omikuji.draw()[0], dummy_omikuji.entries.keys()) From 5cf34632b1dcf4e9f4db614ef4469a39d40fa942 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 8 Mar 2022 11:41:28 +0000 Subject: [PATCH 27/31] =?UTF-8?q?=E9=B3=A9=E3=81=AF=E5=94=90=E6=8F=9A?= =?UTF-8?q?=E3=81=92=EF=BC=81(=E8=87=AA=E5=8B=95=E3=81=A7=E7=9B=B4?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=82=E3=81=92=E3=81=9F=E3=82=88=EF=BC=81?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index f009871a82..27897a0648 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -43,7 +43,8 @@ def draw(self) -> (Hashable, OmikujiResult): """ return choices( population=list(self.entries.items()), - weights=list(map(lambda entry: entry.appearance, self.entries.values())), + weights=list( + map(lambda entry: entry.appearance, self.entries.values())), k=1 )[0] @@ -73,7 +74,7 @@ def draw(self) -> (Hashable, OmikujiResult): ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" ), - 'KYO': OmikujiResult( + 'KYO': OmikujiResult( 75, ":cry: 凶 ちょっと慎重にいったほうがいいかも……" ), From 5efc4c4002bd0b673c2f3c0b9abd27520b19c608 Mon Sep 17 00:00:00 2001 From: fono09 Date: Tue, 8 Mar 2022 20:47:07 +0900 Subject: [PATCH 28/31] =?UTF-8?q?lint=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 2 -- tests/library/test_omikuji.py | 1 - 2 files changed, 3 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index 27897a0648..37bd303fc4 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -5,9 +5,7 @@ """ from typing import Hashable -from enum import Enum, auto from dataclasses import dataclass -from functools import reduce from random import choices diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index 5988cbcb8e..472e04c91e 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -3,7 +3,6 @@ """ import unittest -from enum import Enum, auto from library.omikuji import OmikujiResult, Omikuji From e38753e356915ba26f493c353718c46b7255fe0e Mon Sep 17 00:00:00 2001 From: fono09 Date: Tue, 8 Mar 2022 20:49:10 +0900 Subject: [PATCH 29/31] =?UTF-8?q?=E8=84=B1=E5=AD=97=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/omikuji.py b/library/omikuji.py index 37bd303fc4..c9b0943e0d 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -69,7 +69,7 @@ def draw(self) -> (Hashable, OmikujiResult): ), 'HATO_KICHI': OmikujiResult( 900, - ":dove_of_peace: 鳩吉 お前がになる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" + ":dove_of_peace: 鳩吉 お前が鳩になる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" ), 'KYO': OmikujiResult( From ff6a85e481634b94bd15f81d9427e3d756816b21 Mon Sep 17 00:00:00 2001 From: fono09 Date: Tue, 8 Mar 2022 20:58:51 +0900 Subject: [PATCH 30/31] =?UTF-8?q?lint=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index c9b0943e0d..bb166b0bc8 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -4,7 +4,7 @@ おみくじを返す """ -from typing import Hashable +from typing import Hashable, Tuple from dataclasses import dataclass from random import choices @@ -26,16 +26,15 @@ def __post_init__(self): assert self.message != '' +@dataclass class Omikuji: """ おみくじのコアロジック ガチャではないので排出率を公開するメソッドはあえて実装されていない """ + entries: dict[Hashable, OmikujiResult] - def __init__(self, entries: dict[Hashable, OmikujiResult]): - self.entries = entries - - def draw(self) -> (Hashable, OmikujiResult): + def draw(self) -> Tuple[Hashable, OmikujiResult]: """ おみくじを引く """ @@ -49,7 +48,7 @@ def draw(self) -> (Hashable, OmikujiResult): # 以下おみくじの設定 -omikuji = Omikuji(entries={ +omikuji = Omikuji({ 'DAI_KICHI': OmikujiResult( 200, ":tada: 大吉 何でもうまくいく!!気がする!!" From 2e95cce0042d0374cbfe1373157242f7f56d16dd Mon Sep 17 00:00:00 2001 From: fono09 Date: Wed, 9 Mar 2022 21:41:19 +0900 Subject: [PATCH 31/31] =?UTF-8?q?Enum&dict=E7=B6=99=E6=89=BF=E3=81=A7?= =?UTF-8?q?=E5=9E=8B=E5=AE=89=E5=85=A8=E3=81=AB=E3=81=97=E3=81=A6=E3=81=BF?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/omikuji.py | 72 ++++++----------------------------- plugins/hato.py | 60 ++++++++++++++++++++++++++++- tests/library/test_omikuji.py | 21 +++++++--- tests/plugins/test_hato.py | 12 ++---- 4 files changed, 90 insertions(+), 75 deletions(-) diff --git a/library/omikuji.py b/library/omikuji.py index bb166b0bc8..8ba5583c31 100644 --- a/library/omikuji.py +++ b/library/omikuji.py @@ -4,7 +4,7 @@ おみくじを返す """ -from typing import Hashable, Tuple +from typing import Tuple, TypeVar from dataclasses import dataclass from random import choices @@ -26,66 +26,18 @@ def __post_init__(self): assert self.message != '' -@dataclass -class Omikuji: - """ - おみくじのコアロジック - ガチャではないので排出率を公開するメソッドはあえて実装されていない - """ - entries: dict[Hashable, OmikujiResult] - - def draw(self) -> Tuple[Hashable, OmikujiResult]: - """ - おみくじを引く - """ - return choices( - population=list(self.entries.items()), - weights=list( - map(lambda entry: entry.appearance, self.entries.values())), - k=1 - )[0] - +TOmikujiEnum = TypeVar('TOmikujiEnum') +OmikujiResults = dict[TOmikujiEnum, OmikujiResult] -# 以下おみくじの設定 -omikuji = Omikuji({ - 'DAI_KICHI': OmikujiResult( - 200, - ":tada: 大吉 何でもうまくいく!!気がする!!" - ), - 'CHU_KICHI': OmikujiResult( - 2000, - ":smile: 中吉 そこそこうまくいくかも!?" - ), - 'SHO_KICHI': OmikujiResult( - 3800, - ":smily: 小吉 なんとなくうまくいくかも!?" - ), - - 'KICHI': OmikujiResult( - 3000, - ":smirk: 吉 まあうまくいくかも!?" - ), - 'HATO_KICHI': OmikujiResult( - 900, - ":dove_of_peace: 鳩吉 お前が鳩になる番だ!!羽ばたけ!!!飛べ!!!!唐揚げになれ!!!!!" - ), - - 'KYO': OmikujiResult( - 75, - ":cry: 凶 ちょっと慎重にいったほうがいいかも……" - ), - - 'DAI_KYO': OmikujiResult( - 25, - ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう" - ), - -}) - - -def draw() -> str: +def draw(entries: OmikujiResults) -> Tuple[TOmikujiEnum, OmikujiResult]: """ - おみくじ抽選 + おみくじを引く """ - return omikuji.draw()[1].message + + return choices( + population=list(entries.items()), + weights=list( + map(lambda entry: entry.appearance, entries.values())), + k=1 + )[0] diff --git a/plugins/hato.py b/plugins/hato.py index 65b929615a..d6c702b2cc 100644 --- a/plugins/hato.py +++ b/plugins/hato.py @@ -6,6 +6,7 @@ import json import os import re +from enum import Enum, auto from logging import getLogger from tempfile import NamedTemporaryFile from typing import List @@ -22,7 +23,7 @@ from library.hatokaraage import hato_ha_karaage from library.clientclass import BaseClient from library.jma_amesh import jma_amesh -from library.omikuji import draw as omikuji_draw +from library.omikuji import OmikujiResult, OmikujiResults, draw as omikuji_draw logger = getLogger(__name__) @@ -277,8 +278,63 @@ def yoshiyoshi(client: BaseClient): client.post('よしよし') +# 以下おみくじの設定 +# Refer: dev-hato/hato-bot#876 +class OmikujiEnum(Enum): + """ + おみくじの結果一覧 + """ + DAI_KICHI = auto() + CHU_KICHI = auto() + SHO_KICHI = auto() + KICHI = auto() + SUE_KICHI = auto() + AGE_KICHI = auto() + KYO = auto() + DAI_KYO = auto() + + +omikuji_results = OmikujiResults({ + OmikujiEnum.DAI_KICHI: OmikujiResult( + 12, + ":tada: 大吉 何でもうまくいく!!気がする!!" + ), + OmikujiEnum.KICHI: OmikujiResult( + 100, + ":smirk: 吉 まあうまくいくかも!?" + ), + OmikujiEnum.CHU_KICHI: OmikujiResult( + 100, + ":smile: 中吉 そこそこうまくいくかも!?" + ), + OmikujiEnum.SHO_KICHI: OmikujiResult( + 100, + ":smily: 小吉 なんとなくうまくいくかも!?" + ), + OmikujiEnum.SUE_KICHI: OmikujiResult( + 37, + ":expressionless: 末吉 まあ多分うまくいくかもね……!?" + ), + OmikujiEnum.AGE_KICHI: OmikujiResult( + 2, + ":poultry_leg: 揚げ吉 鳩を揚げると良いことあるよ!!" + ), + OmikujiEnum.KYO: OmikujiResult( + 12, + ":cry: 凶 ちょっと慎重にいったほうがいいかも……" + ), + OmikujiEnum.DAI_KYO: OmikujiResult( + 2, + ":crying_cat_face: 大凶 そういう時もあります……猫になって耐えましょう" + ), +}) + + def omikuji(client: BaseClient): """ おみくじ結果を返す """ - client.post(message=omikuji_draw()) + + logger.debug("%s called 'hato yoshiyoshi'", client.get_send_user()) + logger.debug("%s app called 'hato yoshiyoshi'", client.get_type()) + client.post(omikuji_draw(omikuji_results)[1].message) diff --git a/tests/library/test_omikuji.py b/tests/library/test_omikuji.py index 472e04c91e..af184b1463 100644 --- a/tests/library/test_omikuji.py +++ b/tests/library/test_omikuji.py @@ -4,7 +4,8 @@ import unittest -from library.omikuji import OmikujiResult, Omikuji +from enum import Enum, auto +from library.omikuji import OmikujiResult, OmikujiResults, draw class TestOmikuji(unittest.TestCase): @@ -16,9 +17,19 @@ def test_omikuji_minimum(self): """ おみくじの実装が正常か """ - dummy_omikuji = Omikuji(entries={ - 'KICHI': OmikujiResult(1, "吉"), - 'SUE_KICHI': OmikujiResult(1, "末吉"), + class DummyOmikujiEnum(Enum): + """ + おみくじ設定(ダミー) + """ + KICHI = auto() + SUE_KICHI = auto() + + dummy_omikuji_results = OmikujiResults({ + DummyOmikujiEnum.KICHI: OmikujiResult(1, "吉"), + DummyOmikujiEnum.SUE_KICHI: OmikujiResult(1, "末吉"), }) - self.assertIn(dummy_omikuji.draw()[0], dummy_omikuji.entries.keys()) + self.assertIn( + draw(dummy_omikuji_results)[0], + dummy_omikuji_results.keys() + ) diff --git a/tests/plugins/test_hato.py b/tests/plugins/test_hato.py index 73830c2354..b13b9b58af 100644 --- a/tests/plugins/test_hato.py +++ b/tests/plugins/test_hato.py @@ -10,12 +10,10 @@ import requests_mock import slackbot_settings as conf -from plugins.hato import split_command, amesh, altitude, yoshiyoshi, omikuji +from plugins.hato import split_command, amesh, altitude, yoshiyoshi, omikuji, omikuji_results from tests.library.test_geo import set_mock from tests.plugins import TestClient -from library.omikuji import omikuji as omikuji_instance - class TestSplitCommand(unittest.TestCase): """ @@ -241,16 +239,14 @@ class TestOmikuji(unittest.TestCase): def test_omikuji(self): """ - 登録されたメッセージのいずれかが返ってくることのみ確認 + 設定したおみくじ結果のうち1つが返ってくる """ + client1 = TestClient() omikuji(client1) self.assertIn( client1.get_post_message(), - list(map( - lambda e: e.message, - omikuji_instance.entries.values() - )) + map(lambda e: e.message, omikuji_results.values()) )