- Joined
- Sep 9, 2022
- Messages
- 1
- Reaction score
- 0
I am doing a homework assignment and I'm having some trouble. To start with, it won't allow me to access the methods of the code I want unless I call the function and pass variables outside of my test functions. Second, it is saying certain things aren't callable. I'll post both my provided code and my tests.
my tests
The tests for date say i cannot call a datetime.date, the tests for temperature say i cannot call an int, and setdate 1 and 7 are failing as well.
Python:
# ----------------------------------------------------------------------
# Imports
# ----------------------------------------------------------------------
import datetime
# ----------------------------------------------------------------------
# DateTemp Class
# ----------------------------------------------------------------------
class DateTemp:
def __init__(self, date, temperature):
self._date = date
self._temperature = temperature
@property
def date(self):
return self._date
@date.setter
def date(self, value):
if not isinstance(value, datetime.date):
raise ValueError(f"Value {value} is not a datetime.date")
self._date = value
@property
def temperature(self):
return self._temperature
@temperature.setter
def temperature(self, value):
# If value cannot be parsed, 'float' will raise a ValueError
self._temperature = float(value)
def set_date_from_ints(self, year, month, day):
# If arguments cannot be parsed, 'int' will raise a ValueError
# If arguments do not represent a valid date, 'datetime.date' will raise a ValueError
self._date = datetime.date(int(year), int(month), int(day))
def __str__(self):
return f'The temperature on {self.date} was {self.temperature} F'
def __repr__(self):
return self.__str__()
# ----------------------------------------------------------------------
# Sorting Functions
# As a precondition, assume 'items' is a list of DateTemp objects
# ----------------------------------------------------------------------
def sorted_by_date(items):
return sorted(items, key=lambda x: x.date)
def sorted_by_temp(items):
return sorted(items, key=lambda x: x.temperature)
my tests
Python:
import pytest
import datetemp
import datetime
dt = datetemp.DateTemp(datetime.date(2010, 1, 1), 100)
def test_date1():
x = datetime.date(2010, 1, 1)
assert dt.date(x) == x
def test_date2():
x = 10
with pytest.raises(ValueError):
dt.date(x)
def test_temperature1():
assert dt.temperature(98) == 98
def test_temperature():
x = "a"
with pytest.raises(ValueError):
dt.temperature(x)
def test_setdate1():
assert dt.set_date_from_ints(2010, 1, 1) == datetime.datetime(2010, 1, 1)
def test_setdate2():
with pytest.raises(ValueError):
dt.set_date_from_ints(2010, 0, 1)
def test_setdate3():
with pytest.raises(ValueError):
dt.set_date_from_ints(2010, 13, 1)
def test_setdate4():
with pytest.raises(ValueError):
dt.set_date_from_ints(2010, 1, 0)
def test_setdate5():
with pytest.raises(ValueError):
dt.set_date_from_ints(2010, 1, 32)
def test_setdate6():
with pytest.raises(ValueError):
dt.set_date_from_ints("a", 1, 1)
def test_setdate7():
with pytest.raises(ValueError):
dt.set_date_from_ints(2010.2, 1, 1)
def test_sort1():
a = datetemp.DateTemp(datetime.datetime(2010, 1, 1), 98)
b = datetemp.DateTemp(datetime.datetime(2009, 1, 1), 100)
c = datetemp.DateTemp(datetime.datetime(2011, 1, 1), 68)
list1 = (a, b, c)
assert datetemp.sorted_by_date(list1)
def test_sort2():
a = datetemp.DateTemp(datetime.datetime(2010, 1, 1), 98)
b = datetemp.DateTemp(datetime.datetime(2009, 1, 1), 100)
c = datetemp.DateTemp(datetime.datetime(2011, 1, 1), 68)
list1 = (a, b, c)
assert datetemp.sorted_by_date(list1)
The tests for date say i cannot call a datetime.date, the tests for temperature say i cannot call an int, and setdate 1 and 7 are failing as well.