From 2b249063e0b7ab2e9c66a13f800538b5747ce2a7 Mon Sep 17 00:00:00 2001 From: pablo Date: Thu, 31 Dec 2020 18:28:48 +0100 Subject: [PATCH] Created a new flow generator + tests for it. --- core/parsing_utils.py | 37 ++++++++++++++++++++++++++++++++++++- tests/parsing_utils_test.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/core/parsing_utils.py b/core/parsing_utils.py index b732cf4..019e729 100644 --- a/core/parsing_utils.py +++ b/core/parsing_utils.py @@ -1,4 +1,4 @@ -from typing import Union, Iterable, Dict, Callable +from typing import Union, Iterable, Dict, Callable, Type import re from bs4 import BeautifulSoup @@ -553,3 +553,38 @@ class ParsingFlow: issues[field.field_name] = this_field_issues return issues + + +class ParsingFlowGenerator: + """ + Class for creating multiple, empty flows based on a group of instructions. + """ + + def __init__( + self, + parsing_flow_class: Type[ParsingFlow], + instructions_to_attach_with_params: Dict[ + Type[BaseTargetFieldInstructions], dict + ], + ) -> None: + """ + Set the flow class and group of instructions to use when creating new + instances of the flow class. + :param parsing_flow_class: the flow class to instantiate + :param instructions_to_attach_with_params: a key-value pair of field + instructions class and the paramteres to use when instantiating them + """ + self._parsing_flow_class = parsing_flow_class + self._instructions_to_attach_with_params = instructions_to_attach_with_params + + def get_new_flow(self) -> ParsingFlow: + """ + Instantiate a new parsing flow with the instantiated classes attached. + :return: the new parsing flow + """ + new_parsing_flow = self._parsing_flow_class() + + for instruction, params in self._instructions_to_attach_with_params.items(): + new_parsing_flow.add_instructions(instruction(**params)) + + return new_parsing_flow diff --git a/tests/parsing_utils_test.py b/tests/parsing_utils_test.py index a0a286a..e456ab4 100644 --- a/tests/parsing_utils_test.py +++ b/tests/parsing_utils_test.py @@ -4,6 +4,7 @@ from bs4 import BeautifulSoup from core.parsing_utils import ( ParsingFlow, + ParsingFlowGenerator, ReferenciaFieldInstructions, PrecioFieldInstructions, TamanoCategoricoFieldInstructions, @@ -2725,3 +2726,30 @@ def test_parsing_flow_fails_for_unrelated_html(unrelated_html): assert not parsing_flow.all_non_optional_fields_were_found and len( parsing_flow.issues ) == len(all_instructions) + + +def test_parsing_flow_generator_returns_proper_flows(): + + four_instructions_with_params = { + ReferenciaFieldInstructions: {}, + PrecioFieldInstructions: {}, + TamanoCategoricoFieldInstructions: {}, + SecondaryFeaturesFieldInstructions: { + "field_name": "personal", + "search_keyword": "Personal", + }, + } + + parsing_flow_generator = ParsingFlowGenerator( + parsing_flow_class=ParsingFlow, + instructions_to_attach_with_params=four_instructions_with_params, + ) + + a_new_parsing_flow = parsing_flow_generator.get_new_flow() + + assert ( + isinstance(a_new_parsing_flow, ParsingFlow), + len(a_new_parsing_flow._instructions) == len(four_instructions_with_params), + all([field.found is None for field in a_new_parsing_flow._instructions]), + all([field.valid is None for field in a_new_parsing_flow._instructions]), + )