Created a new flow generator + tests for it.
This commit is contained in:
parent
b8d4893026
commit
2b249063e0
2 changed files with 64 additions and 1 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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]),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue