def test_get_arbitrary_rate_converting_from_base_currency_correct_value(): db = FakeDatabase(dict()) get_base_rate_ = MagicMock() get_base_rate_.return_value = MagicMock() with patch('cx.common.rates.get_base_rate', new=get_base_rate_): assert get_arbitrary_rate(db=db, date=date(2018, 5, 11), from_="EUR", to="USD") == get_base_rate_.return_value
def test_get_arbitrary_rate_converting_to_base_currency_correct_call(): db = FakeDatabase(dict()) get_base_rate_ = MagicMock() with patch('cx.common.rates.get_base_rate', new=get_base_rate_): get_arbitrary_rate(db=db, date=date(2018, 5, 11), from_="USD", to="EUR") get_base_rate_.assert_called_with(db=db, date=date(2018, 5, 11), to="USD")
def test_get_base_rate_validate_currency(date_, currency, value): db = FakeDatabase({ date(2017, 5, 11): { "CZK": 4 }, date(2018, 5, 11): { "PLN": 2 }, }) assert get_base_rate(db, date=date_, to=currency) == value
def test_get_arbitrary_rate_converting_arbitrary_currency_correct_value( currency, rate): db = FakeDatabase(dict()) get_base_rate_ = MagicMock() get_base_rate_.side_effect = lambda db, date, to: rate if to == currency else 2 with patch('cx.common.rates.get_base_rate', new=get_base_rate_): assert get_arbitrary_rate(db=db, date=date(2018, 5, 11), from_="USD", to=currency) == pytest.approx(rate / 2)
def test_get_jirachi(self): # Arrange db = FakeDatabase() engine = Search(db) text = "[Jirachi]" # Act match = engine.find_cards(text)[0] # Assert self.assertEqual("Jirachi", match.name, "Card name should match")
def test_get_last_jirachi(self): # Arrange db = FakeDatabase() engine = Search(db) text = "[Jirachi]" # Act match = engine.find_cards(text)[0] # Assert self.assertEqual( match.series.name, "Team Up", "Oldest Jirachi is in Team Up")
def test_get_jirachi_by_series(self): # Arrange db = FakeDatabase() engine = Search(db) text = "[Jirachi SM7]" # Act match = engine.find_cards(text)[0] # Assert self.assertEqual( match.series.name, "Celestial Storm", "We requested Jirachi {*} from Celestial Storm")
def test_get_incomplete_name(self): # Arrange db = FakeDatabase() engine = Search(db) text = "[Jirach]" # Act match = engine.find_cards(text)[0] # Assert self.assertTrue("Jirachi" in match.name, "Incomplete text should be enough to retrieve a card")
def test_get_base_rate_validate_currency(FakeDatabase): db = FakeDatabase(dict()) fake_currency = MagicMock() validate_currency_ = MagicMock() with patch('cx.common.rates.validate_currency', new=validate_currency_): try: get_base_rate(db=db, date=date(2018, 5, 11), to=fake_currency) except: pass validate_currency_.assert_called_with(fake_currency)
def test_get_none(self): # Arrange db = FakeDatabase() engine = Search(db) text = "[Kuriboh]" # Act match = engine.find_cards(text)[0] # Assert self.assertIsInstance( match, Card, "We always return a card object") self.assertIsInstance( match, ErrorCard, "We return an Error object instead") self.assertFalse( match.is_valid(), "No Pokémon card should have that name")
def test_get_jirachi_by_series_and_number(self): # Arrange db = FakeDatabase() engine = Search(db) text = "[Jirachi SM9 99]" # Act match = engine.find_cards(text)[0] # Assert self.assertEqual("Jirachi", match.name, "Card should be found") self.assertEqual("Team Up", match.series.name, "Card should have the right series") self.assertEqual(99, match.number, "Card should have the right collection number")
def test_get_EX_card(self): # Arrange db = FakeDatabase() engine = Search(db) text1 = "[Pikachu EX]" text2 = "[Pikachu-EX]" # Act match1 = engine.find_cards(text1)[0] match2 = engine.find_cards(text2)[0] # Assert self.assertNotIsInstance( match1, ErrorCard, "Should find a card") self.assertEqual(match1, match2, "Should be the same card")
def test_get_Vmax_card(self): # Arrange db = FakeDatabase() engine = Search(db) text1 = "[Inteleon Vmax]" text2 = "[Inteleon-Vmax]" # Act match1 = engine.find_cards(text1)[0] match2 = engine.find_cards(text2)[0] # Assert self.assertNotIsInstance( match1, ErrorCard, "Should find a card") self.assertEqual(match1, match2, "Should be the same card") self.assertNotEqual(match1.name, "Inteleon V" , "Should not pick the V card")
def test_get_arbitrary_rate_validate_target_currency(): db = FakeDatabase(dict()) target_currency = MagicMock() validate_currency_ = MagicMock() with patch('cx.common.rates.validate_currency', new=validate_currency_): try: get_arbitrary_rate(db=db, date=date(2018, 5, 11), to=target_currency, from_="EUR") except: pass validate_currency_.assert_any_call(target_currency)
def test_get_GX_card(self): # Arrange db = FakeDatabase() engine = Search(db) text1 = "[Pikachu GX]" text2 = "[Pikachu-GX]" # Act match1 = engine.find_cards(text1)[0] match2 = engine.find_cards(text2)[0] # Assert self.assertNotIsInstance( match1, ErrorCard, "Should find a card") # Beware of not picking Pikachu&Zekrom GX self.assertEqual(match1.name, "Pikachu GX", "The right card should be found") self.assertEqual(match1, match2, "Should be the same card")
def test_get_jirachi_by_alternative_series(self): # Arrange db = FakeDatabase() engine = Search(db) text1 = "[Jirachi SM9]" text2 = "[Jirachi TEU]" text3 = "[Jirachi SM09]" # Act match1 = engine.find_cards(text1)[0] match2 = engine.find_cards(text2)[0] match3 = engine.find_cards(text3)[0] # Assert self.assertEqual( match1, match2, "Pokemon.com abbreviation and tournament official abbreviation should match") self.assertEqual( match1, match3, "Pokemon.com abbreviation and Pokécardex abbreviation should match")
# bot.py import os import discord from dotenv import load_dotenv from src.search import Search from tests.fake_database import FakeDatabase # Fake initialization - use a fake database to have your bot working from src.utilities_interface import UtilitiesInterface # Fake initialization - you need to extend the Interface classes to fit your database load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') # Fake initialization - use a fake database to have your bot working db = FakeDatabase() utilities = UtilitiesInterface # / TEMP - you will need to extend the Interfaces in order to interact with your own database searcher = Search(db, utilities) client = discord.Client() @client.event async def on_ready(): # Feedback to ensure the bot is on print(f'{client.user.name} has connected to Discord!') @client.event async def on_message(message): if message.author == client.user: return # avoid recursion cards = searcher.find_cards(message.content)
def test_get_base_rate_validate_currency(message, currency): db = {date(2018, 5, 5): {}} with pytest.raises(MissingDataError, match=message): get_base_rate(FakeDatabase(db), date=date(2018, 5, 5), to=currency)
def test_get_base_rate_validate_date(message, day): with pytest.raises(MissingDataError, match=message): get_base_rate(db=FakeDatabase(dict()), date=date(2018, 5, day), to="CZK")
def test_get_arbitrary_rate_identical_currencies(): db = FakeDatabase(dict()) assert get_arbitrary_rate(db=db, date=date(2018, 5, 11), from_="USD", to="USD") == 1.0