How to use perform_operation method in freezegun

Best Python code snippet using freezegun

unit_tests.py

Source:unit_tests.py Github

copy

Full Screen

...35 result = calculate_prefix("* * 1 2")36 self.assertEqual(result, 2)37class TestPerformOp(unittest.TestCase):38 def test_simple_add(self):39 result = perform_operation("+", 2, 3)40 self.assertEqual(result, 5)41 def test_simple_subtract(self):42 result = perform_operation("-", 8, 3)43 self.assertEqual(result, 5)44 def test_simple_multiply(self):45 result = perform_operation("*", 5, 1)46 self.assertEqual(result, 5)47 def test_simple_divide(self):48 result = perform_operation("/", 10, 2)49 self.assertEqual(result, 5)50 def test_negative_add(self):51 result = perform_operation("+", -2, 7)52 self.assertEqual(result, 5)53 def test_negative_subtract(self):54 result = perform_operation("-", 3, -2)55 self.assertEqual(result, 5)56 def test_negative_multiple(self):57 result = perform_operation("*", -1, -5)58 self.assertEqual(result, 5)59 def test_negative_divide(self):60 result = perform_operation("/", -10, -2)61 self.assertEqual(result, 5)62 def test_large_add(self):63 result = perform_operation("+", 2**30, 1)64 self.assertEqual(result, (2**30 + 1))65 def test_large_subtract(self):66 result = perform_operation("-", 2**30, 1)67 self.assertEqual(result, (2**30 - 1))68 def test_large_multiply(self):69 result = perform_operation("*", 2**30, 1)70 self.assertEqual(result, 2**30)71 def test_large_divide(self):72 result = perform_operation("/", 2**30, 1)73 self.assertEqual(result, 2**30)74 def test_type_error_num(self):75 self.assertRaises(TypeError, perform_operation, "+", 2, "3")76 def test_type_error_op(self):77 self.assertRaises(TypeError, perform_operation, 5, 2, 3)78 def test_value_error(self):79 self.assertRaises(ValueError, perform_operation, "(", 2, 3)80 def test_zero_add(self):81 result = perform_operation("+", 5, 0)82 self.assertEqual(result, 5)83 def test_zero_subtract(self):84 result = perform_operation("-", 5, 0)85 self.assertEqual(result, 5)86 def test_zero_multiply(self):87 result = perform_operation("*", 5, 0)88 self.assertEqual(result, 0)89 def test_zero_divide(self):90 self.assertRaises(ZeroDivisionError, perform_operation, "/", 2, 0)91class CalculateInfix(unittest.TestCase):92 def test_one_calc(self):93 result = calculate_infix("( 1 + 2 )")94 self.assertEqual(result, 3)95 def test_add_one_calc(self):96 result = calculate_infix("( 1 + ( 2 * 3 ) )")97 self.assertEqual(result, 7)98 def test_one_calc_add(self):99 result = calculate_infix("( ( 1 * 2 ) + 3 )")100 self.assertEqual(result, 5)101 def test_two_calc_divide_subtract(self):...

Full Screen

Full Screen

001.py

Source:001.py Github

copy

Full Screen

...42 return x / 243def square(x):44 return x ** 245# The not-so-good way:46def perform_operation(x, chosen_operation="add_one"):47 if chosen_operation == "add_one":48 x = add_one(x)49 elif chosen_operation == "divide_by_two":50 x = divide_by_two(x)51 elif chosen_operation == "square":52 x = square(x)53 else:54 raise Exception("Invalid operation")55 return x56def add_one(x):57 return x + 158def divide_by_two(x):59 return x / 260def square(x):61 return x ** 262def invalid_op(x):63 raise Exception("Invalid operation")64# The better way:65def perform_operation(x, chosen_operation="add_one"):66 ops = {67 "add_one": add_one,68 "divide_by_two": divide_by_two,69 "square": square70 }71 chosen_operation_function = ops.get(chosen_operation, invalid_op)72 return chosen_operation_function(x)73perform_operation(10)74######################################################################################################################75def add(x, to=1):76 return x + to77def divide(x, by=2):78 return x / by79def square(x):80 return x ** 281def invalid_op(x):82 raise Exception("Invalid operation")83def perform_operation(x, chosen_operation, operation_args=None):84 # If operation_args wasn't provided (i.e. it is None), set it to be an empty dictionary85 operation_args = operation_args or {}86 ops = {87 "add": add,88 "divide": divide,89 "square": square90 }91 chosen_operation_function = ops.get(chosen_operation, invalid_op)92 return chosen_operation_function(x, **operation_args)93def example_usage():94 x = 195 x = perform_operation(x, "add", {"to": 4}) # Adds 496 x = perform_operation(x, "add") # Adds 1 since that's the default for 'add'97 x = perform_operation(x, "divide", {"by": 2}) # Divides by 298 x = perform_operation(x, "square") # Squares the number...

Full Screen

Full Screen

py_dos_and_donts.py

Source:py_dos_and_donts.py Github

copy

Full Screen

...35 return x / 236def square(x):37 return x**238# The not-so-good way:39def perform_operation(x, chosen_operation="add_one"):40 if chosen_operation == "add_one":41 x = add_one(x)42 elif chosen_operation == "divide_by_two":43 x = divide_by_two(x)44 elif chosen_operation == "square":45 x = square(x)46 else:47 raise Exception("Invalid operation")48 return x49# the better way50def invalid_op(x):51 raise Exception("Invalid operation")52# The better way:53def perform_operation(x, chosen_operation="add_one"):54 ops = {"add_one": add_one, "divide_by_two": divide_by_two, "square": square}55 chosen_operation_function = ops.get(chosen_operation, invalid_op)56 return chosen_operation_function(x)57# Bonus: Dynamic functions with custom arguments -> dict unpacking58def user_print(user_name, user_type="regular", user_logged_in=False):59 return f"{user_name} is a(n) {user_type} user and they are {'not ' if not user_logged_in else ''}logged in."60# 1. Without dictionary unpacking:61user_print(user_name="testuser1", user_type="admin", user_logged_in=True)62# 2. With dictionary unpacking:63args = {"user_name": "testuser1", "user_type": "admin", "user_logged_in": True}64user_print(**args)65def add(x, to=1):66 return x + to67def divide(x, by=2):68 return x / by69def square(x):70 return x**271def invalid_op(x):72 raise Exception("Invalid operation")73def perform_operation(x, chosen_operation, operation_args=None):74 # If operation_args wasn't provided (i.e. it is None), set it to be an empty dictionary75 operation_args = operation_args or {}76 ops = {"add": add, "divide": divide, "square": square}77 chosen_operation_function = ops.get(chosen_operation, invalid_op)78 return chosen_operation_function(x, **operation_args)79def example_usage():80 x = 181 x = perform_operation(x, "add", {"to": 4}) # Adds 482 x = perform_operation(x, "add") # Adds 1 since that's the default for 'add'83 x = perform_operation(x, "divide", {"by": 2}) # Divides by 2...

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 freezegun 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