Created
December 25, 2021 00:15
-
-
Save a2937/f129bb61b40e510615064e4b242268e9 to your computer and use it in GitHub Desktop.
LearnToCode Calendar tests
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
class Calendar: | |
def __init__(self, day=1, month=8, year=2021): | |
self.is_daytime = True | |
self.day = day | |
self.month = month | |
self.year = year | |
self.month_names = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', | |
'August', 'September', 'October', 'November', 'December'] | |
self.days_count = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
def next(self): | |
self.day += 1 | |
if self.day > self.days_count[self.month]: | |
self.day = 1 # first day of new month | |
self.month += 1 # new month | |
if self.month > 12: | |
self.month = 1 # back to January | |
self.year += 1 # increment year | |
def next_week(self): | |
self.day += 7 | |
if self.day > self.days_count[self.month]: | |
self.day = self.day - self.days_count[self.month] | |
self.month += 1 # new month | |
if self.month > 12: | |
self.month = 1 # back to January | |
self.year += 1 # increment year | |
def next_month(self): | |
self.month += 1 | |
if self.month > 12: | |
self.month = 1 # back to January | |
self.year += 1 # increment year | |
def get_month_string(self): | |
return self.month_names[self.month] | |
def get_day_string(self): | |
return str(self.day) |
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
from unittest import TestCase | |
from calendar import Calendar | |
from datetime import datetime, timedelta | |
class TestCalendar(TestCase): | |
def test_next_month(self): | |
calendar = Calendar() | |
calendar.next_week() | |
defaultDate = datetime(year=2021, month=8, day=1) | |
weekLaterDefault = defaultDate + timedelta(days=7) | |
assert calendar.day == weekLaterDefault.day | |
def test_next_month_end(self): | |
calendar = Calendar(year=2021, month=10, day=25) | |
calendar.next_week() | |
defaultDate = datetime(year=2021, month=10, day=25) | |
weekLaterDefault = defaultDate + timedelta(days=7) | |
assert calendar.day == weekLaterDefault.day |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment