def test_create_snowflake_connection(mocker): # noqa: F811 # Mock out the snowflake connection method w/o mocking out the helper method. mocker.patch.object(snowflake.snowflake.connector, 'connect') mock_cursor = mocker.Mock() mock_connection = mocker.Mock() mock_connection.cursor.return_value = mock_cursor snowflake.snowflake.connector.connect.return_value = mock_connection # Mock the key decryption. mocker.patch.object(snowflake.serialization, 'load_pem_private_key') mock_key = mocker.Mock() mock_key.private_bytes.return_value = 1234 snowflake.serialization.load_pem_private_key.return_value = mock_key # Call the connection method. snowflake.create_snowflake_connection(credentials={ "private_key": "this_is_an_encrypted_private_key", "private_key_passphrase": "passphrase_for_the_private_key", "user": "******", "account": "company-cloud-region" }, role="test_role") snowflake.snowflake.connector.connect.assert_called_with( account='company-cloud-region', autocommit=False, private_key=1234, user='******', warehouse=None, password=None, ) mock_cursor.execute.assert_has_calls(( mocker.call("USE ROLE test_role"), mocker.call("ALTER SESSION SET TIMEZONE = 'UTC'"), ))
def test_loop_define(mocker): mocker.patch.object(basic, "loop_define", side_effect=[2, 3, 0]) mocker.patch.object(basic, "hello") basic.loop_sample_define() basic.loop_define.assert_any_call(1) assert len(basic.loop_define.mock_calls) == 3 assert len(basic.hello.mock_calls) == 2 assert basic.loop_define.mock_calls == [ mocker.call(1), mocker.call(1), mocker.call(1), ]
def test_multi_step_three(mocker): ''' Use mocked randint to test that player makes multiple moves of given length. ''' n_steps = 5 randint_value = 3 # Patch random.randint to always return randint_value mocker.patch('random.randint', return_value=randint_value) # Wrap a "spy" around random.randint to collect information about # how often and with which arguments it is called. randint_spy = mocker.spy(random, 'randint') b = Board(chutes=[], ladders=[]) pl = Player(b) for _ in range(n_steps): pl.move() # Check that player is in correct position assert pl.position == n_steps * randint_value # Check that random.randint was called once per call to move() assert randint_spy.call_count == n_steps # Check that random.randint was always called with arguments 1, 6 randint_spy.assert_has_calls(n_steps * [mocker.call(1, 6)])
def test_create_q_matrices_all_zeros_files_if_wrong_data(mocker, deactivate_open_files): mocker.spy(functions, "newton_cotes") mocked_file = deactivate_open_files mocker.spy(numpy, "savetxt") model = Model(settings.default) model.total_time_steps = 2 model.mass_intervals = [[0., 0.1], [8., 2.]] model.energies = [1e-8, -1e-4] model.create_q_matrices() calls = [mocker.call(f"{settings.default['output_dir']}/imf_supernova_rates", "w+"), mocker.call(f"{settings.default['output_dir']}/qm-matrices", "w+")] mocked_file.assert_has_calls(calls) assert functions.newton_cotes.call_count == 0 assert numpy.savetxt.call_count == model.total_time_steps
def test_return_fractions(mocker, deactivate_open_files): mocker.spy(functions, "return_fraction") mocked_file = deactivate_open_files model = Model({**settings.default, **{"return_fractions": True}}) model.total_time_steps = 2 model.mass_intervals = [[1., 8.], [8., 33.]] model.sn_Ia_rates = [2e-4, 1e-4] model.energies = [3e-4, 1.2e-4] model.create_q_matrices() assert model.context["return_fractions"] is True calls = [mocker.call(f"{settings.default['output_dir']}/imf_supernova_rates", "w+"), mocker.call(f"{settings.default['output_dir']}/qm-matrices", "w+"), mocker.call(f"{settings.default['output_dir']}/return_fractions", "w+")] mocked_file.assert_has_calls(calls) functions.return_fraction.assert_has_calls
def test_create_q_matrices(mocker, deactivate_open_files): mocker.spy(functions, "newton_cotes") mocked_file = deactivate_open_files mocker.spy(numpy, "savetxt") model = Model(settings.default) model.total_time_steps = 2 model.mass_intervals = [[1., 8.], [8., 33.]] model.sn_Ia_rates = [2e-4, 1e-4] model.energies = [3e-4, 1.2e-4] model.create_q_matrices() calls = [mocker.call(f"{settings.default['output_dir']}/imf_supernova_rates", "w+"), mocker.call(f"{settings.default['output_dir']}/qm-matrices", "w+")] mocked_file.assert_has_calls(calls) functions.newton_cotes.assert_has_calls assert numpy.savetxt.call_count == model.total_time_steps