Best Python code snippet using Airtest
files.py
Source:files.py  
...55        if func is None:56            func = self.assertRegexpMatches57        return func(*args, **kwargs)58    def test_tag(self):59        local = self.thumbnailer.get_thumbnail({'size': (100, 100)})60        remote = self.remote_thumbnailer.get_thumbnail({'size': (100, 100)})61        self.assertEqual(62            local.tag(), '<img alt="" height="75" src="%s" width="100" '63            '/>' % local.url)64        self.assertEqual(65            local.tag(alt='A & B'), '<img alt="A & B" height="75" '66            'src="%s" width="100" />' % local.url)67        # Can turn off dimensions.68        self.assertEqual(69            remote.tag(use_size=False), '<img alt="" src="%s" />' % remote.url)70        # Even a remotely generated thumbnail has the dimensions cached if it71        # was just created.72        self.assertEqual(73            remote.tag(),74            '<img alt="" height="75" src="%s" width="100" />' % remote.url)75        # Future requests to thumbnails on remote storage don't get76        # dimensions...77        remote = self.remote_thumbnailer.get_thumbnail({'size': (100, 100)})78        self.assertEqual(79            remote.tag(), '<img alt="" src="%s" />' % remote.url)80        # ...unless explicitly requested.81        self.assertEqual(82            remote.tag(use_size=True),83            '<img alt="" height="75" src="%s" width="100" />' % remote.url)84        # All other arguments are passed through as attributes.85        self.assertEqual(86            local.tag(**{'rel': 'A&B', 'class': 'fish'}),87            '<img alt="" class="fish" height="75" rel="A&B" '88            'src="%s" width="100" />' % local.url)89    def test_tag_cached_dimensions(self):90        settings.THUMBNAIL_CACHE_DIMENSIONS = True91        self.remote_thumbnailer.get_thumbnail({'size': (100, 100)})92        # Look up thumbnail again to ensure dimensions are a *really* cached.93        remote = self.remote_thumbnailer.get_thumbnail({'size': (100, 100)})94        self.assertEqual(95            remote.tag(),96            '<img alt="" height="75" src="%s" width="100" />' % remote.url)97    def test_transparent_thumbnailing(self):98        thumb_file = self.thumbnailer.get_thumbnail(99            {'size': (100, 100)})100        thumb_file.seek(0)101        thumb = Image.open(thumb_file)102        self.assertFalse(103            utils.is_transparent(thumb),104            "%s shouldn't be transparent." % thumb_file.name)105        thumb_file = self.transparent_thumbnailer.get_thumbnail(106            {'size': (100, 100)})107        thumb_file.seek(0)108        thumb = Image.open(thumb_file)109        self.assertTrue(110            utils.is_transparent(thumb),111            "%s should be transparent." % thumb_file.name)112        thumb_file = self.transparent_greyscale_thumbnailer.get_thumbnail(113            {'size': (100, 100)})114        thumb_file.seek(0)115        thumb = Image.open(thumb_file)116        self.assertTrue(117            utils.is_transparent(thumb),118            "%s should be transparent." % thumb_file.name)119    def test_missing_thumb(self):120        opts = {'size': (100, 100)}121        thumb = self.thumbnailer.get_thumbnail(opts)122        thumb_cache = self.thumbnailer.get_thumbnail_cache(123            thumbnail_name=thumb.name)124        thumb_cache.delete()125        thumb.storage.delete(thumb.name)126        self.thumbnailer.get_thumbnail(opts)127    def test_missing_thumb_from_storage(self):128        opts = {'size': (100, 100)}129        thumb = self.thumbnailer.get_thumbnail(opts)130        thumb.storage.delete(thumb.name)131        new_thumb = self.thumbnailer.get_thumbnail(opts)132        self.assertEqual(thumb.name, new_thumb.name)133        self.assertTrue(thumb.storage.exists(new_thumb.name))134    def test_missing_remote_thumb(self):135        opts = {'size': (100, 100)}136        thumb = self.remote_thumbnailer.get_thumbnail(opts)137        thumb_cache = self.remote_thumbnailer.get_thumbnail_cache(138            thumbnail_name=thumb.name)139        thumb_cache.delete()140        thumb.storage.delete(thumb.name)141        self.remote_thumbnailer.get_thumbnail(opts)142    def test_missing_source(self):143        opts = {'size': (100, 100)}144        self.storage.delete(self.thumbnailer.name)145        self.assertRaises(146            exceptions.InvalidImageFormatError,147            self.thumbnailer.get_thumbnail, opts)148    def test_extensions(self):149        self.ext_thumbnailer.thumbnail_extension = 'png'150        thumb = self.ext_thumbnailer.get_thumbnail({'size': (100, 100)})151        self.assertEqual(path.splitext(thumb.name)[1], '.png')152        self.ext_thumbnailer.thumbnail_preserve_extensions = ('foo',)153        thumb = self.ext_thumbnailer.get_thumbnail({'size': (100, 100)})154        self.assertEqual(path.splitext(thumb.name)[1], '.png')155        self.ext_thumbnailer.thumbnail_preserve_extensions = True156        thumb = self.ext_thumbnailer.get_thumbnail({'size': (100, 100)})157        self.assertEqual(path.splitext(thumb.name)[1], '.jpg')158        self.ext_thumbnailer.thumbnail_preserve_extensions = ('foo', 'jpg')159        thumb = self.ext_thumbnailer.get_thumbnail({'size': (100, 100)})160        self.assertEqual(path.splitext(thumb.name)[1], '.jpg')161    def test_high_resolution(self):162        self.ext_thumbnailer.thumbnail_high_resolution = True163        thumb = self.ext_thumbnailer.get_thumbnail({'size': (100, 100)})164        base, ext = path.splitext(thumb.path)165        hires_thumb_file = ''.join([base + '@2x', ext])166        self.assertTrue(path.isfile(hires_thumb_file))167        thumb = Image.open(hires_thumb_file)168        self.assertEqual(thumb.size, (200, 150))169    def test_subsampling(self):170        samplings = {171            0: (1, 1, 1, 1, 1, 1),172            1: (2, 1, 1, 1, 1, 1),173            2: (2, 2, 1, 1, 1, 1),174        }175        thumb = self.ext_thumbnailer.get_thumbnail({'size': (100, 100)})176        im = Image.open(thumb.path)177        self.assertNotIn('ss', thumb.name)178        sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]179        self.assertEqual(sampling, samplings[2])180        thumb = self.ext_thumbnailer.get_thumbnail(181            {'size': (100, 100), 'subsampling': 1})182        im = Image.open(thumb.path)183        self.assertIn('ss1', thumb.name)184        sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]185        self.assertEqual(sampling, samplings[1])186        thumb = self.ext_thumbnailer.get_thumbnail(187            {'size': (100, 100), 'subsampling': 0})188        im = Image.open(thumb.path)189        self.assertIn('ss0', thumb.name)190        sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]191        self.assertEqual(sampling, samplings[0])192    def test_default_subsampling(self):193        settings.THUMBNAIL_DEFAULT_OPTIONS = {'subsampling': 1}194        thumb = self.ext_thumbnailer.get_thumbnail({'size': (100, 100)})195        im = Image.open(thumb.path)196        self.assertIn('ss1', thumb.name)197        sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]198        self.assertEqual(sampling, (2, 1, 1, 1, 1, 1))199    def test_high_resolution_force_off(self):200        self.ext_thumbnailer.thumbnail_high_resolution = True201        thumb = self.ext_thumbnailer.get_thumbnail(202            {'size': (100, 100), 'HIGH_RESOLUTION': False})203        base, ext = path.splitext(thumb.path)204        hires_thumb_file = ''.join([base + '@2x', ext])205        self.assertFalse(path.exists(hires_thumb_file))206    def test_high_resolution_force(self):207        thumb = self.ext_thumbnailer.get_thumbnail(208            {'size': (100, 100), 'HIGH_RESOLUTION': True})209        base, ext = path.splitext(thumb.path)210        hires_thumb_file = ''.join([base + '@2x', ext])211        self.assertTrue(path.isfile(hires_thumb_file))212        thumb = Image.open(hires_thumb_file)213        self.assertEqual(thumb.size, (200, 150))214    def test_highres_infix(self):215        self.ext_thumbnailer.thumbnail_high_resolution = True216        self.ext_thumbnailer.thumbnail_highres_infix = '_2x'217        thumb = self.ext_thumbnailer.get_thumbnail({'size': (100, 100)})218        base, ext = path.splitext(thumb.path)219        hires_thumb_file = ''.join([base + '_2x', ext])220        self.assertTrue(path.isfile(hires_thumb_file))221        thumb = Image.open(hires_thumb_file)222        self.assertEqual(thumb.size, (200, 150))223    @unittest.skipIf(224        'easy_thumbnails.optimize' not in settings.INSTALLED_APPS,225        'optimize app not installed')226    @unittest.skipIf(LogCapture is None, 'testfixtures not installed')227    def test_postprocessor(self):228        """use a mock image optimizing post processor doing nothing"""229        settings.THUMBNAIL_OPTIMIZE_COMMAND = {230            'png': 'easy_thumbnails/tests/mockoptim.py {filename}'}231        with LogCapture() as logcap:232            self.ext_thumbnailer.thumbnail_extension = 'png'233            self.ext_thumbnailer.get_thumbnail({'size': (10, 10)})234            actual = tuple(logcap.actual())[0]235            self.assertEqual(actual[0], 'easy_thumbnails.optimize')236            self.assertEqual(actual[1], 'INFO')237            self.assertRegex(238                actual[2],239                '^easy_thumbnails/tests/mockoptim.py [^ ]+ returned nothing$')240    @unittest.skipIf(241        'easy_thumbnails.optimize' not in settings.INSTALLED_APPS,242        'optimize app not installed')243    @unittest.skipIf(LogCapture is None, 'testfixtures not installed')244    def test_postprocessor_fail(self):245        """use a mock image optimizing post processor doing nothing"""246        settings.THUMBNAIL_OPTIMIZE_COMMAND = {247            'png': 'easy_thumbnails/tests/mockoptim_fail.py {filename}'}248        with LogCapture() as logcap:249            self.ext_thumbnailer.thumbnail_extension = 'png'250            self.ext_thumbnailer.get_thumbnail({'size': (10, 10)})251            actual = tuple(logcap.actual())[0]252            self.assertEqual(actual[0], 'easy_thumbnails.optimize')253            self.assertEqual(actual[1], 'ERROR')254            self.assertRegex(255                actual[2], r'^Command\ .+returned non-zero exit status 1$')256    def test_USE_TZ(self):257        settings.USE_TZ = True258        self.thumbnailer.get_thumbnail({'size': (10, 20)})259        settings.USE_TZ = False260        self.thumbnailer.get_thumbnail({'size': (20, 40)})261    def test_thumbnailfile_options(self):262        opts = {'size': (50, 50), 'crop': True, 'upscale': True}263        thumb = self.thumbnailer.get_thumbnail(opts)264        self.assertEqual(thumb.thumbnail_options, ThumbnailOptions(opts))265    def test_get_thumbnail_name(self):266        opts = {267            'size': (50, 50), 'crop': 'smart', 'upscale': True,268            'target': (10, 10)}269        self.assertEqual(270            self.thumbnailer.get_thumbnail_name(opts),271            'test.jpg.50x50_q85_crop-smart_target-10,10_upscale.jpg')272    def test_default_options_setting(self):273        settings.THUMBNAIL_DEFAULT_OPTIONS = {'crop': True}274        opts = {'size': (50, 50)}275        thumb = self.thumbnailer.get_thumbnail(opts)276        self.assertEqual((thumb.width, thumb.height), (50, 50))277    def test_dimensions_of_cached_image(self):278        opts = {'size': (50, 50)}279        thumb = self.thumbnailer.get_thumbnail(opts)280        self.assertEqual((thumb.width, thumb.height), (50, 38))281        # Now the thumb has been created, check that retrieving this still282        # gives access to the dimensions.283        thumb = self.thumbnailer.get_thumbnail(opts)284        self.assertEqual((thumb.width, thumb.height), (50, 38))285    def test_cached_dimensions_of_cached_image(self):286        settings.THUMBNAIL_CACHE_DIMENSIONS = True287        opts = {'size': (50, 50)}288        thumb = self.thumbnailer.get_thumbnail(opts)289        self.assertEqual((thumb.width, thumb.height), (50, 38))290        # Now the thumb has been created, check that dimesions are in the291        # database.292        dimensions = models.ThumbnailDimensions.objects.all()[0]293        self.assertEqual(294            (thumb.width, thumb.height),295            (dimensions.width, dimensions.height))296    def test_remote_cached_dimensions_queries(self):297        settings.THUMBNAIL_CACHE_DIMENSIONS = True298        opts = {'size': (50, 50)}299        thumb = self.remote_thumbnailer.get_thumbnail(opts)300        thumb.height   # Trigger dimension caching.301        # Get thumb again (which now has cached dimensions).302        thumb = self.remote_thumbnailer.get_thumbnail(opts)303        with self.assertNumQueries(0):304            self.assertEqual(thumb.width, 50)305    def test_add_dimension_cache(self):306        settings.THUMBNAIL_CACHE_DIMENSIONS = True307        opts = {'size': (50, 50)}308        thumb = self.thumbnailer.get_thumbnail(opts)309        self.assertEqual((thumb.width, thumb.height), (50, 38))310        # Delete the created dimensions.311        models.ThumbnailDimensions.objects.all().delete()312        # Now access the thumbnail again.313        thumb = self.thumbnailer.get_thumbnail(opts)314        self.assertEqual(models.ThumbnailDimensions.objects.count(), 0)315        thumb.height316        dimensions = models.ThumbnailDimensions.objects.get()317        # and make sure they match when fetched again.318        thumb = self.thumbnailer.get_thumbnail(opts)319        self.assertEqual(320            (thumb.width, thumb.height),321            (dimensions.width, dimensions.height))322    def test_thumbnail_created_signal(self):323        def signal_handler(sender, **kwargs):324            sender.signal_received = True325        signals.thumbnail_created.connect(signal_handler)326        try:327            thumb = self.thumbnailer.get_thumbnail({'size': (10, 20)})328            self.assertTrue(hasattr(thumb, 'signal_received'))329        finally:330            signals.thumbnail_created.disconnect(signal_handler)331    def test_passive_thumbnailer(self):332        options = {'size': (10, 10)}333        # Explicitly using the generate=False option on get_thumbnail won't334        # generate a missing thumb.335        thumb = self.thumbnailer.get_thumbnail(options, generate=False)336        self.assertEqual(thumb, None)337        # If the thumbnailer has generate=False, get_thumbnail won't generate a338        # missing thumb by default.339        self.thumbnailer.generate = False340        thumb = self.thumbnailer.get_thumbnail(options)341        self.assertEqual(thumb, None)342        # If the thumbnailer has generate=False, get_thumbnail with343        # generate=True will stiff force generation a missing thumb.344        thumb = self.thumbnailer.get_thumbnail(options, generate=True)345        self.assertTrue(thumb)346        # If the thumbnailer has generate=False, get_thumbnail will still347        # return existing thumbnails.348        thumb = self.thumbnailer.get_thumbnail(options)349        self.assertTrue(thumb)350        # Explicitly using the generate=False option on get_thumbnail will351        # still return existing thumbnails.352        thumb = self.thumbnailer.get_thumbnail(options, generate=False)353        self.assertTrue(thumb)354    def test_thumbnail_missed_signal(self):355        def signal_handler(sender, **kwargs):356            sender.missed_signal = kwargs.get('options')357        signals.thumbnail_missed.connect(signal_handler)358        try:359            # Standard generation doesn't trigger signal.360            self.thumbnailer.get_thumbnail({'size': (100, 100)})361            self.assertFalse(hasattr(self.thumbnailer, 'missed_signal'))362            # Retrieval doesn't trigger signal.363            self.thumbnailer.get_thumbnail(364                {'size': (100, 100)}, generate=False)365            self.assertFalse(hasattr(self.thumbnailer, 'missed_signal'))366            # A thumbnail miss does trigger it.367            options = {'size': (10, 20)}368            thumb = self.thumbnailer.get_thumbnail(options, generate=False)369            self.assertEqual(thumb, None)370            self.assertEqual(371                self.thumbnailer.missed_signal, ThumbnailOptions(options))372        finally:373            signals.thumbnail_created.disconnect(signal_handler)374class FakeSourceGenerator(object):375    def __init__(self, fail=False):376        self.fail = fail377    def __call__(self, source, **kwargs):378        if self.fail:379            raise ValueError("Fake source generator failed")380        return source381class EngineTest(TestCase):382    def setUp(self):...test_product_tags.py
Source:test_product_tags.py  
...7    get_product_image_thumbnail,8    get_thumbnail,9)10@override_settings(VERSATILEIMAGEFIELD_SETTINGS={"create_images_on_demand": True})11def test_get_thumbnail():12    instance = Mock()13    cropped_value = Mock(url="crop.jpg")14    thumbnail_value = Mock(url="thumb.jpg")15    instance.crop = {"10x10": cropped_value}16    instance.thumbnail = {"10x10": thumbnail_value}17    cropped = get_thumbnail(instance, 10, method="crop")18    assert cropped == cropped_value.url19    thumb = get_thumbnail(instance, 10, method="thumbnail")20    assert thumb == thumbnail_value.url21def test_get_thumbnail_no_instance(monkeypatch):22    monkeypatch.setattr(23        "saleor.product.templatetags.product_images.choose_placeholder",24        lambda x: "placeholder",25    )26    output = get_thumbnail(image_file=None, size=10, method="crop")27    assert output == static("placeholder")28def test_get_product_image_thumbnail_no_instance(monkeypatch):29    monkeypatch.setattr(30        "saleor.product.templatetags.product_images.choose_placeholder",31        lambda x: "placeholder",32    )33    output = get_product_image_thumbnail(instance=None, size=10, method="crop")34    assert output == static("placeholder")35@patch(36    "saleor.product.templatetags.product_images.AVAILABLE_SIZES",37    {38        "products": (39            "thumbnail__800x800",40            "crop__100x100",41            "crop__1000x1000",42            "crop__2000x2000",43        )44    },45)46@override_settings(VERSATILEIMAGEFIELD_SETTINGS={"create_images_on_demand": False})47def test_get_thumbnail_to_larger():48    instance = Mock()49    cropped_value = Mock(url="crop.jpg")50    instance.crop = {"1000x1000": cropped_value}51    cropped = get_thumbnail(instance, 800, method="crop")52    assert cropped == cropped_value.url53@patch(54    "saleor.product.templatetags.product_images.AVAILABLE_SIZES",55    {56        "products": (57            "crop__10x10",58            "crop__100x100",59            "crop__1000x1000",60            "crop__2000x2000",61        )62    },63)64@override_settings(VERSATILEIMAGEFIELD_SETTINGS={"create_images_on_demand": False})65def test_get_thumbnail_to_smaller():66    instance = Mock()67    cropped_value = Mock(url="crop.jpg")68    instance.crop = {"100x100": cropped_value}69    cropped = get_thumbnail(instance, 400, method="crop")70    assert cropped == cropped_value.url71@patch(72    "saleor.product.templatetags.product_images.AVAILABLE_SIZES",73    {"products": ("thumbnail__800x800",)},74)75@override_settings(76    VERSATILEIMAGEFIELD_SETTINGS={"create_images_on_demand": False},77    PLACEHOLDER_IMAGES={1080: "images/placeholder1080x1080.png"},78)79def test_get_thumbnail_no_match_by_method():80    instance = Mock()81    cropped_value = Mock(url="crop.jpg")82    instance.crop = {"1000x1000": cropped_value}83    with pytest.warns(UserWarning) as record:84        cropped = get_thumbnail(instance, 800, method="crop")85    assert len(record) == 186    assert (87        str(record[0].message)88        == "Thumbnail size crop__800x800 is not defined in settings"89        " and it won't be generated automatically"90    )91    assert cropped == static("images/placeholder1080x1080.png")92def test_choose_placeholder(settings):93    settings.PLACEHOLDER_IMAGES = {94        10: "10_placeholder",95        20: "20_placeholder",96        30: "30_placeholder",97    }98    settings.DEFAULT_PLACEHOLDER = "default_placeholder"...test_backends.py
Source:test_backends.py  
...10            'SEOThumbnailBackend')11    def test_sorl_backend(self):12        assert isinstance(backend, SEOThumbnailBackend)13    def test_filename(self):14        im = get_thumbnail(get_image('seothumb_test.png'),15                           '100x100', crop='center', quality=99)16        assert 'seothumb_test' in im.name17        assert '.png' in im.name18    def test_identical(self):19        im1 = get_thumbnail(get_image('foobar.png'), '100x100')20        im2 = get_thumbnail(get_image('foobar.png'), '100x100')21        assert im1.name == im2.name22    def test_unique_name(self):23        im1 = get_thumbnail(get_image('foobar.png'), '256x256')24        im2 = get_thumbnail(get_image('lorem.png'), '256x256')25        assert im1.name != im2.name26    def test_unique_crop(self):27        im1 = get_thumbnail(get_image('foobar.png'), '100x100')28        im2 = get_thumbnail(get_image('foobar.png'), '100x100', crop='center')29        assert im1.name != im2.name30    def test_unique_size(self):31        im1 = get_thumbnail(get_image('foobar.png'), '50x200')32        im2 = get_thumbnail(get_image('foobar.png'), '100x100')33        assert im1.name != im2.name34    def test_unique_size2(self):35        im1 = get_thumbnail(get_image('foobar.png'), 'x99')36        im2 = get_thumbnail(get_image('foobar.png'), '1000x1000')...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
