-
Notifications
You must be signed in to change notification settings - Fork 0
Feature unittests #4
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: main
Are you sure you want to change the base?
Conversation
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.
Mostly good, glad to see your first shot turned out so well. Please mock away the side-effects completely. Then it generally works for me. This is the library function from geopy
and of course the environment variable reader. Keep it going man! 🚀 🚀 🚀
# Testing | ||
- name: Test code | ||
env: | ||
TNT_EX1_OPENWEATHERMAP_API_KEY: ${{ secrets.TNT_EX1_OPENWEATHERMAP_API_KEY}} |
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.
This is not supposed to happen 🤯 you ought to mock the environment variable away. Unit test means to test a standalone unit 😘
env: | ||
TNT_EX1_OPENWEATHERMAP_API_KEY: ${{ secrets.TNT_EX1_OPENWEATHERMAP_API_KEY}} | ||
run: | ||
./bin/task test |
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.
Please add a newline at the end of the file since some tools don't execute the last line if it ends with EOF (end of file) instead of a newline \n
.
poetry run pytest | ||
-s | ||
--cov=. | ||
--cov-report=html |
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.
Another newline 👀
cmds: | ||
- > | ||
poetry run pytest | ||
-s |
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.
Pleas write out command line options if you can. I have no fricking clue what -s
is ❓
coords_berlin = fetch_location_coords("Berlin") | ||
coords_difference = [sum(x) for x in zip(coords_berlin, (-52.520008, -13.404954))] | ||
assert sum(list(coords_difference)) < 0.01 |
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.
Interesting way to test this 😅 is there maybe an easier and more transparent way to possible test this? Like checking if something equals something without summing up and other tricks?
timeout=15, | ||
) | ||
data = response.json() | ||
data = fetch_new_data(api_key, location_coords) |
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.
This is so beautiful and smart 💡 I'm proud of you 😘
|
||
@mock.patch("weather_data_fetcher.fetch_new_data", autospec=True) | ||
def test_fetch_data(self, request_mock): | ||
request_mock.return_value = { |
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.
You can make your life easier here and just put into the json payload what you extract in the end. This makes everything much much shorter.
if "TNT_EX1_OPENWEATHERMAP_API_KEY" not in os.environ: | ||
dotenv_file = dotenv.find_dotenv() | ||
dotenv.load_dotenv(dotenv_file) |
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.
Please remove this and mock the environment variable getter.
dotenv_file = dotenv.find_dotenv() | ||
dotenv.load_dotenv(dotenv_file) | ||
|
||
fetch_weather_data(os.environ["TNT_EX1_OPENWEATHERMAP_API_KEY"], (52.520008, 13.404954)) |
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.
It is nice how you call the function here, but you only check that it does not error out basically. If you really want to know what was done inside, you need to mock the print
or stdout
and verify how the output message would look like with an assert.
print(output) | ||
|
||
|
||
def fetch_new_data(api_key: str, location_coords: Tuple[float, float]) -> json: |
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.
You have two choices:
- Make the function an internal function by starting with an underscore
_
- Write a more complete docstring describing the function including the parameters and return value
A few unittests to start things off. Main focus is on how to work with Github secrets for the API key.