Skip to content

Commit

Permalink
Test cached properties and add str/repr methods
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
thinkwelltwd committed Apr 4, 2020
1 parent b07f6b5 commit b5b1c58
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
10 changes: 7 additions & 3 deletions device_detector/device_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ def is_known(self) -> bool:
return False

def is_bot(self) -> bool:
if not self.bot:
return False
return self.bot.is_known()
return bool(self.all_details.get('bot'))

def android_device_type(self) -> str:

Expand Down Expand Up @@ -562,6 +560,12 @@ def pretty_print(self) -> str:
)
return 'Client: {} Device: {} OS: {}'.format(client, device, os).strip()

def __str__(self):
return self.user_agent

def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self.user_agent)


class SoftwareDetector(DeviceDetector):

Expand Down
3 changes: 3 additions & 0 deletions device_detector/parser/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ def brand_short_name(self) -> str:
def model(self) -> str:
return self.ua_data.get('model', '')

def __str__(self):
return '%s %s' % (self.model(), self.dtype())


__all__ = [
'Device',
Expand Down
11 changes: 11 additions & 0 deletions device_detector/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ def is_known(self) -> bool:
return True
return False

def __str__(self):
return self.name()

def __repr__(self):
return '%s(%s, %s, %s)' % (
self.__class__.__name__,
self.user_agent,
self.ua_hash,
self.ua_spaceless,
)


__all__ = [
'Parser',
Expand Down
60 changes: 60 additions & 0 deletions device_detector/tests/parser/test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from ..base import ParserBaseTest
from ...device_detector import DeviceDetector


class TestCache(ParserBaseTest):
"""
Values should remain the same on multiple runs when
the parsing on followup runs pulls from cached values.
"""

def test_is_bot(self):
ua = "Mozilla/5.0 (compatible; CloudFlare-AlwaysOnline/1.0; +http://www.cloudflare.com/always-online) AppleWebKit/534.34"

first_run = DeviceDetector(ua).parse()
self.assertTrue(first_run.is_bot())

second_run = DeviceDetector(ua).parse()
self.assertTrue(second_run.is_bot())

def test_device(self):
ua = "Mozilla/5.0 (Linux; U; Android 2.3.3; ja-jp; COOLPIX S800c Build/CP01_WW) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"

first_run = DeviceDetector(ua).parse()
self.assertEqual(first_run.device_type(), 'camera')
self.assertEqual(first_run.device_brand(), 'NN')
self.assertEqual(first_run.device_brand_name(), 'Nikon')

second_run = DeviceDetector(ua).parse()
self.assertEqual(second_run.device_type(), 'camera')
self.assertEqual(second_run.device_brand(), 'NN')
self.assertEqual(second_run.device_brand_name(), 'Nikon')

def test_client(self):
ua = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36"

first_run = DeviceDetector(ua).parse()
self.assertEqual(first_run.client_name(), 'Chrome')
self.assertEqual(first_run.client_short_name(), 'CH')
self.assertEqual(first_run.client_version(), '80.0.3987.162')

second_run = DeviceDetector(ua).parse()
self.assertEqual(second_run.client_name(), 'Chrome')
self.assertEqual(second_run.client_short_name(), 'CH')
self.assertEqual(second_run.client_version(), '80.0.3987.162')

def test_os(self):
ua = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"

first_run = DeviceDetector(ua).parse()
self.assertEqual(first_run.os_name(), 'Ubuntu')
self.assertEqual(first_run.os_short_name(), 'UBT')

second_run = DeviceDetector(ua).parse()
self.assertEqual(second_run.os_name(), 'Ubuntu')
self.assertEqual(second_run.os_short_name(), 'UBT')


__all__ = [
'TestCache',
]

0 comments on commit b5b1c58

Please sign in to comment.