from django.template.defaultfilters import slugify
from django.test import TestCase

from package.models import Package, Version
from pypi.slurper import Slurper

TEST_PACKAGE_NAME = 'Django'
TEST_PACKAGE_VERSION = '1.3'
TEST_PACKAGE_REPO_NAME = 'django-uni-form'

class SlurpAllTests(TestCase):
            
    def test_get_latest_version_number(self):
        
        slurper = Slurper(TEST_PACKAGE_NAME)
        version = slurper.get_latest_version_number(TEST_PACKAGE_NAME)
        self.assertEquals(version, TEST_PACKAGE_VERSION)

    def test_get_or_create_package(self):
        
        slurper = Slurper(TEST_PACKAGE_NAME)
        version = slurper.get_latest_version_number(TEST_PACKAGE_NAME)
        package, created = slurper.get_or_create_package(TEST_PACKAGE_NAME, version)
        self.assertTrue(created)
        self.assertTrue(isinstance(package, Package))
        self.assertEquals(package.title, TEST_PACKAGE_NAME)
        self.assertEquals(package.slug, slugify(TEST_PACKAGE_NAME))

    def test_get_or_create_with_repo(self):

        slurper = Slurper(TEST_PACKAGE_REPO_NAME)
        version = slurper.get_latest_version_number(TEST_PACKAGE_REPO_NAME)        
        package, created = slurper.get_or_create_package(TEST_PACKAGE_REPO_NAME, version)
        self.assertTrue(created)        
        self.assertTrue(isinstance(package, Package))
        self.assertEquals(package.title, TEST_PACKAGE_REPO_NAME)
        self.assertEquals(package.slug, slugify(TEST_PACKAGE_REPO_NAME))


    def test_check_versions(self):
        
        slurper = Slurper(TEST_PACKAGE_REPO_NAME)        
        version = slurper.get_latest_version_number(TEST_PACKAGE_REPO_NAME)
                
        # make me a package (Actually, make me a billionare)
        slurper.get_or_create_package(TEST_PACKAGE_REPO_NAME, version)
                
        # fetch the package for testing
        package = Package.objects.get(title=TEST_PACKAGE_REPO_NAME)
        
        self.assertTrue(package.pypi_downloads > 1000)
[((15, 18, 15, 44), 'pypi.slurper.Slurper', 'Slurper', ({(15, 26, 15, 43): 'TEST_PACKAGE_NAME'}, {}), '(TEST_PACKAGE_NAME)', False, 'from pypi.slurper import Slurper\n'), ((21, 18, 21, 44), 'pypi.slurper.Slurper', 'Slurper', ({(21, 26, 21, 43): 'TEST_PACKAGE_NAME'}, {}), '(TEST_PACKAGE_NAME)', False, 'from pypi.slurper import Slurper\n'), ((31, 18, 31, 49), 'pypi.slurper.Slurper', 'Slurper', ({(31, 26, 31, 48): 'TEST_PACKAGE_REPO_NAME'}, {}), '(TEST_PACKAGE_REPO_NAME)', False, 'from pypi.slurper import Slurper\n'), ((42, 18, 42, 49), 'pypi.slurper.Slurper', 'Slurper', ({(42, 26, 42, 48): 'TEST_PACKAGE_REPO_NAME'}, {}), '(TEST_PACKAGE_REPO_NAME)', False, 'from pypi.slurper import Slurper\n'), ((49, 18, 49, 67), 'package.models.Package.objects.get', 'Package.objects.get', (), '', False, 'from package.models import Package, Version\n'), ((27, 40, 27, 66), 'django.template.defaultfilters.slugify', 'slugify', ({(27, 48, 27, 65): 'TEST_PACKAGE_NAME'}, {}), '(TEST_PACKAGE_NAME)', False, 'from django.template.defaultfilters import slugify\n'), ((37, 40, 37, 71), 'django.template.defaultfilters.slugify', 'slugify', ({(37, 48, 37, 70): 'TEST_PACKAGE_REPO_NAME'}, {}), '(TEST_PACKAGE_REPO_NAME)', False, 'from django.template.defaultfilters import slugify\n')]