Skip to content
This repository has been archived by the owner on May 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from kdmurray91/dev
Browse files Browse the repository at this point in the history
WIP - tidy up code & increase test coverage
  • Loading branch information
Kevin Murray committed Jun 10, 2014
2 parents eb29090 + b7a1f2b commit 2d4b7b5
Show file tree
Hide file tree
Showing 15 changed files with 499 additions and 342 deletions.
26 changes: 13 additions & 13 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,22 +191,22 @@
# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
'papersize': 'a4paper',
# The paper size ('letterpaper' or 'a4paper').
'papersize': 'a4paper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
('index', 'timestreamlib.tex', u'timestreamlib Documentation',
u'Kevin Murray', 'manual'),
('index', 'timestreamlib.tex', u'timestreamlib Documentation',
u'Kevin Murray', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -249,9 +249,9 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'timestreamlib', u'timestreamlib Documentation',
u'Kevin Murray', 'timestreamlib', 'One line description of project.',
'Miscellaneous'),
('index', 'timestreamlib', u'timestreamlib Documentation',
u'Kevin Murray', 'timestreamlib', 'One line description of project.',
'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
Expand All @@ -269,5 +269,5 @@

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {
'python': ('http://docs.python.org/', None),
}
'python': ('http://docs.python.org/', None),
}
36 changes: 18 additions & 18 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
"basic_cr2_exif": path.join(TESTS_DIR, "data", "IMG_0001-CR2-exif.json"),
"empty_dir": path.join(TESTS_DIR, "data", "empty_dir"),
"timestream_manifold": path.join(TESTS_DIR, "data", "timestreams",
"BVZ0022-GC05L-CN650D-Cam07~fullres-orig"),
"BVZ0022-GC05L-CN650D-Cam07~fullres-orig"),
"timestream_nomanifold": path.join(TESTS_DIR, "data", "timestreams",
"nomanifold"),
"nomanifold"),
"timestream_bad": path.join(TESTS_DIR, "data", "timestreams",
"badts"),
"badts"),
"not_a_timestream": path.join(TESTS_DIR, "data", "timestreams",
"not"),
"not"),
}
TS_MANIFOLD_FILES_JPG = [
"timestreams/BVZ0022-GC05L-CN650D-Cam07~fullres-orig/2013/2013_10/2013_10_30/2013_10_30_03/BVZ0022-GC05L-CN650D-Cam07~fullres-orig_2013_10_30_03_00_00_00.JPG",
Expand All @@ -48,24 +48,24 @@
"timestreams/BVZ0022-GC05L-CN650D-Cam07~fullres-orig/2013/2013_10/2013_10_30/2013_10_30_06/BVZ0022-GC05L-CN650D-Cam07~fullres-orig_2013_10_30_06_00_00_00.JPG",
]
TS_MANIFOLD_FILES_JPG = [
path.join(TESTS_DIR, "data", x) for x in TS_MANIFOLD_FILES_JPG
]
path.join(TESTS_DIR, "data", x) for x in TS_MANIFOLD_FILES_JPG
]
TS_MANIFOLD_FILES_TSM = [
path.join(TESTS_DIR, "data", "timestreams",
"BVZ0022-GC05L-CN650D-Cam07~fullres-orig",
"BVZ0022-GC05L-CN650D-Cam07~fullres-orig.tsm"),
]
path.join(TESTS_DIR, "data", "timestreams",
"BVZ0022-GC05L-CN650D-Cam07~fullres-orig",
"BVZ0022-GC05L-CN650D-Cam07~fullres-orig.tsm"),
]

TS_MANIFOLD_FILES = TS_MANIFOLD_FILES_JPG + TS_MANIFOLD_FILES_TSM
TS_MANIFOLD_DATES = [
"2013_10_30_03_00_00",
"2013_10_30_03_30_00",
"2013_10_30_04_00_00",
"2013_10_30_04_30_00",
"2013_10_30_05_00_00",
"2013_10_30_05_30_00",
"2013_10_30_06_00_00",
]
"2013_10_30_03_00_00",
"2013_10_30_03_30_00",
"2013_10_30_04_00_00",
"2013_10_30_04_30_00",
"2013_10_30_05_00_00",
"2013_10_30_05_30_00",
"2013_10_30_06_00_00",
]

if path.exists(FILES["empty_dir"]):
shutil.rmtree(FILES["empty_dir"])
Expand Down
101 changes: 71 additions & 30 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import datetime as dt
from inspect import (
isgenerator,
)
isgenerator,
)
import json
import os
from os import path
from unittest import TestCase, skip, skipIf, skipUnless

from tests import helpers
from timestream.parse import (
_ts_has_manifest,
ts_guess_manifest,
all_files_with_ext,
all_files_with_exts,
ts_iter_images,
ts_get_image,
ts_parse_date,
ts_parse_date_path,
ts_format_date,
)
_ts_has_manifest,
ts_guess_manifest,
all_files_with_ext,
all_files_with_exts,
ts_iter_images,
ts_get_image,
ts_parse_date,
ts_parse_date_path,
ts_format_date,
)


class TestTSHasManifest(TestCase):

"""Test function timestream.parse._ts_has_manifest"""
_multiprocess_can_split_ = True
maxDiff = None
Expand All @@ -28,14 +31,16 @@ def test_good_ts_with_manifold(self):
"""Test _ts_has_manifest with a TS with a manifold"""
res = _ts_has_manifest(helpers.FILES["timestream_manifold"])
self.assertEqual(res, path.join(helpers.FILES["timestream_manifold"],
"BVZ0022-GC05L-CN650D-Cam07~fullres-orig.tsm"))
"BVZ0022-GC05L-CN650D-Cam07~fullres-orig.tsm"))

def test_good_ts_with_no_manifold(self):
"""Test _ts_has_manifest with a TS without a manifold"""
res = _ts_has_manifest(helpers.FILES["timestream_nomanifold"])
self.assertFalse(res)


class TestAllFilesWithExt(TestCase):

"""Test function timestream.parse.all_files_with_ext"""
_multiprocess_can_split_ = True
maxDiff = None
Expand All @@ -54,12 +59,12 @@ def test_with_timestream_ext_jpggccaps(self):

def test_with_timestream_ext_jpg_cs(self):
res = all_files_with_ext(helpers.FILES["timestream_manifold"], "jpg",
cs=True)
cs=True)
self.assertTrue(isgenerator(res))
res = sorted(list(res))
self.assertListEqual(res, [])
res = all_files_with_ext(helpers.FILES["timestream_manifold"], "JPG",
cs=True)
cs=True)
res = sorted(list(res))
self.assertListEqual(res, helpers.TS_MANIFOLD_FILES_JPG)

Expand All @@ -86,40 +91,44 @@ def test_with_bad_param_types(self):
with self.assertRaises(ValueError):
list(all_files_with_ext(".", "jpg", cs="No"))


class TestAllFilesWithExts(TestCase):

"""Test function timestream.parse.all_files_with_exts"""
_multiprocess_can_split_ = True
maxDiff = None

def test_with_timestream_ext_jpg(self):
res = all_files_with_exts(helpers.FILES["timestream_manifold"],
["jpg",])
["jpg", ])
self.assertTrue(isinstance(res, dict))
self.assertDictEqual(res, {"jpg": helpers.TS_MANIFOLD_FILES_JPG})

def test_with_timestream_ext_jpg_tsm(self):
res = all_files_with_exts(helpers.FILES["timestream_manifold"],
["jpg", "tsm"])
["jpg", "tsm"])
self.assertTrue(isinstance(res, dict))
expt = {
"jpg": helpers.TS_MANIFOLD_FILES_JPG,
"tsm": helpers.TS_MANIFOLD_FILES_TSM,
}
}
self.assertDictEqual(res, expt)

def test_with_timestream_ext_jpg_cs(self):
# with incorrect capitialisation
res = all_files_with_exts(helpers.FILES["timestream_manifold"],
["jpg",], cs=True)
["jpg", ], cs=True)
self.assertTrue(isinstance(res, dict))
self.assertDictEqual(res, {"jpg": []})
# With correct capitilisation
res = all_files_with_exts(helpers.FILES["timestream_manifold"],
["JPG",], cs=True)
["JPG", ], cs=True)
self.assertTrue(isinstance(res, dict))
self.assertDictEqual(res, {"JPG": helpers.TS_MANIFOLD_FILES_JPG})


class TestIterImages(TestCase):

"""Test function timestream.parse.ts_iter_images"""
_multiprocess_can_split_ = True
maxDiff = None
Expand All @@ -130,20 +139,22 @@ def test_good_timestream_manifold(self):
self.assertTrue(isgenerator(res))
self.assertListEqual(sorted(list(res)), helpers.TS_MANIFOLD_FILES_JPG)


class TestGuessManifest(TestCase):

"""Tests for timestream.parse.ts_guess_manifest"""
_multiprocess_can_split_ = True
maxDiff = None
expect_good = {
"name": "BVZ0022-GC05L-CN650D-Cam07~fullres-orig",
"start_datetime": "2013_10_30_03_00_00",
"end_datetime": "2013_10_30_06_00_00",
"version": 1,
"image_type": "jpg",
"extension": "JPG",
"interval": 30,
"missing": [],
}
"name": "BVZ0022-GC05L-CN650D-Cam07~fullres-orig",
"start_datetime": "2013_10_30_03_00_00",
"end_datetime": "2013_10_30_06_00_00",
"version": 1,
"image_type": "jpg",
"extension": "JPG",
"interval": 30,
"missing": [],
}

def test_good_ts(self):
got = ts_guess_manifest(helpers.FILES["timestream_manifold"])
Expand All @@ -155,7 +166,9 @@ def test_trailing_slash(self):
self.assertTrue(isinstance(got, dict))
self.assertDictEqual(got, self.expect_good)


class TestGetImage(TestCase):

"""Test function timestream.parse.ts_get_image"""
_multiprocess_can_split_ = True
maxDiff = None
Expand Down Expand Up @@ -204,4 +217,32 @@ def test_get_image_bad_params(self):
with self.assertRaises(ValueError):
# bad subsecond param
ts_get_image(helpers.FILES["timestream_manifold"],
helpers.TS_MANIFOLD_DATES[0], n="this should be an int")
helpers.TS_MANIFOLD_DATES[0], n="this should be an int")


class TestParseDate(TestCase):

"""Test function timestream.parse.ts_parse_date"""

def test_parse_date_valid(self):
"""Test timestream.parse.ts_parse_date with a valid date"""
date_str = "2013_12_11_10_09_08"
date_obj = dt.datetime(2013, 12, 11, 10, 9, 8)
self.assertEqual(ts_parse_date(date_str), date_obj)
# ensure we can pass a datetime and have it silently returned.
self.assertEqual(ts_parse_date(date_obj), date_obj)

def test_parse_date_invalid(self):
"""Test timestream.parse.ts_parse_date with a valid date"""
# Bad format
date_str = "2013-12-11-10-09-08"
with self.assertRaises(ValueError):
ts_parse_date(date_str)
# Bad date
date_str = "2013_14_11_10_09_08"
with self.assertRaises(ValueError):
ts_parse_date(date_str)
# Missing time
date_str = "2013_12_11"
with self.assertRaises(ValueError):
ts_parse_date(date_str)
76 changes: 76 additions & 0 deletions tests/test_parse_validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import datetime as dt
from unittest import TestCase, skip, skipIf, skipUnless
from voluptuous import MultipleInvalid

from tests import helpers
from timestream.parse.validate import (
validate_timestream_manifest,
v_date,
v_datetime,
v_num_str,
)


class TestValidateTimestreamManfiest(TestCase):

"""Tests for ts.parse.validate.validate_timestream_manifest"""
str_dict = {
"name": "BVZ0022-GC05L-CN650D-Cam07~fullres-orig",
"start_datetime": "2013_10_30_03_00_00",
"end_datetime": "2013_10_30_06_00_00",
"version": "1",
"image_type": "jpg",
"extension": "JPG",
"interval": "30",
"missing": [],
}
val_dict = {
"name": "BVZ0022-GC05L-CN650D-Cam07~fullres-orig",
"start_datetime": dt.datetime(2013, 10, 30, 3, 0, 0),
"end_datetime": dt.datetime(2013, 10, 30, 6, 0, 0),
"version": 1,
"image_type": "jpg",
"extension": "JPG",
"interval": 30,
"missing": [],
}

def test_validate_valid(self):
"""Test validate_timestream_manifest with valid manfests"""
self.assertDictEqual(validate_timestream_manifest(self.str_dict),
self.val_dict)
self.assertDictEqual(validate_timestream_manifest(self.val_dict),
self.val_dict)

def test_validate_invalid(self):
"""Test validate_timestream_manifest with invalid manfests"""
with self.assertRaises(TypeError):
validate_timestream_manifest(None)
with self.assertRaises(MultipleInvalid):
validate_timestream_manifest({"A": "b", })

class TestDateValidators(TestCase):
"""Tests for misc date format validators"""

def test_v_date_invalid(self):
"""Test v_date validator with invalid dates"""
# standard date format
date_str = "2013_44_01"
with self.assertRaises(ValueError):
v_date(date_str)
# with different date format
date_str = "2013-44-01"
with self.assertRaises(ValueError):
v_date(date_str)

def test_v_date_valid(self):
"""Test v_date validator with valid dates"""
# standard date format
date_str = "2013_03_01"
self.assertEqual(v_date(date_str), dt.datetime(2013, 03, 01))
date_obj = dt.datetime(2013, 03, 01)
self.assertEqual(v_date(date_obj), date_obj)
# with different date format
date_str = "2013-03-01"
self.assertEqual(v_date(date_str, format="%Y-%m-%d"),
dt.datetime(2013, 03, 01))
Loading

0 comments on commit 2d4b7b5

Please sign in to comment.