Skip to content

Commit 24118ff

Browse files
infinitewarpJ-e-k
authored andcommitted
add some basic tests! (jekirl#332)
1 parent 235462f commit 24118ff

File tree

5 files changed

+83
-10
lines changed

5 files changed

+83
-10
lines changed

pgoapi/game_master.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import csv
22
import re
3+
from os import path
34

45
from six import iteritems
56

67

7-
class PokemonData:
8+
class PokemonData(object):
89
def __init__(self):
910
self.PkMn = 0
1011
self.BaseStamina = 0
@@ -37,7 +38,8 @@ def __init__(self):
3738

3839
GAME_MASTER = {}
3940

40-
with open("GAME_MASTER_POKEMON_v0_2.tsv") as tsvfile:
41+
_game_master_file_path = path.join(path.dirname(path.dirname(__file__)), "GAME_MASTER_POKEMON_v0_2.tsv")
42+
with open(_game_master_file_path) as tsvfile:
4143
tsvreader = csv.DictReader(tsvfile, delimiter='\t')
4244
attributes = []
4345
for row in tsvreader:

pgoapi/pokemon.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
import json
44
from math import sqrt
5+
from os import path
56

67
from pgoapi.game_master import GAME_MASTER
78

89

9-
# TODO wrap this with some error handling?
10-
POKEMON_NAMES = json.load(open("pokemon.en.json"))
10+
POKEMON_NAMES = {}
1111

12+
_names_file_path = path.join(path.dirname(path.dirname(__file__)), "pokemon.en.json")
13+
with open(_names_file_path) as jsonfile:
14+
POKEMON_NAMES.update(json.load(jsonfile))
1215

13-
class Pokemon:
16+
17+
class Pokemon(object):
1418
# Used for calculating the pokemon level
1519
# source http://pokemongo.gamepress.gg/cp-multiplier
1620
CPM_calculation_increments = [
@@ -66,13 +70,13 @@ def __init__(self, pokemon_data, player_level=0,
6670
self.cpm_total = self.cp_multiplier + self.additional_cp_multiplier
6771
self.level_wild = self.get_level_by_cpm(self.cp_multiplier)
6872
self.level = self.get_level_by_cpm(self.cpm_total)
69-
additional_data = GAME_MASTER[self.pokemon_id]
70-
self.family_id = additional_data.FamilyId
73+
additional_data = GAME_MASTER.get(self.pokemon_id)
74+
self.family_id = additional_data.FamilyId if additional_data else None
7175

7276
# Thanks to http://pokemongo.gamepress.gg/pokemon-stats-advanced for the magical formulas
73-
attack = float(additional_data.BaseAttack)
74-
defense = float(additional_data.BaseDefense)
75-
stamina = float(additional_data.BaseStamina)
77+
attack = float(additional_data.BaseAttack) if additional_data else 0.0
78+
defense = float(additional_data.BaseDefense) if additional_data else 0.0
79+
stamina = float(additional_data.BaseStamina) if additional_data else 0.0
7680
self.max_cp = ((attack + self.individual_attack) *
7781
sqrt(defense + self.individual_defense) *
7882
sqrt(stamina + self.individual_stamina) *

tests/__init__.py

Whitespace-only changes.

tests/test_api.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import unittest
2+
3+
from pgoapi.pokemon import Pokemon
4+
5+
6+
mock_caught_eevee = {
7+
'move_1': 221,
8+
'move_2': 26,
9+
'captured_cell_id': 9970561120513919632,
10+
'pokeball': 1,
11+
'pokemon_id': 133,
12+
'creation_time_ms': 1469596340021,
13+
'height_m': 0.2028486435644989,
14+
'stamina_max': 69,
15+
'weight_kg': 1.812894354329834,
16+
'individual_defense': 3,
17+
'cp_multiplier': 0.5822789072940417,
18+
'stamina': 69,
19+
'individual_stamina': 9,
20+
'individual_attack': 15,
21+
'cp': 546,
22+
'id': 10537581451037376812
23+
}
24+
25+
mock_wild_spearow = {
26+
'move_1': 211,
27+
'move_2': 45,
28+
'pokemon_id': 21,
29+
'height_m': 0.2692510320415297,
30+
'stamina_max': 27,
31+
'weight_kg': 1.8720885511213213,
32+
'individual_defense': 5,
33+
'cp_multiplier': 0.3210875988206512,
34+
'stamina': 27,
35+
'individual_stamina': 7,
36+
'individual_attack': 9,
37+
'cp': 97
38+
}
39+
40+
41+
class TestPokemon(unittest.TestCase):
42+
43+
def test_wild_spearow_data(self):
44+
# Type: Spearow CP: 97, IV: 46.67, Lvl: 6.0, LvlWild: 6.0, MaxCP: 17, Score: 97, IV-Norm.: 47
45+
p = Pokemon(mock_wild_spearow, 0, 'CP', {})
46+
self.assertEqual(p.pokemon_type.decode('utf-8'), 'Spearow')
47+
self.assertEqual(p.cp, 97)
48+
self.assertEqual(round(p.iv, 2), 46.67)
49+
self.assertEqual(p.level, 6.0)
50+
self.assertEqual(p.level_wild, 6.0)
51+
self.assertEqual(round(p.max_cp), 17)
52+
self.assertEqual(p.score, 97)
53+
self.assertEqual(round(p.iv_normalized), 47)
54+
55+
def test_caught_eevee_data(self):
56+
# Type: Eevee CP: 546, IV: 60.00, Lvl: 19.0, LvlWild: 19.0, MaxCP: 675, Score: 546, IV-Norm.: 69
57+
p = Pokemon(mock_caught_eevee, 22, 'CP', {})
58+
self.assertEqual(p.pokemon_type.decode('utf-8'), 'Eevee')
59+
self.assertEqual(p.cp, 546)
60+
self.assertEqual(round(p.iv, 2), 60.00)
61+
self.assertEqual(p.level, 19.0)
62+
self.assertEqual(p.level_wild, 19.0)
63+
self.assertEqual(round(p.max_cp), 675)
64+
self.assertEqual(p.score, 546)
65+
self.assertEqual(round(p.iv_normalized), 69)

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ envlist = py27, py35
66
commands =
77
flake8 {toxinidir}
88
isort -c -rc {toxinidir} -sg {toxworkdir}/*
9+
nosetests
910
deps =
1011
flake8
1112
isort
13+
nose
1214
-r{toxinidir}/requirements.txt
1315

1416
[flake8]

0 commit comments

Comments
 (0)