-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
41 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
extra_fixtures/extension_configs/extension/good_param/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extensionName": "good_param", | ||
"param": "yay!" | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
extra_fixtures/extension_configs/extension/not_json/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NOT a JSON file! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,106 @@ | ||
"""Digest tests.""" | ||
import unittest | ||
from ocfl.layout import Layout | ||
import unittest.mock | ||
from ocfl.layout import Layout, LayoutException | ||
from ocfl.pyfs import open_fs | ||
|
||
|
||
class TestAll(unittest.TestCase): | ||
"""TestAll class to run tests.""" | ||
|
||
def test_almost_everything(self): | ||
def test_init_and_config(self): | ||
"""Test almost everything, just a little.""" | ||
d = Layout() | ||
self.assertEqual(type(d), Layout) | ||
self.assertEqual(d.strip_root('a/b', 'a'), 'b') | ||
self.assertEqual(d.strip_root('a/b', ''), 'a/b') | ||
self.assertEqual(d.strip_root('a/b/c', 'a/b/c'), '.') | ||
self.assertEqual(d.strip_root('a/b/c', 'a/b/'), 'c') | ||
self.assertEqual(d.strip_root('a/b/c/', 'a/b'), 'c') | ||
self.assertRaises(Exception, d.strip_root, 'a', 'b') | ||
self.assertRaises(Exception, d.strip_root, 'a', 'a/b') | ||
self.assertRaises(Exception, d.strip_root, '', 'a/b') | ||
self.assertTrue(d.is_valid('')) | ||
self.assertTrue(d.is_valid('anything')) | ||
self.assertEqual(d.encode(''), '') | ||
self.assertEqual(d.encode('something'), 'something') | ||
self.assertEqual(d.encode('http://a.b.c'), 'http%3A%2F%2Fa.b.c') | ||
self.assertEqual(d.decode(''), '') | ||
self.assertEqual(d.decode('something-else'), 'something-else') | ||
self.assertEqual(d.decode('http%3a%2f%2Fa.b.c'), 'http://a.b.c') | ||
self.assertRaises(Exception, d.identifier_to_path, 'id') | ||
layout = Layout() | ||
self.assertEqual(type(layout), Layout) | ||
self.assertEqual(layout.config_file, "extensions/BASE/config.json") | ||
self.assertEqual(layout.config, None) | ||
|
||
def test_strip_root(self): | ||
"""Test strip_root.""" | ||
layout = Layout() | ||
self.assertEqual(layout.strip_root('a/b', 'a'), 'b') | ||
self.assertEqual(layout.strip_root('a/b', ''), 'a/b') | ||
self.assertEqual(layout.strip_root('a/b/c', 'a/b/c'), '.') | ||
self.assertEqual(layout.strip_root('a/b/c', 'a/b/'), 'c') | ||
self.assertEqual(layout.strip_root('a/b/c/', 'a/b'), 'c') | ||
self.assertRaises(LayoutException, layout.strip_root, 'a', 'b') | ||
self.assertRaises(LayoutException, layout.strip_root, 'a', 'a/b') | ||
self.assertRaises(LayoutException, layout.strip_root, '', 'a/b') | ||
|
||
def test_is_valid(self): | ||
"""Test is_valid method.""" | ||
layout = Layout() | ||
self.assertTrue(layout.is_valid('')) | ||
self.assertTrue(layout.is_valid('anything')) | ||
|
||
def test_encodea_and_decode(self): | ||
"""Test encode and decode methods.""" | ||
layout = Layout() | ||
self.assertEqual(layout.encode(''), '') | ||
self.assertEqual(layout.encode('something'), 'something') | ||
self.assertEqual(layout.encode('http://a.b.c'), 'http%3A%2F%2Fa.b.c') | ||
self.assertEqual(layout.decode(''), '') | ||
self.assertEqual(layout.decode('something-else'), 'something-else') | ||
self.assertEqual(layout.decode('http%3a%2f%2Fa.b.c'), 'http://a.b.c') | ||
self.assertRaises(LayoutException, layout.identifier_to_path, 'id') | ||
|
||
def text_read_layout_params(self): | ||
"""Test read_layout_params.""" | ||
root_fs = open_fs("extra_fixtures/extension_configs") | ||
layout = Layout() | ||
layout.NAME = "good_param" | ||
layout.param = None | ||
|
||
def parse_param(value): | ||
layout.param = value | ||
layout.PARAMS = {"param": parse_param} | ||
layout.read_layout_params(root_fs=root_fs) | ||
self.assertEqual(layout.param, "yay!") | ||
# Error cases | ||
# No config file | ||
layout.NAME = "no_config" | ||
self.assertRaises(LayoutException, layout.read_layout_params, root_fs=root_fs) | ||
layout.NAME = "not_json" | ||
self.assertRaises(LayoutException, layout.read_layout_params, root_fs=root_fs) | ||
|
||
def test_check_and_set_layout_params(self): | ||
"""Test check_and_set_layout_params.""" | ||
layout = Layout() | ||
layout.NAME = "mylayout" | ||
layout.param = None | ||
|
||
def parse_param(value): | ||
layout.param = value | ||
layout.PARAMS = {"param": parse_param} | ||
self.assertRaises(LayoutException, | ||
layout.check_and_set_layout_params, config={}) | ||
self.assertRaises(LayoutException, | ||
layout.check_and_set_layout_params, | ||
config={"extensionName": "wrong-name"}) | ||
# Good case | ||
layout.check_and_set_layout_params( | ||
config={"extensionName": "mylayout", | ||
"param": "42", | ||
"extraParam": "whatever"}) | ||
self.assertEqual(layout.param, "42") | ||
|
||
def test_write_layout_params(self): | ||
"""Test write_layout_params.""" | ||
root_fs = open_fs("mem://") | ||
layout = Layout() | ||
# No config_file will return none, so simply exits | ||
self.assertEqual(layout.write_layout_params(root_fs=root_fs), None) | ||
# File exists | ||
layout.NAME = "a_name" | ||
with unittest.mock.patch("ocfl.layout.Layout.config", new_callable=unittest.mock.PropertyMock) as mock_config: | ||
mock_config.return_value = {"hello": "you"} | ||
# First write works.... | ||
layout.write_layout_params(root_fs=root_fs) | ||
# Second hits file exists | ||
self.assertRaises(LayoutException, layout.write_layout_params, | ||
root_fs=root_fs) | ||
# Change name, make bad JSON causing write exception | ||
layout.NAME = "new_name" | ||
mock_config.return_value = {unittest: "key in json.dump must not be module name!"} | ||
self.assertRaises(LayoutException, layout.write_layout_params, | ||
root_fs=root_fs) |