Moved the AI tests into the AI folder. (#315)

This commit is contained in:
2023-11-03 13:45:24 +01:00
committed by GitHub
parent 9d04e46f55
commit ad36c68993
4 changed files with 0 additions and 0 deletions

View File

@ -1,283 +0,0 @@
"""Tests for checking NER Pipeline."""
from unittest.mock import Mock, patch
import pytest
from aki_prj23_transparenzregister.ai.ner_pipeline import EntityPipeline
from aki_prj23_transparenzregister.config.config_template import MongoConnection
@pytest.fixture()
def mock_mongo_connection() -> MongoConnection:
"""Mock MongoConnector class.
Args:
mocker (any): Library mocker
Returns:
Mock: Mocked MongoConnector
"""
return MongoConnection("", "", None, "" "", "")
@pytest.fixture()
def mock_mongo_connector(mocker: Mock) -> Mock:
"""Mock MongoConnector class.
Args:
mocker (any): Library mocker
Returns:
Mock: Mocked MongoConnector
"""
mock = Mock()
mocker.patch(
"aki_prj23_transparenzregister.utils.mongo.connector.MongoConnector",
return_value=mock,
)
mock.database = {"news": Mock()}
return mock
@pytest.fixture()
def mock_spacy(mocker: Mock) -> Mock:
"""Mock MongoConnector class.
Args:
mocker (any): Library mocker
Returns:
Mock: Mocked MongoConnector
"""
mock = Mock()
mocker.patch(
"aki_prj23_transparenzregister.ai.ner_service.NerAnalysisService.init_spacy",
return_value=mock,
)
return mock
# Mocking the NerAnalysisService methods
@patch("aki_prj23_transparenzregister.ai.ner_service.NerAnalysisService.ner_spacy")
def test_entity_pipeline_with_spacy(
mock_ner_spacy: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific NER result
mock_ner_spacy.return_value = {"ORG": 2, "PERSON": 1}
# Create an instance of the EntityPipeline
entity_pipeline = EntityPipeline(mock_mongo_connection)
# Mock the news collection and documents for testing
mock_collection = Mock()
mock_documents = [{"_id": "document1", "title": "Apple Inc is a tech company."}]
# Set the collection to the mock_collection
entity_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method with spaCy NER
entity_pipeline.process_documents(
entity="ORG", doc_attrib="title", ner_selection="use_spacy_ner"
)
# Ensure that ner_spacy was called with the correct parameters
mock_ner_spacy.assert_called_once_with(mock_documents[0], "ORG", "title")
# Ensure that the document in the collection was updated with the NER results
mock_collection.update_one.assert_called_once_with(
{"_id": "document1"},
{"$set": {"companies": {"ORG": 2, "PERSON": 1}}},
)
@patch("aki_prj23_transparenzregister.ai.ner_service.NerAnalysisService.ner_spacy")
def test_entity_pipeline_with_spacy_no_docs(
mock_ner_spacy: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific NER result
mock_ner_spacy.return_value = {"ORG": 2, "PERSON": 1}
# Create an instance of the EntityPipeline
entity_pipeline = EntityPipeline(mock_mongo_connection)
# Mock the news collection and documents for testing
mock_collection = Mock()
mock_documents: list[dict] = []
# Set the collection to the mock_collection
entity_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method with spaCy NER
entity_pipeline.process_documents(
entity="ORG", doc_attrib="title", ner_selection="use_spacy_ner"
)
# Ensure that sentiment_spacy was not called
mock_ner_spacy.assert_not_called()
# Ensure that the document in the collection was not updated
mock_collection.assert_not_called()
@patch(
"aki_prj23_transparenzregister.ai.ner_service.NerAnalysisService.ner_company_list"
)
def test_entity_pipeline_with_companylist_ner(
mock_ner_companylist: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Konfigurieren Sie das Mock-Objekt, um ein spezifisches NER-Ergebnis zurückzugeben
mock_ner_companylist.return_value = {"ORG": 3, "LOCATION": 2}
# Create an instance of the EntityPipeline
entity_pipeline = EntityPipeline(mock_mongo_connection)
# Mock die News-Sammlung und Dokumente für Tests
mock_collection = Mock()
mock_documents = [
{"_id": "document2", "title": "Siemens ist ein deutsches Unternehmen."}
]
# Set the collection to the mock_collection
entity_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method with Company List NER
entity_pipeline.process_documents(
entity="ORG", doc_attrib="title", ner_selection="use_companylist_ner"
)
# Überprüfen Sie, ob ner_company_list mit den richtigen Parametern aufgerufen wurde
mock_ner_companylist.assert_called_once_with(mock_documents[0], "ORG", "title")
# Überprüfen Sie, ob das Dokument in der Sammlung mit den NER-Ergebnissen aktualisiert wurde
mock_collection.update_one.assert_called_once_with(
{"_id": "document2"},
{"$set": {"companies": {"ORG": 3, "LOCATION": 2}}},
)
@patch(
"aki_prj23_transparenzregister.ai.ner_service.NerAnalysisService.ner_company_list"
)
def test_entity_pipeline_with_companylist_ner_no_docs(
mock_ner_companylist: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific NER result
mock_ner_companylist.return_value = {"ORG": 3, "LOCATION": 2}
# Create an instance of the EntityPipeline
entity_pipeline = EntityPipeline(mock_mongo_connection)
# Mock die News-Sammlung und Dokumente für Tests
mock_collection = Mock()
mock_documents: list[dict] = []
# Set the collection to the mock_collection
entity_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method with Company List NER
entity_pipeline.process_documents(
entity="ORG", doc_attrib="title", ner_selection="use_companylist_ner"
)
# Ensure that ner_company_list is not called
mock_ner_companylist.assert_not_called()
# Ensure that the document in the collection was not updated
mock_collection.update_one.assert_not_called()
# Add more test cases for other NER methods (e.g., use_companylist_ner, use_transformer_ner) following a similar pattern.
@patch("aki_prj23_transparenzregister.ai.ner_service.NerAnalysisService.ner_spacy")
def test_entity_pipeline_with_transformer(
mock_ner_transformer: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific NER result
mock_ner_transformer.return_value = {"ORG": 2, "PERSON": 1}
# Create an instance of the EntityPipeline
entity_pipeline = EntityPipeline(mock_mongo_connection)
# Mock the news collection and documents for testing
mock_collection = Mock()
mock_documents = [{"_id": "document1", "title": "Apple Inc is a tech company."}]
# Set the collection to the mock_collection
entity_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method with spaCy NER
entity_pipeline.process_documents(
entity="ORG", doc_attrib="title", ner_selection="use_spacy_ner"
)
# Ensure that ner_spacy was called with the correct parameters
mock_ner_transformer.assert_called_once_with(mock_documents[0], "ORG", "title")
# Ensure that the document in the collection was updated with the NER results
mock_collection.update_one.assert_called_once_with(
{"_id": "document1"},
{"$set": {"companies": {"ORG": 2, "PERSON": 1}}},
)
@patch("aki_prj23_transparenzregister.ai.ner_service.NerAnalysisService.ner_spacy")
def test_entity_pipeline_with_transformer_no_docs(
mock_ner_transformer: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific NER result
mock_ner_transformer.return_value = {"ORG": 2, "PERSON": 1}
# Create an instance of the EntityPipeline
entity_pipeline = EntityPipeline(mock_mongo_connection)
# Mock the news collection and documents for testing
mock_collection = Mock()
mock_documents: list[dict] = []
# Set the collection to the mock_collection
entity_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method with spaCy NER
entity_pipeline.process_documents(
entity="ORG", doc_attrib="title", ner_selection="use_spacy_ner"
)
# Ensure that ner_transformer is not called
mock_ner_transformer.assert_not_called()
# Ensure that the document in the collection was not updated
mock_collection.update_one.assert_not_called()

View File

@ -1,54 +0,0 @@
"""Tests for checking NER Services."""
from aki_prj23_transparenzregister.ai.ner_service import NerAnalysisService
def test_ner_spacy() -> None:
"""Mock TestNerService."""
# Create instance of NerAnalysisService with use_spacy=True
ner_service = NerAnalysisService(
use_spacy=True, use_transformer=False, use_companylist=False
)
# 1st testing
doc = {"title": "Siemens ist ein Unternehmen."}
result = ner_service.ner_spacy(doc, ent_type="ORG", doc_attrib="title")
assert result == {"Siemens": 1}
# 2nd testing
doc = {"text": "BASF ist ein großes Unternehmen."}
result = ner_service.ner_spacy(doc, ent_type="ORG", doc_attrib="text")
assert result == {"BASF": 1}
def test_ner_company_list() -> None:
"""Mock test_ner_company."""
# Create instance of NerAnalysisService with use_companylist=True
ner_service = NerAnalysisService(
use_spacy=False, use_transformer=False, use_companylist=True
)
doc = {"title": "Siemens ist ein Unternehmen."}
result = ner_service.ner_company_list(doc, ent_type="ORG", doc_attrib="title")
assert result == {"siemens": 1}
# 2nd testing
doc = {"text": "BASF ist ein großes Unternehmen."}
result = ner_service.ner_company_list(doc, ent_type="ORG", doc_attrib="text")
assert result == {"basf": 1}
def test_ner_transformer() -> None:
"""Mock test_ner_company."""
# Create instance of NerAnalysisService with use_use_companylist=True
ner_service = NerAnalysisService(
use_spacy=False, use_transformer=True, use_companylist=False
)
doc = {"title": "Siemens ist ein Unternehmen."}
result = ner_service.ner_transformer(doc, ent_type="ORG", doc_attrib="title")
assert result == {"Siemens": 1}
# 2nd testing
doc = {"text": "BASF ist ein großes Unternehmen."}
result = ner_service.ner_transformer(doc, ent_type="ORG", doc_attrib="text")
assert result == {"BASF": 1}

View File

@ -1,210 +0,0 @@
"""Unit test for sentiment pipeline."""
from unittest.mock import Mock, patch
import pytest
from aki_prj23_transparenzregister.ai.sentiment_pipeline import (
SentimentPipeline,
)
from aki_prj23_transparenzregister.config.config_template import MongoConnection
@pytest.fixture()
def mock_mongo_connection() -> MongoConnection:
"""Mock MongoConnector class.
Args:
mocker (any): Library mocker
Returns:
Mock: Mocked MongoConnector
"""
return MongoConnection("", "", None, "" "", "")
@pytest.fixture()
def mock_mongo_connector(mocker: Mock) -> Mock:
"""Mock MongoConnector class.
Args:
mocker (any): Library mocker
Returns:
Mock: Mocked MongoConnector
"""
mock = Mock()
mocker.patch(
"aki_prj23_transparenzregister.utils.mongo.connector.MongoConnector",
return_value=mock,
)
mock.database = {"news": Mock()}
return mock
@pytest.fixture()
def mock_spacy(mocker: Mock) -> Mock:
"""Mock MongoConnector class.
Args:
mocker (any): Library mocker
Returns:
Mock: Mocked MongoConnector
"""
mock = Mock()
mocker.patch(
"aki_prj23_transparenzregister.ai.sentiment_service.SentimentAnalysisService.init_spacy",
return_value=mock,
)
return mock
@patch(
"aki_prj23_transparenzregister.ai.sentiment_service.SentimentAnalysisService.sentiment_spacy"
)
def test_sentiment_pipeline_existing_sentiment(
mock_sentiment_spacy: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific sentiment result
mock_sentiment_spacy.return_value = ("positive", 0.8)
# Create an instance of the SentimentPipeline
sentiment_pipeline = SentimentPipeline(mock_mongo_connection)
# Mock the news collection and documents for testing
mock_collection = Mock()
mock_documents = [
{
"_id": "document1",
"text": "This is a positive text.",
"sentiment": {"label": "neutral", "score": 0.5},
}
]
# Set the collection to the mock_collection
sentiment_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method
sentiment_pipeline.process_documents("text", "use_spacy")
# Ensure that sentiment_spacy was called with the correct text
mock_sentiment_spacy.assert_called_once_with("This is a positive text.")
# Ensure that the document in the collection was not updated with sentiment
# mock_collection.update_one.assert_not_called()
@patch(
"aki_prj23_transparenzregister.ai.sentiment_service.SentimentAnalysisService.sentiment_spacy"
)
def test_sentiment_pipeline_no_documents(
mock_sentiment_spacy: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific sentiment result
mock_sentiment_spacy.return_value = ("positive", 0.8)
# Create an instance of the SentimentPipeline
sentiment_pipeline = SentimentPipeline(mock_mongo_connection)
# Mock the news collection to return an empty result
mock_collection = Mock()
mock_collection.find.return_value = []
# Set the collection to the mock_collection
sentiment_pipeline.news_obj.collection = mock_collection
# Call the process_documents method
sentiment_pipeline.process_documents("text", "use_spacy")
# Ensure that sentiment_spacy was not called
mock_sentiment_spacy.assert_not_called()
# Ensure that the document in the collection was not updated with sentiment
mock_collection.update_one.assert_not_called()
@patch(
"aki_prj23_transparenzregister.ai.sentiment_service.SentimentAnalysisService.sentiment_spacy"
)
def test_sentiment_pipeline_with_spacy(
mock_sentiment_spacy: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific sentiment result
mock_sentiment_spacy.return_value = ("positive", 0.8)
# Create an instance of the SentimentPipeline
sentiment_pipeline = SentimentPipeline(mock_mongo_connection)
# Mock the news collection and documents for testing
mock_collection = Mock()
mock_documents = [{"_id": "document1", "text": "This is a positive text."}]
# Set the collection to the mock_collection
sentiment_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method
sentiment_pipeline.process_documents("text", "use_spacy")
# Ensure that sentiment_spacy was called with the correct text
mock_sentiment_spacy.assert_called_once_with("This is a positive text.")
# Ensure that the document in the collection was updated with the sentiment result
mock_collection.update_one.assert_called_once_with(
{"_id": "document1"},
{"$set": {"sentiment": {"label": "positive", "score": 0.8}}},
)
# Mocking the SentimentAnalysisService methods
@patch(
"aki_prj23_transparenzregister.ai.sentiment_service.SentimentAnalysisService.sentiment_transformer"
)
def test_sentiment_pipeline_with_transformer(
mock_sentiment_transformer: Mock,
mock_mongo_connector: Mock,
mock_mongo_connection: MongoConnection,
mock_spacy: Mock,
) -> None:
# Configure the mock to return a specific sentiment result
mock_sentiment_transformer.return_value = ("negative", 0.6)
# Create an instance of the SentimentPipeline
sentiment_pipeline = SentimentPipeline(mock_mongo_connection)
# Mock the news collection and documents for testing
mock_collection = Mock()
mock_documents = [{"_id": "document2", "text": "This is a negative text."}]
# Set the collection to the mock_collection
sentiment_pipeline.news_obj.collection = mock_collection
# Mock the find method of the collection to return the mock documents
mock_collection.find.return_value = mock_documents
# Call the process_documents method
sentiment_pipeline.process_documents("text", "use_transformer")
# Ensure that sentiment_transformer was called with the correct text
mock_sentiment_transformer.assert_called_once_with("This is a negative text.")
# Ensure that the document in the collection was updated with the sentiment result
mock_collection.update_one.assert_called_once_with(
{"_id": "document2"},
{"$set": {"sentiment": {"label": "negative", "score": 0.6}}},
)

View File

@ -1,78 +0,0 @@
"""Tests for checking Sentiment Services."""
from aki_prj23_transparenzregister.ai.sentiment_service import (
SentimentAnalysisService,
)
def test_sentiment_service_with_spacy_pos() -> None:
"""Mock testing spaCy Sentiment Service with positive sentiment."""
# Init SentimentAnalysisService with spaCy
sentiment_service = SentimentAnalysisService(use_spacy=True)
# run the test
text = "Dies ist ein großartiger Test. Ich liebe es!"
sentiment, score = sentiment_service.sentiment_spacy(text)
assert sentiment == "positive"
assert score > 0
def test_sentiment_service_with_spacy_neg() -> None:
"""Mock testing spaCy Sentiment Service with negative sentiment."""
# Init SentimentAnalysisService with spaCy
sentiment_service = SentimentAnalysisService(use_spacy=True)
# run the test
text = "Dies ist ein wirklich schrecklicher Test. Ich hasse ihn!"
sentiment, score = sentiment_service.sentiment_spacy(text)
assert sentiment == "negative"
assert score > 0
def test_sentiment_service_with_spacy_neut() -> None:
"""Mock testing spaCy Sentiment Service with neutral sentiment."""
# Init SentimentAnalysisService with spaCy
sentiment_service = SentimentAnalysisService(use_spacy=True)
# run the test
text = "Dies ist ein Test."
sentiment, score = sentiment_service.sentiment_spacy(text)
assert sentiment == "neutral"
assert score >= 0
def test_sentiment_service_with_transformer_pos() -> None:
"""Mock testing Transformer Sentiment Service with positive Sentiment."""
# Init SentimentAnalysisService with Transformer
sentiment_service = SentimentAnalysisService(use_transformer=True)
# run the test
text = "Dies ist ein großartiger Test. Ich liebe es!"
sentiment, score = sentiment_service.sentiment_transformer(text)
assert sentiment == "positive"
assert score > 0
def test_sentiment_service_with_transformer_neg() -> None:
"""Mock testing Transformer Sentiment Service with negative Sentiment."""
# Init SentimentAnalysisService with Transformer
sentiment_service = SentimentAnalysisService(use_transformer=True)
# run the test
text = "Dies ist ein wirklich schrecklicher Test. Ich hasse ihn!"
sentiment, score = sentiment_service.sentiment_transformer(text)
assert sentiment == "negative"
assert score > 0
def test_sentiment_service_with_transformer_neut() -> None:
"""Mock testing Transformer Sentiment Service with neutral Sentiment."""
# Init SentimentAnalysisService with Transformer
sentiment_service = SentimentAnalysisService(use_transformer=True)
# run the test
text = "Das ist ein Text, ohne besondere Stimmung."
sentiment, score = sentiment_service.sentiment_transformer(text)
assert sentiment == "neutral"
assert score >= 0