Skip to content

Commit

Permalink
use unittest.mock instead of mock. cs
Browse files Browse the repository at this point in the history
  • Loading branch information
jabdoa2 committed May 9, 2016
1 parent e696639 commit b7b1252
Show file tree
Hide file tree
Showing 40 changed files with 98 additions and 163 deletions.
4 changes: 2 additions & 2 deletions mpf/tests/test_Autofire.py
@@ -1,4 +1,4 @@
from mock import MagicMock
from unittest.mock import MagicMock
from mpf.tests.MpfTestCase import MpfTestCase


Expand Down Expand Up @@ -61,4 +61,4 @@ def test_hw_rule_pulse_inverted_autofire(self):
self.machine.autofires.ac_test_inverted2.disable()

self.machine.default_platform.clear_hw_rule.assert_called_once_with(
self.machine.autofires.ac_test_inverted2.switch, self.machine.autofires.ac_test_inverted2.coil)
self.machine.autofires.ac_test_inverted2.switch, self.machine.autofires.ac_test_inverted2.coil)
2 changes: 1 addition & 1 deletion mpf/tests/test_BallController.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallController(MpfTestCase):
Expand Down
4 changes: 2 additions & 2 deletions mpf/tests/test_BallDevice.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallDevice(MpfTestCase):
Expand Down Expand Up @@ -1663,7 +1663,7 @@ def test_ball_missing_to_pf_and_drain_no_switches(self):
# no more balls on pf
self.assertEqual(0, playfield.balls)
# eject is not failed yet
#self.assertEqual(0, playfield.available_balls)
# self.assertEqual(0, playfield.available_balls)

self.advance_time_and_run(30)

Expand Down
3 changes: 2 additions & 1 deletion mpf/tests/test_BallDeviceAutoManualPlunger.py
@@ -1,5 +1,6 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallDeviceAutoManualPlunger(MpfTestCase):

Expand Down
2 changes: 1 addition & 1 deletion mpf/tests/test_BallDeviceHoldCoil.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallDevicesHoldCoil(MpfTestCase):
Expand Down
3 changes: 1 addition & 2 deletions mpf/tests/test_BallDeviceJamSwitch.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallDeviceJamSwitch(MpfTestCase):
Expand Down Expand Up @@ -648,4 +648,3 @@ def test_device_starts_with_active_jam_switch(self):
self.assertEqual(self.machine.ball_devices.playfield.balls, 1)
self.assertEqual(4, self.machine.ball_controller.num_balls_known)
self.assertEqual(0, self._captured)

2 changes: 1 addition & 1 deletion mpf/tests/test_BallDeviceManualWithTarget.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallDeviceManualWithTarget(MpfTestCase):
Expand Down
3 changes: 1 addition & 2 deletions mpf/tests/test_BallDeviceNoPlungerSwitch.py
@@ -1,6 +1,6 @@

from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallDeviceNoPlungerSwitch(MpfTestCase):
Expand Down Expand Up @@ -51,4 +51,3 @@ def test_add_ball_to_pf(self):

self.assertEqual(self.machine.ball_devices.trough.balls, 1)
self.assertEqual(self.machine.ball_devices.playfield.balls, 0)

11 changes: 2 additions & 9 deletions mpf/tests/test_BallDeviceRouting.py
@@ -1,9 +1,5 @@
import unittest

from mpf.core.machine import MachineController
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
import time
from unittest.mock import MagicMock


class TestBallDeviceRouting(MpfTestCase):
Expand All @@ -22,19 +18,16 @@ def _missing_ball(self):
self._missing += 1

def _captured_from_pf(self, balls, **kwargs):
del kwargs
self._captured += balls

def test_routing_to_pf_on_capture(self):
c_trough1 = self.machine.coils['c_trough1']
c_trough2 = self.machine.coils['c_trough2']
c_target1 = self.machine.coils['c_target1']
c_launcher = self.machine.coils['c_launcher']
c_launcher.pulse = MagicMock()
trough1 = self.machine.ball_devices['test_trough1']
trough2 = self.machine.ball_devices['test_trough2']
launcher = self.machine.ball_devices['test_launcher']
target1 = self.machine.ball_devices['test_target1']
playfield = self.machine.ball_devices['playfield']

self.machine.events.add_handler('balldevice_1_ball_missing',
self._missing_ball)
Expand Down
2 changes: 1 addition & 1 deletion mpf/tests/test_BallDeviceSwitchConfirmation.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallDeviceSwitchConfirmation(MpfTestCase):
Expand Down
10 changes: 5 additions & 5 deletions mpf/tests/test_BallDeviceTriggerEvents.py
@@ -1,9 +1,5 @@
import unittest

from mpf.core.machine import MachineController
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
import time
from unittest.mock import MagicMock


class TestBallDeviceTriggerEvents(MpfTestCase):
Expand All @@ -25,12 +21,16 @@ def _missing_ball(self):
self._missing += 1

def _requesting_ball(self, balls, **kwargs):
del kwargs
self._requesting += balls

def _ball_enter(self, new_balls, unclaimed_balls, **kwargs):
del unclaimed_balls
del kwargs
self._enter += new_balls

def _captured_from_pf(self, balls, **kwargs):
del kwargs
self._captured += balls

def test_manual_successful_eject_to_pf(self):
Expand Down
2 changes: 1 addition & 1 deletion mpf/tests/test_BallLock.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallLock(MpfTestCase):
Expand Down
2 changes: 1 addition & 1 deletion mpf/tests/test_BallSearch.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestBallSearch(MpfTestCase):
Expand Down
3 changes: 1 addition & 2 deletions mpf/tests/test_CoilPlayer.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestCoilPlayer(MpfTestCase):
Expand Down Expand Up @@ -71,7 +71,6 @@ def test_enable(self):
coil.enable = MagicMock()
coil.pulse = MagicMock()


self.machine.events.post('event4')
self.advance_time_and_run()

Expand Down
4 changes: 1 addition & 3 deletions mpf/tests/test_Delay.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestDelay(MpfTestCase):
Expand Down Expand Up @@ -44,5 +44,3 @@ def testBasicFunctions(self):
# Advance another 0.5 sec (callback should not be called since it was cancelled)
self.advance_time_and_run(0.5)
self.callback.assert_not_called()


2 changes: 1 addition & 1 deletion mpf/tests/test_DeviceDriver.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestDeviceDriver(MpfTestCase):
Expand Down
2 changes: 1 addition & 1 deletion mpf/tests/test_DeviceFlasher.py
@@ -1,6 +1,6 @@
from mpf.devices.driver import ConfiguredHwDriver
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestDeviceDriver(MpfTestCase):
Expand Down
2 changes: 0 additions & 2 deletions mpf/tests/test_DeviceGI.py
@@ -1,6 +1,4 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
import time


class TestDeviceGI(MpfTestCase):
Expand Down
4 changes: 2 additions & 2 deletions mpf/tests/test_Diverter.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestDiverter(MpfTestCase):
Expand Down Expand Up @@ -360,4 +360,4 @@ def test_activation_switch_and_eject_confirm_switch(self):
self.assertFalse(diverter.active)

self.advance_time_and_run(4)
assert not self.machine.coils.c_diverter.enable.called
assert not self.machine.coils.c_diverter.enable.called
2 changes: 1 addition & 1 deletion mpf/tests/test_Fadecandy.py
@@ -1,4 +1,4 @@
from mock import MagicMock
from unittest.mock import MagicMock
import json

from mpf.core.rgb_color import RGBColor
Expand Down
11 changes: 4 additions & 7 deletions mpf/tests/test_Flippers.py
@@ -1,11 +1,8 @@
import unittest

from mpf.core.machine import MachineController
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock, call
import time
from unittest.mock import MagicMock, call


class TestTilt(MpfTestCase):
class TestFlippers(MpfTestCase):

def getConfigFile(self):
return 'config.yaml'
Expand Down Expand Up @@ -86,4 +83,4 @@ def test_sw_flip_and_release(self):
self.machine.coils.c_flipper_main.enable.assert_called_once_with()

self.machine.flippers.f_test_hold_eos.sw_release()
self.machine.coils.c_flipper_main.disable.assert_called_once_with()
self.machine.coils.c_flipper_main.disable.assert_called_once_with()
2 changes: 1 addition & 1 deletion mpf/tests/test_GottliebTrough.py
@@ -1,5 +1,5 @@
from mpf.tests.MpfTestCase import MpfTestCase
from mock import MagicMock
from unittest.mock import MagicMock


class TestGottliebTrough(MpfTestCase):
Expand Down
7 changes: 1 addition & 6 deletions mpf/tests/test_HighScoreMode.py
@@ -1,8 +1,7 @@
from collections import OrderedDict

from mock import MagicMock
from unittest.mock import MagicMock
from mpf.tests.MpfTestCase import MpfTestCase
from mpf.core.data_manager import DataManager


class TestHighScoreMode(MpfTestCase):
Expand All @@ -12,10 +11,6 @@ def __init__(self, methodName):
# remove config patch which disables bcp
del self.machine_config_patches['bcp']

# path DataManager so it doesn't actually write anything to disk
DataManager._make_sure_path_exists = MagicMock()
DataManager.save_all = MagicMock()

def getConfigFile(self):
return 'high_score.yaml'

Expand Down
12 changes: 5 additions & 7 deletions mpf/tests/test_Modes.py
@@ -1,6 +1,7 @@
from mock import MagicMock
from unittest.mock import MagicMock
from mpf.tests.MpfTestCase import MpfTestCase


class TestModes(MpfTestCase):

def getConfigFile(self):
Expand Down Expand Up @@ -133,12 +134,9 @@ def test_ball_end(self):
# mode1 is set to keep running on ball end
# mode2 should stop and restart on ball end
# mode3 should stop and stay stopped
self.assertFalse(
self.machine.modes.mode1.config['mode']['stop_on_ball_end'])
self.assertTrue(
self.machine.modes.mode2.config['mode']['stop_on_ball_end'])
self.assertTrue(
self.machine.modes.mode3.config['mode']['stop_on_ball_end'])
self.assertFalse(self.machine.modes.mode1.config['mode']['stop_on_ball_end'])
self.assertTrue(self.machine.modes.mode2.config['mode']['stop_on_ball_end'])
self.assertTrue(self.machine.modes.mode3.config['mode']['stop_on_ball_end'])

# start a game
self.machine.playfield.add_ball = MagicMock()
Expand Down
5 changes: 1 addition & 4 deletions mpf/tests/test_ModesConfigValidation.py
@@ -1,6 +1,3 @@
from mock import MagicMock

import mpf.core.config_validator
from mpf.tests.MpfTestCase import MpfTestCase


Expand All @@ -15,7 +12,7 @@ def getMachinePath(self):
def setUp(self):

self.add_to_config_validator('unrelated_section',
dict(__valid_in__= 'mode'))
dict(__valid_in__ = 'mode'))

self.save_and_prepare_sys_path()

Expand Down
33 changes: 14 additions & 19 deletions mpf/tests/test_MpfTestCase.py
@@ -1,9 +1,7 @@
from mock import MagicMock
from mpf.devices.switch import Switch
from mpf.core.device_manager import DeviceCollection

from unittest.mock import MagicMock
from mpf.tests.MpfTestCase import MpfTestCase


class TestMpfTestCase(MpfTestCase):

def getConfigFile(self):
Expand All @@ -30,8 +28,7 @@ def test_test_case(self):
# test the timed switch event
self.assertIn('switch1', self.machine.switches)
self.switch_callback = MagicMock()
self.machine.switch_controller.add_switch_handler('switch1',
self.switch_callback, ms=1000)
self.machine.switch_controller.add_switch_handler('switch1', self.switch_callback, ms=1000)

self.machine.switch_controller.process_switch('switch1')
self.advance_time_and_run(.5)
Expand All @@ -46,26 +43,24 @@ def test_multiples(self):

self.machine.default_platform.delay.add(ms=500,
callback=self._callback,
id='d1')
_id='d1')
self.machine.default_platform.delay.add(ms=1500,
callback=self._callback,
id='d2')
_id='d2')
self.machine.default_platform.delay.add(ms=2500,
callback=self._callback,
id='d3')
_id='d3')

self.machine.switch_controller.add_switch_handler('switch1',
self._callback, ms=1000, callback_kwargs={'id': 's1'})
self.machine.switch_controller.add_switch_handler('switch1',
self._callback, ms=2000, callback_kwargs={'id': 's2'})
self.machine.switch_controller.add_switch_handler('switch1',
self._callback, ms=3000, callback_kwargs={'id': 's3'})
self.machine.switch_controller.add_switch_handler(
'switch1', self._callback, ms=1000, callback_kwargs={'_id': 's1'})
self.machine.switch_controller.add_switch_handler(
'switch1', self._callback, ms=2000, callback_kwargs={'_id': 's2'})
self.machine.switch_controller.add_switch_handler(
'switch1', self._callback, ms=3000, callback_kwargs={'_id': 's3'})

self.machine.switch_controller.process_switch('switch1')
self.advance_time_and_run(10)

#print(self.id_list)

# make sure the callback was called in the right order
self.assertEqual(self.id_list[0][0], 'd1')
self.assertEqual(self.id_list[1][0], 's1')
Expand All @@ -86,6 +81,6 @@ def test_multiples(self):
self.assertAlmostEqual(self.id_list[5][1] - self.id_list[4][1], .5,
delta=0.01)

def _callback(self, id):
def _callback(self, _id):
# print(self.id_list)
self.id_list.append((id, self.machine.clock.get_time()))
self.id_list.append((_id, self.machine.clock.get_time()))

0 comments on commit b7b1252

Please sign in to comment.