Пример #1
0
class TestFromPitzdir(unittest.TestCase):

    def setUp(self):

        self.p = Project(pathname='/tmp')
        self.p.to_yaml_file()
        self.p.to_pickle()

    def tearDown(self):

        for f in glob.glob('/tmp/*.yaml'):
            os.unlink(f)

        if os.path.isfile('/tmp/project.pickle'):
            os.unlink('/tmp/project.pickle')

    def test_fresh_pickle(self):
        """
        Verify we use the pickle when we can.
        """

        p = Project.from_pitzdir('/tmp')
        assert p.loaded_from == 'pickle', p.loaded_from

    def test_stale_pickle(self):
        """
        Verify we use the yaml files when the pickle is too old.
        """

        stat = os.stat('/tmp/project.pickle')

        os.utime('/tmp/project.pickle',
            (stat.st_atime-1, stat.st_mtime-1))

        print("pickle file: %s"
            % os.stat('/tmp/project.pickle').st_mtime)

        print("newest yaml file: %s"
            % max([os.stat(f).st_mtime for f in glob.glob('/tmp/*.yaml')]))

        p = Project.from_pitzdir('/tmp')
        assert p.loaded_from == 'yaml', p.loaded_from

    def test_from_yaml_files(self):
        """
        Verify we can use the yaml files when no pickle exists.
        """

        os.unlink('/tmp/project.pickle')

        p = Project.from_pitzdir('/tmp')
        assert p.loaded_from == 'yaml', p.loaded_from
Пример #2
0
class TestPicklingProject(unittest.TestCase):

    def setUp(self):

        self.p = Project()
        self.c = Entity(self.p, title="c")
        self.e = Entity(self.p, title="t", c=self.c)

    def tearDown(self):
        self.c.self_destruct(self.p)
        self.e.self_destruct(self.p)

        if os.path.exists('/tmp/project.pickle'):
            os.remove('/tmp/project.pickle')

    def test_to_pickle1(self):

        self.assertRaises(
            ValueError,
            self.p.to_pickle)

    def test_to_pickle2(self):

        self.p.to_pickle('/tmp')
        assert os.path.exists('/tmp/project.pickle')

    def test_unpickle(self):

        assert not os.path.exists('/tmp/project.pickle')
        self.p.to_pickle('/tmp')
        assert os.path.exists('/tmp/project.pickle')
        new_p = Project.from_pickle('/tmp/project.pickle')
        assert self.p.length == new_p.length
        assert new_p.length
        for e in new_p:
            assert e.project