-
Notifications
You must be signed in to change notification settings - Fork 0
Homework4 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Homework4 #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # This is a basic workflow to help you get started with Actions | ||
|
|
||
| name: CI | ||
|
|
||
| on: push | ||
|
|
||
| jobs: | ||
| code_quality: | ||
| runs-on: ubuntu-latest | ||
| name: Checks with black, isort and possibly run tests | ||
| container: python:3.9 | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v2 | ||
| - name: Run script | ||
| run: | | ||
| pip install flake8 isort pytest | ||
| pip install requests | ||
| ls -la | ||
| flake8 . | ||
| isort --check . | ||
| py.test tests |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """ | ||
| Write a function that gets file path as an argument. | ||
| Read the first line of the file. | ||
| If first line is a number return true if number in an interval [1, 3)* | ||
| and false otherwise. | ||
| In case of any error, a ValueError should be thrown. | ||
|
|
||
| Write a test for that function using pytest library. | ||
| You should create files required for the testing inside the test run and | ||
| remove them after the test run. | ||
| (Opposite to previous homeworks when you used files created manually | ||
| before the test.) | ||
|
|
||
| Definition of done: | ||
| - function is created | ||
| - function is properly formatted | ||
| - function has positive and negative tests | ||
| - tests do a cleanup and remove remove files generated by tests | ||
|
|
||
| You will learn: | ||
| - how to test Exceptional cases | ||
| - how to clean up after tests | ||
| - how to check if file exists** | ||
| - how to handle*** and raise**** exceptions in test. Use sample | ||
| from the documentation. | ||
|
|
||
| * https://en.wikipedia.org/wiki/Interval_(mathematics)#Terminology | ||
| ** https://docs.python.org/3/library/os.path.html | ||
| *** https://docs.python.org/3/tutorial/errors.html#handling-exceptions | ||
| **** https://docs.python.org/3/tutorial/errors.html#raising-exceptions | ||
| """ | ||
|
|
||
|
|
||
| def read_magic_number(path: str) -> bool: | ||
| try: | ||
| with open(path, 'r') as file: | ||
| line = file.readline() | ||
| return 1 <= float(line) < 3 | ||
| except BaseException: | ||
| raise ValueError | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """ | ||
| Write a function that accepts an URL as input | ||
| and count how many letters `i` are present in the HTML by this URL. | ||
|
|
||
| Write a test that check that your function works. | ||
| Test should use Mock instead of real network interactions. | ||
|
|
||
| You can use urlopen* or any other network libraries. | ||
| In case of any network error raise ValueError("Unreachable {url}). | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. errors? |
||
|
|
||
| Definition of done: | ||
| - function is created | ||
| - function is properly formatted | ||
| - function has positive and negative tests | ||
| - test could be run without internet connection | ||
|
|
||
| You will learn: | ||
| - how to test using mocks | ||
| - how to write complex mocks | ||
| - how to raise an exception form mocks | ||
| - do a simple network requests | ||
|
|
||
|
|
||
| >>> count_dots_on_i("https://example.com/") | ||
| 59 | ||
|
|
||
| * https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen | ||
| """ | ||
|
|
||
| import requests | ||
| from requests import RequestException | ||
|
|
||
| # i misunderstood that we can use any lib for this task | ||
|
|
||
|
|
||
| def count_dots_on_i(url: str) -> int: | ||
| try: | ||
| r = requests.get(url) | ||
| return r.text.count("i") | ||
| except RequestException: | ||
| raise ValueError(f"Unreachable {url}") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about errors?