Browse Source

add test suite for SteamID + enabled travis/cover

fix #2
pull/6/head
Rossen Georgiev 10 years ago
parent
commit
2d24284e85
  1. 13
      .travis.yml
  2. 2
      Makefile
  3. 11
      README.rst
  4. 5
      requirements.txt
  5. 81
      steam/steamid.py
  6. 245
      tests/test_steamid.py

13
.travis.yml

@ -0,0 +1,13 @@
language: python
python:
- "2.7"
- "3.2"
- "3.3"
- "3.4"
install:
- make init
- pip install coveralls
script:
- make test
after_success:
- coveralls

2
Makefile

@ -18,7 +18,7 @@ init:
test:
rm -f .coverage steam/*.pyc tests/*.pyc
PYTHONHASHSEED=0 nosetests --verbosity 2 --with-coverage --cover-package=steam
PYTHONHASHSEED=0 nosetests --verbosity 1 --with-coverage --cover-package=steam
pylint:
pylint -r n -f colorized steam || true

11
README.rst

@ -1,4 +1,4 @@
|pypi| |license|
|pypi| |license| |coverage| |master_build|
Module for interacting with various Steam_ features
@ -104,7 +104,6 @@ SteamID
'https://steamcommunity.com/gid/103582791429521412'
.. _Steam: https://store.steampowered.com/
.. _Steam Web API: https://developer.valvesoftware.com/wiki/Steam_Web_API
.. _API Key: http://steamcommunity.com/dev/apikey
@ -117,3 +116,11 @@ SteamID
.. |license| image:: https://img.shields.io/pypi/l/steam.svg?style=flat&label=license
:target: https://pypi.python.org/pypi/steam
:alt: MIT License
.. |coverage| image:: https://img.shields.io/coveralls/ValvePython/steam/master.svg?style=flat
:target: https://coveralls.io/r/ValvePython/steam?branch=master
:alt: Test coverage
.. |master_build| image:: https://img.shields.io/travis/ValvePython/steam/master.svg?style=flat&label=master
:target: http://travis-ci.org/ValvePython/steam
:alt: Build status of master branch

5
requirements.txt

@ -0,0 +1,5 @@
nose
coverage
mock
requests
enum34

81
steam/steamid.py

@ -1,6 +1,6 @@
import re
import requests
from .enums import EType, EUniverse
from steam.enums import EType, EUniverse
class SteamID(object):
@ -44,12 +44,11 @@ class SteamID(object):
"""
largs = len(args)
lkwargs = len(kwargs)
if largs == 0 and lkwargs == 0:
if largs == 0 and len(kwargs) == 0:
self.id = 0
self.type = EType.Invalid
self.universe = EType.Invalid
self.universe = EUniverse.Invalid
self.instance = 0
elif largs > 0:
if largs > 1:
@ -71,15 +70,15 @@ class SteamID(object):
value = match.group('value')
# numeric input
if value.isdigit():
if value.isdigit() or (value.startswith('-') and value[1:].isdigit()):
value = int(value)
if 0 > value:
raise ValueError("Expected positive int, got %d" % value)
if value > 2**64-1:
if value >= 2**64:
raise ValueError("Expected a 32/64 bit int")
# 32 bit account id
if value < 2**32-1:
if value < 2**32:
self.id = value
self.type = EType.Individual
self.universe = EUniverse.Public
@ -94,14 +93,14 @@ class SteamID(object):
# textual input e.g. [g:1:4]
else:
# try steam2
match = re.match(r"^STEAM_(?P<universe>\d+)"
match = re.match(r"^STEAM_(?P<universe>[01])"
r":(?P<reminder>[0-1])"
r":(?P<id>\d+)$", value
)
if match:
self.id = (int(match.group('id')) << 1) | int(match.group('reminder'))
self.universe = EUniverse(int(match.group('universe')))
self.universe = EUniverse(1)
self.type = EType(1)
self.instance = 1
return
@ -134,35 +133,36 @@ class SteamID(object):
" (e.g. [g:1:4], STEAM_0:1:1234), got %s" % repr(value)
)
elif lkwargs > 0:
elif len(kwargs):
if 'id' not in kwargs:
raise ValueError("Expected at least 'id' kwarg")
self.id = int(kwargs['id'])
assert self.id <= 0xffffFFFF, "id larger than 32bits"
value = kwargs.get('type', 0)
if type(value) in (int, EType):
self.type = EType(value)
else:
self.type = EType[value.lower().capitalize()]
for kwname in 'type', 'universe':
if kwname in kwargs:
value = kwargs[kwname]
kwenum = getattr(self, "E%s" % kwname.capitalize())
resolved = getattr(kwenum, value, None)
if resolved is None:
try:
resolved = kwenum(value)
except ValueError:
raise ValueError(
"Invalid value for kwarg '%s', see SteamID.E%s" %
(kwname, kwname.capitalize())
)
setattr(self, kwname, resolved)
if self.type in (EType.Individual, EType.GameServer):
self.instance = 1
value = kwargs.get('universe', 0)
if type(value) in (int, EUniverse):
self.universe = EUniverse(value)
else:
self.instance = 0
self.universe = EUniverse[value.lower().capitalize()]
if 'instance' in kwargs:
self.instance = kwargs['instance']
assert self.instance <= 0xffffF, "instance larger than 20bits"
else:
if self.type in (EType.Individual, EType.GameServer):
self.instance = 1
else:
self.instance = 0
def __repr__(self):
return "%s(id=%s, type=%s, universe=%s, instance=%s)" % (
return "<%s(id=%s, type=%s, universe=%s, instance=%s)>" % (
self.__class__.__name__,
self.id,
repr(self.type.name),
@ -175,19 +175,26 @@ class SteamID(object):
@property
def as_steam2(self):
return "STEAM_%s:%s:%s" % (
self.universe.value,
return "STEAM_0:%s:%s" % (
self.id % 2,
self.id >> 1,
)
@property
def as_steam3(self):
return "[%s:%s:%s]" % (
self.ETypeChar[self.type.value],
self.universe.value,
self.id,
)
if self.type is EType.AnonGameServer:
return "[%s:%s:%s:%s]" % (
self.ETypeChar[self.type.value],
self.universe.value,
self.id,
self.instance
)
else:
return "[%s:%s:%s]" % (
self.ETypeChar[self.type.value],
self.universe.value,
self.id,
)
@property
def as_64(self):

245
tests/test_steamid.py

@ -0,0 +1,245 @@
import unittest
import mock
from steam.steamid import SteamID, requests
from steam.enums import EType, EUniverse
class SteamID_initialization(unittest.TestCase):
def compare(self, obj, test_list):
self.assertEqual(obj.id, test_list[0])
self.assertEqual(obj.type, test_list[1])
self.assertEqual(obj.universe, test_list[2])
self.assertEqual(obj.instance, test_list[3])
def test_arg_toomany(self):
with self.assertRaises(ValueError):
SteamID(1, 2)
with self.assertRaises(ValueError):
SteamID(1, 2, 3)
with self.assertRaises(ValueError):
SteamID(1, 2, 3, 4)
######################################################
# 1 ARG
######################################################
@mock.patch.object(requests, "get")
def test_arg_community_url_id(self, mock_requests_get):
ResponseMock = mock.Mock()
ResponseMock.content = 'var asd = {"steamid":"76580280500085312","key":5123}'
mock_requests_get.return_value = ResponseMock
# http
self.compare(SteamID("http://steamcommunity.com/id/testvanity"),
[123456, EType.Individual, EUniverse.Public, 4444]
)
mock_requests_get.assert_called_with("http://steamcommunity.com/id/testvanity")
# https
self.compare(SteamID("https://steamcommunity.com/id/testvanity"),
[123456, EType.Individual, EUniverse.Public, 4444]
)
mock_requests_get.assert_called_with("https://steamcommunity.com/id/testvanity")
# raise
ResponseMock.content = "no steamid json :("
with self.assertRaises(ValueError):
self.compare(SteamID("https://steamcommunity.com/id/testvanity"),
[123456, EType.Individual, EUniverse.Public, 4444]
)
def test_arg_community_url_profiles(self):
# http
self.compare(SteamID("http://steamcommunity.com/profiles/76580280500085312"),
[123456, EType.Individual, EUniverse.Public, 4444]
)
# https
self.compare(SteamID("https://steamcommunity.com/profiles/76580280500085312"),
[123456, EType.Individual, EUniverse.Public, 4444]
)
def test_arg_number_out_of_range(self):
self.assertRaises(ValueError, SteamID, -1)
self.assertRaises(ValueError, SteamID, '-1')
self.assertRaises(ValueError, SteamID, -5555555)
self.assertRaises(ValueError, SteamID, '-5555555')
self.assertRaises(ValueError, SteamID, 2**64)
self.assertRaises(ValueError, SteamID, str(2**64))
self.assertRaises(ValueError, SteamID, 2**128)
self.assertRaises(ValueError, SteamID, str(2**128))
def test_arg_steam32(self):
self.compare(SteamID(1),
[1, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID('1'),
[1, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID(12),
[12, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID('12'),
[12, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID(123),
[123, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID('123'),
[123, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID(12345678),
[12345678, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID('12345678'),
[12345678, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID(0xffffFFFF),
[0xffffFFFF, EType.Individual, EUniverse.Public, 1])
self.compare(SteamID(str(0xffffFFFF)),
[0xffffFFFF, EType.Individual, EUniverse.Public, 1])
def test_arg_steam64(self):
self.compare(SteamID(76580280500085312),
[123456, EType.Individual, EUniverse.Public, 4444]
)
self.compare(SteamID('76580280500085312'),
[123456, EType.Individual, EUniverse.Public, 4444]
)
self.compare(SteamID(103582791429521412),
[4, EType.Clan, EUniverse.Public, 0]
)
self.compare(SteamID('103582791429521412'),
[4, EType.Clan, EUniverse.Public, 0]
)
######################################################
# 1 arg - steam2/steam3 format
######################################################
def test_arg_text_invalid(self):
with self.assertRaises(ValueError):
SteamID("randomtext")
def test_arg_steam2(self):
self.compare(SteamID("STEAM_0:1:1"),
[3, EType.Individual, EUniverse.Public, 1]
)
self.compare(SteamID("STEAM_1:1:1"),
[3, EType.Individual, EUniverse.Public, 1]
)
self.compare(SteamID("STEAM_0:0:4"),
[8, EType.Individual, EUniverse.Public, 1]
)
self.compare(SteamID("STEAM_1:0:4"),
[8, EType.Individual, EUniverse.Public, 1]
)
def test_arg_steam3(self):
self.compare(SteamID("[U:1:1234]"),
[1234, EType.Individual, EUniverse.Public, 1]
)
self.compare(SteamID("[G:1:1234]"),
[1234, EType.GameServer, EUniverse.Public, 1]
)
self.compare(SteamID("[g:1:4]"),
[4, EType.Clan, EUniverse.Public, 0]
)
self.compare(SteamID("[A:1:4]"),
[4, EType.AnonGameServer, EUniverse.Public, 0]
)
self.compare(SteamID("[A:1:1234:567]"),
[1234, EType.AnonGameServer, EUniverse.Public, 567]
)
######################################################
# KWARGS
######################################################
def test_kwarg_id(self):
# id kwarg is required always
with self.assertRaises(ValueError):
SteamID(instance=0)
SteamID(id=None)
self.assertEqual(SteamID(id=555).id, 555)
self.assertEqual(SteamID(id='555').id, 555)
def test_kwarg_type(self):
with self.assertRaises(KeyError):
SteamID(id=5, type="doesn't exist")
with self.assertRaises(ValueError):
SteamID(id=5, type=99999999)
with self.assertRaises(AttributeError):
SteamID(id=5, type=None)
self.assertEqual(SteamID(id=5, type=1).type, EType.Individual)
self.assertEqual(SteamID(id=5, type='Individual').type, EType.Individual)
self.assertEqual(SteamID(id=5, type='iNDIVIDUAL').type, EType.Individual)
def test_kwarg_universe(self):
with self.assertRaises(KeyError):
SteamID(id=5, universe="doesn't exist")
with self.assertRaises(ValueError):
SteamID(id=5, universe=99999999)
with self.assertRaises(AttributeError):
SteamID(id=5, universe=None)
self.assertEqual(SteamID(id=5, universe=1).universe, EUniverse.Public)
self.assertEqual(SteamID(id=5, universe='Public').universe, EUniverse.Public)
self.assertEqual(SteamID(id=5, universe='pUBLIC').universe, EUniverse.Public)
def test_kwarg_instance(self):
self.assertEqual(SteamID(id=5, instance=1234).instance, 1234)
for etype in EType:
self.assertEqual(SteamID(id=5, type=etype).instance,
1 if etype in (EType.Individual, EType.GameServer) else 0)
def test_kwargs_invalid(self):
invalid = [0, EType.Invalid, EUniverse.Invalid, 0]
self.compare(SteamID(), invalid)
self.compare(SteamID(id=0, type=0, universe=0, instance=0), invalid)
self.compare(SteamID(id=0,
type=EType.Invalid,
universe=EUniverse.Invalid,
instance=0,
), invalid)
self.compare(SteamID(id=0,
type='Invalid',
universe='Invalid',
instance=0,
), invalid)
self.compare(SteamID(id=0,
type='iNVALID',
universe='iNVALID',
instance=0,
), invalid)
class SteamID_properties(unittest.TestCase):
def test_repr(self):
# just to cover in coverage
repr(SteamID())
def test_str(self):
self.assertEqual(str(SteamID(76580280500085312)), '76580280500085312')
def test_as_steam2(self):
self.assertEqual(SteamID('STEAM_0:1:4').as_steam2, 'STEAM_0:1:4')
self.assertEqual(SteamID('STEAM_1:1:4').as_steam2, 'STEAM_0:1:4')
def test_as_steam3(self):
self.assertEqual(SteamID('[U:1:1234]').as_steam3, '[U:1:1234]')
self.assertEqual(SteamID('[g:1:4]').as_steam3, '[g:1:4]')
self.assertEqual(SteamID('[A:1:1234:567]').as_steam3, '[A:1:1234:567]')
self.assertEqual(SteamID('[G:1:1234:567]').as_steam3, '[G:1:1234]')
def test_as_32(self):
self.assertEqual(SteamID(76580280500085312).as_32, 123456)
def test_as_64(self):
self.assertEqual(SteamID(76580280500085312).as_64, 76580280500085312)
def test_community_url(self):
# user url
self.assertEqual(SteamID(76580280500085312).community_url,
'https://steamcommunity.com/profiles/76580280500085312'
)
# group url
self.assertEqual(SteamID('[g:1:4]').community_url,
'https://steamcommunity.com/gid/103582791429521412'
)
# else None
self.assertEqual(SteamID('[A:1:4]').community_url,
None
)
Loading…
Cancel
Save