def list_bibliographies(database, count): """ List all the document sets. """ bibliography_sets = Bibliography.list(database, count) click.echo( tabulate.tabulate( [ [ bibset.eid[:8], bibset.description, bibset.modified.strftime('%b %d, %Y, %I:%M%p'), len(bibset.documents) ] for bibset in bibliography_sets ], headers=[ 'Identifier', 'Description', 'Updated at', 'Docs count', ], tablefmt='rst', ) ) total = Bibliography.count(database) if count >= total: click.echo('Showing all the document sets.') else: click.echo( 'Showing {count} out of {total} document sets.' .format(count=count, total=total) )
def create(database, **kwargs): """ Populates the condor database with information from the given files kind parameter indicates what type of files you're working with. """ verbose = kwargs.pop('verbose', False) if verbose: file_names = '\n'.join( f.name for f in kwargs.get('files', []) if hasattr(f, 'name') ) kind = kwargs.get('kind') click.echo(f'I\'m looking for {kind} records in these files:') click.echo(f'{file_names}') kwargs['show_progress_bar'] = verbose _bibliography = Bibliography.from_files(**kwargs) database.add(_bibliography) database.flush() click.echo(f'I\'m writing to {_bibliography.eid}') click.echo('And... I\'m done') click.echo('The database contains {} records'.format( Document.count(database, _bibliography.eid) ))
def test_matrix_creation_endpoint(client, session): bib = Bibliography(eid='123', description='lorem') session.add(bib) session.flush() response = client.post('/matrix', json={ 'bibliography': '123', 'fields': ['title', 'description'], }) assert response.status_code == 200 assert response.json().get('bibliography_eid') == '123'
def test_matrix_creation_endpoint_fails(client, session): bib = Bibliography(eid='123', description='lorem') session.add(bib) session.flush() response = client.post('/matrix', json={ 'bibliography': '1234', 'fields': ['title', 'description'], }) assert response.status_code == 404 assert 'message' in response.json()
def get_bibliography(eid, session: CondorSession) -> Response: """ List the bibliography that has the specified eid from the database. """ bibliography = Bibliography.find_by_eid(session, eid) if not bibliography: return Response( {'message': 'The especified eid is not found on database'}, status=404, ) return Response(sc.Bibliography(bibliography))
def test_single_bibliography_existing(client, session): # Given an existing bibliography bib = Bibliography(eid='123', description='lorem') session.add(bib) session.commit() # When I ask for it res = client.get('/bibliography/123') # Then I get a successful response assert res.status_code == 200 # And the response is non empty assert res.json().get('eid') == '123' assert res.json().get('description') == 'lorem'
def test_bibliography_endpoint_when_many_bibliographies(client, session): # Create a bibliography bib = Bibliography(eid='345', description='lorem') session.add(bib) session.commit() # When I request the list bibliography res = client.get('/bibliography') # Then I get a successful response assert res.status_code == 200 # The response is non empty assert len(res.json()) == 1 # And has bibliographies assert any(b.get('eid') == '345' for b in res.json())
def test_document_endpoint_actually_returns_documents(client, session): # Given some records matching records in the database bib = Bibliography(eid='123', description='lorem') session.add(bib) session.flush() doc = Document(eid='345', bibliography_eid='123', title='lorem', keywords='{}', description='lorem', language='english', hash='alksdjksjdf') session.add(doc) session.commit() # When I request the documents with the given bibliography eid res = client.get('/document?bibliography=123') # Then I receive a successful response assert res.status_code == 200 # And the response is non empty assert len(res.json()) > 0
def test_individual_matrix_endpoint_existing(client, session): # Given some records matching records in the database bib = Bibliography(eid='123', description='lorem') session.add(bib) session.flush() mat = TermDocumentMatrix( eid='345', bibliography_eid='123', bibliography_options='', processing_options='', term_list_path='', matrix_path='', ) session.add(mat) session.commit() # When I request the matrix with the eid res = client.get('/matrix/345') # Then I receive a successful response assert res.status_code == 200 # And the response is non empty assert len(res.json()) > 0
def create_matrix(descriptor: sc.MatrixDescriptor, session: CondorSession) -> Response: """ Create a term document matrix. """ bibliography = Bibliography.find_by_eid(session, descriptor.get('bibliography')) if bibliography is None: return Response( { 'message': 'The especified bibliography eid is not found on database' }, status=404, ) td_matrix = TermDocumentMatrix.from_bibliography_set( bibliography, regularise=descriptor.get('regularise'), fields=descriptor.get('fields')) session.add(td_matrix) session.commit() return Response(sc.Matrix(td_matrix))
def test_matrix_endpoint_when_there_are_matrix(client, session): # Given some records matching records in the database bib = Bibliography(eid='123', description='lorem') session.add(bib) session.flush() mat = TermDocumentMatrix( eid='345', bibliography_eid='123', bibliography_options='', processing_options='', term_list_path='', matrix_path='', ) session.add(mat) session.commit() # When I request the documents with the given bibliography eid res = client.get('/matrix') # Then I receive a successful response assert res.status_code == 200 # The response is non empty assert len(res.json()) == 1 # And has matrix assert any(m.get('eid') == '345' for m in res.json())
def get_all_bibliographies(session: CondorSession) -> List[sc.Bibliography]: """ List all bibliographies in the database. """ return [sc.Bibliography(bib) for bib in Bibliography.list(session)]