|
| 1 | +# ============================================================================= |
| 2 | +# >> IMPORTS |
| 3 | +# ============================================================================= |
| 4 | +from unittest import TestCase, main |
| 5 | + |
| 6 | + |
| 7 | +# ============================================================================= |
| 8 | +# >> TEST CLASSES |
| 9 | +# ============================================================================= |
| 10 | +class TestCoreSteamWebAPI(TestCase): |
| 11 | + """Tests functionality of the SteamWebAPI within core.py.""" |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + """Initializes a SteamWebAPI instance.""" |
| 15 | + from steamwebapi.core import SteamWebAPI |
| 16 | + self.steamwebapi = SteamWebAPI() |
| 17 | + |
| 18 | + # ------------------------------------------------------------------------- |
| 19 | + # API Key Tests |
| 20 | + # ------------------------------------------------------------------------- |
| 21 | + def test_api_key_equalto_settings_api_key(self): |
| 22 | + """Tests that initializing SteamWebAPI without arguments retrieves |
| 23 | + the STEAM_API_KEY from ./steamwebapi/settings.py. |
| 24 | +
|
| 25 | + """ |
| 26 | + from steamwebapi.settings import STEAM_API_KEY |
| 27 | + self.assertEqual( |
| 28 | + self.steamwebapi.key, |
| 29 | + STEAM_API_KEY, |
| 30 | + 'The settings.STEAM_API_KEY was not initialized as the default ' + |
| 31 | + 'argument for SteamWebAPI().key.', |
| 32 | + ) |
| 33 | + |
| 34 | + def test_api_key_not_equalto_settings_api_key(self): |
| 35 | + """Tests that changing the Steam Web API key from within the |
| 36 | + SteamWebAPI instance works, as well as ensure that it differs from the |
| 37 | + STEAM_API_KEY from ./steamwebapi/settings.py. |
| 38 | +
|
| 39 | + """ |
| 40 | + from steamwebapi.settings import STEAM_API_KEY |
| 41 | + self.steamwebapi.key = 'XXXX0000XXXX0000XXXX0000XXXX0000' |
| 42 | + self.assertNotEqual( |
| 43 | + self.steamwebapi.key, |
| 44 | + STEAM_API_KEY, |
| 45 | + 'The SteamWebAPI().key instance is the same as ' + |
| 46 | + 'settings.STEAM_API_KEY is the same although it should have ' + |
| 47 | + 'changed.', |
| 48 | + ) |
| 49 | + |
| 50 | + def test_api_key_initialized_from__init__with_keyword_arg(self): |
| 51 | + """Tests that initializing SteamWebAPI with the keyword argument 'key' |
| 52 | + creates a SteamWebAPI().key instance attribute which equals the |
| 53 | + keyword argument. This prevents regression of changing the name of the |
| 54 | + argument. |
| 55 | +
|
| 56 | + """ |
| 57 | + from steamwebapi.core import SteamWebAPI |
| 58 | + self.steamwebapi = SteamWebAPI(key='XXXX0000XXXX0000XXXX0000XXXX0000') |
| 59 | + self.assertEqual( |
| 60 | + self.steamwebapi.key, |
| 61 | + 'XXXX0000XXXX0000XXXX0000XXXX0000', |
| 62 | + 'The SteamWebAPI().key is not equal to the keyword argument.', |
| 63 | + ) |
| 64 | + |
| 65 | + def test_api_key_initialized_from__init__no_keyword_arg(self): |
| 66 | + """Tests that initializing SteamWebAPI without the keyword argument |
| 67 | + 'key' creates a SteamWebAPI().key instance attribute which equals the |
| 68 | + keyword argument. This ensures that we do not alter the argument order |
| 69 | + causing a regression for users that do not provide keyword arguments. |
| 70 | +
|
| 71 | + """ |
| 72 | + from steamwebapi.core import SteamWebAPI |
| 73 | + # Set a fake (bad) Steam Web API Key |
| 74 | + self.steamwebapi = SteamWebAPI('XXXX0000XXXX0000XXXX0000XXXX0000') |
| 75 | + self.assertEqual( |
| 76 | + self.steamwebapi.key, |
| 77 | + 'XXXX0000XXXX0000XXXX0000XXXX0000', |
| 78 | + 'The SteamWebAPI().key was not set when provided as the first ' + |
| 79 | + 'argument without being a keyword argument.', |
| 80 | + ) |
| 81 | + |
| 82 | + def test_api_key_invalid_28_alphanumeric_characters(self): |
| 83 | + """Tests initializing SteamWebAPI with an invalid Steam Web API Key. |
| 84 | + The Steam Web API Key should be 32 alphanumeric characters long. We |
| 85 | + will test by passing in 28 alphanumeric characters. |
| 86 | +
|
| 87 | + """ |
| 88 | + from steamwebapi.core import SteamWebAPI |
| 89 | + from steamwebapi.util.exceptions import APIKeyInvalidError |
| 90 | + with self.assertRaises(APIKeyInvalidError): |
| 91 | + # Send a 28 instead of 32 character API key |
| 92 | + self.steamwebapi = SteamWebAPI('XXXX0000XXXX0000XXXX0000XXXX') |
| 93 | + |
| 94 | + def test_api_key_invalid_non_alphanumeric_character(self): |
| 95 | + """Tests initializing SteamWebAPI with an invalid Steam Web API Key. |
| 96 | + The Steam Web API Key should be 32 alphanumeric characters long. We |
| 97 | + will test by passing in a non-alphanumeric character. |
| 98 | +
|
| 99 | + """ |
| 100 | + from steamwebapi.core import SteamWebAPI |
| 101 | + from steamwebapi.util.exceptions import APIKeyInvalidError |
| 102 | + with self.assertRaises(APIKeyInvalidError): |
| 103 | + # Add a "." (non-alphanumeric character) |
| 104 | + self.steamwebapi = SteamWebAPI('XXXX0000XXXX0000XXXX.000XXXX0000') |
| 105 | + |
| 106 | + def test_api_key_empty(self): |
| 107 | + """Tests initializing SteamWebAPI with an empty string as the Steam Web |
| 108 | + API Key. Ensures that the ./steamwebapi/core.API_KEY_RE does not raise |
| 109 | + the APIKeyInvalidError. |
| 110 | +
|
| 111 | + """ |
| 112 | + from steamwebapi.core import SteamWebAPI |
| 113 | + self.steamwebapi = SteamWebAPI(key='') |
| 114 | + self.assertEqual( |
| 115 | + self.steamwebapi.key, |
| 116 | + '', |
| 117 | + 'The SteamWebAPI().key was initialized as an empty string, but ' + |
| 118 | + 'not allowed.', |
| 119 | + ) |
| 120 | + |
| 121 | + # ------------------------------------------------------------------------- |
| 122 | + # Default Language Tests |
| 123 | + # ------------------------------------------------------------------------- |
| 124 | + def test_default_language_equalto_settings_default_language(self): |
| 125 | + """Tests that initializing SteamWebAPI without arguments retrieves |
| 126 | + the DEFAULT_LANGUAGE from ./steamwebapi/settings.py. |
| 127 | +
|
| 128 | + """ |
| 129 | + from steamwebapi.settings import DEFAULT_LANGUAGE |
| 130 | + self.assertEqual( |
| 131 | + self.steamwebapi.language, |
| 132 | + DEFAULT_LANGUAGE, |
| 133 | + 'The settings.DEFAULT_LANGUAGE was not initialized as the ' + |
| 134 | + 'default argument for SteamWebAPI().language.', |
| 135 | + ) |
| 136 | + |
| 137 | + def test_default_language_not_equalto_settings_default_language(self): |
| 138 | + """Tests that changing the default language from within the SteamWebAPI |
| 139 | + instance works, as well as ensure that it differs from the |
| 140 | + DEFAULT_LANGUAGE from ./steamwebapi/settings.py. |
| 141 | +
|
| 142 | + """ |
| 143 | + from steamwebapi.settings import DEFAULT_LANGUAGE |
| 144 | + self.steamwebapi.language = 'not_a_real_language' |
| 145 | + self.assertNotEqual( |
| 146 | + self.steamwebapi.language, |
| 147 | + DEFAULT_LANGUAGE, |
| 148 | + 'The SteamWebAPI().language instance is the same as ' + |
| 149 | + 'settings.DEFAULT_LANGUAGE is the same although it should have ' + |
| 150 | + 'changed.', |
| 151 | + ) |
| 152 | + |
| 153 | + def test_default_language_initialized_from__init__with_keyword_arg(self): |
| 154 | + """Tests that initializing SteamWebAPI with the keyword argument |
| 155 | + 'language' creates a SteamWebAPI().language instance attribute which |
| 156 | + equals the keyword argument. This prevents regression of changing the |
| 157 | + name of the argument. |
| 158 | +
|
| 159 | + """ |
| 160 | + from steamwebapi.core import SteamWebAPI |
| 161 | + # Set a fake (bad) Steam Web API Key |
| 162 | + self.steamwebapi = SteamWebAPI(language='not_a_real_language') |
| 163 | + self.assertEqual( |
| 164 | + self.steamwebapi.language, |
| 165 | + 'not_a_real_language', |
| 166 | + 'The SteamWebAPI().language is not equal to the keyword argument.', |
| 167 | + ) |
| 168 | + |
| 169 | + def test_default_language_initialized_from__init__no_keyword_arg(self): |
| 170 | + """Tests that initializing SteamWebAPI without the keyword argument |
| 171 | + 'language' creates a SteamWebAPI().language instance attribute which |
| 172 | + equals the keyword argument. This ensures that we do not alter the |
| 173 | + argument order causing a regression for users that do not provide |
| 174 | + keyword arguments. |
| 175 | +
|
| 176 | + """ |
| 177 | + from steamwebapi.core import SteamWebAPI |
| 178 | + # Set a fake (bad) Steam Web API Key |
| 179 | + self.steamwebapi = SteamWebAPI( |
| 180 | + 'XXXX0000XXXX0000XXXX0000XXXX0000', |
| 181 | + 'not_a_real_language', |
| 182 | + ) |
| 183 | + self.assertEqual( |
| 184 | + self.steamwebapi.language, |
| 185 | + 'not_a_real_language', |
| 186 | + 'The language was not set when provided as the second argument ' + |
| 187 | + 'without being a keyword argument.', |
| 188 | + ) |
| 189 | + |
| 190 | + |
| 191 | +# ============================================================================= |
| 192 | +# >> TEST FUNCTIONS |
| 193 | +# ============================================================================= |
| 194 | +if __name__ == '__main__': |
| 195 | + main() |
0 commit comments