-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/bibliography export #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lpi-tn
wants to merge
7
commits into
main
Choose a base branch
from
Feature/bibliography-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2432a1
feat(bibliography): add bibliography export endpoint and model
lpi-tn d4d8ab5
fix(helpers): improve error logging in stringify_docs_content
lpi-tn da6a38c
test(bibliography): add unit tests for RIS formatting and document co…
lpi-tn 483be44
test(bibliography): add unit tests for bibliography export functionality
lpi-tn 58a1ed3
fix(helpers): enhance error logging for publication formatting
lpi-tn 4217cea
Apply suggestions from code review
lpi-tn f4a8281
lint happy
lpi-tn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from fastapi import APIRouter | ||
| from starlette.responses import PlainTextResponse | ||
|
|
||
| from src.app.models.bibliography import DocumentIDs | ||
| from src.app.services.helpers import welearn_document_to_ris | ||
| from src.app.services.sql_db.queries import get_documents_by_ids | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.post("/export_bibliography", response_class=PlainTextResponse) | ||
| async def export_bibliography(body: DocumentIDs) -> str: | ||
| documents_ids = [str(u) for u in body.documents_ids] | ||
| docs = get_documents_by_ids(documents_ids) | ||
|
|
||
| records = [welearn_document_to_ris(doc) for doc in docs] | ||
| return "\n".join(records) + ("\n" if records else "") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from uuid import UUID | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class DocumentIDs(BaseModel): | ||
| documents_ids: list[UUID] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from fastapi.testclient import TestClient | ||
|
|
||
| from src.app.core.config import settings | ||
| from src.main import app | ||
|
|
||
| client = TestClient(app) | ||
|
|
||
|
|
||
| @mock.patch( | ||
| "src.app.shared.infra.security.check_api_key_sync", | ||
| new=mock.MagicMock(return_value=True), | ||
| ) | ||
| class BibliographyApiTests(unittest.IsolatedAsyncioTestCase): | ||
| @mock.patch("src.app.api.api_v1.endpoints.bibliography.welearn_document_to_ris") | ||
| @mock.patch("src.app.api.api_v1.endpoints.bibliography.get_documents_by_ids") | ||
| async def test_export_bibliography_success( | ||
| self, get_documents_by_ids_mock, welearn_document_to_ris_mock, *mocks | ||
| ): | ||
| doc1 = mock.sentinel.doc1 | ||
| doc2 = mock.sentinel.doc2 | ||
| get_documents_by_ids_mock.return_value = [doc1, doc2] | ||
| welearn_document_to_ris_mock.side_effect = [ | ||
| "TY - JFULL\nER - ", | ||
| "TY - BOOK\nER - ", | ||
| ] | ||
|
|
||
| payload = { | ||
| "documents_ids": [ | ||
| "12345678-1234-5678-1234-567812345678", | ||
| "87654321-4321-8765-4321-876543218765", | ||
| ] | ||
| } | ||
| response = client.post( | ||
| f"{settings.API_V1_STR}/bibliography/export_bibliography", | ||
| json=payload, | ||
| headers={"X-API-Key": "test"}, | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual(response.text, "TY - JFULL\nER - \nTY - BOOK\nER - \n") | ||
| get_documents_by_ids_mock.assert_called_once_with(payload["documents_ids"]) | ||
| welearn_document_to_ris_mock.assert_has_calls( | ||
| [mock.call(doc1), mock.call(doc2)] | ||
| ) | ||
|
|
||
| @mock.patch("src.app.api.api_v1.endpoints.bibliography.welearn_document_to_ris") | ||
| @mock.patch("src.app.api.api_v1.endpoints.bibliography.get_documents_by_ids") | ||
| async def test_export_bibliography_empty_documents( | ||
| self, get_documents_by_ids_mock, welearn_document_to_ris_mock, *mocks | ||
| ): | ||
| get_documents_by_ids_mock.return_value = [] | ||
|
|
||
| response = client.post( | ||
| f"{settings.API_V1_STR}/bibliography/export_bibliography", | ||
| json={"documents_ids": ["12345678-1234-5678-1234-567812345678"]}, | ||
| headers={"X-API-Key": "test"}, | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual(response.text, "") | ||
| welearn_document_to_ris_mock.assert_not_called() | ||
|
|
||
| def test_export_bibliography_invalid_payload(self, *mocks): | ||
| response = client.post( | ||
| f"{settings.API_V1_STR}/bibliography/export_bibliography", | ||
| json={"documents_ids": ["not-a-uuid"]}, | ||
| headers={"X-API-Key": "test"}, | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 422) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.