From 907f074f62be96ee3adb47c02d8dae78b9e193b1 Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Mon, 19 Feb 2018 22:38:00 +0530 Subject: [PATCH 1/4] Review Changes Review Changes Review Changes Review Changes Review Changes --- .travis.yml | 1 + autowebcompat/utils.py | 4 ++-- tests/test_utils.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/test_utils.py diff --git a/.travis.yml b/.travis.yml index 6bd327e1..415cb915 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,4 @@ install: - pip install -r test-requirements.txt script: - flake8 . + - pytest ./tests/test_utils.py diff --git a/autowebcompat/utils.py b/autowebcompat/utils.py index 8e2f356a..753f7268 100644 --- a/autowebcompat/utils.py +++ b/autowebcompat/utils.py @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..fff715ac --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,42 @@ +import os +import numpy as np +from tempfile import TemporaryDirectory +from autowebcompat import utils +from PIL import Image + + +def test_get_bugs(): + bugs = utils.get_bugs() + assert(isinstance(bugs, list)) + + +def test_mkdir(): + d = TemporaryDirectory() + direc_path = os.path.join(d.name + "\\test") + utils.mkdir(direc_path) + assert(os.path.isdir(direc_path)) + utils.mkdir(direc_path) + + +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') + img.save("./data_resized/Image.jpg") + img = utils.load_image("Image.jpg", "./data_resized") + assert(isinstance(img, np.ndarray)) + os.remove("./data_resized/Image.jpg") + os.rmdir("data_resized") + + +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 = os.path.join(d.name + "\\test.csv") + utils.write_labels(label, file_name=file_path) + assert(os.path.exists(file_path)) From 3be5f4d2808772a664eaed503655dd1afe83f54a Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Tue, 20 Feb 2018 00:07:35 +0530 Subject: [PATCH 2/4] Minor Changes --- tests/test_utils.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index fff715ac..0bb45028 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,8 +1,8 @@ import os -import numpy as np from tempfile import TemporaryDirectory -from autowebcompat import utils +import numpy as np from PIL import Image +from autowebcompat import utils def test_get_bugs(): @@ -12,21 +12,18 @@ def test_get_bugs(): def test_mkdir(): d = TemporaryDirectory() - direc_path = os.path.join(d.name + "\\test") + direc_path = d.name + "/test" utils.mkdir(direc_path) assert(os.path.isdir(direc_path)) utils.mkdir(direc_path) -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') - img.save("./data_resized/Image.jpg") - img = utils.load_image("Image.jpg", "./data_resized") +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)) - os.remove("./data_resized/Image.jpg") - os.rmdir("data_resized") def test_read_labels(): @@ -37,6 +34,6 @@ def test_read_labels(): def test_write_labels(): label = {1: 1, 2: 2} d = TemporaryDirectory() - file_path = os.path.join(d.name + "\\test.csv") + file_path = d.name + "/test.csv" utils.write_labels(label, file_name=file_path) assert(os.path.exists(file_path)) From c8c602765ff32517e66c470498ffa412d0a61a1f Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Tue, 20 Feb 2018 00:30:08 +0530 Subject: [PATCH 3/4] Minor Changes --- tests/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index 0bb45028..b1323b30 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -24,6 +24,7 @@ def test_load_image(tmpdir): 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(): From 9099367b90fcd245f363bfd464fe8b9abb38f54d Mon Sep 17 00:00:00 2001 From: PAWAN SASANKA AMMANAMANCHI Date: Tue, 20 Feb 2018 00:35:15 +0530 Subject: [PATCH 4/4] Updated travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 415cb915..2e6b56a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,4 +6,4 @@ install: - pip install -r test-requirements.txt script: - flake8 . - - pytest ./tests/test_utils.py + - pytest ./tests/test_*.py