bh@250: # Copyright (C) 2007, 2008, 2009 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@58: import stat bh@12: import unittest bh@12: from datetime import datetime bh@12: bh@250: from treepkg.status import RevisionStatus, Status, EnumFieldDesc, \ bh@250: StringFieldDesc, DateFieldDesc bh@250: from treepkg.util import writefile bh@12: bh@187: from filesupport import FileTestMixin bh@12: bh@12: bh@12: bh@187: class TestStatus(unittest.TestCase, FileTestMixin): bh@12: bh@12: def setUp(self): bh@187: self.filename = self.temp_file_name("status") 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@58: def test_status_file_permissions(self): bh@58: status = RevisionStatus(self.filename) bh@58: status.error() bh@58: bh@58: mode = os.stat(self.filename).st_mode bh@58: self.assertEquals(stat.S_IMODE(mode) & 0444, 0444) bh@58: 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@259: self.assertEquals(status.tags, "") 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") bh@250: bh@250: def test_serialize(self): bh@250: class TestStatus(Status): bh@250: _magic = "TestStatus 1.0\n" bh@250: bh@250: status = EnumFieldDesc() bh@250: status.add("not_running", "Process is not running", bh@250: default=True) bh@250: status.add("running", "Process is running") bh@250: status.add("error", "An error occurred") bh@250: bh@250: start = DateFieldDesc(default=None) bh@250: bh@250: name = StringFieldDesc(default="fred") bh@250: bh@250: status = TestStatus(None) bh@250: status.running() bh@250: status.start = datetime(2007, 3, 9, 17, 32, 55) bh@250: status.name = "Dave" bh@250: bh@250: self.assertEquals(sorted(status.serialize()), bh@250: ["TestStatus 1.0\n", bh@250: "name: Dave\n", bh@250: "start: 2007-03-09 17:32:55\n", bh@250: "status: running\n"]) bh@250: bh@250: def test_deserialize(self): bh@250: class TestStatus(Status): bh@250: _magic = "TestStatus 1.0\n" bh@250: bh@250: status = EnumFieldDesc() bh@250: status.add("not_running", "Process is not running", bh@250: default=True) bh@250: status.add("running", "Process is running") bh@250: status.add("error", "An error occurred") bh@250: bh@250: start = DateFieldDesc(default=None) bh@250: bh@250: name = StringFieldDesc(default="fred") bh@250: bh@250: status = TestStatus(None) bh@250: status.deserialize(iter(["TestStatus 1.0\n", bh@250: "name: Dave\n", bh@250: "start: 2007-03-09 17:32:55\n", bh@250: "status: running\n"])) bh@250: self.assertEquals(status.status.name, "running") bh@250: self.assertEquals(status.start, datetime(2007, 3, 9, 17, 32, 55)) bh@250: self.assertEquals(status.name, "Dave")