def _method( upper_index: int = 0, type: str = '', marker: str = ''): return ContentElementsSimple( upper_index=upper_index, type=type, name=f'name of {type}', element=ContentElement({ 'title': f'Title for {type} {marker}', 'content': f'Content for {type} {marker}' }) )
def test_ContentElementsBlock_get_set_element( element, elements_dict, # elements_inst ): _upper_index = 9 _size = 3 _type = 'vblock' _subtype = 'txt' _name = 'name' _elements_dict = elements_dict(_size) _block_instance = ContentElementsBlock(upper_index=_upper_index, type=_type, subtype=_subtype, name=_name, elements=_elements_dict) '''success''' for i, _element in enumerate(_elements_dict): assert _block_instance.get_element(i).value == _element # print('\ntest_content_elements_block:', # '\n test_ContentElementsBlock_get_set_element') # print(' element ->', element(99)) '''success, dictionary''' _index = randrange(_size) _element_dict = element(99) _block_instance.set_element(index=_index, value=_element_dict) assert _block_instance.get_element(index=_index).value\ == _element_dict '''success, dictionary''' _index = randrange(_size) _element_inst = ContentElement(element(100)) _block_instance.set_element(index=_index, value=_element_inst) assert _block_instance.get_element(index=_index).value\ == _element_inst.value '''fail, wrong element type''' _wrong_element = 'wrong' _index = randrange(_size) with pytest.raises(WrongElementTypeError) as e_info: _block_instance.set_element(index=_index, value=_wrong_element) assert str(e_info.value) ==\ ("Elements should be '['dict', 'ContentElement']', but at least " f"one of the elements has type '{type(_wrong_element)}'.") '''fail, indexes''' _negative_index = -1 with pytest.raises(WrongIndexError) as e_info: _block_instance.get_element(_negative_index) assert str(e_info.value)\ == (f"Length of element array is {_size} but you try to operate " f"with index '{_negative_index}'.") with pytest.raises(WrongIndexError) as e_info: _block_instance.get_element(_size) assert str(e_info.value)\ == (f"Length of element array is {_size} but you try to operate " f"with index '{_size}'.")
def test_ContentElement_check_keys(value): '''success''' # assert ContentElement.check_keys(value()) '''no keys''' with pytest.raises(WrongElementKeyError) as e_info: ContentElement.check_keys() assert str(e_info.value)\ == ("Content element value key should be either 'title' or " "'content', but nothing has been provided.") '''wrong key''' _wrong_title_key = 'wrong_title' _wrong_key_value = {**value(), _wrong_title_key: 'Mock title!'} _wrong_key_value.pop('title') with pytest.raises(WrongElementKeyError) as e_info: ContentElement.check_keys(_wrong_key_value) assert str(e_info.value)\ == ("Content element value key should be either 'title' " f"or 'content', but one of them is '{_wrong_title_key}'.") _wrong_content_key = 'wrong_content' _wrong_key_value = {**value(), _wrong_content_key: 'Mock content.'} _wrong_key_value.pop('content') with pytest.raises(WrongElementKeyError) as e_info: ContentElement.check_keys(_wrong_key_value) assert str(e_info.value)\ == ("Content element value key should be either 'title' " f"or 'content', but one of them is '{_wrong_content_key}'.")
def test_ContentElementsBlock_get_set_elements( element, elements_dict, # elements_inst ): _upper_index = 9 _size = 3 _type = 'vblock' _subtype = 'txt' _name = 'name' _elements_dict = elements_dict(_size) _block_instance = ContentElementsBlock(upper_index=_upper_index, type=_type, subtype=_subtype, name=_name, elements=_elements_dict) assert len(_block_instance.elements) == _size # print('\ntest_content_elements_block:', # '\n test_ContentElementsBlock_get_set_elements') '''getter testing''' for i, element in enumerate(_elements_dict): assert element == _block_instance.elements[i].value '''setter, dictionary testing''' _size = 5 _elements_dict = elements_dict(_size) _block_instance.elements = _elements_dict assert len(_block_instance.elements) == _size for i, element in enumerate(_elements_dict): assert element == _block_instance.elements[i].value '''setter, ContentElement instance''' _size = 6 _elements_dict = elements_dict(_size) _elements_instances = [] for element in _elements_dict: _elements_instances.append(ContentElement(element)) _block_instance.elements = _elements_instances assert len(_block_instance.elements) == _size for i, element in enumerate(_elements_dict): assert element == _block_instance.elements[i].value '''fail, wrong element type''' _wrong_type_element = 25 _elements_instances.append(_wrong_type_element) with pytest.raises(WrongElementTypeError) as e_info: _block_instance.elements = _elements_instances assert str(e_info.value) ==\ ("Elements should be '['dict', 'ContentElement']', but at least " f"one of the elements has type '{type(_wrong_type_element)}'.")
def _method( upper_index: int = 0, type: str = '', subtype: str = '', qnt: int = 1, marker: str = ''): _elements = [] for i in range(qnt): _elements.append(ContentElement({ 'title': f'Title for {type} ContentElement No {i}; {marker}!', 'content': f'Content for {type} ContentElement No {i}; {marker}.' })) return ContentElementsBlock( upper_index=upper_index, type=type, subtype=subtype, name=f'name of {type}', elements=_elements )
def test_ContentElement_init(value): '''Checking initiation and setter / getter''' '''success''' content_element = ContentElement(value()) common_items = content_element.value.items()\ & value().items() assert len(content_element.value) == len(value()) == len(common_items) _changed_title = 'Changed title' content_element.value = {'title': _changed_title} assert content_element.value.get('title') == _changed_title assert content_element.value.get('content') == value().get('content') '''wrong key''' '''setter''' _changed_content = 'Changed content' _wrong_content_key = 'wrong_content' with pytest.raises(WrongElementKeyError) as e_info: content_element.value = {_wrong_content_key: _changed_content} assert str(e_info.value)\ == ("Content element value key should be either 'title' " f"or 'content', but one of them is '{_wrong_content_key}'.") '''init''' _wrong_title_key = 'wrong_title' _wrong_key_value = {**value(), _wrong_title_key: 'Mock title!'} _wrong_key_value.pop('title') with pytest.raises(WrongElementKeyError) as e_info: content_element = ContentElement(_wrong_key_value) assert str(e_info.value)\ == ("Content element value key should be either 'title' " f"or 'content', but one of them is '{_wrong_title_key}'.") _wrong_content_key = 'wrong_content' _wrong_key_value = {**value(), _wrong_content_key: 'Mock content.'} _wrong_key_value.pop('content') with pytest.raises(WrongElementKeyError) as e_info: content_element = ContentElement(_wrong_key_value) assert str(e_info.value)\ == ("Content element value key should be either 'title' " f"or 'content', but one of them is '{_wrong_content_key}'.")
def _method(size: int = 0): _elements = [] [_elements.append(ContentElement(element(i))) for i in range(size)] return _elements
def _method(value): return ContentElement(value)