Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/343.housekeeping
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated test coverage for the codebase
9 changes: 4 additions & 5 deletions tests/unit/data/cogent/cogent3_result.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
[
{
"summary": "During this maintenance window, you will experience an interruption in service while Zayo completes the maintenance activities; the interruption is expected to be less than 7 hours; however, due to the complexity of the work, your downtime may be longer.",
"account": "Cogent Customer",
"maintenance_id": "VN12345",
"circuits": [
{
"circuit_id": "1-300123456",
"impact": "OUTAGE"
}
],
"end": 1649671200,
"maintenance_id": "VN12345",
"start": 1667475840,
"status": "CONFIRMED",
"summary": "During this maintenance window, you will experience an interruption in service while Zayo completes the maintenance activities; the interruption is expected to be less than 7 hours; however, due to the complexity of the work, your downtime may be longer."
"start": 1647057600,
"end": 1649671200
}
]
11 changes: 11 additions & 0 deletions tests/unit/data/cogent/cogent3_subject_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"circuits": [
{
"circuit_id": "1-877-7",
"impact": "OUTAGE"
}
],
"status": "CONFIRMED"
}
]
112 changes: 111 additions & 1 deletion tests/unit/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from circuit_maintenance_parser.parsers.att import HtmlParserATT1, XlsxParserATT1
from circuit_maintenance_parser.parsers.aws import SubjectParserAWS1, TextParserAWS1
from circuit_maintenance_parser.parsers.bso import HtmlParserBSO1
from circuit_maintenance_parser.parsers.cogent import HtmlParserCogent1
from circuit_maintenance_parser.parsers.cogent import HtmlParserCogent1, SubjectParserCogent1
from circuit_maintenance_parser.parsers.colt import CsvParserColt1, SubjectParserColt1, SubjectParserColt2
from circuit_maintenance_parser.parsers.crowncastle import HtmlParserCrownCastle1
from circuit_maintenance_parser.parsers.equinix import HtmlParserEquinix, SubjectParserEquinix
Expand Down Expand Up @@ -252,6 +252,11 @@ def default(self, o):
Path(dir_path, "data", "cogent", "cogent2.html"),
Path(dir_path, "data", "cogent", "cogent2_result.json"),
),
(
SubjectParserCogent1,
Path(dir_path, "data", "cogent", "cogent3.eml"),
Path(dir_path, "data", "cogent", "cogent3_subject_result.json"),
),
# Colt
(
CsvParserColt1,
Expand Down Expand Up @@ -844,3 +849,108 @@ def test_parser_no_data(parser_class):
"""Test parser with no data."""
with pytest.raises(ParserError):
parser_class().parse(b"", parser_class.get_data_types()[0]) # pylint: disable=protected-access


def test_openai_parser_import_error():
"""Test OpenAI parser raises ImportError when openai package is not available."""
from unittest.mock import patch

from circuit_maintenance_parser.parsers.openai import OpenAIParser

# Mock the absence of openai package
with patch("circuit_maintenance_parser.parsers.openai._HAS_OPENAI", False):
parser = OpenAIParser()
with pytest.raises(ImportError, match="openai extra is required"):
parser.get_llm_response("test content")


def test_openai_parser_api_success():
"""Test OpenAI parser with successful API response."""
import sys
from unittest.mock import MagicMock, patch

from circuit_maintenance_parser.parsers.openai import OpenAIParser

# Mock successful API response
mock_response = MagicMock()
mock_response.usage.total_tokens = 100
mock_response.choices = [MagicMock()]
mock_response.choices[0].message.content = '{"test": "data"}'

mock_client = MagicMock()
mock_client.chat.completions.create.return_value = mock_response

# Mock the OpenAI module
mock_openai_module = MagicMock()
mock_openai_module.OpenAI.return_value = mock_client

with patch.dict(sys.modules, {"openai": mock_openai_module}):
with patch("circuit_maintenance_parser.parsers.openai._HAS_OPENAI", True):
# Re-import to get the mocked OpenAI
from circuit_maintenance_parser.parsers import openai as openai_parser

openai_parser.OpenAI = mock_openai_module.OpenAI

parser = OpenAIParser()
result = parser.get_llm_response("test content")

assert result == {"test": "data"}
assert parser.tokens_used == 100


def test_openai_parser_api_exception():
"""Test OpenAI parser handles API exceptions."""
import sys
from unittest.mock import MagicMock, patch

from circuit_maintenance_parser.parsers.openai import OpenAIParser

mock_client = MagicMock()
mock_client.chat.completions.create.side_effect = Exception("API Error")

# Mock the OpenAI module
mock_openai_module = MagicMock()
mock_openai_module.OpenAI.return_value = mock_client

with patch.dict(sys.modules, {"openai": mock_openai_module}):
with patch("circuit_maintenance_parser.parsers.openai._HAS_OPENAI", True):
from circuit_maintenance_parser.parsers import openai as openai_parser

openai_parser.OpenAI = mock_openai_module.OpenAI

parser = OpenAIParser()
result = parser.get_llm_response("test content")

assert result is None


def test_openai_parser_invalid_json():
"""Test OpenAI parser handles invalid JSON response."""
import sys
from unittest.mock import MagicMock, patch

from circuit_maintenance_parser.parsers.openai import OpenAIParser

# Mock API response with invalid JSON
mock_response = MagicMock()
mock_response.usage.total_tokens = 50
mock_response.choices = [MagicMock()]
mock_response.choices[0].message.content = "not valid json"

mock_client = MagicMock()
mock_client.chat.completions.create.return_value = mock_response

# Mock the OpenAI module
mock_openai_module = MagicMock()
mock_openai_module.OpenAI.return_value = mock_client

with patch.dict(sys.modules, {"openai": mock_openai_module}):
with patch("circuit_maintenance_parser.parsers.openai._HAS_OPENAI", True):
from circuit_maintenance_parser.parsers import openai as openai_parser

openai_parser.OpenAI = mock_openai_module.OpenAI

parser = OpenAIParser()
result = parser.get_llm_response("test content")

assert result is None