def test_fromkeys_default(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__setitem__", autospec=True) as mock__setitem__: d.fromkeys(["file1", "file2"], "testing") mock__setitem__.assert_has_calls( [call("file1", "testing"), call("file2", "testing")])
def test__setitem__no_xattrs(self, mock_getxattr): mock_getxattr.side_effect = OSError read_data = pickle.dumps({"a": 1}) d = iodict.IODict(path="/not/a/path") with patch("builtins.open", unittest.mock.mock_open(read_data=read_data)): d.__setitem__("not-an-item", {"a": 1})
def test__iter__no_items(self, mock_chdir, mock_getcwd, mock_exists, mock_scandir): mock_getcwd.return_value = "/" mock_exists.return_value = True mock_scandir.return_value = [] d = iodict.IODict(path="/not/a/path") self.assertEqual([i for i in d.__iter__()], [])
def test__getitem__(self): read_data = pickle.dumps({"a": 1}) d = iodict.IODict(path="/not/a/path") with patch("builtins.open", unittest.mock.mock_open(read_data=read_data)): item = d.__getitem__("not-an-item") self.assertEqual(item, {"a": 1})
def test_init_thread_lock(self): import threading import _thread d = iodict.IODict(path="/not/a/path", lock=threading.Lock()) self.mock_makedirs.assert_called_with("/not/a/path", exist_ok=True) assert isinstance(d._lock, _thread.LockType)
def test_keys(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__iter__", autospec=True) as mock__iter__: mock__iter__.return_value = ["file1", "file2"] return_items = [i for i in d.keys()] self.assertEqual(return_items, ["file1", "file2"])
def test_pop(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__getitem__", autospec=True) as mock__getitem__: mock__getitem__.return_value = "value" with patch.object(d, "__delitem__", autospec=True) as mock__delitem__: self.assertEqual(d.pop("file1"), "value") mock__delitem__.assert_called_with("file1")
def test_pop_default(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__getitem__", autospec=True) as mock__getitem__: mock__getitem__.side_effect = KeyError with patch.object(d, "__delitem__", autospec=True) as mock__delitem__: self.assertEqual(d.pop("file1", "default1"), "default1") mock__delitem__.assert_called_with("file1")
def test_setdefault(self): read_data = pickle.dumps("") d = iodict.IODict(path="/not/a/path") with patch("builtins.open", unittest.mock.mock_open(read_data=read_data)): item = d.setdefault("not-an-item") self.assertEqual(item, None)
def test_update(self): mapping = {"a": 1, "b": 2, "c": 3} d = iodict.IODict(path="/not/a/path") with patch.object(d, "__setitem__", autospec=True) as mock__setitem__: d.update(mapping) mock__setitem__.assert_has_calls( [call("a", 1), call("b", 2), call("c", 3)])
def test_clear(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__iter__", autospec=True) as mock__iter__: mock__iter__.return_value = ["file1", "file2"] with patch.object(d, "__delitem__", autospec=True) as mock__delitem__: d.clear() mock__delitem__.assert_has_calls( [call("file1"), call("file2")])
def test__exit__(self, mock_listxattr, mock_scandir): with patch("builtins.open", unittest.mock.mock_open()): with patch.object(iodict.IODict, "clear", autospec=True) as mock_clear: with iodict.IODict(path="/not/a/path") as d: d["not-an-item"] = {"a": 1} self.assertFalse(d) mock_clear.assert_called()
def test_pop_no_default_keyerror(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__getitem__", autospec=True) as mock__getitem__: mock__getitem__.side_effect = KeyError with patch.object(d, "__delitem__", autospec=True) as mock__delitem__: with self.assertRaises(KeyError): d.pop("file1") mock__delitem__.assert_called_with("file1")
def test_items(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__iter__", autospec=True) as mock__iter__: mock__iter__.return_value = ["file1", "file2"] with patch.object(d, "__getitem__", autospec=True) as mock__getitem__: mock__getitem__.side_effect = ["value1", "value2"] return_items = [i for i in d.items()] self.assertEqual(return_items, [("file1", "value1"), ("file2", "value2")])
def test_popitem(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__getitem__", autospec=True): with patch.object(d, "__delitem__", autospec=True): with patch.object(d, "__iter__", autospec=True) as mock__iter__: mock__iter__.return_value = iter(["file1", "file2"]) with patch.object(d, "pop", autospec=True) as mock_pop: mock_pop.side_effect = StopIteration with self.assertRaises(KeyError): d.popitem()
def test_popitem(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__getitem__", autospec=True): with patch.object(d, "__delitem__", autospec=True): with patch.object(d, "__iter__", autospec=True) as mock__iter__: mock__iter__.return_value = iter(["file1", "file2"]) with patch.object(d, "pop", autospec=True) as mock_pop: mock_pop.side_effect = ["value1", "value2"] item_value = d.popitem() self.assertEqual(item_value, "value1")
def test__iter__generatorexit(self, mock_exists, mock_scandir): mock_exists.side_effect = [True, True, GeneratorExit] mock_scandir.return_value = [MockItem("file1"), MockItem("file2")] d = iodict.IODict(path="/not/a/path") with patch("os.getxattr") as mock_getxattr: mock_getxattr.side_effect = [ b"key1", b"A\xd8kl\xc1\xb1\xd9]", b"key2", b"A\xd8kn\x0f}\xda\x16", ] return_items = [i for i in d.__iter__()] self.assertEqual(return_items, ["key1"])
def test__iter__not_found(self, mock_chdir, mock_getcwd, mock_exists, mock_scandir): mock_getcwd.return_value = "/" mock_exists.return_value = True mock_scandir.return_value = [MockItem("file1")] d = iodict.IODict(path="/not/a/path") with patch("os.getxattr") as mock_getxattr: mock_getxattr.side_effect = [ b"key1", b"A\xd8kn\x0f}\xda\x16", FileNotFoundError, ] return_items = [i for i in d.__iter__()] self.assertEqual(return_items, ["key1"])
def test__iter__index(self, mock_chdir, mock_getcwd, mock_exists, mock_scandir): mock_getcwd.return_value = "/" mock_exists.return_value = True mock_scandir.return_value = [MockItem("file1"), MockItem("file2")] d = iodict.IODict(path="/not/a/path") with patch("os.getxattr") as mock_getxattr: mock_getxattr.side_effect = [ b"key1", b"A\xd8kn\x0f}\xda\x16", b"key2", b"A\xd8kl\xc1\xb1\xd9]", ] self.assertEqual([i for i in d.__iter__(index=1)], ["key1"])
def test__setitem__(self, mock_listxattr, mock_getxattr, mock_setxattr): read_data = pickle.dumps({"a": 1}) d = iodict.IODict(path="/not/a/path") with patch("builtins.open", unittest.mock.mock_open(read_data=read_data)): d.__setitem__("not-an-item", {"a": 1}) mock_listxattr.assert_called_with("/not/a/path") mock_getxattr.assert_called_with( "/not/a/path/9139d70ac1859518deb6c9adb589348b8e5b00b9b6acffdb4ee6443d", "user.birthtime", ) mock_setxattr.assert_called_with( "/not/a/path/9139d70ac1859518deb6c9adb589348b8e5b00b9b6acffdb4ee6443d", "user.key", b"not-an-item", )
def test__iter__reindex(self, mock_exists, mock_scandir): mock_exists.side_effect = [True, False, True, True] mock_scandir.side_effect = [ [MockItem("file1"), MockItem("file2")], [MockItem("file2")], ] d = iodict.IODict(path="/not/a/path") with patch("os.getxattr") as mock_getxattr: mock_getxattr.side_effect = [ b"key1", b"A\xd8kn\x0f}\xda\x16", b"key2", b"A\xd8kl\xc1\xb1\xd9]", b"key2", b"A\xd8kl\xc1\xb1\xd9]", ] self.assertEqual([i for i in d.__iter__(index=0)], ["key2"])
def test__iter__no_xattr( self, mock_chdir, mock_getcwd, mock_exists, mock_scandir, ): mock_getcwd.return_value = "/" mock_exists.return_value = True mock_scandir.return_value = [ MockItem("key1"), MockItem("key2"), ] d = iodict.IODict(path="/not/a/path") with patch("os.getxattr") as mock_getxattr: mock_getxattr.side_effect = OSError return_items = [i for i in d.__iter__()] self.assertEqual(return_items, ["key1", "key2"])
def test__iter__no_exist(self, mock_exists, mock_scandir): mock_exists.side_effect = [True, True, False, True, True] mock_scandir.return_value = [ MockItem("file1"), MockItem("file2"), MockItem("file3"), ] d = iodict.IODict(path="/not/a/path") with patch("os.getxattr") as mock_getxattr: mock_getxattr.side_effect = [ b"key1", b"A\xd8kn\x0f}\xda\x16", FileNotFoundError, FileNotFoundError, b"key3", b"A\xd8kl\xc1\xb1\xd9]", ] self.assertEqual([i for i in d.__iter__()], ["key3", "key1"])
def test_init_no_xattr(self, mock_listxattr): mock_listxattr.side_effect = OSError("error") d = iodict.IODict(path="/not/a/path") assert d._encoder == str
def test_init_xattr(self, mock_listxattr): mock_listxattr.return_value = ["user.birthtime"] d = iodict.IODict(path="/not/a/path") mock_listxattr.assert_called() assert d._encoder == utils.object_sha3_224
def test_init_no_lock(self): import multiprocessing d = iodict.IODict(path="/not/a/path") self.mock_makedirs.assert_called_with("/not/a/path", exist_ok=True) assert isinstance(d._lock, multiprocessing.synchronize.Lock)
def test__delitem___(self, mock_unlink, mock_listxattr): d = iodict.IODict(path="/not/a/path") d.__delitem__("not-an-item") mock_unlink.assert_called_with( "/not/a/path/9139d70ac1859518deb6c9adb589348b8e5b00b9b6acffdb4ee6443d" )
def test_repr(self, mock_scandir): d = iodict.IODict(path="/not/a/path") self.assertEqual(d.__repr__(), "{}")
def test__delitem___no_xattr(self, mock_unlink): d = iodict.IODict(path="/not/a/path") d.__delitem__("not-an-item") mock_unlink.assert_called_with("/not/a/path/not-an-item")
def test_get_default(self): d = iodict.IODict(path="/not/a/path") with patch.object(d, "__getitem__", autospec=True) as mock__getitem__: mock__getitem__.side_effect = KeyError self.assertEqual(d.get("file1", "default"), "default")