forked from mate-academy/py-online-course
-
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.
- Loading branch information
IvanRamyk
committed
Jan 19, 2022
1 parent
efd423e
commit aa4302a
Showing
3 changed files
with
165 additions
and
8 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,4 +1,57 @@ | ||
# Python boilerplate for GitHub tasks | ||
|
||
- Read [the guideline](https://github.com/mate-academy/py-task-guideline/blob/main/README.md) before start | ||
- Implement the task described [here](app/main.py) | ||
|
||
We want to create a website for finding the best online courses. | ||
In order to store information about courses, we need a new `OnlineCourse` class. | ||
Could you implement it for us? | ||
|
||
`OnlineCourse` constructor takes three arguments: | ||
* `name` - course name (should be stored in `self.name`) | ||
* `description` - course description (should be stored in `self.description`) | ||
* `weeks` - duration of the course in weeks (should be stored in `self.weeks`) | ||
|
||
```python | ||
course = OnlineCourse( | ||
name="Python Basics", | ||
description="The best course to start learn Python", | ||
weeks=2, | ||
) | ||
print(course.description) # The best course to start learn Python | ||
``` | ||
|
||
Often we will receive information about the course in the form of a `course_dict` dictionary | ||
with such fields: | ||
* `course_dict["name"]` - course name | ||
* `course_dict["description"]` - course description | ||
* `course_dict["days"]` - duration of the course in days | ||
|
||
To convert course duration to weeks, `OnlineCourse` should have `days_to_weeks` **staticmethod**, | ||
that takes one argument `days` and convert this number to weeks. | ||
|
||
Note: The last week may not be whole. | ||
|
||
Example: | ||
```python | ||
OnlineCourse.days_to_weeks(10) == 2 | ||
OnlineCourse.days_to_weeks(14) == 2 | ||
OnlineCourse.days_to_weeks(15) == 3 | ||
``` | ||
|
||
`OnlineCourse` should have `from_dict` **classmethod**. It should take | ||
two parameters: | ||
* `cls` - a default parameter for classmethod | ||
* `course_dict` - a dictionary described above | ||
|
||
Method should return a new instance of `OnlineCourse` with correct attributes. | ||
It should use `days_to_weeks` method to convert days to weeks. | ||
Example: | ||
```python | ||
course_dict = { | ||
"name": "Python Core", | ||
"description": "After this course you will know everything about Python", | ||
"days": 12, | ||
} | ||
python_course = OnlineCourse.from_dict(course_dict) | ||
print(python_course.weeks) # 2 | ||
``` |
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,3 +1,3 @@ | ||
# TODO: add initial code | ||
def hello_world(): | ||
return "Hello, world!" | ||
class OnlineCourse: | ||
# write your code here | ||
pass |
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,6 +1,110 @@ | ||
# TODO: add tests | ||
from app.main import hello_world | ||
import pytest | ||
|
||
from app.main import OnlineCourse | ||
|
||
def test_hello_world(): | ||
assert hello_world() == "Hello, world!" | ||
|
||
@pytest.mark.parametrize( | ||
"name,description,weeks", | ||
[ | ||
("Python Basics", "The first Python course you pass", 2), | ||
("Python Basics Extended", "You will learn how to write code in Python", 3), | ||
("Python Core", "The best course ever", 4), | ||
], | ||
) | ||
def test_constructor(name, description, weeks): | ||
course = OnlineCourse(name=name, description=description, weeks=weeks) | ||
assert course.name == name, ( | ||
f"Course should have 'name' equal to {name} " | ||
f"when course is created with " | ||
f"'OnlineCourse(name={name}, description={description}, weeks={weeks})'" | ||
) | ||
|
||
assert course.description == description, ( | ||
f"Course should have 'description' equal to {description} " | ||
f"when course is created with " | ||
f"'OnlineCourse(name={name}, description={description}, weeks={weeks})'" | ||
) | ||
|
||
assert course.weeks == weeks, ( | ||
f"Course should have 'weeks' equal to {weeks} " | ||
f"when course is created with " | ||
f"'OnlineCourse(name={name}, description={description}, weeks={weeks})'" | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"days,weeks", | ||
[ | ||
(1, 1), | ||
(6, 1), | ||
(7, 1), | ||
(8, 2), | ||
(12, 2), | ||
(14, 2), | ||
(15, 3), | ||
(70, 10), | ||
(71, 11), | ||
(121, 18), | ||
], | ||
) | ||
def test_days_to_weeks(days, weeks): | ||
assert OnlineCourse.days_to_weeks(days) == weeks, ( | ||
f"Staticmethod 'days_to_weeks' should return {weeks} " | ||
f"when 'days' is equal to {days}" | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dictionary,name,description,weeks", | ||
[ | ||
( | ||
{ | ||
"name": "Python Basics", | ||
"description": "The best course to start learning Python!", | ||
"days": 13, | ||
}, | ||
"Python Basics", | ||
"The best course to start learning Python!", | ||
2, | ||
), | ||
( | ||
{ | ||
"name": "Python Basics Extended", | ||
"description": "After this course you will know everything about Python data types", | ||
"days": 16, | ||
}, | ||
"Python Basics Extended", | ||
"After this course you will know everything about Python data types", | ||
3, | ||
), | ||
( | ||
{ | ||
"name": "Python Core", | ||
"description": "My favourite course", | ||
"days": 28, | ||
}, | ||
"Python Core", | ||
"My favourite course", | ||
4, | ||
), | ||
], | ||
) | ||
def test_from_dict_method(dictionary, name, description, weeks): | ||
course = OnlineCourse.from_dict(dictionary) | ||
assert course.name == name, ( | ||
f"Course should have 'name' equal to {name} " | ||
f"when course is created with " | ||
f"'OnlineCourse.from_dict({dictionary})'" | ||
) | ||
|
||
assert course.description == description, ( | ||
f"Course should have 'description' equal to {description} " | ||
f"when course is created with " | ||
f"'OnlineCourse.from_dict({dictionary})'" | ||
) | ||
|
||
assert course.weeks == weeks, ( | ||
f"Course should have 'weeks' equal to {weeks} " | ||
f"when course is created with " | ||
f"'OnlineCourse.from_dict({dictionary})'" | ||
) |