How to use test_interactive method in tox

Best Python code snippet using tox_python

test_rename.py

Source:test_rename.py Github

copy

Full Screen

1import logging2import re3from unittest.mock import patch4import pytest5from ebook_homebrew.exceptions import InvalidNumberParameterTypeError, \6 TargetSrcFileNotFoundError, ChangeFileNameOSError, InvalidImageParameterTypeError7from ebook_homebrew.rename import ChangeFilename8_logger = logging.getLogger(name=__name__)9class TestChangeFilename(object):10 def setup_method(self, method):11 _logger.info("method{}".format(method.__name__))12 with patch("os.chdir"):13 self.target = ChangeFilename(directory_path="test", digits="3", extension="jpg")14 @pytest.mark.parametrize("test_input, expected", [15 (["test001.jpg", 5], "00001.jpg"),16 (["test001foo2.jpg", 5], "00001.jpg"),17 (["001.jpg", 3], "001.jpg"),18 (["001.jpg", 2], "001.jpg")])19 def test_ok_create_new_name(self, test_input, expected):20 match_obj = re.search("\\d{3}", test_input[0])21 actual = self.target._create_new_name(match_obj, test_input[1], ".jpg")22 assert actual == expected23 def test_error_create_new_name(self):24 with pytest.raises(InvalidNumberParameterTypeError):25 self.target._create_new_name("test", 5, ".jpg")26 @pytest.mark.parametrize("file_list, is_file, expected", [27 (["test001test.jpg", "test002foo.jpg"], False, 0),28 (["test001test.jpg", "test002foo.png"], False, 0),29 (["test001test.jpg", "testfoobar.jpg"], False, 0),30 (["test001test.jpg", "test001foo.jpg"], True, 2),31 ([], False, 0)])32 def test_ok_filename_to_digit_number(self, file_list, is_file, expected):33 with patch("os.listdir") as mock_listdir, patch("os.path.isfile") as mock_isfile, \34 patch.object(self.target, "_rename_file"):35 mock_listdir.return_value = file_list36 mock_isfile.return_value = is_file37 actual = self.target.filename_to_digit_number()38 assert len(actual) is expected39 mock_listdir.assert_called_once_with("test")40 def test_file_not_found_error_filename_to_digit(self):41 with patch("os.listdir") as mock_listdir:42 mock_listdir.side_effect = FileNotFoundError43 with pytest.raises(TargetSrcFileNotFoundError):44 self.target.filename_to_digit_number()45 def test_os_error_filename_to_digit(self):46 with patch("os.listdir") as mock_listdir, patch("os.path.isfile") as mock_isfile:47 mock_listdir.return_value = ["test001foo.jpg"]48 mock_isfile.side_effect = OSError49 with pytest.raises(ChangeFileNameOSError):50 self.target.filename_to_digit_number()51 @staticmethod52 def interactive_input(test_input):53 for out in test_input:54 yield out55 @pytest.mark.parametrize("test_interactive, is_file_return, expected", [56 (["y", "foo.jpg"], [True, False], True),57 (["Y", ""], [True, False], True),58 (["Y", "foo.jpg", "bar.jpg"], [True, True, False], True),59 (["N", ""], [True, False], True),60 (["r", "y", "y"], [True, False], True),61 (["r", "r", "foo.jpg"], [True, False, False], True),62 (["r", "c"], [True, False], True),63 (["r", "n"], [True, False], True)])64 def test_ok_change_name_manually(self, test_interactive, is_file_return, expected):65 with patch("os.listdir") as mock_listdir, patch("os.path.isfile") as mock_isfile, \66 patch.object(self.target, "_rename_file"), \67 patch("builtins.input") as mock_input, \68 patch.object(self.target, "_remove_file"), \69 patch.object(self.target, "_check_image_file"):70 mock_listdir.return_value = ["test001test.jpg"]71 mock_isfile.side_effect = is_file_return72 mock_input.side_effect = self.interactive_input(test_interactive)73 self.target.filename_to_digit_number()74 actual = self.target.change_name_manually(overwrite=False)75 assert actual == expected76 @pytest.mark.parametrize("test_interactive, is_file_return", [77 (["r", "c"], [True, False])])78 def test_skip_change_name_manually(self, test_interactive, is_file_return):79 with patch("os.listdir") as mock_listdir, patch("os.path.isfile") as mock_isfile, \80 patch("builtins.input") as mock_input, \81 patch.object(self.target, "_check_image_file") as mock_image:82 mock_listdir.return_value = ["test001test.jpg"]83 mock_isfile.side_effect = is_file_return84 mock_input.side_effect = self.interactive_input(test_interactive)85 mock_image.side_effect = InvalidImageParameterTypeError86 self.target.filename_to_digit_number()87 actual = self.target.change_name_manually(overwrite=False)88 assert actual is True89 @pytest.mark.parametrize("test_file_list, is_file_return, test_input", [90 (["001.jpg", "002.jpg"], False, ["foo", "bar"]),91 (["001.jpg", "002.jpg"], False, ["foo", None]),92 (["001.jpg", "002.jpg"], False, [None, "bar"]),93 (["001.jpg", "002.txt"], False, ["foo", "bar"]),94 (["001.jpg", "aaa.jpg"], True, ["foo", "bar"]),95 (["001.jpg", "foo001bar.jpg"], [False, True], ["foo", "bar"])])96 def test_add_before_after_str(self, test_file_list, is_file_return, test_input):97 with patch("os.listdir") as mock_listdir, patch("os.path.isfile") as mock_isfile, \98 patch.object(self.target, "_rename_file"):99 mock_listdir.return_value = test_file_list100 mock_isfile.return_value = is_file_return101 actual = self.target.add_before_after_str(*test_input)...

Full Screen

Full Screen

test_interactive.py

Source:test_interactive.py Github

copy

Full Screen

1#!/usr/bin/python32# -*- encoding: utf-8 -*-3# Copyright © 2017 the Snipe contributors4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9#10# 1. Redistributions of source code must retain the above copyright11# notice, this list of conditions and the following disclaimer.12#13# 2. Redistributions in binary form must reproduce the above14# copyright notice, this list of conditions and the following15# disclaimer in the documentation and/or other materials provided16# with the distribution.17#18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND19# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,20# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF21# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE22# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED25# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON27# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR28# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF29# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30# SUCH DAMAGE.31'''32Unit tests for interactive function infrastructure33'''34import inspect35import os36import unittest37from unittest.mock import (Mock)38from typing import (TYPE_CHECKING)39import snipe.interactive as interactive # noqa: F40140class TestInteractive(unittest.TestCase):41 def test__keyword(self):42 f = interactive._keyword('foo')43 self.assertTrue(inspect.isfunction(f))44 self.assertIsNone(f())45 self.assertEqual('bar', f(foo='bar'))46 def test_integer_argument(self):47 if TYPE_CHECKING:48 return49 self.assertEqual(50 5, interactive.integer_argument(argument=5))51 self.assertEqual(52 -1, interactive.integer_argument(argument='-'))53 self.assertEqual(54 4, interactive.integer_argument(argument=[True]))55 self.assertEqual(56 3, interactive.positive_integer_argument(57 argument=-3))58 self.assertIsNone(interactive.positive_integer_argument())59 def test_isinteractive(self):60 self.assertTrue(interactive.isinteractive())61 def test_call(self):62 def callable(i: interactive.integer_argument):63 return 564 self.assertEqual(5, interactive.call(callable))65 def callable(i: interactive.integer_argument=4):66 return i67 self.assertEqual(4, interactive.call(callable))68 def callable(i):69 pass70 with self.assertRaises(Exception):71 interactive.call(callable)72class TestUnCompleter(unittest.TestCase):73 def test(self):74 u = interactive.UnCompleter()75 self.assertEqual([], u.candidates)76 self.assertFalse(u.live)77 self.assertEqual([], u.matches())78 self.assertIsNone(u.roll(5))79 self.assertIsNone(u.roll_to('foo'))80 self.assertFalse(u.check(None, None))81 self.assertEqual((None, None), u.expand('value'))82class TestCompleter(unittest.TestCase):83 def test(self):84 c = interactive.Completer(['aaa', 'bab', 'aab'])85 self.assertEqual(86 [(0, 'aaa', 'aaa'), (1, 'aab', 'aab')], c.matches('aa'))87 c.roll(1)88 self.assertEqual(89 [(0, 'aab', 'aab'), (2, 'aaa', 'aaa')], c.matches('aa'))90 c.roll_to('aaa')91 self.assertEqual(92 [(0, 'aaa', 'aaa'), (1, 'aab', 'aab')], c.matches('aa'))93 self.assertEqual('aaa', c.expand('a'))94 self.assertEqual('ccc', c.expand('ccc'))95class TestFileCompleter(unittest.TestCase):96 def test(self):97 cwd = os.getcwd()98 try:99 os.chdir(os.path.dirname(__file__))100 f = interactive.FileCompleter()101 self.assertEqual(1, len(f.matches('test_interactive')))102 self.assertEqual(103 ('test_interactive.py', 'test_interactive.py'),104 f.matches('interactive')[0][1:])105 self.assertEqual('test_interactive.py', f.expand('interactive'))106 f.directory = '../tests'107 self.assertEqual(108 ('test_interactive.py', 'test_interactive.py'),109 f.matches('interactive')[0][1:])110 self.assertEqual(111 '../tests/test_interactive.py',112 f.expand('../tests/interactive'))113 finally:114 os.chdir(cwd)115class TestDestCompleter(unittest.TestCase):116 def test(self):117 roost = Mock()118 roost.name = 'roost'119 irccloud = Mock()120 irccloud.name = 'irccloud'121 context = Mock()122 context.backends = [roost, irccloud]123 d = interactive.DestCompleter(124 ['roost; foo', 'roost; bar', 'irccloud; baz'], context)125 self.assertEqual(['roost; bar'], [x[1] for x in d.matches('bar')])126 self.assertEqual(['roost; bar'], [x[1] for x in d.matches('r;bar')])127 self.assertEqual(128 ['irccloud; baz'], [x[1] for x in d.matches('i;baz')])129 self.assertEqual(130 ['irccloud; baz'], [x[1] for x in d.matches('i;')])131 self.assertEqual(...

Full Screen

Full Screen

tests.py

Source:tests.py Github

copy

Full Screen

...19 )(function)()20 return output[1] # нужен только "выведенный" результат21 return inner22 return wrapper23def test_interactive():24 def should_not_input(*_):25 raise AssertionError("Shouldn't ask for input")26 output = []27 @interactive(28 'Foo',29 input_function=should_not_input,30 output_function=output.append,31 )32 def bar():33 return 'Bar'34 assert bar() is None, "Interactive function should not return something!"35 assert output == ['Foo', 'Bar']36 print('test_interactive is Ok!')37def test_non_interactive():38 @non_interactive()39 def foo():40 return 4241 assert foo() == 4242 print('test_non_interactive is Ok!')43def test_asks():44 @non_interactive(x='42', y='57')45 @asks('x', 'Enter first number: ')46 @asks('y', 'Enter second number: ')47 def add(x, y):48 return int(x) + int(y)49 assert add() == 9950 print('test_asks is Ok!')51def test_asks_order():52 order = []53 @interactive(54 '',55 input_function=lambda key, _: order.append(key) or '',56 output_function=lambda _: None,57 )58 @asks('a', '?')59 @asks('b', '??')60 def two_args(a, b):61 return a + b62 two_args()63 assert order == ['a', 'b'], "Questions should be asked in ceratin order!"64 print('test_asks_order is ok!')65test_interactive()66test_non_interactive()67test_asks()...

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:

YouTube

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

Run tox 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?

Helpful

NotHelpful