From 56f3666cedb9c2fc6b6f9a704e8b74e34b3808e3 Mon Sep 17 00:00:00 2001 From: hugovk Date: Sat, 1 Mar 2014 12:36:35 +0200 Subject: [PATCH] If no country, return None rather than Country class. Fixes http://stackoverflow.com/q/14609467/724176 --- pylast.py | 9 +++++++-- test_pylast.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pylast.py b/pylast.py index edf8184..ef1a89d 100644 --- a/pylast.py +++ b/pylast.py @@ -404,7 +404,7 @@ class _Network(object): def is_caching_enabled(self): """Returns True if caching is enabled.""" - return not (self.cache_backend == None) + return not (self.cache_backend is None) def _get_cache_backend(self): @@ -3052,7 +3052,12 @@ class User(_BaseObject): doc = self._request("user.getInfo", True) - return Country(_extract(doc, "country"), self.network) + country = _extract(doc, "country") + + if country is None: + return None + else: + return Country(country, self.network) def get_age(self): """Returns the user's age.""" diff --git a/test_pylast.py b/test_pylast.py index dc2a562..fb30c7c 100644 --- a/test_pylast.py +++ b/test_pylast.py @@ -108,6 +108,26 @@ class TestSequenceFunctions(unittest.TestCase): # Just check date because of timezones self.assertEquals(unixtime_registered, u"1037793040") + def test_get_genderless_user(self): + # Arrange + lastfm_user = self.network.get_user("test_user") # currently no gender set + + # Act + gender = lastfm_user.get_gender() + + # Assert + self.assertIsNone(gender) + + + def test_get_countryless_user(self): + # Arrange + lastfm_user = self.network.get_user("test_user") # currently no country set + + # Act + country = lastfm_user.get_country() + + # Assert + self.assertIsNone(country) if __name__ == '__main__': unittest.main()