bh@12: # Copyright (C) 2007 by Intevation GmbH bh@12: # Authors: bh@12: # Bernhard Herzog bh@12: # bh@12: # This program is free software under the GPL (>=v2) bh@12: # Read the file COPYING coming with the software for details. bh@12: bh@36: """Tests for the Status classes""" bh@12: bh@12: import os bh@12: import unittest bh@12: from datetime import datetime bh@12: bh@40: from treepkg.status import RevisionStatus, Status, EnumFieldDesc bh@12: from treepkg.util import ensure_directory, writefile bh@12: bh@12: bh@12: bh@12: class TestStatus(unittest.TestCase): bh@12: bh@12: def tempfilename(self): bh@12: tempdir = os.path.join(os.path.dirname(__file__), "temp") bh@12: ensure_directory(tempdir) bh@12: return os.path.join(tempdir, self.id()) bh@12: bh@12: def setUp(self): bh@12: self.filename = self.tempfilename() bh@12: if os.path.exists(self.filename): bh@12: os.remove(self.filename) bh@12: bh@12: def test_status(self): bh@36: status = RevisionStatus(self.filename) bh@43: status.error() bh@12: bh@36: otherstatus = RevisionStatus(self.filename) bh@43: self.assertEquals(otherstatus.status.name, "error") bh@12: bh@12: def test_getting_unknown_fields(self): bh@36: status = RevisionStatus(self.filename) bh@12: self.assertRaises(AttributeError, getattr, status, "unknown_field") bh@12: bh@12: def test_setting_unknown_fields(self): bh@36: status = RevisionStatus(self.filename) bh@12: self.assertRaises(AttributeError, bh@12: setattr, status, "unknown_field", "some value") bh@12: bh@32: def test_default_values(self): bh@36: status = RevisionStatus(self.filename) bh@43: self.assertEquals(status.status.name, "unknown") bh@12: self.assertEquals(status.start, None) bh@32: self.assertEquals(status.stop, None) bh@12: bh@12: def test_date(self): bh@12: timestamp = datetime(2007, 3, 9, 17, 32, 55) bh@36: status = RevisionStatus(self.filename) bh@12: status.start = timestamp bh@12: bh@36: otherstatus = RevisionStatus(self.filename) bh@12: self.assertEquals(otherstatus.start, timestamp) bh@12: bh@12: def test_magic(self): bh@12: writefile(self.filename, bh@12: "Some other magic\nstart: 2007-03-09 17:32:55\n") bh@36: self.assertRaises(ValueError, RevisionStatus, self.filename) bh@40: bh@40: def test_status_enum(self): bh@40: bh@40: class TestStatus(Status): bh@40: status = EnumFieldDesc() bh@40: status.add("not_running", "Process is not running", bh@40: default=True) bh@40: status.add("running", "Process is running") bh@40: status.add("error", "An error occurred") bh@40: bh@40: status = TestStatus(self.filename) bh@40: status.running() bh@40: bh@40: otherstatus = TestStatus(self.filename) bh@40: self.assertEquals(otherstatus.status.name, "running")