Skip to content

Commit

Permalink
Mutable repository
Browse files Browse the repository at this point in the history
  • Loading branch information
mmulich committed Nov 15, 2011
1 parent 2db8a92 commit a04a664
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
16 changes: 13 additions & 3 deletions pkgmeta/repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import os
from collections import Mapping
from collections import MutableMapping

from pkgmeta.exceptions import ReleaseNotFound
from pkgmeta.config import RepositoryConfig
Expand All @@ -10,14 +10,14 @@
__all__ = ('Repository',)


class Repository(Mapping):
class Repository(MutableMapping):
"""A repository of Python distribution metadata. The structure is
organized by package name then by release (version)."""

def __init__(self, config):
if not isinstance(config, RepositoryConfig):
raise TypeError("Expected a "
"pkgmeta.config.RepositoryConfig object.")
", pkgmeta.config.RepositoryConfig object.")
self.config = config
self.storage = self.config.storage # For convenience

Expand All @@ -37,6 +37,16 @@ def __iter__(self):
def __len__(self):
return len(self.storage)

def __setitem__(self, key, value):
if not isinstance(value, ReleaseSet):
raise TypeError("Expected a pkgmeta.releases.ReleaseSet, "
"received '%s'" % type(value))
self.storage[key] = value

def __delitem__(self, key):
raise Exception("Can't remove items from a read-only repository")


# ############## #
# Public API #
# ############## #
Expand Down
46 changes: 44 additions & 2 deletions pkgmeta/tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pkgmeta.tests import unittest
from pkgmeta.tests.base import BaseTestCase
from pkgmeta.tests.mock_metadata import ALL_DISTS, SOLARCAL
from pkgmeta.tests.utils import populate_repo
from pkgmeta.tests.utils import make_metadata, populate_repo


class RepositoryTestCase(BaseTestCase):
Expand Down Expand Up @@ -31,7 +31,8 @@ def make_one(self, location=None):
location = self.repo_directory
from pkgmeta.config import RepositoryConfig
from pkgmeta.storage import FS_STORAGE_TYPE
config = RepositoryConfig('', type=FS_STORAGE_TYPE, location=location)
config = RepositoryConfig('repo', type=FS_STORAGE_TYPE,
location=location)
return self.target_cls(config)

def test_init_without_config(self):
Expand Down Expand Up @@ -78,3 +79,44 @@ def test_iteration(self):
def test_len(self):
repo = self.make_one()
self.assertEqual(len(repo), len(ALL_DISTS))


class RepositoryMutationTestCase(unittest.TestCase):
"""Test for the mutation of a repository. Specifically checking for
addition and deletion."""

@property
def target_cls(self):
from pkgmeta.repository import Repository
return Repository

def make_releaseset(self, common, versions, extras={}):
from pkgmeta.metadata import Metadata
from pkgmeta.releases import ReleaseSet
return ReleaseSet(make_metadata(common, versions, extras, cls=Metadata))

def make_one(self):
from pkgmeta.config import RepositoryConfig
from pkgmeta.storage import RUNTIME_STORAGE_TYPE
config = RepositoryConfig('repo')
return self.target_cls(config)

def test_setting_releaseset(self):
repo = self.make_one()
#: Make sure the repo is empty to start
self.assertEqual(len(repo), 0)
#: Set a releaseset
release = self.make_releaseset(*SOLARCAL)
repo[release.name] = release
self.assertEqual(len(repo), 1)
self.assertIn(SOLARCAL[0]['name'], repo)

def test_setting_with_invalid_type(self):
repo = self.make_one()
#: Make sure the repo is empty to start
self.assertEqual(len(repo), 0)
#: Set a releaseset
release = self.make_releaseset(*SOLARCAL)
repo[release.name] = release
self.assertEqual(len(repo), 1)
self.assertIn(SOLARCAL[0]['name'], repo)

0 comments on commit a04a664

Please sign in to comment.