def test_gets_doc(app, client: TApp, admin, user, get_headers, by): """ corret doc getting """ md = "# GOO GOO" with app.app_context(): repo = InstructionDocumentRepository() doc = InstructionDocument(name=f'coolish doc' + ' ' + by if by else 'None', description='. . .', created_by=admin) managed_doc = InstructionDocumentManager(document=doc) repo.save(doc) page = InstructionDocumentPage(document_id=doc.id, md=md) managed_doc.add_page(page, admin.id) doc_id = doc.id doc_slug = doc.slug get_doc_data = {"uid": str(uuid.uuid4())} if by: get_doc_data[by] = int(doc_id) if by == 'document_id' else str( doc_slug) print('get_doc_data', get_doc_data) response = client.post_json('/api/instruction-documents/get-doc', get_doc_data, headers=get_headers('nouser')) assert response.json['status'] == 'OK' assert response.json['uid'] == get_doc_data['uid'] assert len(response.json['data']['pages']) == 1 assert response.json['data']['pages'][0]['page_num'] == 1 assert response.json['data']['pages'][0]['document_id'] == doc_id assert response.json['data']['pages'][0]['md'] == md
def test_search_docs(app, client: TApp, admin, user, get_headers): """ corret doc searching """ added_count = 0 with app.app_context(): repo = InstructionDocumentRepository() for i in range(1, 10): repo.save(InstructionDocument(f'[{i}] searched doc', 'foo', admin)) added_count += 1 for i in range(1, 6): repo.save( InstructionDocument(f'[{i}] ...', f'xyz {i} searched doc', admin)) added_count += 1 search_docs_data = { "uid": str(uuid.uuid4()), "search": "SearChed dOc", "page": 1, "limit": 100 } response = client.post_json('/api/instruction-documents/search-docs', search_docs_data, headers=get_headers('nouser')) assert response.json['status'] == 'OK' assert response.json['uid'] == search_docs_data['uid'] assert response.json['data']['page'] == 1 assert len(response.json['data']['results']) == added_count pass
def test_lists_docs(app, client: TApp, admin, user, get_headers): """ corret doc listing """ with app.app_context(): repo = InstructionDocumentRepository() for i in range(1, 10): repo.save(InstructionDocument(f'[{i}]test doc', 'foo', admin)) list_docs_data = {"uid": str(uuid.uuid4()), "page": 2, "limit": 3} response = client.post_json('/api/instruction-documents/list-docs', list_docs_data, headers=get_headers('nouser')) assert response.json['status'] == 'OK' assert response.json['uid'] == list_docs_data['uid'] assert response.json['data']['page'] == list_docs_data['page'] assert response.json['data']['prev_num'] == list_docs_data['page'] - 1 assert response.json['data']['next_num'] == list_docs_data['page'] + 1 assert len(response.json['data']['results']) == 3
class InstructionDocumentManager: def __init__(self, document_id: int = None, document: InstructionDocument = None): self.__page_repo = InstructionDocumentPageRepository() self.__doc_repo = InstructionDocumentRepository() self.__document = self.__doc_repo.get( document_id) if document_id else document if not self.__document: raise ValueError('Document not provided!') @property def document(self): return self.__document def update(self, user_id: int, **kwargs): valid_kwargs = ('name', 'description') for k, v in kwargs.items(): if k in valid_kwargs: setattr(self.__document, k, v) self.__document.slug = get_slug(self.__document.name) self.__document.updated_by_user_id = user_id self.__document.updated = datetime.utcnow() self.__doc_repo.save(self.__document) def pages(self) -> List[InstructionDocumentPage]: return self.__page_repo.filter( InstructionDocumentPage.doc(self.__document.id)) def page_count(self) -> int: return len(self.pages()) def add_page(self, page: InstructionDocumentPage, user_id: int) -> InstructionDocumentPage: page.page_num = self.page_count() + 1 self.__page_repo.save(page) self.__document.updated_by_user_id = user_id self.__document.updated = datetime.utcnow() self.__doc_repo.save(self.__document) return page def delete_page(self, user_id: int, page_num: int): page = self.__page_repo.get_by(document_id=self.__document.id, page_num=page_num) self.__page_repo.delete(page.id) for page in self.pages(): if page.page_num > page_num: page.page_num = page.page_num - 1 self.__page_repo.save(page) self.__document.updated_by_user_id = user_id self.__document.updated = datetime.utcnow() self.__doc_repo.save(self.__document)