def test_to_dict(self, pipeline_config: PipelineConfig) -> None: """Test conversion to a dict.""" report = Report(count=3, pipeline=pipeline_config) project = flexmock() project_dict = {"aresto momentum": "avada kedavra"} project.should_receive("to_dict").with_args( keep_thoth_section=True).and_return( project_dict).twice() # In test and in the report. product = Product( project=project, score=0.666, justification=[{ "gryffindor": "le gladium leviosa" }], advised_runtime_environment=RuntimeEnvironment.from_dict( {"python_version": "3.6"}), ) report.add_product(product) assert report.product_count() == 1 assert list(report.iter_products()) == [product] assert report.to_dict() == { "pipeline": pipeline_config.to_dict(), "products": [product.to_dict()], "stack_info": [], "resolver_iterations": 0, "accepted_final_states_count": 0, "discarded_final_states_count": 0, }
def test_call_post_run_report(self, pipeline_config: PipelineConfig) -> None: """Test calling post-run method with report.""" report = Report(count=2, pipeline=pipeline_config) for unit in pipeline_config.iter_units(): flexmock(unit).should_receive("post_run_report").with_args( report=report ).and_return(None).once() pipeline_config.call_post_run_report(report)
def test_to_dict_metadata(self, pipeline_config: PipelineConfig) -> None: """Test conversion to a dict with passed metadata.""" report = Report(count=3, pipeline=pipeline_config) project = flexmock() project_dict = {"aresto momentum": "avada kedavra"} project.should_receive("to_dict").with_args( keep_thoth_section=True).and_return(project_dict) product = Product( project=project, score=0.666, justification=[{ "gryffindor": "le gladium leviosa" }], advised_runtime_environment=RuntimeEnvironment.from_dict( {"python_version": "3.6"}), ) report.add_product(product) stack_info = [{"type": "WARNING", "message": "Hello, metadata"}] stack_info_metadata = { "thoth.adviser": { "stack_info": stack_info, } } report.set_stack_info([{"foo": "bar"}]) assert "THOTH_ADVISER_METADATA" not in os.environ os.environ["THOTH_ADVISER_METADATA"] = json.dumps(stack_info_metadata) try: assert report.product_count() == 1 assert list(report.iter_products()) == [product] assert report.to_dict() == { "pipeline": pipeline_config.to_dict(), "products": [product.to_dict()], "stack_info": list(chain(stack_info, report.stack_info)), "resolver_iterations": 0, "accepted_final_states_count": 0, "discarded_final_states_count": 0, } except Exception: os.environ.pop("THOTH_ADVISER_METADATA") raise
def test_stack_info(self, pipeline_config: PipelineConfig) -> None: """Test setting a global stack information.""" report = Report(count=2, pipeline=pipeline_config) report.set_stack_info([{"foo": "bar"}]) assert report.product_count() == 0 assert report.to_dict() == { "pipeline": { "boots": [], "sieves": [], "steps": [], "strides": [], "wraps": [], }, "products": [], "stack_info": [{ "foo": "bar" }], }
def test_stack_info(self, pipeline_config: PipelineConfig) -> None: """Test setting a global stack information.""" report = Report(count=2, pipeline=pipeline_config) report.set_stack_info([{"foo": "bar"}]) assert report.product_count() == 0 assert report.to_dict() == { "pipeline": { "boots": [], "pseudonyms": [], "sieves": [], "steps": [], "strides": [], "wraps": [], }, "products": [], "stack_info": [{ "foo": "bar" }], "resolver_iterations": 0, "accepted_final_states_count": 0, "discarded_final_states_count": 0, }
def test_add_product(self, pipeline_config: PipelineConfig) -> None: """Test adding a product to a report.""" report = Report(count=2, pipeline=pipeline_config) product1 = Product(project=None, score=0.42, justification=[], advised_runtime_environment=None) assert report.add_product(product1) is True assert report.product_count() == 1 assert list(report.iter_products()) == [product1] assert list(report.iter_products_sorted()) == [product1] product2 = Product(project=None, score=0.0, justification=[], advised_runtime_environment=None) assert report.add_product(product2) is True assert report.product_count() == 2 assert set(report.iter_products()) == {product1, product2} assert list(report.iter_products_sorted()) == [product1, product2] assert list( report.iter_products_sorted(reverse=True)) == [product1, product2] assert list(report.iter_products_sorted(reverse=False)) == [ product2, product1 ] product3 = Product(project=None, score=0.98, justification=[], advised_runtime_environment=None) assert report.add_product(product3) is True assert report.product_count() == 2 assert set(report.iter_products()) == {product3, product1} assert list(report.iter_products_sorted()) == [product3, product1] assert list( report.iter_products_sorted(reverse=True)) == [product3, product1] assert list(report.iter_products_sorted(reverse=False)) == [ product1, product3 ] product4 = Product( project=None, score=0.666, justification=[], advised_runtime_environment=None, ) assert report.add_product(product4) is True assert report.product_count() == 2 assert set(report.iter_products()) == {product3, product4} assert list(report.iter_products_sorted()) == [product3, product4] assert list( report.iter_products_sorted(reverse=True)) == [product3, product4] assert list(report.iter_products_sorted(reverse=False)) == [ product4, product3 ] product5 = Product( project=None, score=-0.99999, justification=[], advised_runtime_environment=None, ) assert report.add_product(product5) is False assert report.product_count() == 2 assert set(report.iter_products()) == {product3, product4} assert list(report.iter_products_sorted()) == [product3, product4] assert list( report.iter_products_sorted(reverse=True)) == [product3, product4] assert list(report.iter_products_sorted(reverse=False)) == [ product4, product3 ]