Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion subsystem/intake.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def __init__(self):

self.encoder: CANcoder = CANcoder(config.intake_cancoder_id)


def init(self):
self.horizontal_motor.init()
self.pivot_motor.init()
Expand Down
101 changes: 40 additions & 61 deletions tests/subsystems/test_climber.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,40 @@
# from unittest.mock import MagicMock

# import pytest
# import rev
# from pytest import MonkeyPatch

# import config
# import constants
# from subsystem import Climber
# from toolkit.motors.ctre_motors import TalonFX

# @pytest.fixture()
# def climber() -> Climber:
# # Create a climber, but it has mock
# # classes for its dependencies
# my_climber = Climber()
# my_climber.climber_motor = MagicMock()
# # my_climber.init()
# return my_climber

# def climber_zero(climber: Climber):

# climber.init()
# climber.climber_motor.init.assert_called()

# @pytest.mark.parametrize(
# "test_input",
# [
# (1),
# (2),
# (3),
# (4)
# ]
# )

# def test_set_angle(test_input, climber: Climber):
# climber.set_angle(test_input)
# climber.climber_motor.set_target_position.assert_called_with(test_input * constants.climber_gear_ratio)

# def test_get_angle(climber: Climber):
# climber.get_angle()
# climber.climber_motor.get_sensor_position.assert_called()

# @pytest.mark.parametrize(
# "test_input",
# [
# (1),
# (2),
# (3),
# (4)
# ]
# )

# def test_set_raw_output(test_input, climber: Climber):
# climber.set_raw_output(test_input)
# climber.climber_motor.set_raw_output.assert_called_with(test_input)
# climber.climber_motor.set_raw_output.assert_called_with(test_input)

# def test_get_raw_output(climber: Climber):
# climber.get_raw_output()
# climber.climber_motor.get_applied_output.assert_called()
import pytest
from subsystem import Climber
from unittest.mock import MagicMock

@pytest.fixture
def climber() -> Climber:
climber = Climber()
climber.climber_motor = MagicMock()
return climber

def test_climber_init(climber: Climber):
climber.init()
climber.climber_motor.init.assert_called()
climber.climber_motor.set_sensor_position.assert_called_with(0)

def test_zero(climber: Climber):
climber.zero()
climber.climber_motor.set_sensor_position.assert_called_with(0)
assert climber.zeroed == True

@pytest.mark.parametrize(
"test_input",
[
(0.0),
(0.5),
(1.0),
(-0.5)
]
)
def test_set_raw_output(test_input, climber: Climber):
climber.set_raw_output(test_input)
climber.climber_motor.set_raw_output.assert_called_with(test_input)

def test_get_motor_revolutions(climber: Climber):
assert climber.get_motor_revolutions() == climber.climber_motor.get_sensor_position()

def test_update_table(climber: Climber):
climber.update_table()
climber.climber_motor.get_sensor_position.assert_called()
climber.climber_motor.get_motor_current.assert_called()
104 changes: 82 additions & 22 deletions tests/subsystems/test_intake.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from unittest.mock import MagicMock

import pytest

import math
import config

import constants
from subsystem import Intake


@pytest.fixture
def intake() -> Intake:
intake = Intake()
Expand All @@ -16,21 +15,10 @@ def intake() -> Intake:

return intake


# @pytest.fixture
# def intake() -> Intake:
# intake = Intake()

# intake.horizontal_motor = MagicMock()
# intake.vertical_motor = MagicMock()
# intake.pivot_motor = MagicMock()

# return intake


# # def test_intake_init(intake: Intake):
# # intake.init()

def test_init(intake: Intake):
intake.init()
intake.horizontal_motor.init.assert_called()
intake.pivot_motor.init.assert_called()

def test_roll_in(intake: Intake):
intake.roll_in()
Expand All @@ -39,6 +27,17 @@ def test_roll_in(intake: Intake):
)
assert intake.intake_running is True

def test_intake_algae(intake: Intake):
intake.intake_algae()
intake.horizontal_motor.set_raw_output.assert_called_with(
-config.intake_algae_speed
)
assert intake.intake_running is True

def test_stop(intake: Intake):
intake.stop()
intake.horizontal_motor.set_raw_output.assert_called_with(0)
assert intake.intake_running is False

def test_roll_out(intake: Intake):
intake.roll_out()
Expand All @@ -47,8 +46,69 @@ def test_roll_out(intake: Intake):
)
assert intake.intake_running is True

def test_extake_algae(intake: Intake):
intake.extake_algae()
intake.horizontal_motor.set_raw_output.assert_called_with(
config.extake_algae_speed
)
assert intake.intake_running is True

def test_stop(intake: Intake):
intake.stop()
intake.horizontal_motor.set_raw_output.assert_called_with(0)
assert intake.intake_running is False
def test_get_horizontal_motor_current(intake: Intake):
assert intake.get_horizontal_motor_current() == intake.horizontal_motor.get_motor_current()

def test_get_pivot_motor_current(intake: Intake):
assert intake.get_pivot_motor_current() == intake.pivot_motor.get_motor_current()

# Since limit angle is a simple conditional, we can just test using a few angles

@pytest.mark.parametrize("angle", [
config.intake_min_angle,
config.intake_max_angle,
config.intake_min_angle - 1,
config.intake_max_angle + 1
])
def test_limit_angle(angle, intake: Intake):
if angle <= config.intake_min_angle:
assert intake.limit_angle(angle) == config.intake_min_angle
elif angle > config.intake_max_angle:
assert intake.limit_angle(angle) == config.intake_max_angle
else:
assert intake.limit_angle(angle) == angle

def test_zero_pivot(intake: Intake):
intake.encoder = MagicMock()

position_mock = MagicMock()
position_mock.value = 0.7
intake.encoder.get_absolute_position.return_value = position_mock

intake.zero_pivot()

expected_angle = ((position_mock.value - config.intake_encoder_zero) / constants.intake_encoder_gear_ratio * 2 * math.pi)

assert intake.pivot_angle == expected_angle
assert intake.pivot_zeroed is True

expected_sensor_position = expected_angle * constants.intake_pivot_gear_ratio / (2 * math.pi)
intake.pivot_motor.set_sensor_position.assert_called_with(expected_sensor_position)

@pytest.mark.parametrize("pivot_angle", [
config.intake_min_angle,
config.intake_max_angle,
config.intake_min_angle - 1,
config.intake_max_angle + 1
])
def test_get_and_set_pivot_angle(intake: Intake, pivot_angle: float):
intake.pivot_motor.get_sensor_position.return_value = (pivot_angle / (2 * math.pi)) * constants.intake_pivot_gear_ratio
intake.set_pivot_angle(pivot_angle)
assert intake.get_pivot_angle() == pivot_angle
def test_stop_pivot(intake: Intake):
intake.stop_pivot()
intake.pivot_motor.set_raw_output.assert_called_with(0)

def test_update_table(intake: Intake):
intake.update_table()
intake.pivot_motor.get_motor_current.assert_called()
intake.pivot_motor.get_applied_output.assert_called()
intake.pivot_motor.get_sensor_velocity.assert_called()
intake.pivot_motor.get_sensor_acceleration.assert_called()