Esempio n. 1
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_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)
Esempio n. 3
0
def test_content_save_wrong_fk(language, lang_testing_result, views,
                               view_testing_result, content_schema,
                               content_get_schema, saved_content_instance):
    '''
    Saving instance with foreign keys not in respective tables or without foreign key.
    '''
    _content_gen = saved_content_instance()
    # Get content instance for cleaning up and pks:
    _content = next(_content_gen)
    _content_json = content_get_schema.dump(_content)
    _allowed_json = {'view_id': views, 'locale_id': language}
    _new_content_json = _content_json.copy()
    for key in _allowed_json.keys():
        _new_content_json[key] = _allowed_json[key]
    _new_content = content_schema.load(_new_content_json,
                                       session=dbs_global.session)
    result = _new_content.save_to_db()
    # print(result)
    if (lang_testing_result == 'None') and (view_testing_result == 'None'):
        assert result is None
        _new_content.delete_fm_db()
    else:
        result.find('foreign key constraint fails') != -1

    # Clean up db:
    next(_content_gen)
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
Esempio n. 5
0
 def create_save(record_id: str = ''):
     record = content_schema.load(
         {
             'identity': record_id,
             'view_id': _view_id,
             'locale_id': _locale_id,
             'title': f'Title {record_id}',
             'content': f'Content {record_id}'
         },
         session=dbs_global.session)
     record.save_to_db()
     return record
Esempio n. 6
0
def test_ContentModel_serialize(client, element_dict):
    _identity = 'mock_identity'
    _view_id = choice(global_constants.get_VIEWS_PKS)
    _locale_id = choice(global_constants.get_PKS)
    _element = element_dict(marker='Element for serialization.')

    _instance = content_schema.load(
        {
            'identity': _identity,
            'view_id': _view_id,
            'locale_id': _locale_id,
            **_element
        },
        session=dbs_global.session)
    result = _instance.serialize()
    assert result.get('title') == _element.get('title')
    assert result.get('content') == _element.get('content')
Esempio n. 7
0
def test_ContentModel_find_all_like(client, 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_00 = choice(global_constants.get_VIEWS_PKS)
    _view_id_01 = _view_id_00
    # _view_id_01 = choice([item for item in global_constants.get_VIEWS_PKS
    #                       if item != _view_id_00])
    _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
    # _size_01 = randrange(3, 10)
    _type_00 = choice(ContentElementsBlock._types)
    _type_01 = choice(ContentElementsBlock._types)
    _subtype_00 = choice(ContentElementsBlock._subtypes)
    _subtype_01 = choice(ContentElementsBlock._subtypes)
    _name_00 = f'name of {_type_00}'
    _name_01 = f'name of {_type_01}'
    _identity_00 = '_'.join(
        [str(_upper_index_00).zfill(2), _type_00, _subtype_00])
    _identity_01 = '_'.join(
        [str(_upper_index_01).zfill(2), _type_01, _subtype_01])

    _elements_dict_00 = elements_dict(_size_00)
    _elements_dict_01 = elements_dict(_size_01)
    _block_instance_00 = ContentElementsBlock(upper_index=_upper_index_00,
                                              type=_type_00,
                                              subtype=_subtype_00,
                                              name=_name_00,
                                              elements=_elements_dict_00)
    _block_instance_01 = ContentElementsBlock(upper_index=_upper_index_01,
                                              type=_type_01,
                                              subtype=_subtype_01,
                                              name=_name_01,
                                              elements=_elements_dict_01)
    assert len(_block_instance_00.elements) == _size_00
    assert len(_block_instance_01.elements) == _size_01
    for element in _block_instance_00.serialize_to_content:
        _content_record = content_schema.load(
            {
                **element, 'view_id': _view_id_00,
                'locale_id': _locale_id
            },
            session=dbs_global.session)
        _content_record.save_to_db()
    for element in _block_instance_01.serialize_to_content:
        _content_record = content_schema.load(
            {
                **element, 'view_id': _view_id_01,
                'locale_id': _locale_id
            },
            session=dbs_global.session)
        _content_record.save_to_db()
    _seaching_criterions = {'view_id': _view_id_00, 'locale_id': _locale_id}
    _identity_like = f'{_identity_00}%'
    _found = ContentModel.find_identity_like(
        searching_criterions=_seaching_criterions,
        identity_like=_identity_like)
    assert len(_found) == _size_00

    _identity_like = f'{_identity_01}%'
    _found = ContentModel.find_identity_like(
        searching_criterions=_seaching_criterions,
        identity_like=_identity_like)
    assert len(_found) == _size_01
Esempio n. 8
0
def test_add_element_to_block(client):
    '''
    The following tests here:
    Update db intact - successfull update.
    Deleting redundant record.
    '''
    '''clean up contents table'''
    [_content.delete_fm_db() for _content in ContentModel.find()]
    '''Create test constants'''
    _content = ContentModel()
    _locale_id = choice(global_constants.get_PKS)
    _view_id = choice(global_constants.get_VIEWS_PKS)
    _record_id_body = '01_vblock_txt'
    # _ul_index = randrange(9)
    # _element_block_type = choice(
    #     global_constants.get_UPPER_LEVEL_TYPES_PKS)
    # _element_block_subtype = choice(
    #     global_constants.get_UPPER_LEVEL_SUBTYPES_PKS)
    _qnt = 4
    _block_id = '_'.join([_record_id_body, str(_qnt + 1)])
    _item_index = randrange(_qnt)
    _record_ids = _content.elem_ids('', _block_id)
    _user_id = randrange(128)
    '''Fill contents table'''
    def create_save(record_id: str = ''):
        record = content_schema.load(
            {
                'identity': record_id,
                'view_id': _view_id,
                'locale_id': _locale_id,
                'title': f'Title {record_id}',
                'content': f'Content {record_id}'
            },
            session=dbs_global.session)
        record.save_to_db()
        return record

    [create_save(_record_id) for _record_id in _record_ids]
    '''Testing add element'''
    '''Delete redundant record'''
    _redundant_record_id = '_'.join([_record_id_body, str(_qnt).zfill(3)])
    _redundant_record = content_schema.load(
        {
            'identity': _redundant_record_id,
            'view_id': _view_id,
            'locale_id': _locale_id,
            'title': f'Title {_redundant_record_id}',
            'content': f'Content {_redundant_record_id}'
        },
        session=dbs_global.session)
    _redundant_record.save_to_db()
    assert len(
        ContentModel.find({
            'view_id': _view_id,
            'locale_id': _locale_id
        })) == _qnt + 1

    result = ContentModel.add_element_to_block(_block_id, _item_index,
                                               _view_id, _locale_id, _user_id)
    '''Check what's happened'''
    _records_from_db = ContentModel.find({
        'view_id': _view_id,
        'locale_id': _locale_id
    })
    assert len(_records_from_db) == _qnt
    assert isinstance(result, dict)
    '''Success insertion'''
    result = ContentModel.add_element_to_block(_block_id, _item_index,
                                               _view_id, _locale_id, _user_id)
    # print('\ntest_model_contents, add element to block\n'
    #       'result ->', result)
    '''Check what's happened'''
    _records_from_db = ContentModel.find({
        'view_id': _view_id,
        'locale_id': _locale_id
    })
    assert len(_records_from_db) == _qnt + 1
    for (index, _record_from_db) in enumerate(_records_from_db):
        _json_from_db = content_schema.dump(_record_from_db)
        # print('\ntest_model_contents, \n index ->', index)
        # print('test_model_contents, \n '
        #       '_json_from_db ->', _json_from_db)
        if index < _item_index:
            assert _json_from_db.get('identity').split(
                '_')[-1] == _json_from_db.get('title').split('_')[-1]
            assert _json_from_db.get('identity').split(
                '_')[-1] == _json_from_db.get('content').split('_')[-1]
        if index == _item_index:
            assert _json_from_db.get('title') == 'dummy'
            assert _json_from_db.get('content') == 'dummy'
        if index > _item_index:
            assert _json_from_db.get('identity').split('_')[-1] == str(
                int(_json_from_db.get('title').split('_')[-1]) + 1).zfill(3)
            assert _json_from_db.get('identity').split('_')[-1] == str(
                int(_json_from_db.get('content').split('_')[-1]) + 1).zfill(3)