Skip to content

Commit

Permalink
Add tests for optional auditor vars
Browse files Browse the repository at this point in the history
  • Loading branch information
avanwinkle committed Feb 22, 2024
1 parent e794298 commit 9822ecf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions mpf/plugins/auditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ def audit_player(self, **kwargs):

for item in set(self.config['player']):
for player in self.machine.game.player_list:
# Don't audit empty values, except score
if item != "score" and not self.machine.game.player[item]:
# Don't audit values that haven't been initialized on the player, either by
# a value set during gameplay or with an initial_value in the player_vars config
if not item in self.machine.game.player.vars:
continue

self.current_audits['player'][item]['top'] = (
Expand Down
7 changes: 7 additions & 0 deletions mpf/tests/machine_files/auditor/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
game:
balls_per_game: 1

player_vars:
score:
initial_value: 0
my_var:
initial_value: 0

auditor:
events:
- test_event1
- test_event2
player:
- my_var
- optional_var

modes:
- base
Expand Down
2 changes: 2 additions & 0 deletions mpf/tests/machine_files/auditor/modes/base/config/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ variable_player:
not_audited: 100
add_score_odd:
score: 123
add_optional_var:
optional_var: 20
6 changes: 5 additions & 1 deletion mpf/tests/test_Auditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ def test_auditor_player_vars(self):
self.assertEqual(200, auditor.current_audits['player']['my_var']['average'])
self.assertEqual([200], auditor.current_audits['player']['my_var']['top'])
self.assertEqual(1, auditor.current_audits['player']['my_var']['total'])
self.assertEqual(0, auditor.current_audits['player']['optional_var']['total'])
self.assertNotIn("not_audited", auditor.current_audits['player'])

# start a game
self.start_game()

self.post_event("add_score")
self.post_event("add_score")
self.post_event("add_optional_var")
self.assertPlayerVarEqual(200, "score")
self.assertPlayerVarEqual(20, "optional_var")

self.drain_all_balls()
self.assertGameIsNotRunning()
Expand All @@ -61,7 +64,8 @@ def test_auditor_player_vars(self):
self.assertNotIn("not_audited", auditor.current_audits['player'])

self.assertEqual({'score': {'top': [200, 100], 'average': 150.0, 'total': 2},
'my_var': {'top': [200, 0], 'average': 100.0, 'total': 2}},
'my_var': {'top': [200, 0], 'average': 100.0, 'total': 2},
'optional_var': {'top': [20], 'average': 20.0, 'total': 1}},
auditor.data_manager.written_data["player"])

# test rounding the average to an integer
Expand Down

0 comments on commit 9822ecf

Please sign in to comment.