Skip to content

Commit

Permalink
additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Jan 11, 2024
1 parent a0c9015 commit 655d8d5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# MediaWiki Changelog

## Version 0.7.5

* Move configuration items to a configuration data class
* Will allow for the deprication of some top level properties in lieu of changing against the `Configuration` class

## Version 0.7.4

* Add typing support
Expand Down
7 changes: 1 addition & 6 deletions mediawiki/configuraton.py
Expand Up @@ -29,7 +29,6 @@ class Configuration:
_reset_session: bool = field(default=True, init=False, repr=False)
_clear_memoized: bool = field(default=False, init=False, repr=False)
_rate_limit_last_call: Optional[datetime] = field(default=None, init=False, repr=False)
_login: bool = field(default=False, init=False, repr=False)

def __init__(
self,
Expand Down Expand Up @@ -91,7 +90,7 @@ def __repr__(self):
keys = [
x.replace("_", "", 1)
for x in sorted(asdict(self).keys())
if x not in ["_login", "_rate_limit_last_call", "_clear_memoized", "_reset_session"]
if x not in ["_rate_limit_last_call", "_clear_memoized", "_reset_session"]
]
full = [f"{x}={self.__getattribute__(x)}" for x in keys]
return f"Configuration({', '.join(full)})"
Expand Down Expand Up @@ -224,8 +223,6 @@ def username(self) -> Optional[str]:
def username(self, username: Optional[str]):
"""set the username, if needed, to log into the mediawiki site"""
self._username = username
if self.username and self.password:
self._login = True

@property
def password(self) -> Optional[str]:
Expand All @@ -236,8 +233,6 @@ def password(self) -> Optional[str]:
def password(self, password: Optional[str]):
"""set the password, if needed, to log into the mediawiki site"""
self._password = password
if self.username and self.password:
self._login = True

@property
def refresh_interval(self) -> Optional[int]:
Expand Down
1 change: 1 addition & 0 deletions mediawiki/mediawiki.py
Expand Up @@ -335,6 +335,7 @@ def login(self, username: str, password: str, strict: bool = True) -> bool:
res = self._post_response(params)
if res["login"]["result"] == "Success":
self._is_logged_in = True
self._config._login = False
return True
self._is_logged_in = False
reason = res["login"]["reason"]
Expand Down
28 changes: 28 additions & 0 deletions tests/mediawiki_test.py
Expand Up @@ -37,6 +37,10 @@ def __init__(
rate_limit_wait=timedelta(milliseconds=50),
cat_prefix="Category",
user_agent=None,
username=None,
password=None,
proxies=None,
verify_ssl=True,
):
"""new init"""

Expand All @@ -55,8 +59,15 @@ def __init__(
rate_limit_wait=rate_limit_wait,
cat_prefix=cat_prefix,
user_agent=user_agent,
username=username,
password=password,
proxies=proxies,
verify_ssl=verify_ssl,
)

def __repr__(self):
return super().__repr__()

def _get_response(self, params):
"""override the __get_response method"""
new_params = json.dumps(tuple(sorted(params.items())))
Expand Down Expand Up @@ -151,6 +162,18 @@ def test_extensions(self):
response = site.responses[site.api_url]
self.assertEqual(site.extensions, response["extensions"])

def test_repr_function(self):
"""test the config repr function"""
site = MediaWikiOverloaded()
res = (
"Configuration(api_url=https://en.wikipedia.org/w/api.php, category_prefix=Category, "
"lang=en, password=None, proxies=None, rate_limit=False, rate_limit_min_wait=0:00:00.050000, "
"refresh_interval=None, timeout=15.0, use_cache=True, "
"user_agent=python-mediawiki/VERSION-0.7.4/(https://github.com/barrust/mediawiki)/BOT, username=None, verify_ssl=True)"
)
print(str(site._config))
self.assertEqual(str(site._config), res)

def test_change_api_url(self):
"""test switching the api url"""
site = MediaWikiOverloaded()
Expand Down Expand Up @@ -347,6 +370,11 @@ def test_successful_login(self):
self.assertEqual(site.logged_in, True)
self.assertEqual(res, True)

def test_successful_login_on_load(self):
"""test login success on load!"""
site = MediaWikiOverloaded(username="username", password="fakepassword")
self.assertEqual(site.logged_in, True)

def test_failed_login(self):
"""test that login failure throws the correct exception"""
site = MediaWikiOverloaded()
Expand Down

0 comments on commit 655d8d5

Please sign in to comment.