# Path: oembed/tests/tests/base.py
# class BaseOEmbedTestCase(TestCase):
#     fixtures = ['oembed_testdata.json']
#     urls = 'oembed.tests.urls'
#     
#     # third party providers (StoredProvider)
#     flickr_url = 'http://www.flickr.com/photos/neilkrug/2554073003/'
#     youtube_url = 'http://www.youtube.com/watch?v=nda_OSWeyn8'
#     
#     # django providers (DjangoProvider and DjangoDatebasedProvider)
#     category_url = 'http://example.com/testapp/category/1/'
#     blog_url = 'http://example.com/testapp/blog/2010/may/01/entry-1/'
#     rich_url = 'http://example.com/testapp/rich/rich-one/'
#     
#     category_embed = '<img src="http://example.com/media/images/test_image1_800x600.jpg" alt="Category 1" ></img>'
#     
#     def setUp(self):
#         "Set up test environment"
#         # load up all the providers and register the test-only provider
#         oembed.autodiscover()
#         
#         # refresh the attribute-cached time the db providers were last updated
#         oembed.site._db_updated = None
#         
#         self.storage = DummyMemoryStorage()
#         
#         # monkeypatch default_storage
#         self.orig_default_storage = storage.default_storage
#         storage.default_storage = self.storage
#         
#         # swap media root & media url
#         self.media_root, self.media_url = settings.MEDIA_ROOT, settings.MEDIA_URL
#         settings.MEDIA_ROOT = MEDIA_ROOT
#         settings.MEDIA_URL = MEDIA_URL
# 
#         # swap out template dirs
#         self.template_dirs = settings.TEMPLATE_DIRS
#         cur_dir = os.path.dirname(__file__)
#         settings.TEMPLATE_DIRS = [os.path.join(os.path.dirname(cur_dir), 'templates')]
#         
#         # swap out file storage backend
#         self.orig_file_storage = settings.DEFAULT_FILE_STORAGE
#         settings.DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE
# 
#         # create 2 images for testing
#         test_image = Image.new('CMYK', (1024, 768), (255, 255, 255, 255))
#         self.test_img_buffer = StringIO()
#         test_image.save(self.test_img_buffer, 'JPEG')
#         
#         self.test_img_file = ContentFile(self.test_img_buffer.getvalue())
#         self.test_img_location = 'images/test_image1.jpg'
#         storage.default_storage.save(self.test_img_location, self.test_img_file)
# 
#     def tearDown(self):
#         settings.MEDIA_ROOT = self.media_root
#         settings.MEDIA_URL = self.media_url
#         settings.TEMPLATE_DIRS = self.template_dirs
#         settings.DEFAULT_FILE_STORAGE = self.orig_file_storage
#         storage.default_storage = self.orig_default_storage
# 
#     def _sort_by_pk(self, list_or_qs):
#         # decorate, sort, undecorate using the pk of the items
#         # in the list or queryset
#         annotated = [(item.pk, item) for item in list_or_qs]
#         annotated.sort()
#         return map(lambda item_tuple: item_tuple[1], annotated)
#     
#     def assertQuerysetEqual(self, a, b):
#         # assert list or queryset a is the same as list or queryset b
#         return self.assertEqual(self._sort_by_pk(a), self._sort_by_pk(b))


from django.contrib.sites.models import Site
from oembed.tests.tests.base import BaseOEmbedTestCase
from oembed.utils import size_to_nearest, relative_to_full, load_class, cleaned_sites, scale
    def test_size_to_nearest_defaults(self):
        self.assertEqual((800, 600), size_to_nearest(800, 600))
        self.assertEqual((800, 600), size_to_nearest(850, 650))

        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))

        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))
        
        self.assertEqual((200, 200), size_to_nearest(400, 250, force_fit=True))

    def test_relative_to_full(self):
        self.assertEqual('http://test.com/a/b/', relative_to_full('/a/b/', 'http://test.com'))
        self.assertEqual('http://test.com/a/b/', relative_to_full('/a/b/', 'http://test.com/c/d/?cruft'))
        self.assertEqual('http://test.com/a/b/', relative_to_full('http://test.com/a/b/', 'http://test.com'))
        self.assertEqual('http://blah.com/a/b/', relative_to_full('http://blah.com/a/b/', 'http://test.com'))
        self.assertEqual('/a/b/', relative_to_full('/a/b/', ''))
    
    def test_load_class(self):
        parser_class = load_class('oembed.parsers.html.HTMLParser')
        self.assertEqual(parser_class.__name__, 'HTMLParser')
        self.assertEqual(parser_class.__module__, 'oembed.parsers.html')
    
    def test_cleaned_sites(self):
        sites = Site.objects.all()
        cleaned = cleaned_sites()
        example = cleaned[1] # example site
        self.assertEquals(example[1], 'example.com')
        self.assertEquals(example[2], 'http://example.com')

# Path: oembed/tests/tests/base.py
# class BaseOEmbedTestCase(TestCase):
#     fixtures = ['oembed_testdata.json']
#     urls = 'oembed.tests.urls'
#     
#     # third party providers (StoredProvider)
#     flickr_url = 'http://www.flickr.com/photos/neilkrug/2554073003/'
#     youtube_url = 'http://www.youtube.com/watch?v=nda_OSWeyn8'
#     
#     # django providers (DjangoProvider and DjangoDatebasedProvider)
#     category_url = 'http://example.com/testapp/category/1/'
#     blog_url = 'http://example.com/testapp/blog/2010/may/01/entry-1/'
#     rich_url = 'http://example.com/testapp/rich/rich-one/'
#     
#     category_embed = '<img src="http://example.com/media/images/test_image1_800x600.jpg" alt="Category 1" ></img>'
#     
#     def setUp(self):
#         "Set up test environment"
#         # load up all the providers and register the test-only provider
#         oembed.autodiscover()
#         
#         # refresh the attribute-cached time the db providers were last updated
#         oembed.site._db_updated = None
#         
#         self.storage = DummyMemoryStorage()
#         
#         # monkeypatch default_storage
#         self.orig_default_storage = storage.default_storage
#         storage.default_storage = self.storage
#         
#         # swap media root & media url
#         self.media_root, self.media_url = settings.MEDIA_ROOT, settings.MEDIA_URL
#         settings.MEDIA_ROOT = MEDIA_ROOT
#         settings.MEDIA_URL = MEDIA_URL
# 
#         # swap out template dirs
#         self.template_dirs = settings.TEMPLATE_DIRS
#         cur_dir = os.path.dirname(__file__)
#         settings.TEMPLATE_DIRS = [os.path.join(os.path.dirname(cur_dir), 'templates')]
#         
#         # swap out file storage backend
#         self.orig_file_storage = settings.DEFAULT_FILE_STORAGE
#         settings.DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE
# 
#         # create 2 images for testing
#         test_image = Image.new('CMYK', (1024, 768), (255, 255, 255, 255))
#         self.test_img_buffer = StringIO()
#         test_image.save(self.test_img_buffer, 'JPEG')
#         
#         self.test_img_file = ContentFile(self.test_img_buffer.getvalue())
#         self.test_img_location = 'images/test_image1.jpg'
#         storage.default_storage.save(self.test_img_location, self.test_img_file)
# 
#     def tearDown(self):
#         settings.MEDIA_ROOT = self.media_root
#         settings.MEDIA_URL = self.media_url
#         settings.TEMPLATE_DIRS = self.template_dirs
#         settings.DEFAULT_FILE_STORAGE = self.orig_file_storage
#         storage.default_storage = self.orig_default_storage
# 
#     def _sort_by_pk(self, list_or_qs):
#         # decorate, sort, undecorate using the pk of the items
#         # in the list or queryset
#         annotated = [(item.pk, item) for item in list_or_qs]
#         annotated.sort()
#         return map(lambda item_tuple: item_tuple[1], annotated)
#     
#     def assertQuerysetEqual(self, a, b):
#         # assert list or queryset a is the same as list or queryset b
#         return self.assertEqual(self._sort_by_pk(a), self._sort_by_pk(b))


# Path: oembed/tests/tests/utils.py
from django.contrib.sites.models import Site
from oembed.tests.tests.base import BaseOEmbedTestCase
from oembed.utils import size_to_nearest, relative_to_full, load_class, cleaned_sites, scale

    def test_size_to_nearest_defaults(self):
        self.assertEqual((800, 600), size_to_nearest(800, 600))
        self.assertEqual((800, 600), size_to_nearest(850, 650))

        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))

        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))
        
        self.assertEqual((200, 200), size_to_nearest(400, 250, force_fit=True))

    def test_relative_to_full(self):
        self.assertEqual('http://test.com/a/b/', relative_to_full('/a/b/', 'http://test.com'))
        self.assertEqual('http://test.com/a/b/', relative_to_full('/a/b/', 'http://test.com/c/d/?cruft'))
        self.assertEqual('http://test.com/a/b/', relative_to_full('http://test.com/a/b/', 'http://test.com'))
        self.assertEqual('http://blah.com/a/b/', relative_to_full('http://blah.com/a/b/', 'http://test.com'))
        self.assertEqual('/a/b/', relative_to_full('/a/b/', ''))
    
    def test_load_class(self):
        parser_class = load_class('oembed.parsers.html.HTMLParser')
        self.assertEqual(parser_class.__name__, 'HTMLParser')
        self.assertEqual(parser_class.__module__, 'oembed.parsers.html')
    
    def test_cleaned_sites(self):
        sites = Site.objects.all()
        cleaned = cleaned_sites()
        example = cleaned[1] # example site
        self.assertEquals(example[1], 'example.com')
        self.assertEquals(example[2], 'http://example.com')

        self.assertEquals(example[0], 'https?:\/\/(?:www[^\.]*\.)?example.com')