Best Python code snippet using tempest_python
test_accounts.py
Source:test_accounts.py  
...121        self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))122        test_account_class = accounts.Accounts('v2', 'test_name')123        with mock.patch('six.moves.builtins.open', mock.mock_open(),124                        create=True) as open_mock:125            test_account_class._get_free_hash(hash_list)126            lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),127                                     'test_accounts',128                                     hash_list[0])129            open_mock.assert_called_once_with(lock_path, 'w')130        mkdir_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,131                                  'test_accounts')132        mkdir_mock.mock.assert_called_once_with(mkdir_path)133    @mock.patch('oslo_concurrency.lockutils.lock')134    def test_get_free_hash_no_free_accounts(self, lock_mock):135        hash_list = self._get_hash_list(self.test_accounts)136        # Emulate pre-existing lock dir137        self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))138        # Emulate all lcoks in list are in use139        self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))140        test_account_class = accounts.Accounts('v2', 'test_name')141        with mock.patch('six.moves.builtins.open', mock.mock_open(),142                        create=True):143            self.assertRaises(exceptions.InvalidConfiguration,144                              test_account_class._get_free_hash, hash_list)145    @mock.patch('oslo_concurrency.lockutils.lock')146    def test_get_free_hash_some_in_use_accounts(self, lock_mock):147        # Emulate no pre-existing lock148        self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))149        hash_list = self._get_hash_list(self.test_accounts)150        test_account_class = accounts.Accounts('v2', 'test_name')151        def _fake_is_file(path):152            # Fake isfile() to return that the path exists unless a specific153            # hash is in the path154            if hash_list[3] in path:155                return False156            return True157        self.stubs.Set(os.path, 'isfile', _fake_is_file)158        with mock.patch('six.moves.builtins.open', mock.mock_open(),159                        create=True) as open_mock:160            test_account_class._get_free_hash(hash_list)161            lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),162                                     'test_accounts',163                                     hash_list[3])164            open_mock.assert_has_calls([mock.call(lock_path, 'w')])165    @mock.patch('oslo_concurrency.lockutils.lock')166    def test_remove_hash_last_account(self, lock_mock):167        hash_list = self._get_hash_list(self.test_accounts)168        # Pretend the pseudo-lock is there169        self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))170        # Pretend the lock dir is empty171        self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))172        test_account_class = accounts.Accounts('v2', 'test_name')173        remove_mock = self.useFixture(mockpatch.Patch('os.remove'))174        rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))...test_preprov_creds.py
Source:test_preprov_creds.py  
...139        test_account_class = preprov_creds.PreProvisionedCredentialProvider(140            **self.fixed_params)141        with mock.patch('six.moves.builtins.open', mock.mock_open(),142                        create=True) as open_mock:143            test_account_class._get_free_hash(hash_list)144            lock_path = os.path.join(self.fixed_params['accounts_lock_dir'],145                                     hash_list[0])146            open_mock.assert_called_once_with(lock_path, 'w')147        mkdir_path = os.path.join(self.fixed_params['accounts_lock_dir'])148        mkdir_mock.mock.assert_called_once_with(mkdir_path)149    @mock.patch('oslo_concurrency.lockutils.lock')150    def test_get_free_hash_no_free_accounts(self, lock_mock):151        hash_list = self._get_hash_list(self.test_accounts)152        # Emulate pre-existing lock dir153        self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))154        # Emulate all lcoks in list are in use155        self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))156        test_account_class = preprov_creds.PreProvisionedCredentialProvider(157            **self.fixed_params)158        with mock.patch('six.moves.builtins.open', mock.mock_open(),159                        create=True):160            self.assertRaises(lib_exc.InvalidCredentials,161                              test_account_class._get_free_hash, hash_list)162    @mock.patch('oslo_concurrency.lockutils.lock')163    def test_get_free_hash_some_in_use_accounts(self, lock_mock):164        # Emulate no pre-existing lock165        self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))166        hash_list = self._get_hash_list(self.test_accounts)167        test_account_class = preprov_creds.PreProvisionedCredentialProvider(168            **self.fixed_params)169        def _fake_is_file(path):170            # Fake isfile() to return that the path exists unless a specific171            # hash is in the path172            if hash_list[3] in path:173                return False174            return True175        self.stubs.Set(os.path, 'isfile', _fake_is_file)176        with mock.patch('six.moves.builtins.open', mock.mock_open(),177                        create=True) as open_mock:178            test_account_class._get_free_hash(hash_list)179            lock_path = os.path.join(self.fixed_params['accounts_lock_dir'],180                                     hash_list[3])181            open_mock.assert_has_calls([mock.call(lock_path, 'w')])182    @mock.patch('oslo_concurrency.lockutils.lock')183    def test_remove_hash_last_account(self, lock_mock):184        hash_list = self._get_hash_list(self.test_accounts)185        # Pretend the pseudo-lock is there186        self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))187        # Pretend the lock dir is empty188        self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))189        test_account_class = preprov_creds.PreProvisionedCredentialProvider(190            **self.fixed_params)191        remove_mock = self.useFixture(mockpatch.Patch('os.remove'))192        rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))...main.py
Source:main.py  
...11from flask import Flask, abort, redirect, request12app = Flask(__name__)13DEFAULT_STORING_TERM_DAYS = 714PREPAID_STORING_TERM_DAYS = 3015def _get_free_hash():16    while not hash_exists(hash_val := str(uuid4())[:8]):17        return hash_val18@app.route('/<hash_url>', methods=['GET', 'DELETE'])19def short_link(hash_url):20    if request.method == 'GET':21        if hash_exists(hash_url):22            return redirect(get_link_by_hash(hash_url), code=301)23        else:24            return abort(404)25    elif request.method == 'DELETE':26        delete_link(hash_url)27        return '', 20428@app.route('/add', methods=['POST'])29def add_short_link():30    try:31        url = request.json['url']32    except KeyError:33        return '"url" was expected in body', 40034    if request.json.get('prepaid'):35        expired = datetime.now() + timedelta(days=PREPAID_STORING_TERM_DAYS)36    else:37        expired = datetime.now() + timedelta(days=DEFAULT_STORING_TERM_DAYS)38    hash_url = _get_free_hash()39    add_short_link_to_database(url, hash_url, expired)40    return f'{request.url_root}{hash_url}'41if __name__ == '__main__':...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!!
