コード例 #1
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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")])
コード例 #2
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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})
コード例 #3
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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__()], [])
コード例 #4
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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})
コード例 #5
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
    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)
コード例 #6
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
    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"])
コード例 #7
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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")
コード例 #8
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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")
コード例 #9
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
    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)
コード例 #10
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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)])
コード例 #11
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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")])
コード例 #12
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
    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()
コード例 #13
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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")
コード例 #14
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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")])
コード例 #15
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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()
コード例 #16
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
    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")
コード例 #17
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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"])
コード例 #18
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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"])
コード例 #19
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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"])
コード例 #20
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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",
     )
コード例 #21
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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"])
コード例 #22
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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"])
コード例 #23
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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"])
コード例 #24
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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
コード例 #25
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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
コード例 #26
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
    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)
コード例 #27
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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"
     )
コード例 #28
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 def test_repr(self, mock_scandir):
     d = iodict.IODict(path="/not/a/path")
     self.assertEqual(d.__repr__(), "{}")
コード例 #29
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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")
コード例 #30
0
ファイル: test_iodict.py プロジェクト: peznauts/directord
 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")