def test_PageView_insert_vals_fail(view_name, locale, simple_element, block_element, elements): _view_name = view_name() _locale = locale() _elements = elements() _page_view = PageView(view_name=_view_name, locale=_locale, elements=_elements) _insert_position = 1 _el_type = 'header' _marker = 'insert to beginning' _simple_element = simple_element(type=_el_type, marker=_marker) '''not element type''' simple_kwargs = { # 'element_type': _simple_element.type, # 'subtype': '', 'name': _simple_element.name, 'element_value': _simple_element.element.value } with pytest.raises(WrongValueError) as e_info: _page_view.insert_vals(_insert_position, **simple_kwargs) assert str(e_info.value) == ( f"You try to use '{''}' as upper level element type. It's wrong.") '''wrong element type''' _wrong_element_type = 5 simple_kwargs = { 'element_type': _wrong_element_type, # 'subtype': '', 'name': _simple_element.name, 'element_value': _simple_element.element.value } with pytest.raises(WrongTypeError) as e_info: _page_view.insert_vals(_insert_position, **simple_kwargs) assert str( e_info.value) == ("You try to operate upper level element type - " f"'{type(_wrong_element_type)}', it's wrong.") '''wrong subtype''' _insert_position = 2 _el_type = 'hblock' _el_subtype = 'txt' _qnt = 3 _marker = 'insert to end' _block_element = block_element(type=_el_type, subtype=_el_subtype, qnt=_qnt, marker=_marker) _element_values = [item.value for item in _block_element.elements] _wrong_subtype = 'shit' _block_kwargs = { 'element_type': _block_element.type, 'subtype': _wrong_subtype, 'name': _block_element.name, 'element_value': _element_values } with pytest.raises(WrongValueError) as e_info: _page_view.insert_vals(_insert_position, **_block_kwargs) assert str(e_info.value) == ( "Block element subtype shoud be within " f"'{ContentElementsBlock._subtypes}', but provided subtype is " f"'{_wrong_subtype}'.")
def test_PageView_insert_vals_success(client, view_name, locale, simple_element, block_element, elements): # '''clean up tables''' # [_content.delete_fm_db() for _content in ContentModel.find()] # [_structure.delete_fm_db() for _structure in StructureModel.find()] _view_name = view_name() _locale = 'en' # _locale = locale() _elements = elements() _page_view = PageView(view_name=_view_name, locale=_locale, elements=_elements) '''insert simple upper level element to beginning''' _insert_position = 0 _el_type = 'header' _marker = 'inserted to beginning' _simple_element = simple_element(type=_el_type, marker=_marker) _inserting_json = dumps(ul_element_extractor(_simple_element), sort_keys=True) simple_kwargs = { 'element_type': _simple_element.type, # 'subtype': '', 'name': _simple_element.name, 'element_value': _simple_element.element.value } _length = len(_page_view.elements) _last_elem_dict = _page_view.get_element_vals(_length - 1) _page_view.insert_vals(_insert_position, **simple_kwargs) '''testing results''' '''length''' assert len(_page_view.elements) == _length + 1 '''inserted element should be save as in page view''' assert dumps(_page_view.get_element_vals(_insert_position), sort_keys=True) == _inserting_json '''check last element''' _last_elem_dict['index'] = _last_elem_dict.get('index') + 1 _last_elem_json = dumps(_last_elem_dict, sort_keys=True) assert dumps(_page_view.get_element_vals(_length), sort_keys=True) == _last_elem_json # print('\ntest_page_view:\n test_PageView_insert_vals') # print(' simple_kwargs ->', simple_kwargs) # [print(dumps(element.serialize_to_content, indent=4)) # for element in _page_view.elements] '''insert to end''' _length = len(_page_view.elements) _insert_position = _length _el_type = 'footer' _marker = 'insert to end' _simple_element = simple_element(upper_index=_insert_position, type=_el_type, marker=_marker) _inserting_json = dumps(ul_element_extractor(_simple_element), sort_keys=True) simple_kwargs = { 'element_type': _simple_element.type, # 'subtype': '', 'name': _simple_element.name, 'element_value': _simple_element.element.value } _last_elem_dict = _page_view.get_element_vals(_length - 1) _page_view.insert_vals(_insert_position, **simple_kwargs) '''tests''' assert len(_page_view.elements) == _length + 1 assert dumps(_page_view.get_element_vals(_insert_position), sort_keys=True) == _inserting_json '''insert in a between''' _length = len(_page_view.elements) _insert_position = randrange(1, _length) _el_type = 'hblock' _el_subtype = 'pix' _qnt = randrange(7) _marker = 'insert to someware' _block_element = block_element(upper_index=_insert_position, type=_el_type, subtype=_el_subtype, qnt=_qnt, marker=_marker) _inserting_dict = ul_element_extractor(_block_element) _element_values = [item.value for item in _block_element.elements] _block_kwargs = { 'element_type': _block_element.type, 'subtype': _block_element.subtype, 'name': _block_element.name, 'element_value': _element_values } _page_view.insert_vals(_insert_position, **_block_kwargs) '''testing''' assert len(_page_view.elements) == _length + 1 _inserting_json = dumps(_inserting_dict, sort_keys=True) assert dumps(_page_view.get_element_vals(_insert_position), sort_keys=True) == _inserting_json