-
Notifications
You must be signed in to change notification settings - Fork 41
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
Add unit tests for the functions in utils.py #63
Conversation
@@ -0,0 +1,3 @@ | |||
{ | |||
"test_utils.py::test_answer": true |
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.
Hey, I think this file should be in the .gitignore.
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.
Indeed, and it should also be removed from the PR.
tests/test_utils.py
Outdated
from keras.preprocessing.image import load_img, img_to_array | ||
|
||
|
||
def mkdir(dir_name): |
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.
The functions that need to be tested should not be here.
For functions that require the |
Yes, this is what we should do. |
OK, let me know when the PR is ready and I'll review it. |
@marco-c So i've written basic tests for 6 functions, just asserting the existence of the objects in case of functions with no specific parameters. For writing a couples_generator, getImageGenerator there is image_test or image_train so i have written no function for the same. |
tests/test_utils.py
Outdated
def test_mkdir(): | ||
direc_path = os.path.dirname(os.path.realpath(__file__)) + "test" | ||
utils.mkdir(direc_path) | ||
assert(os.path.exists(direc_path)) |
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 assert that this is a directory.
Also, you can test calling it again to check that it doesn't fail when the directory already exists.
tests/test_utils.py
Outdated
assert(os.path.exists(direc_path)) | ||
|
||
|
||
label = {} |
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.
Not sure you need to define this globally.
tests/test_utils.py
Outdated
|
||
def test_read_labels(): | ||
labels = utils.read_labels(file_name='labels.csv') | ||
assert('labels' in locals() or 'labels' in globals()) |
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 assertion is strange. Just assert that labels is not null and that it is a dictionary.
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.
i was just asserting the existence of the variable
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 defined it at line 15, so it clearly exists 😄
tests/test_utils.py
Outdated
def test_write_labels(): | ||
utils.write_labels(label, file_name='test.csv') | ||
fname = os.path.dirname(os.path.realpath(__file__)) + "\\test.csv" | ||
assert(os.path.exists(fname)) |
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.
Also assert that you can use read_labels to read the labels back.
tests/test_utils.py
Outdated
assert(os.path.exists(fname)) | ||
|
||
|
||
images = {"Image.png": [23434, 324124]} |
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 unused.
tests/test_utils.py
Outdated
|
||
def test_load_image(): | ||
img = utils.load_image("Image.png") | ||
assert(img) |
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.
assert that this is a numpy 2x2 array
tests/test_utils.py
Outdated
|
||
def test_get_bugs(): | ||
Object = utils.get_bugs() | ||
assert(Object) |
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.
assert that this is a list
tests/test_utils.py
Outdated
def test_balance(): | ||
Sample = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] | ||
it = utils.balance(Sample) | ||
assert(it) |
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 actually assert what balance returns here and not just check that it is not null.
I'm not sure what you mean here, can you expand? |
Nit: Can you put the tests in the same order as the functions of test_utils.py? |
Also, don't forget to use tempfile to create the temporary directory where you store the temporary test files. |
@marco-c I have added temporary directory to the unit tests wherever I am making a directory or a file. I have also made other minor changes you asked me to do. I had two doubts though, What is balance returning? and I was getting a row index out of range error while reading the written labels do you have any idea what could cause it? |
tests/test_utils.py
Outdated
|
||
|
||
def test_get_bugs(): | ||
Object = utils.get_bugs() |
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.
Nit: rename Object
to bugs
.
tests/test_utils.py
Outdated
d = TemporaryDirectory() | ||
direc_path = os.path.join(d.name + "\\test") | ||
utils.mkdir(direc_path) | ||
assert(os.path.isdir(direc_path)) |
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.
Second part from #63 (comment)
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.
@marco-c Instead of writing a test for it , would it not make more sense to change the mkdir function to check if a directory with the same name exists and not make it if it does? since the function the doesnt return any error message it is pretty hard to assert on anything . If some error message were returned i could assert on the same .
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 don't need to add an assertion, the assertion is implied in the fact that utils.mkdir
doesn't throw an exception.
tests/test_utils.py
Outdated
|
||
|
||
def test_load_image(): | ||
img = utils.load_image("Image") |
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 file doesn't exist in the repository, so I'm not sure the test will pass.
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.
@marco-c earlier i had the load_image function in the file and i was using a random dictionary to just check if the code was working, Because an empty file name would result in a parameter not declared error. Should i remove the 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.
You can add a parent_dir
argument to load_image
, with the default being data_resized
.
Then in the test you can create a fake image in the temporary directory, pass the temporary directory as parent_dir to load_image and then assert that the image is loaded as a 2x2 numpy array with the correct dimensions.
You can see the function implementation or the callers of the functions to understand. If you can't figure it out, please remove the current
I'm not sure, as I haven't seen what you were trying to do. If you can't figure it out, please file a follow-up issue to add the test. |
Please also update the .travis.yml file to actually run the tests. |
I've noticed there's a nice pytest feature that you can use instead of manually using |
@marco-c i've made all the minor changes you asked me to, i have removed the balance test and will open a pr for the same, because balance seems to be interacting with Couples-generator but we aren't writing tests for functions which need the data directory. One last thing is the error in the Travis-cl build it is giving an image not found error with the Load_image test, while I am able to pass the test successfully on my pc. Any Thoughts? |
It seems you haven't done #63 (comment) and #63 (comment).
The balance function is generic, it doesn't require CouplesGenerator to work. Anyway yes, please open an issue to add a test for this function and an issue for each function in utils.py that isn't tested yet.
Doing #63 (comment) will fix the problem. |
@marco-c i did modify both the load image file with the parent directory and try it with both temporary and an actual directory. I will look further into, thankyou. |
Also don't download the image from the network, it's not great for unit tests (what if the image is removed from the website? The tests will start failing). Create a fake image using PIL. |
tests/test_utils.py
Outdated
os.mkdir('data_resized') | ||
fake_img = np.random.rand(30, 30, 3) * 255 | ||
img = Image.fromarray(fake_img.astype('uint8')).convert('RGBA') | ||
img.save(".\data_resized\Image.jpg", "JPEG") |
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.
Don't specify the format, it's automatically detected from the file extension.
Review Changes Review Changes Review Changes Review Changes
@marco-c i will open the balance PR as soon as this is merged, i have done all changes you've asked me for . Thankyou |
I still don't see #63 (comment) 😄 Also, in |
tests/test_utils.py
Outdated
def test_load_image(): | ||
os.mkdir('data_resized') | ||
fake_img = np.random.rand(30, 30, 3) * 255 | ||
img = Image.fromarray(fake_img.astype('uint8')).convert('RGBA') |
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.
Use Image.new
instead of creating a numpy array and then convering it into an image.
tests/test_utils.py
Outdated
d = TemporaryDirectory() | ||
file_path = os.path.join(d.name + "\\test.csv") | ||
utils.write_labels(label, file_name=file_path) | ||
assert(os.path.exists(file_path)) |
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.
Remember to file an issue about adding a test that writes labels and then reads them back.
tests/test_utils.py
Outdated
|
||
def test_mkdir(): | ||
d = TemporaryDirectory() | ||
direc_path = os.path.join(d.name + "\\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.
You're calling os.path.join
with a single string, which is basically useless.
Either do os.path.join(d.name, 'test')
or d.name + '/test'
.
tests/test_utils.py
Outdated
import numpy as np | ||
from tempfile import TemporaryDirectory | ||
from autowebcompat import utils | ||
from PIL import Image |
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.
Sort the imports in this way:
- Built-in modules;
- Modules from third-party libraries;
- Our modules.
@marco-c do you necessarily want the new pytest feature and not the Temporary Directory () way? |
.travis.yml
Outdated
@@ -6,3 +6,4 @@ install: | |||
- pip install -r test-requirements.txt | |||
script: | |||
- flake8 . | |||
- pytest ./tests/test_utils.py |
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 going to execute test_utils.py
only, please make it run any test that is in the tests
directory, so we don't have to update this line the next time we add a 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.
Don't forget about this, @Shashi456
The pytest feature is nicer, so I would prefer it. If you can't, please file an issue for someone else to do it. |
Would you want the PR's to be referenced to this? @marco-c |
Do you mean the issues? Yes |
tests/test_utils.py
Outdated
|
||
|
||
def test_read_labels(): | ||
labels = utils.read_labels(file_name='labels.csv') |
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.
Create a fake labels.csv file in a temporary directory, so you can assert more things about the result.
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.
Actually, don't do it, we'll do it as part of the other test that writes and reads labels.
img = Image.new("RGB", (30, 30)) | ||
img.save(d.name + "/Image.jpg") | ||
img = utils.load_image("Image.jpg", d.name) | ||
assert(isinstance(img, np.ndarray)) |
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 assert the dimensions of the numpy array too.
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.
Thanks! I see you have filed follow-up issues. I'm marking them good first bugs for newcomers, but since they are follow-ups to your PR, you can take them if you want (just say so in the issue if you do).
Please provide your feedback on this pull request here. Privacy statement: We don't store any personal information such as your email address or name. We ask for GitHub authentication as an anonymous identifier to account for duplicate feedback entries and to see people specific preferences. |
Fixes #6
These are just very basic unit tests , which is my understanding of them. Since we are just checking each module individually and seeing if they do the job they are supposed to do. Also another thing i wanted to ask is how could i go about writing tests for functions which had no parameters, should i just check the existence of what has been returned? Thanks for your help.
@marco-c I will clean up the code and use tempfile in the upcoming commit, this was just for a basic check to see if you were expecting similar unit tests.