コード例 #1
0
def test_StructureModel_find_by_ids(client, create_test_content):
    lng = 'en'
    _content_details = create_test_content(locale=lng)
    _ids = {
        'view_id': _content_details.get('view_id'),
        'locale_id': _content_details.get('locale_id')
    }
    '''success'''
    _attributes = StructureModel.find_by_ids(ids=_ids).attributes
    for key in [
            item for item in _content_details.keys()
            if item not in ['view_id', 'locale_id']
    ]:
        _index = _content_details.get(key).get('upper_index')
        _contant_extract = {
            k: v
            for (k, v) in _content_details.get(key).items()
            if k not in ['upper_index', 'structure']
        }
        assert _attributes.get(str(_index).zfill(2)) == _contant_extract
    # print('\ntest_model_structure:\n test_StructureModel_find_by_ids',)
    # print('  _contant_extract ->', _contant_extract)
    '''fails'''
    '''wrong keys'''
    _wrong = '_wrong_key'
    _wrong_ids = {**_ids, _wrong: _content_details.get('view_id')}
    _wrong_ids.pop('view_id')
    with pytest.raises(InvalidRequestError) as e_info:
        StructureModel.find_by_ids(ids=_wrong_ids)
    assert str(e_info.value) ==\
        (f'Entity namespace for "structure" has no property "{_wrong}"')
    '''not found'''
    _wrong_ids = {**_ids, 'view_id': 'wrong'}
    assert StructureModel.find_by_ids(ids=_wrong_ids) is None
コード例 #2
0
def test_change_element_qnt(saved_structure_instance, structure_get_schema,
                            attributes):
    '''clean up structure tables'''
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    '''Fill structure table'''
    fill_structure()
    '''Choose constants to work with structure'''
    _criterions = {
        'view_id': choice(global_constants.get_VIEWS_PKS),
        'locale_id': choice(global_constants.get_PKS)
    }
    _block_index = 1
    _qnt = attributes.get(str(_block_index).zfill(2)).get('qnt')
    _first_user_id = randrange(128)
    '''Update structure'''
    _updating_structure = StructureModel.find_by_ids(_criterions)
    _updating_structure.update({'attributes': attributes})
    _us_dumped = structure_get_schema.dump(_updating_structure)
    _element_qnt = _us_dumped.get('attributes').get(
        str(_block_index).zfill(2)).get('qnt')
    _user_id = _us_dumped.get('user_id')
    '''Expect it's updated as per available constants'''
    assert _element_qnt == _qnt
    assert _user_id == 0
    '''add element'''
    _updating_structure.change_element_qnt('inc', _block_index, _first_user_id)
    '''Get elements and check expectations'''
    _updated_structure = StructureModel.find_by_ids(_criterions)

    _us_dumped = structure_get_schema.dump(_updated_structure)
    _element_qnt = _us_dumped.get('attributes').get(
        str(_block_index).zfill(2)).get('qnt')
    _user_id = _us_dumped.get('user_id')
    '''Expect it's updated as per available constants'''
    assert _element_qnt == _qnt + 1
    assert _first_user_id == _user_id

    # print('\ntest_model_structure:\n test_add_element',
    #       '\n  _updated_structure ->',
    #       structure_get_schema.dump(_updated_structure),
    #       '\n  _element_qnt ->', _element_qnt)
    '''remove element'''
    _second_user_id = randrange(128)
    _updating_structure.change_element_qnt('dec', _block_index,
                                           _second_user_id)
    '''Get elements and check expectations'''
    _updated_structure = StructureModel.find_by_ids(_criterions)

    _us_dumped = structure_get_schema.dump(_updated_structure)
    _element_qnt = _us_dumped.get('attributes').get(
        str(_block_index).zfill(2)).get('qnt')
    _user_id = _us_dumped.get('user_id')
    '''Expect it's updated as per available constants'''
    assert _element_qnt == _qnt
    assert _second_user_id == _user_id
コード例 #3
0
def test_StructureModel_remove_element_cls(saved_structure_instance,
                                           structure_get_schema, attributes):
    '''Clean up'''
    StructureModel.query.delete()
    '''Create structures with empty attribure, it should be 10 of them.'''
    _global_view_keys = global_constants.get_VIEWS_PKS
    _global_locales_keys = global_constants.get_PKS
    _structure_gens = [
        saved_structure_instance({
            'view_id': _view_key,
            'locale_id': _locale_key,
            # 'attributes': attributes
        }) for _view_key in _global_view_keys
        for _locale_key in _global_locales_keys
    ]
    [next(_structure_gen) for _structure_gen in _structure_gens]
    '''choose one of the structure'''
    _criterions = {
        'view_id': choice(_global_view_keys),
        'locale_id': choice(_global_locales_keys)
    }

    _instance = StructureModel.find_by_ids(_criterions)
    _instance.update({'attributes': attributes})
    # _index = randrange(len(attributes))
    # _element_structure = StructureModel.get_element_cls(
    #     _criterions, _index)
    '''Clean up'''
    [next(_structure_gen) for _structure_gen in _structure_gens]
コード例 #4
0
def test_insert_upper_level_element(saved_structure_instance,
                                    structure_get_schema, attributes):
    '''clean up structure tables'''
    [_structure.delete_fm_db() for _structure in StructureModel.find()]
    '''fill structure table'''
    fill_structure()
    '''choose constants to work with structure'''
    _criterions = {
        'view_id': choice(global_constants.get_VIEWS_PKS),
        'locale_id': choice(global_constants.get_PKS)
    }
    _uplev_elem_index = 1
    _uplev_elem_items = {
        'type': 'hblock',
        'subtype': 'pix',
        'qnt': '3',
        'name': 'fancy name'
    }
    _first_user_id = randrange(128)
    '''update original structure to work with'''
    _updating_structure = StructureModel.find_by_ids(_criterions)
    _updating_structure.update({'attributes': attributes})
    '''insert upper level element'''
    _updating_structure.insert_element(_uplev_elem_index, _uplev_elem_items,
                                       _first_user_id)
コード例 #5
0
 def save_to_db_structure(self,
                          view_id: str = '',
                          locale_id: str = '',
                          user_id: int = 0) -> Union[None, str]:
     '''get existing structure record'''
     _structure_instance = StructureModel.find_by_ids(ids={
         'view_id': view_id,
         'locale_id': locale_id
     })
     '''no such structure records'''
     if _structure_instance is None:
         _structure_instance = structure_schema.load(
             {
                 'view_id': view_id,
                 'locale_id': locale_id,
                 'user_id': user_id,
                 'attributes': self.serialize_to_structure
             },
             session=dbs_global.session)
         return _structure_instance.save_to_db()
     else:
         _structure_attributes = {**_structure_instance.attributes}
     key = str(self.upper_index).zfill(2)
     _structure_attributes[key] = self.serialize_to_structure.get(key)
     # print('\nContentElementsSimple:\n save_to_db',
     #       '\n  _structure_instance ->', _structure_instance)
     return _structure_instance.update({
         'attributes': _structure_attributes,
         'user_id': user_id
     })
コード例 #6
0
 def load_fm_db(cls,
                identity: str = '',
                view_id: str = '',
                locale_id: str = '',
                load_name: bool = False) -> 'ContentElementsSimple':
     '''
     The method creates ContentElementsSimple instance based on info
         from db content table. The method should be called by
         PageView instance.
     I do not check loaded elements validity due to productivity.
     '''
     content_model = ContentModel.find_by_identity_view_locale(
         identity=identity, view_id=view_id, locale_id=locale_id)
     if content_model is None:
         return None
     _info = content_model.identity.split('_')
     _value = {
         'title': content_model.title,
         'content': content_model.content
     }
     instance = ContentElementsSimple(upper_index=int(_info[0]),
                                      type=_info[1],
                                      element=_value)
     if load_name:
         _structure_instance = StructureModel.find_by_ids(
             ids={
                 'view_id': view_id,
                 'locale_id': locale_id
             })
         if _structure_instance is not None:
             instance.name = _structure_instance.attributes.get(
                 str(instance.upper_index).zfill(2)).get('name')
     return instance
コード例 #7
0
 def load_fm_db(cls, identity: str = '', view_id: str = '',
                locale_id: str = '', load_name: bool = False
                ) -> Union[None, 'ContentElementsBlock']:
     '''here identity consisn of upper_index, type, subtype -
         01_type_subtype'''
     '''
     The method creates ContentElementsBlock instance based on info
         from db content table. Name is got from structure table.
         In case of structure table record for this page does not
         exist name would be empty.
     I do not check loaded elements validity due to productivity.
     '''
     _splitted_identity = identity.split('_')
     _elements = ContentModel.find_identity_like(
         searching_criterions={
             'view_id': view_id, 'locale_id': locale_id},
         identity_like=f'{identity}%')
     if len(_elements) == 0:
         return None
     instance = ContentElementsBlock(
         upper_index=int(_splitted_identity[0]),
         type=_splitted_identity[1],
         subtype=_splitted_identity[2],
         elements=[
             element.serialize() for element in _elements
         ])
     if load_name:
         _structure_instance = StructureModel.find_by_ids(
             ids={'view_id': view_id, 'locale_id': locale_id})
         if _structure_instance is not None:
             instance.name = _structure_instance.attributes.get(
                 str(instance.upper_index).zfill(2)).get('name')
     return instance
コード例 #8
0
def test_update(saved_structure_instance, structure_get_schema, attributes):
    '''Clean up the structure table'''
    StructureModel.query.delete()
    '''Create structures, it should be 10 of them.'''
    _global_view_keys = global_constants.get_VIEWS_PKS
    _global_locales_keys = global_constants.get_PKS
    [{
        'view_id': _view_key,
        'locale_id': _locale_key
    } for _view_key in _global_view_keys
     for _locale_key in _global_locales_keys]
    _structure_gens = [
        saved_structure_instance({
            'view_id': _view_key,
            'locale_id': _locale_key
        }) for _view_key in _global_view_keys
        for _locale_key in _global_locales_keys
    ]
    [next(_structure_gen) for _structure_gen in _structure_gens]
    '''Choose random keys to update structure'''
    _criterions = {
        'view_id': choice(_global_view_keys),
        'locale_id': choice(_global_locales_keys)
    }
    _structure_for_update = StructureModel.find_by_ids(_criterions)
    assert structure_get_schema.dump(_structure_for_update).get(
        'updated') is None
    _attributes = {
        'key1': 'test_key1',
        'key2': 'test_key2',
        'key3': 'test_key3',
    }
    _structure_for_update.update({'attributes': _attributes})
    _updated_schema = StructureModel.find_by_ids(_criterions)
    assert structure_get_schema.dump(_structure_for_update).get(
        'updated') is not None
    assert structure_get_schema.dump(_updated_schema).get(
        'attributes') == _attributes
    '''Clean up'''
    [next(_structure_gen) for _structure_gen in _structure_gens]
コード例 #9
0
def test_delete(saved_structure_instance, structure_get_schema, view_id,
                attributes):
    '''Clean up'''
    StructureModel.query.delete()
    '''Create structures, it should be 10 of them.'''
    _global_view_keys = global_constants.get_VIEWS_PKS
    _global_locales_keys = global_constants.get_PKS
    [{
        'view_id': _view_key,
        'locale_id': _locale_key
    } for _view_key in _global_view_keys
     for _locale_key in _global_locales_keys]
    _structure_gens = [
        saved_structure_instance({
            'view_id': _view_key,
            'locale_id': _locale_key,
            'attributes': {
                'view_id': _view_key,
                'locale_id': _locale_key
            }
        }) for _view_key in _global_view_keys
        for _locale_key in _global_locales_keys
    ]
    [next(_structure_gen) for _structure_gen in _structure_gens]
    '''Choose random keys to update structure'''
    _criterions = {
        'view_id': choice(_global_view_keys),
        'locale_id': choice(_global_locales_keys)
    }
    _structure_to_delete = StructureModel.find_by_ids(_criterions)

    _structure_to_delete.delete_fm_db()

    _deleted_structure = StructureModel.find_by_ids(_criterions)
    _left_stractures = StructureModel.find()
    assert _deleted_structure is None
    assert len(_left_stractures) == 9
    '''Clean up'''
    [next(_structure_gen) for _structure_gen in _structure_gens]
コード例 #10
0
def test_StructureModel_get_element_both(client, create_test_content):
    lng_00 = 'en'
    lng_01 = 'ru'
    _content_details_00 = create_test_content(locale=lng_00, user_id=1)
    create_test_content(locale=lng_01, clean=False)
    _block_01 = _content_details_00.get('block_00')
    _simple_00 = _content_details_00.get('simple_00')
    _ids = {
        'view_id': _content_details_00.get('view_id'),
        'locale_id': _content_details_00.get('locale_id'),
    }
    _found = StructureModel.find_by_ids(ids=_ids)
    _upper_index = _block_01.get('upper_index')
    _element = _found.get_element(upper_index=_upper_index)
    assert _element ==\
        _block_01.get('structure').get(str(_upper_index).zfill(2))
    _upper_index = _simple_00.get('upper_index')
    _element = StructureModel.get_element_cls(ids=_ids,
                                              upper_index=_upper_index)
    assert _element ==\
        _simple_00.get('structure').get(str(_upper_index).zfill(2))
コード例 #11
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}'
    }