def test_ContentElementsSimple_load_fm_db(client, value):
    '''clean up structure and content tables'''
    # [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]
    '''init'''
    '''Create test constants'''
    _view_id = choice(global_constants.get_VIEWS_PKS)
    _locale_id = choice(global_constants.get_PKS)
    _record_id = '_'.join([
        str(randrange(99)).zfill(2),
        choice(ContentElementsSimple._types)])
    _value = value('simple element load fm db testing')
    '''Fill contents table'''
    _record = content_schema.load({
        'identity': _record_id, 'view_id': _view_id,
        'locale_id': _locale_id, 'title': _value.get('title'),
        'content': _value.get('content')
    }, session=dbs_global.session)
    _record.save_to_db()
    content_elements_simple = ContentElementsSimple.load_fm_db(
        identity=_record_id, view_id=_view_id, locale_id=_locale_id)
    assert content_elements_simple.upper_index\
        == int(_record_id.split('_')[0])
    assert content_elements_simple.type == _record_id.split('_')[1]
    _simple_json = json.dumps(
        content_elements_simple.element.value, sort_keys=True)
    # _value_json =
    assert _simple_json == json.dumps(_value, sort_keys=True)
コード例 #2
0
def test_PageView_load_fm_db(client, view_name, locale, elements):
    '''clean up content table'''
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]

    _user_id = randrange(128)
    _view_name = view_name()
    _locale = locale()
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=_user_id)
    '''success'''
    _page_view_fm_db = PageView.load_fm_db(ids={
        'view_id': _view_name,
        'locale_id': _locale
    })
    assert _page_view_fm_db.view_name == _page_view.view_name
    assert _page_view_fm_db.locale == _page_view.locale
    assert len(_page_view_fm_db.elements) == len(_page_view.elements)
    for i, element in enumerate(_page_view.elements):
        _type = type(element)
        assert isinstance(_page_view_fm_db.elements[i], _type)
        assert _page_view_fm_db.elements[i].name == element.name
        if isinstance(element, ContentElementsBlock):
            assert len(_page_view_fm_db.elements[i].elements)\
                == len(element.elements)
    '''fails'''
    _wrong = 'wrong'
    _fm_db = PageView.load_fm_db({'view_id': _wrong, 'locale_id': locale})
    assert _fm_db is None
コード例 #3
0
def test_ContentElementsBlock_load_fm_db(client, element, elements_dict):
    '''clean up content tables'''
    # [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]

    _view_id = choice(global_constants.get_VIEWS_PKS)
    _locale_id = choice(global_constants.get_PKS)
    _upper_index_00 = randrange(100)
    _upper_index_01 = randrange(100)
    while _upper_index_01 == _upper_index_00:
        _upper_index_01 = randrange(100)
    _size_00 = 3
    _size_01 = 5
    _type_00 = choice(ContentElementsBlock._types)
    _type_01 = choice(
        [item for item in ContentElementsBlock._types if item != _type_00])
    _subtype = choice(ContentElementsBlock._subtypes)
    _name_00 = f'name of {_type_00}'
    _name_01 = f'name of {_type_01}'

    _elements_dict_00 = elements_dict(_size_00)
    _block_instance_00 = ContentElementsBlock(upper_index=_upper_index_00,
                                              type=_type_00,
                                              subtype=_subtype,
                                              name=_name_00,
                                              elements=_elements_dict_00)
    assert len(_block_instance_00.elements) == _size_00
    for element in _block_instance_00.serialize_to_content:
        _content_record = content_schema.load(
            {
                **element, 'view_id': _view_id,
                'locale_id': _locale_id
            },
            session=dbs_global.session)
        _content_record.save_to_db()

    _elements_dict_01 = elements_dict(_size_01)
    _block_instance_01 = ContentElementsBlock(upper_index=_upper_index_01,
                                              type=_type_01,
                                              subtype=_subtype,
                                              name=_name_01,
                                              elements=_elements_dict_01)
    assert len(_block_instance_01.elements) == _size_01

    for element in _block_instance_01.serialize_to_content:
        _content_record = content_schema.load(
            {
                **element, 'view_id': _view_id,
                'locale_id': _locale_id
            },
            session=dbs_global.session)
        _content_record.save_to_db()
    '''testing'''
    _identity = '_'.join([str(_upper_index_00).zfill(2), _type_00, _subtype])
    loaded_block_instance = ContentElementsBlock.load_fm_db(
        identity=_identity, view_id=_view_id, locale_id=_locale_id)

    for i, instance in enumerate(loaded_block_instance.elements):
        assert instance.value == _elements_dict_00[i]
def test_ContentElementsSimple_delete_fm_db(client, value):
    '''clean up structure and content tables'''
    # [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]
    '''init'''
    _view_id = choice(global_constants.get_VIEWS_PKS)
    _locale_id = choice(global_constants.get_PKS)
    _upper_index = randrange(100)
    _type = 'header'
    _name = 'name'
    _value = value('db value')
    _element = value('element value')
    _identity = '_'.join([str(_upper_index).zfill(2), _type])

    '''creating record in db'''
    _record = content_schema.load({
        'identity': _identity,
        'view_id': _view_id, 'locale_id': _locale_id,
        'title': _value.get('title'), 'content': _value.get('content')
    }, session=dbs_global.session)
    _record.save_to_db()
    '''test it exists'''
    _found_db_instance = ContentModel.find_by_identity_view_locale(
        identity=_identity,
        view_id=_view_id, locale_id=_locale_id
    )
    assert _found_db_instance.identity\
        == _identity
    assert _found_db_instance.view_id == _view_id
    assert _found_db_instance.locale_id == _locale_id
    assert _found_db_instance.title == _value.get('title')
    assert _found_db_instance.content == _value.get('content')

    '''create element instance with same PKs and delete record with
        the element'''
    _element_instance = ContentElementsSimple(
        upper_index=_upper_index, type=_type,
        name=_name, element=_element)
    _element_instance.delete_fm_db(view_id=_view_id, locale_id=_locale_id)
    '''check there is nothing in db'''
    _found_db_instance = ContentModel.find_by_identity_view_locale(
        identity=_identity,
        view_id=_view_id, locale_id=_locale_id)
    assert _found_db_instance is None

    '''try to delete the instance once more'''
    with pytest.raises(RecordNotFoundError) as e_info:
        _element_instance.delete_fm_db(
            view_id=_view_id, locale_id=_locale_id)
    # assert str(e_info.value)\
    #     == (f"Record with identity '{_identity}', view id '{_view_id}' "
    #         f"and locale id '{_locale_id}' has not been found.")
    assert str(e_info.value).find(_identity) != -1
    assert str(e_info.value).find(_view_id) != -1
    assert str(e_info.value).find(_locale_id) != -1
コード例 #5
0
def test_PageView_remove_vals(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 = locale()
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _length = len(_page_view.elements)
    _remove_position = randrange(_length)
    _element_in_page = dumps(_page_view.get_element_vals(_remove_position),
                             sort_keys=True)
    _removed_element = dumps(_page_view.remove_vals(_remove_position),
                             sort_keys=True)
    assert len(_page_view.elements) == _length - 1
    assert _removed_element == _element_in_page
    _last_element = _page_view.get_element_vals(_length - 2)
    assert _last_element.get('index') == len(_page_view.elements) - 1
    _page_view.save_to_db(user_id=randrange(128))
コード例 #6
0
def test_PageView_move_element(client, view_name, locale, simple_element,
                               block_element, elements):
    '''it works I don't like to test success movements'''
    '''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 = locale()
    _ids = {'view_id': _view_name, 'locale_id': _locale}
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=1)
    _length = len(_page_view.elements)
    '''success'''
    '''moving up'''
    # _index = 1
    _index = randrange(1, _length)
    _direction = 'up'
    _moving_element = _page_view.get_element_vals(_index)
    _moving_element.pop('index')
    _moving_element_json = dumps(_moving_element, sort_keys=True)

    _shifting_element = _page_view.get_element_vals(_index - 1)
    _shifting_element.pop('index')
    _shifting_element_json = dumps(_shifting_element, sort_keys=True)

    _page_view.move_element(_index, _direction)
    _page_view.save_to_db(user_id=2)
    _loaded_view = PageView.load_fm_db(ids=_ids)

    _moved_element = _loaded_view.get_element_vals(_index - 1)
    _moved_element.pop('index')
    _moved_element_json = dumps(_moved_element, sort_keys=True)

    _shifted_element = _loaded_view.get_element_vals(_index)
    _shifted_element.pop('index')
    _shifted_element_json = dumps(_shifted_element, sort_keys=True)
    assert _moved_element_json == _moving_element_json
    assert _shifted_element_json == _shifting_element_json
    # print('\ntest_page_view:\n test_PageView_move_element')
    # [print(dumps(element.serialize_to_content, indent=4))
    #  for element in _loaded_view.elements]
    '''moving down'''
    _index = randrange(0, _length - 1)
    _direction = 'down'
    _moving_element = _page_view.get_element_vals(_index)
    _moving_element.pop('index')
    _moving_element_json = dumps(_moving_element, sort_keys=True)

    _shifting_element = _page_view.get_element_vals(_index + 1)
    _shifting_element.pop('index')
    _shifting_element_json = dumps(_shifting_element, sort_keys=True)

    _page_view.move_element(_index, _direction)

    _moved_element = _page_view.get_element_vals(_index + 1)
    _moved_element.pop('index')
    _moved_element_json = dumps(_moved_element, sort_keys=True)

    _shifted_element = _page_view.get_element_vals(_index)
    _shifted_element.pop('index')
    _shifted_element_json = dumps(_shifted_element, sort_keys=True)
    assert _moved_element_json == _moving_element_json
    assert _shifted_element_json == _shifting_element_json
    '''fails'''
    '''wrong combination index - direction'''
    _direction = 'up'
    _index = 0
    with pytest.raises(WrongIndexError) as e_info:
        _page_view.move_element(_index, _direction)
    assert str(e_info.value).find('-1') != -1

    _direction = 'down'
    _index = _length - 1
    with pytest.raises(WrongIndexError) as e_info:
        _page_view.move_element(_index, _direction)
    assert str(e_info.value).find('4') != -1
    '''wrong direction'''
    _direction = 'f**k'
    with pytest.raises(WrongDirection) as e_info:
        _page_view.move_element(_index, _direction)
    assert str(e_info.value).find(_direction) != -1
コード例 #7
0
def test_ContentElementsBlock_save_to_db_content_structure(
        client, element, elements_dict):
    '''clean up content table'''
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]
    '''create testing consants'''
    _view_id = choice(global_constants.get_VIEWS_PKS)
    _locale_id = choice(global_constants.get_PKS)
    _upper_index = randrange(100)
    _user_id = randrange(128)
    _size = 10
    # _size = randrange(3, 20)
    _type = choice(ContentElementsBlock._types)
    _subtype = choice(ContentElementsBlock._subtypes)
    _name = f'name of {_type}'
    _elements_dict = elements_dict(_size)
    '''create ContentElementsBlock instance'''
    _block_instance = ContentElementsBlock(upper_index=_upper_index,
                                           type=_type,
                                           subtype=_subtype,
                                           name=_name,
                                           elements=_elements_dict)
    assert len(_block_instance.elements) == _size
    '''save it to db'''
    _block_instance.save_to_db_content(view_id=_view_id,
                                       locale_id=_locale_id,
                                       user_id=_user_id,
                                       save_structure=True)
    '''load insance having PKs and check it adequate to source'''
    _loaded_instance = ContentElementsBlock.load_fm_db(identity='_'.join(
        [str(_upper_index).zfill(2), _type, _subtype]),
                                                       view_id=_view_id,
                                                       locale_id=_locale_id,
                                                       load_name=True)
    assert _loaded_instance.upper_index == _upper_index
    assert _loaded_instance.type == _type
    assert _loaded_instance.subtype == _subtype
    assert _loaded_instance.name == _name
    for i, element in enumerate(_loaded_instance.elements):
        assert element.value == _elements_dict[i]
    # print('\ntest_content_elements_block:',
    #       '\n test_ContentElementsBlock_save_to_db_content',
    #       '\n  _loaded_instance ->', _loaded_instance)
    '''get records directly from content table'''
    _identity_like = '_'.join([str(_upper_index).zfill(2), _type, _subtype])
    _content_model_instance = ContentModel.find_identity_like(
        searching_criterions={
            'view_id': _view_id,
            'locale_id': _locale_id
        },
        identity_like=f"{_identity_like}%",
    )
    assert len(_content_model_instance) == _size
    for i, element in enumerate(_content_model_instance):
        _element_dict = content_get_schema.dump(element)
        assert _element_dict == {
            'identity': '_'.join([_identity_like,
                                  str(i).zfill(3)]),
            'view_id': _view_id,
            'locale_id': _locale_id,
            'user_id': _user_id,
            **_elements_dict[i]
        }
    '''get record from structure table, check it'''
    _structure_dict = StructureModel.find_by_ids({
        'view_id': _view_id,
        'locale_id': _locale_id
    }).get_element(_upper_index)
    assert _structure_dict == {
        'type': _type,
        'subtype': _subtype,
        'qnt': _size,
        'name': f'name of {_type}'
    }
    '''correct loaded ContentElementsBlock instance and reduce element
        quantity'''
    _block_instance.remove(randrange(len(_block_instance.elements)))
    _block_instance.remove(randrange(len(_block_instance.elements)))
    _index = randrange(len(_block_instance.elements))
    _new_title = f"corrected title for element No '{_index}'"
    _new_element = {
        **_block_instance.get_element(index=_index).value, 'title': _new_title
    }
    _block_instance.set_element(index=_index, value=_new_element)
    '''save it to db'''
    _block_instance.save_to_db_content(view_id=_view_id,
                                       locale_id=_locale_id,
                                       user_id=_user_id,
                                       save_structure=True)
    '''load new instance and check it adequate to changes'''
    _corrected_model_instance = ContentModel.find_identity_like(
        searching_criterions={
            'view_id': _view_id,
            'locale_id': _locale_id
        },
        identity_like=f"{_identity_like}%",
    )
    assert len(_corrected_model_instance) == _size - 2
    assert _corrected_model_instance[_index].serialize().get('title')\
        == _new_title
    '''get record from structure table, check it'''
    _structure_dict = StructureModel.find_by_ids({
        'view_id': _view_id,
        'locale_id': _locale_id
    }).get_element(_upper_index)
    assert _structure_dict == {
        'type': _type,
        'subtype': _subtype,
        'qnt': _size - 2,
        'name': f'name of {_type}'
    }
コード例 #8
0
def test_ContentElementsBlock_move(client, view_name, locale, element,
                                   elements_dict):
    '''clean up content table'''
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]
    '''object variables'''
    _view_name = view_name()
    _locale = locale()
    _upper_index = randrange(3, 20)
    # _size = 3
    _size = randrange(2, 10)
    _type = 'vblock'
    _subtype = 'txt'
    _name = f'name of {_type}'

    _elements_dict = elements_dict(_size)
    # _element = element(_size)
    _block_instance = ContentElementsBlock(upper_index=_upper_index,
                                           type=_type,
                                           subtype=_subtype,
                                           name=_name,
                                           elements=_elements_dict)
    assert len(_block_instance._elements) == _size
    _block_instance.save_to_db_content(view_id=_view_name,
                                       locale_id=_locale,
                                       user_id=randrange(128),
                                       save_structure=True)
    # _index = 1
    _index = randrange(_size - 1)
    '''fail'''
    '''wrong direction'''
    _wrong = 'f**k'
    with pytest.raises(WrongDirection) as e_info:
        _block_instance.move(index=_index, direction=_wrong)
    assert str(e_info.value).find(_wrong) != -1
    '''success, down'''
    _direction = 'down'
    _moving_element = _block_instance.get_element(_index)
    _replacing_element = _block_instance.get_element(_index + 1)
    _block_instance.move(index=_index, direction=_direction)
    _block_instance.save_to_db_content(view_id=_view_name,
                                       locale_id=_locale,
                                       user_id=randrange(128))
    _loaded_instance = ContentElementsBlock.load_fm_db(identity='_'.join(
        [str(_upper_index).zfill(2), _type, _subtype]),
                                                       view_id=_view_name,
                                                       locale_id=_locale,
                                                       load_name=True)
    _moved_element = _loaded_instance.get_element(_index + 1)
    _replaced_element = _loaded_instance.get_element(_index)
    assert _moving_element.value == _moved_element.value
    assert _replacing_element.value == _replaced_element.value
    '''success, up'''
    _index = randrange(1, _size)
    _direction = 'up'
    _moving_element = _block_instance.get_element(_index)
    _replacing_element = _block_instance.get_element(_index - 1)
    _block_instance.move(index=_index, direction=_direction)
    _block_instance.save_to_db_content(view_id=_view_name,
                                       locale_id=_locale,
                                       user_id=randrange(128))
    _loaded_instance = ContentElementsBlock.load_fm_db(identity='_'.join(
        [str(_upper_index).zfill(2), _type, _subtype]),
                                                       view_id=_view_name,
                                                       locale_id=_locale,
                                                       load_name=True)
    _moved_element = _loaded_instance.get_element(_index - 1)
    _replaced_element = _loaded_instance.get_element(_index)
    assert _moving_element.value == _moved_element.value
    assert _replacing_element.value == _replaced_element.value
    '''fail, wrong direction - index combination'''
    _index = 0
    _direction = 'up'
    with pytest.raises(WrongIndexError) as e_info:
        _block_instance.move(index=_index, direction=_direction)
    assert str(e_info.value).find(str(_index - 1))
    _index = len(_block_instance.elements) - 1
    _direction = 'down'
    with pytest.raises(WrongIndexError) as e_info:
        _block_instance.move(index=_index, direction=_direction)
    assert str(e_info.value).find(str(_index + 1))
コード例 #9
0
def test_UpperLevelHandling_patch(client, create_test_content, user_instance,
                                  access_token, view_name, locale, elements,
                                  lng, test_word_00, test_word_01,
                                  test_word_02, test_word_03, test_word_04,
                                  test_word_05):
    '''clean up tables'''
    [_content.delete_fm_db() for _content in ContentModel.find()]
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    '''create and save new contents in tables'''
    _view_name = view_name()
    _locale = lng
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=randrange(128))
    # _index = 1
    '''Create user and admin'''
    _user = user_instance({'role_id': 'user'})
    _user.save_to_db()
    _admin = user_instance({'role_id': 'admin'})
    _admin.save_to_db()
    '''create tokens'''
    _admin_access_token = access_token(_admin)
    _user_access_token = access_token(_user)
    '''creating headers'''
    _admin_headers = {
        'Authorization': f'Bearer {_admin_access_token}',
        'Content-Type': 'application/json',
        'Accept-Language': _locale
    }
    _user_headers = {
        'Authorization': f'Bearer {_user_access_token}',
        'Content-Type': 'application/json',
        'Accept-Language': _locale
    }
    '''success'''
    '''remove random element'''
    _index = randrange(len(_elements) - 1)
    _identity = str(_index).zfill(2)
    _json = {'view_id': _view_name, 'identity': _identity}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_admin_headers)
    assert _resp.status_code == 200
    assert _resp.json.get('message').find(test_word_00) != -1
    '''failues'''
    '''No tokens'''
    _no_token_header = {
        k: v
        for (k, v) in _admin_headers.items() if k != 'Authorization'
    }
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_no_token_header)
    assert _resp.status_code == 401
    assert _resp.json.get('description').find(test_word_01) != -1
    assert _resp.json.get('error') == 'authorization_required'
    '''Non admin'''
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_user_headers)
    assert _resp.status_code == 401
    assert _resp.json.get('message').find(test_word_02) != -1
    '''No block identity'''
    _json_no_id = {k: v for (k, v) in _json.items() if k != 'identity'}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json_no_id,
                         headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload') == "'identity'"
    '''wrong identity'''
    '''first part is not integer'''
    _wrong_identity = 'f**k'
    _json = {'view_id': _view_name, 'identity': _wrong_identity}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json == (
        f"invalid literal for int() with base 10: '{_wrong_identity}'")
    '''index out of range'''
    _wrong_identity = '-1'
    _json = {'view_id': _view_name, 'identity': _wrong_identity}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload').find(_wrong_identity) != -1

    _wrong_identity = str(len(_page_view.elements))
    _json = {'view_id': _view_name, 'identity': _wrong_identity}
    _resp = client.patch(url_for('contents_bp.upperlevelhandling'),
                         json=_json,
                         headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload').find(_wrong_identity) != -1
コード例 #10
0
def test_UpperLevelHandling_post(client, create_test_content, user_instance,
                                 access_token, view_name, locale, elements,
                                 lng, test_word, test_word_01, test_word_02,
                                 test_word_03, test_word_04):
    '''clean up tables'''
    [_content.delete_fm_db() for _content in ContentModel.find()]
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    '''create and save new contents in tables'''
    _view_name = view_name()
    _locale = lng
    _elements = elements()
    _page_view = PageView(view_name=_view_name,
                          locale=_locale,
                          elements=_elements)
    _page_view.save_to_db(user_id=randrange(128))
    # _index = 1
    '''Create user and admin'''
    _user = user_instance({'role_id': 'user'})
    _user.save_to_db()
    _admin = user_instance({'role_id': 'admin'})
    _admin.save_to_db()
    '''create tokens'''
    _admin_access_token = access_token(_admin)
    _user_access_token = access_token(_user)
    '''creating headers'''
    _admin_headers = {
        'Authorization': f'Bearer {_admin_access_token}',
        'Content-Type': 'application/json',
        'Accept-Language': _locale
    }
    _user_headers = {
        'Authorization': f'Bearer {_user_access_token}',
        'Content-Type': 'application/json',
        'Accept-Language': _locale
    }
    '''success'''
    _direction = 'up'
    _index = randrange(1, len(_elements))
    _index_key = str(_index).zfill(2)
    _info_json = _elements[_index].serialize_to_structure
    _identity = '_'.join(
        [list(_info_json.keys())[0],
         _info_json.get(_index_key).get('type')])
    if isinstance(_page_view.elements[_index], ContentElementsBlock):
        _identity = '_'.join(
            [_identity, _info_json.get(_index_key).get('subtype')])
    _json = {
        'view_id': _view_name,
        'identity': _identity,
        'direction': _direction
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json,
                        headers=_admin_headers)
    assert _resp.status_code == 200
    assert _resp.json.get('message').find(test_word) != -1
    '''down'''
    _direction = 'down'
    _index = randrange(len(_elements) - 1)
    _info_json = _elements[_index].serialize_to_structure
    _identity = '_'.join([
        list(_info_json.keys())[0],
        _info_json.get(str(_index).zfill(2)).get('type')
    ])
    if isinstance(_page_view.elements[_index], ContentElementsBlock):
        _identity = '_'.join(
            [_identity,
             _info_json.get(str(_index).zfill(2)).get('subtype')])
    _json = {
        'view_id': _view_name,
        'identity': _identity,
        'direction': _direction
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json,
                        headers=_admin_headers)
    assert _resp.status_code == 200
    assert _resp.json.get('message').find(test_word) != -1
    '''failues'''
    '''No tokens'''
    _no_token_header = {
        k: v
        for (k, v) in _admin_headers.items() if k != 'Authorization'
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json,
                        headers=_no_token_header)
    assert _resp.status_code == 401
    assert _resp.json.get('description').find(test_word_01) != -1
    '''Non admin'''
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json,
                        headers=_user_headers)
    assert _resp.status_code == 401
    assert _resp.json.get('message').find(test_word_02) != -1
    '''No block identity'''
    _json_no_id = {k: v for (k, v) in _json.items() if k != 'identity'}
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json_no_id,
                        headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload') == "'identity'"
    '''wrong direction - index combination'''
    _index = 0
    _direction = 'up'
    _index_key = str(_index).zfill(2)
    _info_json = _elements[_index].serialize_to_structure
    _identity = '_'.join([_index_key, _info_json.get(_index_key).get('type')])
    if isinstance(_page_view.elements[_index], ContentElementsBlock):
        _identity = '_'.join(
            [_identity, _info_json.get(_index_key).get('subtype')])
    _json_up = {
        'view_id': _view_name,
        'identity': _identity,
        'direction': _direction
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json_up,
                        headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload').find(str(_index - 1)) != -1

    _index = len(_elements) - 1
    _direction = 'down'
    _index_key = str(_index).zfill(2)
    _info_json = _elements[_index].serialize_to_structure
    _identity = '_'.join([_index_key, _info_json.get(_index_key).get('type')])
    if isinstance(_page_view.elements[_index], ContentElementsBlock):
        _identity = '_'.join(
            [_identity, _info_json.get(_index_key).get('subtype')])
    _json_down = {
        'view_id': _view_name,
        'identity': _identity,
        'direction': _direction
    }
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_json_down,
                        headers=_admin_headers)
    assert _resp.status_code == 400
    assert _resp.json.get('message').find(test_word_03) != -1
    assert _resp.json.get('payload').find(str(_index + 1)) != -1
    '''generate not found error'''
    _wrong_view_id = 'wrong'
    _wrong_view_id_json = {**_json, 'view_id': _wrong_view_id}
    _resp = client.post(url_for('contents_bp.upperlevelhandling'),
                        json=_wrong_view_id_json,
                        headers=_admin_headers)
    assert _resp.status_code == 404
    assert _resp.json.get('message').find(test_word_04) != -1
    assert _resp.json.get('message').find(_wrong_view_id) != -1
    '''clean up users'''
    if _user is not None:
        _user.delete_fm_db()
    if _admin is not None:
        _admin.delete_fm_db()
コード例 #11
0
def test_ContentElementsSimple_save_to_db_content_structure(
        client, value):
    '''clean up structure and content tables'''
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    [_content.delete_fm_db() for _content in ContentModel.find()]
    '''init'''
    _view_id = choice(global_constants.get_VIEWS_PKS)
    _locale_id = choice(global_constants.get_PKS)
    _upper_index = randrange(100)
    _user_id = randrange(64)
    _type = 'header'
    _name = 'name'
    # _value = value('db value')
    _element = value('element value')

    '''creating element for testing'''
    _content_element = ContentElementsSimple(
        upper_index=_upper_index, type=_type,
        name=_name, element=_element)
    _content_element.save_to_db_content(
        view_id=_view_id, locale_id=_locale_id, user_id=_user_id,
        save_structure=True)
    '''testing useing load'''
    _loaded_instance = ContentElementsSimple.load_fm_db(
        identity={'_'.join([str(_upper_index).zfill(2), _type])},
        view_id=_view_id, locale_id=_locale_id, load_name=True)

    '''test loaded element'''
    assert _loaded_instance.upper_index == _upper_index
    assert _loaded_instance.type == _type
    assert _loaded_instance.name == _name
    assert _loaded_instance.element.value == _element

    '''testing records in tables'''
    _found_db_instance = ContentModel.find_by_identity_view_locale(
        identity='_'.join([str(_upper_index).zfill(2), _type]),
        view_id=_view_id, locale_id=_locale_id
    )
    assert _found_db_instance.identity\
        == '_'.join([str(_upper_index).zfill(2), _type])
    assert _found_db_instance.title == _element.get('title')
    assert _found_db_instance.content == _element.get('content')

    '''update db record with element instance, same PKs'''
    _element = value(marker='new value')
    _name = f'new {_name}'
    # _upper_index = randrange(50, 100)
    _user_id = randrange(64, 128)
    _new_instance = ContentElementsSimple(
        upper_index=_upper_index, type=_type,
        name=_name, element=_element)
    _new_instance.save_to_db_content(
        view_id=_view_id, locale_id=_locale_id,
        user_id=_user_id, save_structure=True)
    _found_identity = ContentElementsSimple.load_fm_db(
        identity='_'.join([str(_upper_index).zfill(2), _type]),
        view_id=_view_id,
        locale_id=_locale_id,
        load_name=True
    )
    assert _found_identity.upper_index == _upper_index
    assert _found_identity.type == _type
    # assert _found_identity.name == _name
    assert _found_identity.element.value == _element