Exemple #1
0
    def test_singleton(self):
        a = Registry()
        b = Registry()
        self.assertIs(a, b)

        a[1] = 2
        self.assertEqual({1: 2}, b)
Exemple #2
0
    def test_persist(self):
        try:
            Registry.set(1, 2, 3, 4)
            tmp = tempfile.mkdtemp()
            file_path = os.path.join(tmp, "foo.json")
            Registry.persist(file_path)

            Registry.set(1, 2, 3, 5)
            Registry._obj = {}

            Registry.from_file(file_path)

            self.assertEqual({"1": {"2": {"3": 4}}}, Registry())
        finally:
            shutil.rmtree(tmp)
Exemple #3
0
    def test_from_file(self):
        try:
            tmp = tempfile.mkdtemp()
            file_path = os.path.join(tmp, "foo.json")
            with open(file_path, "w") as fp:
                json.dump(dict(a=True), fp)

            Registry.from_file(file_path)

            self.assertEqual(dict(a=True), Registry())

            Registry.set("a", False)

            self.assertFalse(Registry.get("a"))

            Registry.from_file(file_path)
            self.assertFalse(Registry.get("a"))

        finally:
            shutil.rmtree(tmp)
Exemple #4
0
 def tearDown(self):
     Registry().clear()
     super(TestCase, self).tearDown()
Exemple #5
0
    def test_clear(self):
        Registry.set(1, 2, 3, 4, 5)
        self.assertEqual({4: 5}, Registry.get(1, 2, 3))

        Registry.clear()
        self.assertEqual({}, Registry())
Exemple #6
0
    def test_set(self):
        Registry.set(1, 2, 3, 4, 5)
        self.assertEqual({1: {2: {3: {4: 5}}}}, Registry())

        Registry.set(1, 3, 5)
        self.assertEqual({1: {2: {3: {4: 5}}, 3: 5}}, Registry())