Skip to content
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

Merged
merged 4 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ install:
- pip install -r test-requirements.txt
script:
- flake8 .
- pytest ./tests/test_utils.py
Copy link
Owner

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.

Copy link
Owner

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

4 changes: 2 additions & 2 deletions autowebcompat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def prepare_images():
images = {}


def load_image(fname):
def load_image(fname, parent_dir='data_resized'):
global images

if fname in images:
return images[fname]

img = load_img(os.path.join('data_resized', fname), target_size=(32, 24))
img = load_img(os.path.join(parent_dir, fname), target_size=(32, 24))
x = img_to_array(img, data_format=keras.backend.image_data_format())

images[fname] = x
Expand Down
39 changes: 39 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
from tempfile import TemporaryDirectory
import numpy as np
from PIL import Image
Copy link
Owner

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:

  1. Built-in modules;
  2. Modules from third-party libraries;
  3. Our modules.

from autowebcompat import utils


def test_get_bugs():
bugs = utils.get_bugs()
assert(isinstance(bugs, list))


def test_mkdir():
d = TemporaryDirectory()
direc_path = d.name + "/test"
utils.mkdir(direc_path)
assert(os.path.isdir(direc_path))
Copy link
Owner

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)

Copy link
Contributor Author

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 .

Copy link
Owner

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.

utils.mkdir(direc_path)


def test_load_image(tmpdir):
d = TemporaryDirectory()
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))
Copy link
Owner

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.



def test_read_labels():
labels = utils.read_labels(file_name='labels.csv')
Copy link
Owner

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.

Copy link
Owner

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.

assert(isinstance(labels, dict))


def test_write_labels():
label = {1: 1, 2: 2}
d = TemporaryDirectory()
file_path = d.name + "/test.csv"
utils.write_labels(label, file_name=file_path)
assert(os.path.exists(file_path))
Copy link
Owner

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.