File: store_test.py

package info (click to toggle)
python-pypump 0.7-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 560 kB
  • sloc: python: 3,153; makefile: 134
file content (112 lines) | stat: -rw-r--r-- 3,215 bytes parent folder | download | duplicates (4)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from __future__ import absolute_import

import os
import shutil
import stat

from pypump import AbstractStore, JSONStore
from tests import PyPumpTest


class TestStore(AbstractStore):
    """ Provide a more testable store """
    save_called = False

    def save(self):
        """
        Should save data in store
        This will just change a flag to show it's been called
        """
        self.save_called = True


class StoreTest(PyPumpTest):
    """
    Test the store class
    """

    def test_store_and_get(self):
        """ Test that a value can be stored and then retrived """
        store = TestStore()

        # Check that it raises a key error when nothing has been stored.
        def empty_store_key():
            store["coffee"]
        self.assertRaises(KeyError, empty_store_key)

        # Store something
        store["coffee"] = "awesome"

        # Check we can get the same value back out
        self.assertEqual(store["coffee"], "awesome")

    def test_save_on_set(self):
        """ Test that save is called when a value is set """
        store = TestStore()

        # Check that save hasn't been called yet.
        self.assertEqual(store.save_called, False)

        # set some information
        store["coffee"] = "awesome"

        # Check save has been called
        self.assertEqual(store.save_called, True)

    def test_prefix(self):
        """ Test that the prefix is applied to the get and set keys """
        store = TestStore()
        store.prefix = "hai"

        # Test that we can store something and get it back
        store["key"] = "value"
        self.assertEqual(store["key"], "value")

        # Remove the prefix and check that we have to manually prefix
        # the key to get back the previously stored value.
        store.prefix = None

        # Unprefixed shouldn't exist.
        def empty_store_key():
            store["key"]
        self.assertRaises(KeyError, empty_store_key)

        self.assertEqual(store["hai-key"], "value")


class JSONStoreTest(PyPumpTest):
    """
    Test the JSON implementation of the store class
    """

    def setUp(self):
        filename = os.path.abspath(".")
        filename = os.path.join(filename, "pypumpstoretest.json")

        self.assertFalse(os.path.exists(filename), "{0} already exists".format(filename))

        self.filename = filename

    def tearDown(self):
        try:
            os.remove(self.filename)
        except OSError:
            pass

    def test_creating_pypump_dir(self):
        os.environ["XDG_CONFIG_HOME"] = os.path.join(os.path.abspath("."), "pypump_config")
        self.assertFalse(os.path.exists(os.environ["XDG_CONFIG_HOME"]), "{0} already exists".format(os.environ["XDG_CONFIG_HOME"]))

        store = JSONStore()
        store["unittest"] = "framework"

        shutil.rmtree(os.environ["XDG_CONFIG_HOME"], ignore_errors=True)

    def test_permissions(self):
        store = JSONStore(filename=self.filename)
        store["unittest"] = "framework"

        mode = os.stat(self.filename).st_mode

        # we're only going to test to make sure "others" can't read the file
        self.assertEqual(mode & stat.S_IRWXO, 0, "File mode is insecure")