def it_warns_on_no_seq_from_uniprot(): csv_string = """ UniprotAC P1 """ with zest.mock(helpers._protein_csv_warning) as m_warn: with zest.mock(helpers._uniprot_lookup) as m_lookup: m_lookup.returns([]) helpers.protein_csv_df(csv_string) assert m_warn.called_once()
def it_uses_inputs_args_if_specified(): with zest.mock(PipelineTask._config_dirty, returns=None) as m_config_dirty: with zest.mock( PipelineTask._out_of_date, returns=(True, "because, reasons") ) as m_out_of_date: pt = _pt(Munch(inputs=Munch(a="../a"))) inputs = [src_dir / "a", src_dir / "b"] pt.is_dirty(inputs) assert m_out_of_date.n_calls == 1 calls = m_out_of_date.normalized_calls()[0] assert calls["parents"] == inputs assert calls["children"] == dst_dir
def it_copies(): with zest.mock(sim_v1_worker._rand3, returns=np.full((n_samples, 2, 3), 1.0)): sim_v1_worker._step_2_initialize_samples_including_dark_sampling( flu, p_bright, n_samples=n_samples) assert flu[0][ 0] == 1 # Check that the original flu was not modified.
def it_allows_zeros_as_first_value_in_unnormalized_abundance_data(): with zest.mock(log.info) as m_log: unnormalized_abundance_data_with_zeros = PrepParams(proteins=[ _fake_protein(0), _fake_protein(5), _fake_protein(10) ])
def it_uses_batch_size(): with zest.mock(zap._cpu_count) as m: sl = zap._make_batch_slices(_batch_size=2, n_rows=6, _limit_slice=None) assert sl == [(0, 2), (2, 4), (4, 6)] assert not m.called()
def it_exceptions_serially(): with zest.mock(pretend_unit_under_test.foo) as m_foo: m_foo.exceptions_serially([ValueError, TypeError]) with zest.raises(ValueError): pretend_unit_under_test.foo() with zest.raises(TypeError): pretend_unit_under_test.foo()
def it_raises_on_duplicate_sequences(): with zest.mock(_error) as m_error: with zest.raises(ValueError): pro_spec_df = pd.DataFrame( dict(name=["name1", "name2"], sequence=["ABC", "ABC"])) _step_1_check_for_uniqueness(pro_spec_df) assert m_error.called()
def scope_mocks(): with zest.mock(pretend_unit_under_test.foo) as m_foo: pretend_unit_under_test.foo() # Had the real foo been called it would have # raised NotImplementedError assert m_foo.called_once()
def it_warns_and_normalized_unnormalized_abundance_data(): with zest.mock(log.info) as m_log: unnormalized_abundance_data = PrepParams( proteins=[_fake_protein(10), _fake_protein(5)]) assert m_log.called_once() assert unnormalized_abundance_data.proteins[0].abundance == 2
def it_converts_to_power_of_2(): with zest.mock(worker._convert_message): nd2 = _make_nd2(63) m_nd2.returns(nd2) result = worker.ims_import(src_path, ims_import_params) assert result.field_chcy_ims(0).shape == (n_channels, n_cycles, 64, 64)
def it_reruns_on_a_previous_failure(): with zest.mock(Task1.get_output_state, returns=PipelineState.error) as m_get_output_state: _set_dirty(True, False, False) _p() assert m_task1_start.called_once() assert not m_task2_start.called() assert not m_task3_start.called()
def it_warns_on_more_than_one_seq_from_uniprot(): csv_string = """ UniprotAC P1 """ with zest.mock(helpers._protein_csv_warning) as m_warn: with zest.mock(helpers._uniprot_lookup) as m_lookup: m_lookup.returns([{ "id": "foo", "seqstr": "123" }, { "id": "bar", "seqstr": "123456" }]) df = helpers.protein_csv_df(csv_string) assert len(df) == 1 and df.loc[0, "seqstr"] == "123456" assert m_warn.called_once()
def it_traps_exceptions_in_tasks(): with zest.mock(Task1.error) as m_error: _set_dirty(True, False, False) e = Exception("problem") m_task1_start.exceptions(e) _p() assert m_error.called_once_with_kws(e=e) m_task1_start.exceptions(None)
def it_does_run_on_upstream_change_even_if_previous_success(): with zest.mock(Task1.get_output_state, returns=PipelineState.success) as m_get_output_state: _set_dirty(True, True, True) Pipeline(src_dir, dst_dir, tasks) assert m_task1_start.called_once() assert m_task2_start.called_once() assert m_task3_start.called_once()
def it_does_not_darken(): # Similarly a low number will keep everything lit with zest.mock(sim_v1_worker._rand3, returns=np.full((n_samples, 2, 3), 0.1)): flu_samples = sim_v1_worker._step_2_initialize_samples_including_dark_sampling( flu, p_bright, n_samples=n_samples) assert flu_samples.shape == (n_samples, 2, 3) assert np.all(flu_samples[:, 1, 1] == 1.0) assert np.all(flu_samples[:, 0, 0] == 1.0)
def it_normalizes_calls_into_kwargs(): # normalized_call() is a handy when you want to just know # what was passed to the mock but you don't care if # it was passed as args or kwargs. with zest.mock(pretend_unit_under_test.foo) as m_foo: pretend_unit_under_test.foo("arg1", arg2="arg2") kwargs = m_foo.normalized_call() assert kwargs == dict(arg1="arg1", arg2="arg2")
def it_bubbles_exceptions(): with zest.mock(zap._show_work_order_exception) as m_ex: with zest.raises(ValueError): work_orders[0].fn = test2 zap.work_orders( work_orders, _process_mode=True, _trap_exceptions=False, ) assert m_ex.called_once()
def it_nans_missing_abundances(): csv_string = """ UniprotAC P1 """ with zest.mock(helpers._uniprot_lookup) as m: m.returns([{"id:": "foo", "seqstr": "ABC"}]) df = helpers.protein_csv_df(csv_string) assert (df.loc[0, "id"] == "P1" and df.loc[0, "seqstr"] == "ABC" and np.isnan(df.loc[0, "abundance"]))
def it_darkens(): # Large random numbers mean it will go dark so a 0.9 will # darken the 0.5 but not the 1.0 with zest.mock(sim_v1_worker._rand3, returns=np.full((n_samples, 2, 3), 0.9)): flu_samples = sim_v1_worker._step_2_initialize_samples_including_dark_sampling( flu, p_bright, n_samples=n_samples) assert flu_samples.shape == (n_samples, 2, 3) assert np.all(flu_samples[:, 1, 1] == 1.0) assert np.all(flu_samples[:, 0, 0] == 0.0)
def it_hooks(): with zest.mock(pretend_unit_under_test.foo) as m_foo: got_callback = False def _callback(): nonlocal got_callback got_callback = True m_foo.hook(_callback) pretend_unit_under_test.foo() assert got_callback is True
def it_converts_to_power_of_2(): with zest.mock(worker._convert_message): m_nd2.hook_to_call = lambda _: _make_nd2( 63, "cycle", n_fields) result = worker.ims_import(src_path, ims_import_params) assert result.field_chcy_ims(0).shape == ( n_channels, n_fields, 64, 64, )
def it_retries(): progress = MockFunction() with zest.mock(zap._mock_BrokenProcessPool_exception) as m: m.exceptions(BrokenProcessPool) results = zap.work_orders(work_orders, _process_mode=True, _progress=progress) assert progress.calls == [ ((1, 2, True), {}), ((2, 2, True), {}), ]
def it_can_degrade_one_sample_and_not_another(): samples = npf([ [[n, 1, 0], [n, 1, 0]], [[0, 1, 1], [0, 0, 1]], ]) n_samples = 2 with zest.mock(sim_v1_worker._rand1, returns=np.full((n_samples, ), [0.0, 1.0])): result = sim_v1_worker._step_3a_cycle_edman(samples, is_mock=False, p_edman_failure=0.5) expected = npf([ [[n, 1, 0], [n, 1, 0]], [[n, 1, 1], [n, 0, 1]], ]) assert np_array_same(expected, result)
def it_lookups_uniprot(): csv_string = """ UniprotAC, Abundance P1, 10 """ with zest.mock(helpers._uniprot_lookup) as m: m.returns([{"id:": "foo", "seqstr": "ABC"}]) df = helpers.protein_csv_df(csv_string) assert df.loc[0, "seqstr"] == "ABC" def it_uses_uniprot_ac_as_name(): assert df.loc[0, "id"] == "P1" def it_imports_abundance(): assert df.loc[0, "abundance"] == 10.0 zest()
def it_solves_for_batch_size_by_scaling_the_cpu_count(): with zest.mock(zap._cpu_count, returns=2): sl = zap._make_batch_slices(_batch_size=None, n_rows=32, _limit_slice=None) assert sl == [ (0, 3), (3, 6), (6, 9), (9, 12), (12, 15), (15, 18), (18, 21), (21, 24), (24, 27), (27, 30), (30, 32), ]
def it_returns_dirty_check_config(): with zest.mock(PipelineTask._config_dirty, returns="config dirty"): pt = _pt(Munch(inputs=Munch(a="../a"))) assert pt.is_dirty() is True assert pt.dirty_reason == "config dirty"
def it_prints_array_shape_if_not_specified(): with zest.mock(check._print) as p: check.array_t(arr) assert "(3,)" in p.normalized_call()["msg"]
def it_checks_against_normalized_call(): with zest.mock(pretend_unit_under_test.foo) as m_foo: pretend_unit_under_test.foo("arg1", arg2="arg2") assert m_foo.called_once_with_kws(arg1="arg1", arg2="arg2")
def it_calls_the_task_success(): with zest.mock(Task1.success) as m_success: _set_dirty(True, False, False) _p() assert m_success.called_once()
def it_exceptions(): with zest.mock(pretend_unit_under_test.foo) as m_foo: m_foo.exceptions(ValueError) with zest.raises(ValueError): pretend_unit_under_test.foo()