How to use fake_open method in autotest

Best Python code snippet using autotest_python Github


Full Screen

1"""Test `osf upload` command"""2import mock3from mock import call4from mock import patch5from mock import mock_open6import pytest7from osfclient import OSF8from osfclient.cli import upload9from osfclient.tests.mocks import MockArgs10from osfclient.tests.mocks import MockProject11def test_anonymous_doesnt_work():12 args = MockArgs(project='1234')13 with pytest.raises(SystemExit) as e:14 upload(args)15 expected = 'To upload a file you need to provide either a username and password or a token.'16 assert expected in e.value.args[0]17@patch.object(OSF, 'project', return_value=MockProject('1234'))18def test_select_project(OSF_project):19 args = MockArgs(username='',20 project='1234',21 source='foo/bar.txt',22 destination='bar/bar/foo.txt')23 def simple_getenv(key):24 if key == 'OSF_PASSWORD':25 return 'secret'26 fake_open = mock_open()27 with patch('', fake_open):28 with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):29 upload(args)30 assert call('foo/bar.txt', 'rb') in fake_open.mock_calls31 # the mock project created by calling OSF().project()32 fake_project = OSF_project.return_value33 expected = [call('osfstorage')]34 assert fake_project._storage_mock.mock_calls == expected35 # assert fake_project.mock_calls == expected36 expected = [call.create_file('bar/bar/foo.txt', fake_open.return_value,37 force=False, update=False)]38 # we should call the create_file method on the return39 # value of _storage_mock40 assert fake_project._storage_mock.return_value.mock_calls == expected41@patch.object(OSF, 'project', return_value=MockProject('1234'))42def test_recursive_requires_directory(OSF_project):43 # test that we check if source is a directory when using recursive mode44 args = MockArgs(username='',45 project='1234',46 source='foo/bar.txt',47 recursive=True,48 destination='bar/bar/foo.txt')49 def simple_getenv(key):50 if key == 'OSF_PASSWORD':51 return 'secret'52 with pytest.raises(RuntimeError) as e:53 with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):54 with patch('osfclient.cli.os.path.isdir', return_value=False):55 upload(args)56 assert 'recursive' in str(e.value)57 assert 'Expected source (foo/bar.txt)' in str(e.value)58@patch.object(OSF, 'project', return_value=MockProject('1234'))59def test_recursive_upload(OSF_project):60 # test that we check if source is a directory when using recursive mode61 args = MockArgs(username='',62 project='1234',63 source='foobar/',64 recursive=True,65 destination='BAR/')66 def simple_getenv(key):67 if key == 'OSF_PASSWORD':68 return 'secret'69 fake_open = mock_open()70 fake_storage = # it is important we use foobar/ and not foobar to match with args.source72 # to mimick the behaviour of os.walk()73 dir_contents = [('foobar/', None, ['bar.txt', 'abc.txt']),74 ('foobar/baz', None, ['bar.txt', 'abc.txt'])75 ]76 with patch('', fake_open):77 with patch('os.walk', return_value=iter(dir_contents)):78 with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):79 with patch('osfclient.cli.os.path.isdir', return_value=True):80 upload(args)81 assert call('foobar/bar.txt', 'rb') in fake_open.mock_calls82 assert call('foobar/abc.txt', 'rb') in fake_open.mock_calls83 assert call('foobar/baz/bar.txt', 'rb') in fake_open.mock_calls84 assert call('foobar/baz/abc.txt', 'rb') in fake_open.mock_calls85 # two directories with two files each -> four calls plus all the86 # context manager __enter__ and __exit__ calls87 assert len(fake_open.mock_calls) == 4 + 4*288 fake_storage.assert_has_calls([89 call.create_file('BAR/./bar.txt', mock.ANY, force=False, update=False),90 call.create_file('BAR/./abc.txt', mock.ANY, force=False, update=False),91 call.create_file('BAR/baz/bar.txt', mock.ANY, force=False, update=False),92 call.create_file('BAR/baz/abc.txt', mock.ANY, force=False, update=False)93 ])94 # two directories with two files each -> four calls95 assert len(fake_storage.mock_calls) == 496@patch.object(OSF, 'project', return_value=MockProject('1234'))97def test_recursive_upload_with_subdir(OSF_project):98 # test that an extra level of subdirectory is created on the remote side99 # this is because args.source does not end in a /100 args = MockArgs(username='',101 project='1234',102 source='foobar',103 recursive=True,104 destination='BAR/')105 def simple_getenv(key):106 if key == 'OSF_PASSWORD':107 return 'secret'108 fake_open = mock_open()109 fake_storage = # it is important we use foobar and not foobar/ to match with args.source111 # to mimick the behaviour of os.walk()112 dir_contents = [('foobar', None, ['bar.txt', 'abc.txt']),113 ('foobar/baz', None, ['bar.txt', 'abc.txt'])114 ]115 with patch('', fake_open):116 with patch('os.walk', return_value=iter(dir_contents)):117 with patch('osfclient.cli.os.getenv', side_effect=simple_getenv):118 with patch('osfclient.cli.os.path.isdir', return_value=True):119 upload(args)120 assert call('foobar/bar.txt', 'rb') in fake_open.mock_calls121 assert call('foobar/abc.txt', 'rb') in fake_open.mock_calls122 assert call('foobar/baz/bar.txt', 'rb') in fake_open.mock_calls123 assert call('foobar/baz/abc.txt', 'rb') in fake_open.mock_calls124 # two directories with two files each -> four calls plus all the125 # context manager __enter__ and __exit__ calls126 assert len(fake_open.mock_calls) == 4 + 4*2127 fake_storage.assert_has_calls([128 call.create_file('BAR/foobar/./bar.txt', mock.ANY,129 update=False, force=False),130 call.create_file('BAR/foobar/./abc.txt', mock.ANY,131 update=False, force=False),132 call.create_file('BAR/foobar/baz/bar.txt', mock.ANY,133 update=False, force=False),134 call.create_file('BAR/foobar/baz/abc.txt', mock.ANY,135 update=False, force=False)136 ])137 # two directories with two files each -> four calls...

Full Screen

Full Screen Github


Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3# Copyright (c) 2020 Richard Hull and contributors4# See LICENSE.rst for details.5"""6Tests for the :py:class:`luma.core.device.framebuffer` class.7"""8import os9import pytest10from luma.core.render import canvas11from luma.core.framebuffer import full_frame12from luma.core.device import linux_framebuffer13import luma.core.error14from helpers import multi_mock_open, get_reference_file15from unittest.mock import patch, call16WIDTH = 12417HEIGHT = 5518SCREEN_RES = f"{WIDTH},{HEIGHT}"19BITS_PER_PIXEL = "24"20def swap_red_and_blue(data, step):21 arr = bytearray(data)22 for i in range(0, len(arr), step):23 [red, green, blue] = arr[i: i + 3]24 arr[i: i + 3] = [blue, green, red]25 return bytes(arr)26def test_display_id_as_dev_fb_number():27 with patch("", multi_mock_open(SCREEN_RES, BITS_PER_PIXEL, None)):28 device = linux_framebuffer("/dev/fb9")29 assert == 930def test_display_id_from_environ():31 os.environ["FRAMEBUFFER"] = "/dev/fb16"32 with patch("", multi_mock_open(SCREEN_RES, BITS_PER_PIXEL, None)):33 device = linux_framebuffer()34 assert == 1635def test_unknown_display_id():36 with patch("", multi_mock_open(SCREEN_RES, BITS_PER_PIXEL, None)):37 with pytest.raises(luma.core.error.DeviceNotFoundError):38 linux_framebuffer("invalid fb")39def test_read_screen_resolution():40 with patch(41 "", multi_mock_open(SCREEN_RES, BITS_PER_PIXEL, None)42 ) as fake_open:43 device = linux_framebuffer("/dev/fb1")44 assert device.width == 12445 assert device.height == 5546 fake_open.assert_has_calls([call("/sys/class/graphics/fb1/virtual_size", "r")])47def test_read_bits_per_pixel():48 with patch(49 "", multi_mock_open(SCREEN_RES, BITS_PER_PIXEL, None)50 ) as fake_open:51 device = linux_framebuffer("/dev/fb1")52 assert device.bits_per_pixel == 2453 fake_open.assert_has_calls(54 [call("/sys/class/graphics/fb1/bits_per_pixel", "r")]55 )56@pytest.mark.parametrize("bits_per_pixel,bgr", [57 (16, False),58 (24, False),59 (24, True),60 (32, False),61 (32, True),62])63def test_display(bits_per_pixel, bgr):64 bytes_per_pixel = bits_per_pixel // 865 with open(get_reference_file(f"fb_{bits_per_pixel}bpp.raw"), "rb") as fp:66 reference = if bgr:68 reference = swap_red_and_blue(reference, step=bytes_per_pixel)69 with patch("", multi_mock_open(SCREEN_RES, str(bits_per_pixel), None)) as fake_open:70 device = linux_framebuffer("/dev/fb1", framebuffer=full_frame(), bgr=bgr)71 fake_open.assert_has_calls([call("/dev/fb1", "wb")])72 fake_open.reset_mock()73 with canvas(device, dither=True) as draw:74 draw.rectangle((0, 0, 64, 32), fill="red")75 draw.rectangle((64, 0, 128, 32), fill="yellow")76 draw.rectangle((0, 32, 64, 64), fill="orange")77 draw.rectangle((64, 32, 128, 64), fill="white")78[79 call(n * WIDTH * bytes_per_pixel)80 for n in range(HEIGHT)81 ])82 fake_open.return_value.write.assert_has_calls([83 call(reference[n:n + (WIDTH * bytes_per_pixel)])84 for n in range(0, len(reference), WIDTH * bytes_per_pixel)85 ])86 fake_open.return_value.flush.assert_called_once()87def test_unsupported_bit_depth():88 with patch("", multi_mock_open(SCREEN_RES, "19", None)):89 with pytest.raises(AssertionError) as ex:90 linux_framebuffer("/dev/fb4")91 assert str(ex.value) == 'Unsupported bit-depth: 19'92def test_cleanup():93 with patch("", multi_mock_open(SCREEN_RES, BITS_PER_PIXEL, None)) as fake_open:94 device = linux_framebuffer("/dev/fb1", framebuffer=full_frame())95 device.cleanup()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?