diff --git a/.gitignore b/.gitignore index 5e9eef2..3743051 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,4 @@ dmypy.json # IDE .idea +/.vs diff --git a/README.md b/README.md index 33c0486..ce933d7 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ The application allows you to automate the process of generating a bibliography Supported citation styles: - ГОСТ Р 7.0.5-2008 +- American Psychological Association 7 ## Installation @@ -57,6 +58,8 @@ to fill it without changing the original template: docker compose run app python main.py --citation gost --path_input /media/input.xlsx --path_output /media/output.docx ``` + The command is given for GOST formatting, to change the citation style you need to change the citation parameter ('gost' or 'apa' for GOST or APA7 style citation respectively). + Also, it is possible to omit the arguments to use their defaults: ```shell docker compose run app python main.py diff --git a/docs/source/index.rst b/docs/source/index.rst index f552b0d..908ccde 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -7,6 +7,7 @@ Поддерживаемые стили цитирования: - ГОСТ Р 7.0.5-2008 + - American Psychological Association 7 Установка ========= @@ -95,7 +96,7 @@ docker compose run app python main.py --citation gost --path_input /media/input.xlsx --path_output /media/output.docx - Таким образом можно определять стиль цитирования и пути ко входному и выходному файлам. + Таким образом можно определять стиль цитирования (`gost` - для ГОСТ Р 7.0.5-2008 и `apa` - для APA 7) и пути ко входному и выходному файлам. Автоматизация ============= diff --git a/src/formatters/models.py b/src/formatters/models.py index c9236ca..b86fe2b 100644 --- a/src/formatters/models.py +++ b/src/formatters/models.py @@ -78,3 +78,59 @@ class ArticlesCollectionModel(BaseModel): publishing_house: str year: int = Field(..., gt=0) pages: str + + +class DissertationModel(BaseModel): + """ + Модель дисссертации: + + .. code-block:: + + DissertationModel( + author="Иванов И.М.", + title="Наука как искусство", + degree="д-р. / канд.", + branch="экон.", + speciality_code="01.01.01", + city="СПб.", + year=2020, + pages=199, + ) + """ + + author: str = Field(..., description="Фамилия и инициалы автора") + title: str = Field(..., description="Название диссертации") + degree: str = Field(..., description="Ученая степень (д-р. / канд.)") + branch: str = Field(..., description="Отрасль наук (сокращённо)") + speciality_code: str = Field(..., description="Код специальности") + city: str = Field(..., description="Город издательства") + year: int = Field(..., gt=0, description="Год защиты") + pages: int = Field(..., gt=0, description="Количество страниц") + + +class AbstractModel(BaseModel): + """ + Модель автореферата: + + .. code-block:: + + AbstractModel( + author="Иванов И.М.", + title="Наука как искусство", + degree="д-р. / канд.", + branch="экон.", + speciality_code="01.01.01", + city="СПб.", + year=2020, + pages=199, + ) + """ + + author: str = Field(..., description="Фамилия и инициалы автора") + title: str = Field(..., description="Название диссертации") + degree: str = Field(..., description="Ученая степень (д-р. / канд.)") + branch: str = Field(..., description="Отрасль наук (сокращённо)") + speciality_code: str = Field(..., description="Код специальности") + city: str = Field(..., description="Город издательства") + year: int = Field(..., gt=0, description="Год издания") + pages: int = Field(..., gt=0, description="Количество страниц") diff --git a/src/formatters/styles/apa.py b/src/formatters/styles/apa.py new file mode 100644 index 0000000..c789ee6 --- /dev/null +++ b/src/formatters/styles/apa.py @@ -0,0 +1,283 @@ +""" Стиль цитирования по APA. """ +from string import Template + +from pydantic import BaseModel + +from formatters.models import ( + BookModel, + InternetResourceModel, + ArticlesCollectionModel, + DissertationModel, + AbstractModel, +) +from formatters.styles.base import BaseCitationStyle +from logger import get_logger + + +logger = get_logger(__name__) + + +class APABookFormatter(BaseCitationStyle): + """ + Форматирование для книг в стиле APA. + """ + + data: BookModel + + @property + def template(self) -> Template: + return Template("$authors ($year). $title. $edition. $city: $publishing_house.") + + def substitute(self) -> str: + """ + Замена переменных в шаблоне источника. + + :return: Отформатированная строка источника. + """ + + logger.info('Форматирование книги "%s" в стиле APA...', self.data.title) + + return self.template.substitute( + authors=self.get_authors(), + year=self.data.year, + title=self.data.title, + edition=self.get_edition(), + city=self.data.city, + publishing_house=self.data.publishing_house, + ) + + def get_authors(self) -> str: + """ + Получение списка авторов в формате APA. + + :return: Отформатированный список авторов. + """ + authors = self.data.authors.split( + ", " + ) # Предполагая, что имена авторов разделены запятой + formatted_authors = [] + for author in authors: + # Разбиваем полное имя автора на фамилию и инициалы + author_parts = author.split(" ") + # Если есть хотя бы фамилия и одна инициала + if len(author_parts) >= 2: + # Форматируем фамилию, инициалы и точку + formatted_author = ( + author_parts[0] + + " " + + " ".join([initial[0] + "." for initial in author_parts[1:]]) + ) + formatted_authors.append(formatted_author) + else: + # В случае, если в имени только одно слово (без инициалов) + formatted_authors.append(author) + # Объединяем отформатированных авторов через запятую и пробел + if len(formatted_authors) == 1: + return formatted_authors[0] + if len(formatted_authors) == 2: + return formatted_authors[0] + " & " + formatted_authors[1] + else: + # Если авторов больше двух, добавляем "и др." + return formatted_authors[0] + " et al." + + def get_edition(self) -> str: + """ + Получение отформатированной информации об издании. + + :return: Информация об издании. + """ + return f"({self.data.edition} изд.)" if self.data.edition else "" + + +class APAInternetResourceFormatter(BaseCitationStyle): + """ + Форматирование для интернет-ресурсов в стиле APA. + """ + + data: InternetResourceModel + + @property + def template(self) -> Template: + return Template( + "$article (б. д.). $website URL: $link (дата обращения: $access_date)." + ) + + def substitute(self) -> str: + """ + Замена переменных в шаблоне источника. + + :return: Отформатированная строка источника. + """ + + logger.info( + 'Форматирование интернет-ресурса "%s" в стиле APA...', self.data.article + ) + + return self.template.substitute( + article=self.data.article, + website=self.data.website, + link=self.data.link, + access_date=self.data.access_date, + ) + + +class APACollectionArticleFormatter(BaseCitationStyle): + """ + Форматирование для статьи из сборника в стиле APA. + """ + + data: ArticlesCollectionModel + + @property + def template(self) -> Template: + return Template( + "$authors ($year). $article_title. $collection_title (pp. $pages). $city: $publishing_house." + ) + + def substitute(self) -> str: + """ + Замена переменных в шаблоне источника. + + :return: Отформатированная строка источника. + """ + + logger.info( + 'Форматирование статьи из сборника "%s" в стиле APA...', + self.data.article_title, + ) + + return self.template.substitute( + authors=self.get_authors(), + year=self.data.year, + article_title=self.data.article_title, + collection_title=self.data.collection_title, + pages=self.data.pages, + city=self.data.city, + publishing_house=self.data.publishing_house, + ) + + def get_authors(self) -> str: + """ + Получение списка авторов в формате APA. + + :return: Отформатированный список авторов. + """ + authors = self.data.authors.split( + ", " + ) # Предполагая, что имена авторов разделены запятой + formatted_authors = [] + for author in authors: + # Разбиваем полное имя автора на фамилию и инициалы + author_parts = author.split(" ") + # Если есть хотя бы фамилия и одна инициала + if len(author_parts) >= 2: + # Форматируем фамилию, инициалы и точку + formatted_author = ( + author_parts[0] + + " " + + " ".join([initial[0] + "." for initial in author_parts[1:]]) + ) + formatted_authors.append(formatted_author) + else: + # В случае, если в имени только одно слово (без инициалов) + formatted_authors.append(author) + # Объединяем отформатированных авторов через запятую и пробел + if len(formatted_authors) == 1: + return formatted_authors[0] + if len(formatted_authors) == 2: + return formatted_authors[0] + " & " + formatted_authors[1] + else: + # Если авторов больше двух, добавляем "и др." + return formatted_authors[0] + " et al." + + +class APADissertationFormatter(BaseCitationStyle): + """ + Форматирование для диссертаций в стиле APA. + """ + + data: DissertationModel + + @property + def template(self) -> Template: + return Template( + "$author. ($year). $title ($degree) [Диссертация]. $branch, $speciality_code. $city" + ) + + def substitute(self) -> str: + + logger.info('Форматирование диссертации "%s" ...', self.data.title) + + return self.template.substitute( + author=self.data.author, + year=self.data.year, + title=self.data.title, + degree=self.data.degree, + branch=self.data.branch, + speciality_code=self.data.speciality_code, + city=self.data.city, + ) + + +class APAAbstractFormatter(BaseCitationStyle): + """ + Форматирование для автореферата в стиле APA. + """ + + data: AbstractModel + + @property + def template(self) -> Template: + return Template( + "$author. ($year). $title ($degree) [Автореферат]. $branch, $speciality_code. $city" + ) + + def substitute(self) -> str: + + logger.info('Форматирование автореферата "%s" ...', self.data.title) + + return self.template.substitute( + author=self.data.author, + year=self.data.year, + title=self.data.title, + degree=self.data.degree, + branch=self.data.branch, + speciality_code=self.data.speciality_code, + city=self.data.city, + ) + + +class APACitationFormatter: + """ + Базовый класс для итогового форматирования списка источников. + """ + + formatters_map = { + BookModel.__name__: APABookFormatter, + InternetResourceModel.__name__: APAInternetResourceFormatter, + ArticlesCollectionModel.__name__: APACollectionArticleFormatter, + DissertationModel.__name__: APADissertationFormatter, + AbstractModel.__name__: APAAbstractFormatter, + } + + def __init__(self, models: list[BaseModel]) -> None: + """ + Конструктор. + + :param models: Список объектов для форматирования + """ + + formatted_items = [] + for model in models: + formatted_items.append(self.formatters_map.get(type(model).__name__)(model)) # type: ignore + + self.formatted_items = formatted_items + + def format(self) -> list[BaseCitationStyle]: + """ + Форматирование списка источников. + + :return: + """ + + return sorted(self.formatted_items, key=lambda item: item.formatted) diff --git a/src/formatters/styles/gost.py b/src/formatters/styles/gost.py index b237f8a..3a4dff2 100644 --- a/src/formatters/styles/gost.py +++ b/src/formatters/styles/gost.py @@ -5,7 +5,13 @@ from pydantic import BaseModel -from formatters.models import BookModel, InternetResourceModel, ArticlesCollectionModel +from formatters.models import ( + BookModel, + InternetResourceModel, + ArticlesCollectionModel, + DissertationModel, + AbstractModel, +) from formatters.styles.base import BaseCitationStyle from logger import get_logger @@ -103,6 +109,64 @@ def substitute(self) -> str: ) +class GOSTDissertation(BaseCitationStyle): + """ + Форматирование для диссертаций. + """ + + data: DissertationModel + + @property + def template(self) -> Template: + return Template( + "$author. $title [$degree] дис. : $branch, $speciality_code / $city, $year. - $pages с." + ) + + def substitute(self) -> str: + + logger.info('Форматирование диссертаций "%s" ...', self.data.title) + + return self.template.substitute( + author=self.data.author, + title=self.data.title, + degree=self.data.degree, + branch=self.data.branch, + speciality_code=self.data.speciality_code, + city=self.data.city, + year=self.data.year, + pages=self.data.pages, + ) + + +class GOSTAbstract(BaseCitationStyle): + """ + Форматирование для автореферата. + """ + + data: AbstractModel + + @property + def template(self) -> Template: + return Template( + "$author. $title [$degree] : $branch, $speciality_code / $city, $year. - $pages с." + ) + + def substitute(self) -> str: + + logger.info('Форматирование автореферата "%s" ...', self.data.title) + + return self.template.substitute( + author=self.data.author, + title=self.data.title, + degree=self.data.degree, + branch=self.data.branch, + speciality_code=self.data.speciality_code, + city=self.data.city, + year=self.data.year, + pages=self.data.pages, + ) + + class GOSTCitationFormatter: """ Базовый класс для итогового форматирования списка источников. @@ -112,6 +176,8 @@ class GOSTCitationFormatter: BookModel.__name__: GOSTBook, InternetResourceModel.__name__: GOSTInternetResource, ArticlesCollectionModel.__name__: GOSTCollectionArticle, + DissertationModel.__name__: GOSTDissertation, + AbstractModel.__name__: GOSTAbstract, } def __init__(self, models: list[BaseModel]) -> None: diff --git a/src/main.py b/src/main.py index 7a9fa8e..bb93a04 100644 --- a/src/main.py +++ b/src/main.py @@ -5,6 +5,7 @@ import click +from formatters.styles.apa import APACitationFormatter from formatters.styles.gost import GOSTCitationFormatter from logger import get_logger from readers.reader import SourcesReader @@ -21,7 +22,7 @@ class CitationEnum(Enum): """ GOST = "gost" # ГОСТ Р 7.0.5-2008 - MLA = "mla" # Modern Language Association + # MLA = "mla" # Modern Language Association APA = "apa" # American Psychological Association @@ -77,9 +78,17 @@ def process_input( ) models = SourcesReader(path_input).read() - formatted_models = tuple( - str(item) for item in GOSTCitationFormatter(models).format() - ) + if citation == CitationEnum.GOST.name: + formatted_models = tuple( + str(item) for item in GOSTCitationFormatter(models).format() + ) + elif citation == CitationEnum.APA.name: + formatted_models = tuple( + str(item) for item in APACitationFormatter(models).format() + ) + else: + logger.error("Неверно указан стиль цитирования: %s", citation) + return logger.info("Генерация выходного файла ...") Renderer(formatted_models).render(path_output) diff --git a/src/readers/reader.py b/src/readers/reader.py index 9007a80..2f41c2a 100644 --- a/src/readers/reader.py +++ b/src/readers/reader.py @@ -7,7 +7,13 @@ import openpyxl from openpyxl.workbook import Workbook -from formatters.models import BookModel, InternetResourceModel, ArticlesCollectionModel +from formatters.models import ( + BookModel, + InternetResourceModel, + ArticlesCollectionModel, + DissertationModel, + AbstractModel, +) from logger import get_logger from readers.base import BaseReader @@ -90,6 +96,60 @@ def attributes(self) -> dict: } +class DissertationReader(BaseReader): + """ + Чтение модели диссертации. + """ + + @property + def model(self) -> Type[DissertationModel]: + return DissertationModel + + @property + def sheet(self) -> str: + return "Диссертация" + + @property + def attributes(self) -> dict: + return { + "author": {0: str}, + "title": {1: str}, + "degree": {2: str}, + "branch": {3: str}, + "speciality_code": {4: str}, + "city": {5: str}, + "year": {6: int}, + "pages": {7: str}, + } + + +class AbstractReader(BaseReader): + """ + Чтение модели автореферата. + """ + + @property + def model(self) -> Type[AbstractModel]: + return AbstractModel + + @property + def sheet(self) -> str: + return "Автореферат" + + @property + def attributes(self) -> dict: + return { + "author": {0: str}, + "title": {1: str}, + "degree": {2: str}, + "branch": {3: str}, + "speciality_code": {4: str}, + "city": {5: str}, + "year": {6: int}, + "pages": {7: str}, + } + + class SourcesReader: """ Чтение из источника данных. @@ -100,6 +160,8 @@ class SourcesReader: BookReader, InternetResourceReader, ArticlesCollectionReader, + DissertationReader, + AbstractReader, ] def __init__(self, path: str) -> None: diff --git a/src/tests/conftest.py b/src/tests/conftest.py index ac5c9aa..624e20f 100644 --- a/src/tests/conftest.py +++ b/src/tests/conftest.py @@ -3,7 +3,13 @@ """ import pytest -from formatters.models import BookModel, InternetResourceModel, ArticlesCollectionModel +from formatters.models import ( + BookModel, + InternetResourceModel, + ArticlesCollectionModel, + DissertationModel, + AbstractModel, +) @pytest.fixture @@ -58,3 +64,41 @@ def articles_collection_model_fixture() -> ArticlesCollectionModel: year=2020, pages="25-30", ) + + +@pytest.fixture +def dissertation_fixture() -> DissertationModel: + """ + Фикстура модели диссертации. + :return: DissertationModel + """ + + return DissertationModel( + author="Иванов И.М.", + title="Наука как искусство", + degree="д-р. / канд.", + branch="экон.", + speciality_code="01.01.01", + city="СПб.", + year=2020, + pages=199, + ) + + +@pytest.fixture +def abstract_fixture() -> AbstractModel: + """ + Фикстура модели автореферата. + :return: DissertationModel + """ + + return AbstractModel( + author="Иванов И.М.", + title="Наука как искусство", + degree="д-р. / канд.", + branch="экон.", + speciality_code="01.01.01", + city="СПб.", + year=2020, + pages=199, + ) diff --git a/src/tests/formatters/test_apa.py b/src/tests/formatters/test_apa.py new file mode 100644 index 0000000..909b910 --- /dev/null +++ b/src/tests/formatters/test_apa.py @@ -0,0 +1,136 @@ +""" +Тестирование функций оформления списка источников по APA. +""" +from formatters.base import BaseCitationFormatter +from formatters.models import ( + BookModel, + InternetResourceModel, + ArticlesCollectionModel, + DissertationModel, + AbstractModel, +) +from formatters.styles.apa import ( + APABookFormatter, + APAInternetResourceFormatter, + APACollectionArticleFormatter, + APADissertationFormatter, + APAAbstractFormatter, +) + + +class TestAPA: + """ + Тестирование оформления списка источников согласно стилю APA. + """ + + def test_book(self, book_model_fixture: BookModel) -> None: + """ + Тестирование форматирования книги в стиле APA. + + :param BookModel book_model_fixture: Фикстура модели книги + :return: + """ + + model = APABookFormatter(book_model_fixture) + + assert ( + model.formatted + == "Иванов И. & Петров С. (2020). Наука как искусство. (3-е изд.). СПб.: Просвещение." + ) + + def test_internet_resource( + self, internet_resource_model_fixture: InternetResourceModel + ) -> None: + """ + Тестирование форматирования интернет-ресурса в стиле APA. + + :param InternetResourceModel internet_resource_model_fixture: Фикстура модели интернет-ресурса + :return: + """ + + model = APAInternetResourceFormatter(internet_resource_model_fixture) + + assert ( + model.formatted + == "Наука как искусство (б. д.). Ведомости URL: https://www.vedomosti.ru (дата обращения: 01.01.2021)." + ) + + def test_articles_collection( + self, articles_collection_model_fixture: ArticlesCollectionModel + ) -> None: + """ + Тестирование форматирования сборника статей в стиле APA. + + :param ArticlesCollectionModel articles_collection_model_fixture: Фикстура модели сборника статей + :return: + """ + + model = APACollectionArticleFormatter(articles_collection_model_fixture) + + assert ( + model.formatted + == "Иванов И. & Петров С. (2020). Наука как искусство. Сборник научных трудов (pp. 25-30). СПб.: АСТ." + ) + + def test_dissertation(self, dissertation_fixture: DissertationModel) -> None: + """ + Тестирование форматирования диссертации в стиле APA. + :param DissertationModel dissertation_fixture: Фикстура модели диссертации + :return: + """ + + model = APADissertationFormatter(dissertation_fixture) + + assert ( + model.formatted + == "Иванов И.М.. (2020). Наука как искусство (д-р. / канд.) [Диссертация]. экон., 01.01.01. СПб." + ) + + def test_abstract(self, abstract_fixture: AbstractModel) -> None: + """ + Тестирование форматирования автореферата в стиле APA. + :param AbstractModel abstract_fixture: Фикстура модели автореферата + :return: + """ + + model = APAAbstractFormatter(abstract_fixture) + + assert ( + model.formatted + == "Иванов И.М.. (2020). Наука как искусство (д-р. / канд.) [Автореферат]. экон., 01.01.01. СПб." + ) + + def test_citation_formatter( + self, + book_model_fixture: BookModel, + internet_resource_model_fixture: InternetResourceModel, + articles_collection_model_fixture: ArticlesCollectionModel, + dissertation_fixture: DissertationModel, + abstract_fixture: AbstractModel, + ) -> None: + """ + Тестирование функции итогового форматирования списка источников. + + :param BookModel book_model_fixture: Фикстура модели книги + :param InternetResourceModel internet_resource_model_fixture: Фикстура модели интернет-ресурса + :param ArticlesCollectionModel articles_collection_model_fixture: Фикстура модели сборника статей + :param DissertationModel dissertation_fixture: Фикстура модели диссертации + :param AbstractModel abstract_fixture: Фикстура модели автореферата + :return: + """ + + models = [ + APABookFormatter(book_model_fixture), + APAInternetResourceFormatter(internet_resource_model_fixture), + APACollectionArticleFormatter(articles_collection_model_fixture), + APADissertationFormatter(dissertation_fixture), + APAAbstractFormatter(abstract_fixture), + ] + result = BaseCitationFormatter(models).format() + + # Тестирование сортировки списка источников + assert result[0] == models[0] + assert result[1] == models[2] + assert result[2] == models[4] + assert result[3] == models[3] + assert result[4] == models[1] diff --git a/src/tests/formatters/test_gost.py b/src/tests/formatters/test_gost.py index c93e1e7..c460ad1 100644 --- a/src/tests/formatters/test_gost.py +++ b/src/tests/formatters/test_gost.py @@ -3,8 +3,20 @@ """ from formatters.base import BaseCitationFormatter -from formatters.models import BookModel, InternetResourceModel, ArticlesCollectionModel -from formatters.styles.gost import GOSTBook, GOSTInternetResource, GOSTCollectionArticle +from formatters.models import ( + BookModel, + InternetResourceModel, + ArticlesCollectionModel, + DissertationModel, + AbstractModel, +) +from formatters.styles.gost import ( + GOSTBook, + GOSTInternetResource, + GOSTCollectionArticle, + GOSTDissertation, + GOSTAbstract, +) class TestGOST: @@ -61,11 +73,41 @@ def test_articles_collection( == "Иванов И.М., Петров С.Н. Наука как искусство // Сборник научных трудов. – СПб.: АСТ, 2020. – С. 25-30." ) + def test_dissertation(self, dissertation_fixture: DissertationModel) -> None: + """ + Тестирование форматирования диссертации. + :param DissertationModel dissertation_fixture: Фикстура модели диссертации + :return: + """ + + model = GOSTDissertation(dissertation_fixture) + + assert ( + model.formatted + == "Иванов И.М.. Наука как искусство [д-р. / канд.] дис. : экон., 01.01.01 / СПб., 2020. - 199 с." + ) + + def test_abstract(self, abstract_fixture: AbstractModel) -> None: + """ + Тестирование форматирования автореферата. + :param AbstractModel abstract_fixture: Фикстура модели автореферата + :return: + """ + + model = GOSTAbstract(abstract_fixture) + + assert ( + model.formatted + == "Иванов И.М.. Наука как искусство [д-р. / канд.] : экон., 01.01.01 / СПб., 2020. - 199 с." + ) + def test_citation_formatter( self, book_model_fixture: BookModel, internet_resource_model_fixture: InternetResourceModel, articles_collection_model_fixture: ArticlesCollectionModel, + dissertation_fixture: DissertationModel, + abstract_fixture: AbstractModel, ) -> None: """ Тестирование функции итогового форматирования списка источников. @@ -73,6 +115,8 @@ def test_citation_formatter( :param BookModel book_model_fixture: Фикстура модели книги :param InternetResourceModel internet_resource_model_fixture: Фикстура модели интернет-ресурса :param ArticlesCollectionModel articles_collection_model_fixture: Фикстура модели сборника статей + :param DissertationModel dissertation_fixture: Фикстура модели диссертации + :param AbstractModel abstract_fixture: Фикстура модели автореферата :return: """ @@ -80,10 +124,14 @@ def test_citation_formatter( GOSTBook(book_model_fixture), GOSTInternetResource(internet_resource_model_fixture), GOSTCollectionArticle(articles_collection_model_fixture), + GOSTDissertation(dissertation_fixture), + GOSTAbstract(abstract_fixture), ] result = BaseCitationFormatter(models).format() # тестирование сортировки списка источников assert result[0] == models[2] assert result[1] == models[0] - assert result[2] == models[1] + assert result[2] == models[4] + assert result[3] == models[3] + assert result[4] == models[1] diff --git a/src/tests/readers/test_readers.py b/src/tests/readers/test_readers.py index 67d863b..98d6d75 100644 --- a/src/tests/readers/test_readers.py +++ b/src/tests/readers/test_readers.py @@ -5,12 +5,20 @@ import pytest -from formatters.models import BookModel, InternetResourceModel, ArticlesCollectionModel +from formatters.models import ( + BookModel, + InternetResourceModel, + ArticlesCollectionModel, + DissertationModel, + AbstractModel, +) from readers.reader import ( BookReader, SourcesReader, InternetResourceReader, ArticlesCollectionReader, + DissertationReader, + AbstractReader, ) from settings import TEMPLATE_FILE_PATH @@ -104,6 +112,60 @@ def test_articles_collection(self, workbook: Any) -> None: # проверка общего количества атрибутов assert len(model_type.schema().get("properties", {}).keys()) == 7 + def test_abstract(self, workbook: Any) -> None: + """ + Тестирование чтения автореферата. + + :param workbook: Объект тестовой рабочей книги. + """ + + models = AbstractReader(workbook).read() + + assert len(models) == 1 + model = models[0] + + model_type = AbstractModel + + assert isinstance(model, model_type) + assert model.author == "Иванов И.М." + assert model.title == "Наука как искусство" + assert model.degree == "д-р. / канд." + assert model.branch == "экон." + assert model.speciality_code == "01.01.01" + assert model.city == "СПб." + assert model.year == 2020 + assert model.pages == 199 + + # проверка общего количества атрибутов + assert len(model_type.schema().get("properties", {}).keys()) == 8 + + def test_dissertation(self, workbook: Any) -> None: + """ + Тестирование чтения диссертации. + + :param workbook: Объект тестовой рабочей книги. + """ + + models = DissertationReader(workbook).read() + + assert len(models) == 1 + model = models[0] + + model_type = DissertationModel + + assert isinstance(model, model_type) + assert model.author == "Иванов И.М." + assert model.title == "Наука как искусство" + assert model.degree == "д-р. / канд." + assert model.branch == "экон." + assert model.speciality_code == "01.01.01" + assert model.city == "СПб." + assert model.year == 2020 + assert model.pages == 199 + + # проверка общего количества атрибутов + assert len(model_type.schema().get("properties", {}).keys()) == 8 + def test_sources_reader(self) -> None: """ Тестирование функции чтения всех моделей из источника. @@ -111,7 +173,7 @@ def test_sources_reader(self) -> None: models = SourcesReader(TEMPLATE_FILE_PATH).read() # проверка общего считанного количества моделей - assert len(models) == 8 + assert len(models) == 10 # проверка наличия всех ожидаемых типов моделей среди типов считанных моделей model_types = {model.__class__.__name__ for model in models} @@ -119,4 +181,6 @@ def test_sources_reader(self) -> None: BookModel.__name__, InternetResourceModel.__name__, ArticlesCollectionModel.__name__, + DissertationModel.__name__, + AbstractModel.__name__, }