Source

pypi / tests / test_store.py

Full commit
import distutils2

from distutils2.metadata import DistributionMetadata, _ATTR2FIELD

import os
import unittest
import config
import pkg_resources
import cStringIO


import store
import trove

DATA_DIR = os.path.join(os.path.dirname(trove.__file__), "tests", "data")
CONFIG_FILE = os.path.join(DATA_DIR, "config.ini")
DUMP = os.path.join(DATA_DIR, "test_dump.sql")
DUMP_DATA = os.path.join(DATA_DIR, "test_dump-data.sql")

user1 = ("user1", "pass", "test@admin.org", None)
user2 = ("user2", "pass", "test@admin.org", None)
user3 = ("user3", "pass", "test3@admin.org", None)

_FIELD2ATTR = dict([(value,key) for key,value in _ATTR2FIELD.items()])

class TestStore ( unittest.TestCase ):

    def _convert_to_attr(self, metadata):
        """convert field name to attribute cause pypi is using field name and 
        not attribute like in distutils2
        """
        return dict([(_FIELD2ATTR[key], value) for key,value in metadata.items()]) 

    def setUp(self):
        self.store = store.Store( config.Config( CONFIG_FILE ) )
        self.store.open()
        self.store.get_cursor().execute("".join(open(DUMP).readlines()))

        self.PKG_INFO = os.path.join(DATA_DIR, 'PKG-INFO')
        self.pkg_info_metadata = DistributionMetadata(self.PKG_INFO)
        self.metadata = self._convert_to_attr(self.pkg_info_metadata)

        self.PKG_INFO2 = os.path.join(DATA_DIR, 'PKG-INFO2')
        self.pkg_info_metadata2 = DistributionMetadata(self.PKG_INFO2)
        self.metadata2 = self._convert_to_attr(self.pkg_info_metadata2)

    def tearDown(self):
        self.store.close()

    def test_store_package(self):
        package = self.store.get_package("pypi", "0.1")

        self.assertRaises(TypeError, package.values) 
        self.store.store_package("pypi", "0.1", self.metadata)

        package = self.store.get_package("pypi", "0.1")    
        assert len(package.values()) == 21

        package = self.store.get_package("pypi", "0.2")
        self.assertRaises(TypeError, package.values) 

        package = self.store.get_package("pypi", "0.1")
        package = dict(package.items())
        package = self.store.store_package("pypi", "0.1", package)
        assert package is None

        self.metadata['author'] = 'Foo'
        package = self.store.store_package("pypi", "0.1", self.metadata)

        assert package is not None

    def test_fix_ordering(self):
        self.store.store_package("pypi", "0.2", self.metadata)

        assert self.store.fix_ordering("pypi", "0.2") == 10.0

        self.store.store_package("pypi", "0.1", self.metadata)

        assert self.store.fix_ordering("pypi", "0.2") == 11.0    
        assert self.store.fix_ordering("pypi", "0.1") == 10.0

    def test_has_package(self):
        assert self.store.has_package('pypi') == 0

        self.store.store_package("pypi", "0.1", self.metadata)
        assert self.store.has_package('pypi') == 1

        self.store.store_package("pypi", "0.2", self.metadata)
        assert self.store.has_package('pypi') == 1

        self.store.store_package("PYPI2", "0.1", self.metadata)
        assert self.store.has_package('pypi2') == 0

    def test_find_package(self):
        assert len(self.store.find_package('pypi')) == 0

        self.store.store_package("pypi", "0.1", self.metadata)
        assert len(self.store.find_package('pypi')) == 1

        self.store.store_package("pypi", "0.2", self.metadata)
        assert len(self.store.find_package('pypi')) == 1

        self.store.store_package("PYPI2", "0.1", self.metadata)
        assert len(self.store.find_package('pypi2')) == 1

        self.store.store_package("PYPI", "0.3", self.metadata)
        assert len(self.store.find_package('pypi')) == 2

    def test_has_release(self):
        assert self.store.has_release("pypi", "0.1") == False

        self.store.store_package("pypi", "0.1", self.metadata)

        assert self.store.has_release("pypi", "0.1") == True
        assert self.store.has_release("pypi", "0.2") == False

        assert self.store.has_release("PYPI", "0.1") == False
        assert self.store.has_release("false package", "0.1") == False

    def test_get_cheesecake_index(self):
        """get_cheesecake_index ????"""

    def test_get_package(self):
        self.store.store_package("CLVault", "0.5", self.metadata2)
        package = self.store.get_package("CLVault", "0.5")
        package_info = package.as_dict()

        # just to be sure
        assert dict(package.items()) == package_info
        
        for key in package_info.keys():
            if self.pkg_info_metadata2.is_metadata_field(key):
                store_value = package_info.get(key)
                metad_value = self.pkg_info_metadata2.get(key)           
                if metad_value in ('UNKNOWN', []):
                    metad_value = None
                if store_value in ('UNKNOWN', [], u'{}'):
                    store_value = None
                if isinstance(metad_value,list) and \
                        isinstance(store_value, unicode):
                    store_value = store_value.strip('{}').split(',')
                self.assertEqual(store_value, metad_value, 
                                 "Invalid metadata comparison for key %s (%s != %s)" \
                                     % (key, store_value, metad_value))

    def test_get_package_url(self):
        self.store.store_package("CLVault", "0.5", self.metadata2)
        assert len(self.store.get_package_urls("CLVault")) == 1
        
        self.store.store_package("CLVault", "0.6", self.metadata2)        
        assert len(self.store.get_package_urls("CLVault")) == 2
        assert self.store.get_package_urls("CLVault2") is None
        
        url = self.store.get_package_urls("CLVault")
        url = url.pop()
        assert url == ('http://www.google.com', 'download', '0.6 download_url')

    def test_get_stable_version(self):
        assert False

    def test_get_packages(self):
        assert len(self.store.get_packages()) == 0
        
        self.store.store_package("CLVault", "0.5", self.metadata2)
        
        assert len(self.store.get_packages()) == 1
        
        self.store.store_package("CLVault", "0.6", self.metadata2)

        assert len(self.store.get_packages()) == 1

    def test_get_journal(self):
        self.store.store_package("CLVault", "0.5", self.metadata2)
        journal = self.store.get_journal("CLVault", "0.5")
        
    def test_create_user(self):        
        self.store.store_user(*user1)

        assert self.store.get_user("user1") is not None
        self.assertRaises(ValueError, self.store.store_user, *user2)

    def test_get_users(self):
        self.store.store_user(*user1)
        self.store.store_user(*user3)
        self.assertTrue(len(self.store.get_users())==2)

    def test_get_user(self):
        self.store.store_user(*user1)
        self.assertTrue('user1' in self.store.get_user("user1").values())

    def test_get_user_by_email(self):
        self.store.store_user(*user1)
        users = self.store.get_user_by_email("test@admin.org").values()
        self.assertTrue('user1' in users)

    def test_sshkeys(self):
        self.store.store_user(*user1)
        assert len(self.store.get_sshkeys('user1')) == 0

        self.store.add_sshkey('user1', "sfasfaf")
        assert len(self.store.get_sshkeys('user1')) == 1

        self.store.delete_sshkey(self.store.get_sshkeys('user1')[0].values()[0])
        assert len(self.store.get_sshkeys('user1')) == 0

    def test_get_openid(self):
        self.store.store_user(*user1)
        assert len(self.store.get_openids('user1')) == 0

        self.store.associate_openid('user1', 'openid_user1')
        assert len(self.store.get_openids('user1')) == 1

    def test_get_user_by_openid(self):
        self.store.store_user(*user1)
        assert 'user1' not in self.store.get_user_by_openid('openid_user1')

        self.store.associate_openid('user1', 'openid_user1')
        assert 'user1' in self.store.get_user_by_openid('openid_user1')

        self.store.drop_openid('openid_user1')
        assert 'user1' not in self.store.get_user_by_openid('openid_user1')
    
#     def test_openid_provider(self):
#         self.assertTrue(False)

#     def test_role(self):
#         self.asserTrue(False)

#     def test_oth(self):
#         self.assertTrue(False)

#     def test_user_package(self):
#         self.asserTrue(False)
        
#     def test_user_package_details(self):
#         self.assertTrue(False)

#     def test_trows(self):
#         self.assertTrue(False)

#     def test_tally(self):
#         self.assertTrue(False)

#     def test_browse(self):
#         self.assertTrue(False)

#     def test_file_handling(self):
#         self.assertTrue(False)

#     def test_logging(self):
#         self.assertTrue(False)
        
#     def test_upload_times(self):
#         self.assertTrue(False)

#     def test_mirror(self):
#         self.assertTrue(False)

#     def test_cookie(self):
#         self.assertTrue(False)

#     def test_session(self):
#         self.assertTrue(False)

#     def test_nonce(self):
#         self.assertTrue(False)

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