diff test/test_status.py @ 250:b3f9cc956acc

Make the Status class usable without a filename and provide methods to (de-)serialize to and from memory to make writing tests easier.
author Bernhard Herzog <bh@intevation.de>
date Thu, 09 Apr 2009 18:20:04 +0000
parents b19bf5895a55
children 27824d3add92
line wrap: on
line diff
--- a/test/test_status.py	Mon Mar 09 16:00:44 2009 +0000
+++ b/test/test_status.py	Thu Apr 09 18:20:04 2009 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2007, 2008 by Intevation GmbH
+# Copyright (C) 2007, 2008, 2009 by Intevation GmbH
 # Authors:
 # Bernhard Herzog <bh@intevation.de>
 #
@@ -12,8 +12,9 @@
 import unittest
 from datetime import datetime
 
-from treepkg.status import RevisionStatus, Status, EnumFieldDesc
-from treepkg.util import ensure_directory, writefile
+from treepkg.status import RevisionStatus, Status, EnumFieldDesc, \
+     StringFieldDesc, DateFieldDesc
+from treepkg.util import writefile
 
 from filesupport import FileTestMixin
 
@@ -82,3 +83,51 @@
 
         otherstatus = TestStatus(self.filename)
         self.assertEquals(otherstatus.status.name, "running")
+
+    def test_serialize(self):
+        class TestStatus(Status):
+            _magic = "TestStatus 1.0\n"
+
+            status = EnumFieldDesc()
+            status.add("not_running", "Process is not running",
+                       default=True)
+            status.add("running", "Process is running")
+            status.add("error", "An error occurred")
+
+            start = DateFieldDesc(default=None)
+
+            name = StringFieldDesc(default="fred")
+
+        status = TestStatus(None)
+        status.running()
+        status.start = datetime(2007, 3, 9, 17, 32, 55)
+        status.name = "Dave"
+
+        self.assertEquals(sorted(status.serialize()),
+                          ["TestStatus 1.0\n",
+                           "name: Dave\n",
+                           "start: 2007-03-09 17:32:55\n",
+                           "status: running\n"])
+
+    def test_deserialize(self):
+        class TestStatus(Status):
+            _magic = "TestStatus 1.0\n"
+
+            status = EnumFieldDesc()
+            status.add("not_running", "Process is not running",
+                       default=True)
+            status.add("running", "Process is running")
+            status.add("error", "An error occurred")
+
+            start = DateFieldDesc(default=None)
+
+            name = StringFieldDesc(default="fred")
+
+        status = TestStatus(None)
+        status.deserialize(iter(["TestStatus 1.0\n",
+                                 "name: Dave\n",
+                                 "start: 2007-03-09 17:32:55\n",
+                                 "status: running\n"]))
+        self.assertEquals(status.status.name, "running")
+        self.assertEquals(status.start, datetime(2007, 3, 9, 17, 32, 55))
+        self.assertEquals(status.name, "Dave")
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)