diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-12-25 13:40:58 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-12-25 14:25:20 +0100 |
commit | 65406f7b72f4004e4483573640cf0d555877dd55 (patch) | |
tree | b9776c2cff52c1f6e9592b2f5613068b618421f7 | |
parent | 0bdaea4591fae05a1a93b6ddcf066f92e8f224f4 (diff) |
Make sure unit tests can be run from outside the package
The cards from usr/share are not accessible when running the tests from
a temporary directory. This happens for instance when running
$ pybuild --test --test-pytest
Make sure that this command would succeed by mocking the calls to open.
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
-rw-r--r-- | tests/test_cards.py | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/tests/test_cards.py b/tests/test_cards.py index a5e4437..73f42a0 100644 --- a/tests/test_cards.py +++ b/tests/test_cards.py @@ -1,19 +1,26 @@ +import json import unittest +from unittest.mock import patch, mock_open from swiftstory.cards import Cards class TestCards(unittest.TestCase): - def test_get_cards_fr(self): - self.assertTrue(Cards.get_white_cards("fr")) - self.assertTrue(Cards.get_black_cards("fr")) + def test_get_cards_supported_language(self): + white_cards = json.dumps([ + "White Card 1", "White Card 2", + "White Card 3", "White Card 4", + ]) + black_cards = json.dumps(["Black Card 1", "Black Card 2"]) - def test_get_cards_en(self): - self.assertTrue(Cards.get_white_cards("en")) - self.assertTrue(Cards.get_black_cards("en")) + with patch("swiftstory.cards.open", mock_open(read_data=white_cards)): + self.assertIn("White Card 2", Cards.get_white_cards("en")) + with patch("swiftstory.cards.open", mock_open(read_data=black_cards)): + self.assertIn("Black Card 1", Cards.get_black_cards("en")) def test_get_cards_unknown_language(self): - with self.assertRaises(FileNotFoundError): - Cards.get_white_cards("zz") - with self.assertRaises(FileNotFoundError): - Cards.get_black_cards("zz") + with patch("swiftstory.cards.open", side_effect=FileNotFoundError): + with self.assertRaises(FileNotFoundError): + Cards.get_white_cards("zz") + with self.assertRaises(FileNotFoundError): + Cards.get_black_cards("zz") |