Skip to content

Commit

Permalink
Add unit tests for the functions in utils.py (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashi456 authored and marco-c committed Feb 19, 2018
1 parent 042f34c commit 7396079
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
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))
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))

0 comments on commit 7396079

Please sign in to comment.