-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: KonanAI <[email protected]>
- Loading branch information
Showing
1 changed file
with
68 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,246 +1,96 @@ | ||
import pytest | ||
from src.tap.classes.timeframe import Time | ||
|
||
|
||
def test_time_init_with_seconds(): | ||
@pytest.fixture(params=[(1.0, "second"), (100, "frame")]) | ||
def time_obj(request): | ||
# Arrange | ||
data = 1.0 | ||
unit = "second" | ||
data, unit = request.param | ||
return Time(data, unit) | ||
|
||
# Act | ||
time_obj = Time(data, unit) | ||
|
||
def test_time_init(time_obj): | ||
# Assert | ||
assert time_obj.second == data | ||
assert time_obj.frame == data * Time.FRAMES_PER_SECOND | ||
|
||
|
||
def test_time_init_with_frames(): | ||
# Arrange | ||
data = 100 | ||
unit = "frame" | ||
|
||
# Act | ||
time_obj = Time(data, unit) | ||
|
||
# Assert | ||
assert time_obj.second == data / Time.FRAMES_PER_SECOND | ||
assert time_obj.frame == data | ||
|
||
|
||
def test_time_init_with_invalid_unit(): | ||
# Arrange | ||
data = 1.0 | ||
unit = "invalid_unit" | ||
|
||
# Act & Assert | ||
with pytest.raises(ValueError): | ||
Time(data, unit) | ||
if time_obj.unit == "second": | ||
assert time_obj.second == time_obj.data | ||
assert time_obj.frame == time_obj.data * Time.FRAMES_PER_SECOND | ||
elif time_obj.unit == "frame": | ||
assert time_obj.second == time_obj.data / Time.FRAMES_PER_SECOND | ||
assert time_obj.frame == time_obj.data | ||
|
||
|
||
def test_time_init_with_incorrect_data_type(): | ||
# Arrange | ||
data = "invalid_data" | ||
unit = "second" | ||
|
||
@pytest.mark.parametrize("data, unit", [ | ||
(1.0, "invalid_unit"), | ||
("invalid_data", "second") | ||
]) | ||
def test_time_init_with_invalid_values(data, unit): | ||
# Act & Assert | ||
with pytest.raises(TypeError): | ||
with pytest.raises((ValueError, TypeError)): | ||
Time(data, unit) | ||
|
||
|
||
def test_addition_of_two_times(): | ||
@pytest.mark.parametrize("operator, result", [ | ||
("add", 2.0), | ||
("sub", 1.0) | ||
]) | ||
def test_time_arithmetic_operations(time_obj, operator, result): | ||
# Arrange | ||
time_obj1 = Time(1.0, "second") | ||
time_obj2 = Time(1.0, "second") | ||
|
||
# Act | ||
result = time_obj1 + time_obj2 | ||
|
||
# Assert | ||
assert result.second == 2.0 | ||
|
||
|
||
def test_subtraction_of_two_times(): | ||
# Arrange | ||
time_obj1 = Time(2.0, "second") | ||
time_obj2 = Time(1.0, "second") | ||
|
||
# Act | ||
result = time_obj1 - time_obj2 | ||
|
||
# Assert | ||
assert result.second == 1.0 | ||
|
||
|
||
def test_multiplication_with_scalar(): | ||
# Arrange | ||
time_obj = Time(2.0, "second") | ||
scalar = 2.0 | ||
|
||
# Act | ||
result = time_obj * scalar | ||
|
||
# Assert | ||
assert result.second == 4.0 | ||
|
||
|
||
def test_multiplication_with_non_integer_result(): | ||
# Arrange | ||
time_obj = Time(3.11, "second") | ||
scalar = 0.5 | ||
|
||
# Act & Assert | ||
with pytest.raises(ValueError): | ||
time_obj * scalar | ||
|
||
|
||
def test_division_by_scalar(): | ||
# Arrange | ||
time_obj = Time(2.0, "second") | ||
scalar = 2.0 | ||
|
||
# Act | ||
result = time_obj / scalar | ||
|
||
# Assert | ||
assert result.second == 1.0 | ||
|
||
|
||
def test_division_by_zero(): | ||
# Arrange | ||
time_obj = Time(2.0, "second") | ||
scalar = 0.0 | ||
|
||
if operator == "add": | ||
assert (time_obj + time_obj2).second == result | ||
elif operator == "sub": | ||
assert (time_obj - time_obj2).second == result | ||
|
||
|
||
@pytest.mark.parametrize("scalar, result", [ | ||
(2.0, 4.0), | ||
(1.5, ValueError), | ||
(0.0, ZeroDivisionError) | ||
]) | ||
def test_time_scalar_operations(time_obj, scalar, result): | ||
# Act & Assert | ||
with pytest.raises(ZeroDivisionError): | ||
time_obj / scalar | ||
|
||
|
||
def test_division_with_non_integer_result(): | ||
# Arrange | ||
time_obj = Time(2.0, "second") | ||
scalar = 1.5 | ||
if isinstance(result, type) and issubclass(result, Exception): | ||
with pytest.raises(result): | ||
time_obj / scalar | ||
time_obj * scalar | ||
else: | ||
assert (time_obj / scalar).second == result | ||
assert (time_obj * scalar).second == 2 * result | ||
|
||
|
||
@pytest.mark.parametrize("operator, time_obj2_data, result", [ | ||
("lt", 2.0, True), | ||
("le", 1.0, True), | ||
("gt", 1.0, False), | ||
("ge", 2.0, True), | ||
("eq", 2.0, True), | ||
("ne", 2.0, False) | ||
]) | ||
def test_time_comparison_operations(time_obj, operator, time_obj2_data, result): | ||
# Arrange | ||
time_obj2 = Time(time_obj2_data, "second") | ||
|
||
# Act & Assert | ||
with pytest.raises(ValueError): | ||
time_obj / scalar | ||
|
||
|
||
def test_less_than_operator(): | ||
# Arrange | ||
time_obj1 = Time(1.0, "second") | ||
time_obj2 = Time(2.0, "second") | ||
|
||
# Act | ||
result = time_obj1 < time_obj2 | ||
|
||
# Assert | ||
assert result | ||
|
||
|
||
def test_less_than_or_equal_operator(): | ||
# Arrange | ||
time_obj1 = Time(1.0, "second") | ||
time_obj2 = Time(1.0, "second") | ||
|
||
# Act | ||
result = time_obj1 <= time_obj2 | ||
|
||
# Assert | ||
assert result | ||
|
||
|
||
def test_greater_than_operator(): | ||
# Arrange | ||
time_obj1 = Time(2.0, "second") | ||
time_obj2 = Time(1.0, "second") | ||
|
||
# Act | ||
result = time_obj1 > time_obj2 | ||
assert eval(f"time_obj {operator} time_obj2") is result | ||
|
||
# Assert | ||
assert result | ||
|
||
|
||
def test_greater_than_or_equal_operator(): | ||
# Arrange | ||
time_obj1 = Time(2.0, "second") | ||
time_obj2 = Time(2.0, "second") | ||
|
||
# Act | ||
result = time_obj1 >= time_obj2 | ||
|
||
# Assert | ||
assert result | ||
|
||
|
||
def test_equality_operator(): | ||
# Arrange | ||
time_obj1 = Time(2.0, "second") | ||
time_obj2 = Time(2.0, "second") | ||
|
||
# Act | ||
result = time_obj1 == time_obj2 | ||
|
||
# Assert | ||
assert result | ||
|
||
|
||
def test_inequality_operator(): | ||
# Arrange | ||
time_obj1 = Time(1.0, "second") | ||
time_obj2 = Time(2.0, "second") | ||
|
||
# Act | ||
result = time_obj1 != time_obj2 | ||
|
||
# Assert | ||
assert result | ||
|
||
|
||
def test_second_property(): | ||
# Arrange | ||
data = 1.0 | ||
unit = "second" | ||
time_obj = Time(data, unit) | ||
|
||
# Act | ||
result = time_obj.second | ||
|
||
# Assert | ||
assert result == data | ||
|
||
|
||
def test_frame_property(): | ||
# Arrange | ||
data = 100 | ||
unit = "frame" | ||
time_obj = Time(data, unit) | ||
|
||
# Act | ||
result = time_obj.frame | ||
|
||
# Assert | ||
assert result == data | ||
|
||
|
||
def test_str_representation(): | ||
# Arrange | ||
time_obj = Time(1.0, "second") | ||
|
||
# Act | ||
result = str(time_obj) | ||
|
||
# Assert | ||
assert result == "Time(second=1.0, frame=100)" | ||
def test_time_properties(time_obj): | ||
# Act & Assert | ||
if time_obj.unit == "second": | ||
assert time_obj.second == time_obj.data | ||
elif time_obj.unit == "frame": | ||
assert time_obj.frame == time_obj.data | ||
|
||
|
||
def test_repr_representation(): | ||
def test_time_str_repr(time_obj): | ||
# Arrange | ||
time_obj = Time(1.0, "second") | ||
|
||
expected_result = f"Time(second={time_obj.second}, frame={time_obj.frame})" | ||
# Act | ||
result = repr(time_obj) | ||
str_result = str(time_obj) | ||
repr_result = repr(time_obj) | ||
|
||
# Assert | ||
assert result == "Time(second=1.0, frame=100)" | ||
assert str_result == expected_result | ||
assert repr_result == expected_result |