File: test_picklesaver.py

package info (click to toggle)
python-pyutil 3.3.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 884 kB
  • sloc: python: 7,198; makefile: 6
file content (37 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# -*- coding: utf-8-with-signature-unix; fill-column: 77 -*-
# -*- indent-tabs-mode: nil -*-

#  This file is part of pyutil; see README.rst for licensing terms.

from __future__ import print_function
import os

try:
    from twisted.trial import unittest
except ImportError as le:
    print("Skipping %s since it requires Twisted and Twisted could not be imported: %s" % (__name__, le,))
else:
    from pyutil import PickleSaver, fileutil

    class Thingie(PickleSaver.PickleSaver):
        def __init__(self, fname, delay=30):
            PickleSaver.PickleSaver.__init__(self, fname=fname, attrs={'tmp_store':'False'}, DELAY=delay)

    class PickleSaverTest(unittest.TestCase):
        def _test_save_now(self, fname):
            thingie = Thingie(fname, delay=0)
            thingie.tmp_store = 'True'
            thingie.lazy_save() # Note: it was constructed with default save delay of 0.

        def test_save_now(self):
            """
            This test should create a lazy save object, save it with no delay and check if the file exists.
            """
            tempdir = fileutil.NamedTemporaryDirectory()

            fname = os.path.join(tempdir.name, "picklesavertest")
            self._test_save_now(fname)
            self.assertTrue(os.path.isfile(fname), "The file [%s] does not exist." %(fname,))

            tempdir.shutdown()