
Import what we need.

  >>> from tests import *

  >>> from smart.cache import *
  >>> from smart.pm import *
  >>> from smart.channel import *
  >>> from smart.transaction import *


Create a test environment.

  >>> class TestChannelSet(object):
  ...     def setChannels(self, channels):
  ...         pass
  ...     def isAvailable(self, channel):
  ...         return True
  >>> class TestPackageManager(PackageManager):
  ...     def commit(self, changeset, pkgpaths):
  ...         for pkg in changeset:
  ...             pkg.installed = changeset[pkg] is INSTALL
  >>> class TestPackageInfo(PackageInfo):
  ...     pass
  >>> class TestPackage(Package):
  ...     packagemanager = TestPackageManager

  >>> class TestInstalledLoader(Loader):
  ...
  ...     def __init__(self):
  ...         Loader.__init__(self)
  ...         self._installed = True
  ...
  ...     def getChannel(self):
  ...         return PackageChannel("dummy", "installed")

  >>> class TestAvailableLoader(Loader):
  ...
  ...     def getChannel(self):
  ...         return PackageChannel("dummy", "available")
  ...
  ...     def getInfo(self, pkg):
  ...         return TestPackageInfo(pkg, self)
  ...
  ...     def load(self):
  ...         pkgA = self.buildPackage(
  ...             (TestPackage, "A", "1"),
  ...             [], [], [], [])
  ...         pkgA.loaders[self] = "A"


Then, we create instances of them.

  >>> installed_loader = TestInstalledLoader()
  >>> available_loader = TestAvailableLoader()

We need a progress and a fetcher.

  >>> from smart.progress import Progress
  >>> from smart.fetcher import Fetcher
  >>> progress = Progress()
  >>> fetcher = Fetcher()


We'll also create a cache, to include these loader into.

  >>> cache = Cache()
  >>> cache.addLoader(installed_loader)
  >>> cache.addLoader(available_loader)


Loading the cache should activate the loader.

  >>> cache.load()


Create a new transaction to perform the install operation, using the
cache just built, and the install policy.

  >>> transaction = Transaction(cache, PolicyInstall)


Mark packages for installation.

  >>> pkg = cache.getPackages("A")[0]
  >>> transaction.enqueue(pkg, INSTALL)
  >>> transaction.run()

Commit the transaction.

      # fake channels being available...
  >>> ctrl._achanset = TestChannelSet()
  >>> ctrl.commitTransaction(transaction)
  True
  >>> pkg.installed
  True

Finally, verify origin.

  >>> pkgconf.getOrigin(pkg)
  'available'

Check for removal too.

  >>> transaction.enqueue(pkg, REMOVE)
  >>> transaction.run()
  >>> ctrl.commitTransaction(transaction)
  True
  >>> pkg.installed
  False
  >>> pkgconf.getOrigin(pkg)
  

vim:ft=doctest
