From 5ba62f2e3d3792777ceddbef29728afdbfaf0e95 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Fri, 22 Nov 2013 19:44:35 +0100 Subject: [PATCH] basic json object loading --- src/back/kite/lockfile.py | 2 +- src/back/tests/test_jsonfile.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/back/tests/test_jsonfile.py diff --git a/src/back/kite/lockfile.py b/src/back/kite/lockfile.py index 750a2e8..d94b2d0 100644 --- a/src/back/kite/lockfile.py +++ b/src/back/kite/lockfile.py @@ -4,7 +4,7 @@ class LockingException(Exception): pass class FileLock(object): - def __init__(self, name, mode): + def __init__(self, name, mode="r+w"): self.name = name self.fd = open(name, mode) diff --git a/src/back/tests/test_jsonfile.py b/src/back/tests/test_jsonfile.py new file mode 100644 index 0000000..cf3104f --- /dev/null +++ b/src/back/tests/test_jsonfile.py @@ -0,0 +1,21 @@ +import unittest +import tempfile +import fcntl +import os +import kite.jsonfile +from kite.jsonfile import JsonFile + +class TestJsonfile(unittest.TestCase): + def test_locked_loading(self): + fd, path = tempfile.mkstemp() + fd = os.fdopen(fd, "w") + fd.write("{}") + fd.close() + json_file = JsonFile(path) + self.assertEqual(json_file.data, {}, msg="Value is not deserialized correctly") + + fd = open(path, "w+") + fd.write("[]") + fd.close() + json_file.refresh() + self.assertEqual(json_file.data, [], msg="Value is not refreshed correctly") -- 2.43.0