Testing Class Material
This commit is contained in:
commit
05b1f6cdd5
85 changed files with 102796 additions and 0 deletions
13
extra_slides/mocking/telescope/telescope_driver.py
Normal file
13
extra_slides/mocking/telescope/telescope_driver.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
|
||||
def connect(address):
|
||||
import time
|
||||
time.sleep(5)
|
||||
return '1'
|
||||
|
||||
def get_angle(address):
|
||||
return 0.0
|
||||
|
||||
def set_angle(address, angle):
|
||||
if angle < 0 or angle > 1.40:
|
||||
raise IOError('Telescope jammed -- please call technical support')
|
||||
return True
|
30
extra_slides/mocking/telescope/telescope_model.py
Normal file
30
extra_slides/mocking/telescope/telescope_model.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import telescope_driver
|
||||
|
||||
|
||||
class TelescopeModel(object):
|
||||
|
||||
# Minimum safe elevation angle (see handbook).
|
||||
MIN_ANGLE = 0.0
|
||||
|
||||
# Maximum safe elevation angle (see handbook).
|
||||
MAX_ANGLE = 80.0
|
||||
|
||||
def __init__(self, address):
|
||||
self.address = address
|
||||
# Connect to telescope
|
||||
self.connection = telescope_driver.connect(address)
|
||||
# Get initial state of telescope.
|
||||
self.current_angle = telescope_driver.get_angle(self.connection)
|
||||
|
||||
def set_elevation_angle(self, angle):
|
||||
""" Set the elevation angle of the telescope (in rad).
|
||||
|
||||
If the angle is outside the range allowed by the manufacturer,
|
||||
raise a ValueError.
|
||||
"""
|
||||
|
||||
if angle < self.MIN_ANGLE or angle > self.MAX_ANGLE:
|
||||
raise ValueError('Unsafe elevation angle: {}'.format(angle))
|
||||
|
||||
telescope_driver.set_angle(self.connection, angle)
|
||||
self.current_angle = angle
|
12
extra_slides/mocking/telescope/test_telescope_model.py
Normal file
12
extra_slides/mocking/telescope/test_telescope_model.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
import numpy as np
|
||||
from py.test import raises
|
||||
|
||||
from telescope_model import TelescopeModel
|
||||
|
||||
|
||||
def test_unsafe_elevation_angle():
|
||||
telescope = TelescopeModel(address='10.2.1.1')
|
||||
elevation_angle = np.pi / 2.0
|
||||
|
||||
with raises(ValueError):
|
||||
telescope.set_elevation_angle(elevation_angle)
|
|
@ -0,0 +1,29 @@
|
|||
from unittest import mock
|
||||
|
||||
import numpy as np
|
||||
from py.test import raises
|
||||
|
||||
from telescope_model import TelescopeModel
|
||||
|
||||
|
||||
def test_unsafe_elevation_angle():
|
||||
with mock.patch('telescope_model.telescope_driver'):
|
||||
telescope = TelescopeModel(address='10.2.1.1')
|
||||
elevation_angle = np.pi / 2.0
|
||||
with raises(ValueError):
|
||||
telescope.set_elevation_angle(elevation_angle)
|
||||
|
||||
|
||||
def test_model_initialization():
|
||||
connection_id = 'bogus_connection'
|
||||
initial_angle = 1.23
|
||||
|
||||
with mock.patch('telescope_model.telescope_driver') as driver:
|
||||
driver.connect.return_value = connection_id
|
||||
driver.get_angle.return_value = initial_angle
|
||||
|
||||
telescope = TelescopeModel(address='10.2.1.1')
|
||||
assert telescope.connection == connection_id
|
||||
assert driver.connect.call_count == 1
|
||||
assert telescope.current_angle == initial_angle
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue