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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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_*.py
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
40 changes: 40 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
from tempfile import TemporaryDirectory
import numpy as np
from PIL import Image
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))
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.

assert(img.shape == (32, 24, 3))


def test_read_labels():
labels = utils.read_labels(file_name='labels.csv')
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))