import os
import unittest
from io import StringIO

import piupartslib.packagesdb


class FakeLogDB(piupartslib.packagesdb.LogDB):
    """A fake version of the LogDB class, for testing

    This version simulates filesystem actions so that there is no need
    to do actual I/O. Cleaner, although not quite as thorough.

    """

    def __init__(self):
        self.dict = {
            "pass": [],
            "fail": [],
            "untestable": [],
            "reserved": [],
            "bugged": [],
            "affected": [],
        }

    def listdir(self, dirname):
        return self.dict[dirname]

    def _parse(self, pathname):
        return os.path.dirname(pathname), os.path.basename(pathname)

    def exists(self, pathname):
        vdir, base = self._parse(pathname)
        return base in self.dict[vdir]

    def remove_file(self, pathname):
        vdir, base = self._parse(pathname)
        if base in self.dict[vdir]:
            del self.dict[vdir]

    def create(self, subdir, package, version, contents):
        return True


class PackagesDbTests(unittest.TestCase):
    def new_db(self, packages_file_contents):
        db = piupartslib.packagesdb.PackagesDB(FakeLogDB())
        db.read_packages_file(StringIO(packages_file_contents))
        return db

    def reserve(self, packages_file_contents):
        db = self.new_db(packages_file_contents)
        return db.reserve_package()

    def testNoPackages(self):
        p = self.reserve("")
        self.assertEqual(p, None)

    def testNoDeps(self):
        p = self.reserve(
            """\
Package: foo
Version: 1.0-1
"""
        )
        self.assertNotEqual(p, None)
        self.assertEqual(p["Package"], "foo")


if __name__ == "__main__":
    unittest.main()

# vi:set et ts=4 sw=4 :
