120 lines
4.3 KiB
Python
120 lines
4.3 KiB
Python
"""Tests for the NLP analyzer module."""
|
|
|
|
import pytest
|
|
from nl2gherkin.nlp.analyzer import ActionType, ActorType, NLPAnalyzer
|
|
|
|
|
|
class TestNLPAnalyzer:
|
|
"""Test cases for NLPAnalyzer."""
|
|
|
|
@pytest.fixture
|
|
def analyzer(self):
|
|
"""Create an analyzer instance."""
|
|
return NLPAnalyzer()
|
|
|
|
def test_user_story_parsing(self, analyzer):
|
|
"""Test parsing classic user story format."""
|
|
text = "As a user, I want to login so that I can access my account"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.actor == "user"
|
|
assert "login" in result.action.lower()
|
|
assert result.benefit is not None
|
|
|
|
def test_user_story_with_admin(self, analyzer):
|
|
"""Test parsing admin user story."""
|
|
text = "As an administrator, I want to manage users so that I can control access"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.actor == "administrator"
|
|
assert "manage" in result.action.lower() or result.action is not None
|
|
assert result.benefit is not None
|
|
|
|
def test_simple_requirement(self, analyzer):
|
|
"""Test parsing a simple requirement without user story format."""
|
|
text = "The system should send an email notification"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.action is not None or "send" in result.raw_text.lower()
|
|
|
|
def test_action_extraction_create(self, analyzer):
|
|
"""Test action extraction for create operations."""
|
|
text = "As a user, I want to create a new document"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.action_type in [ActionType.CREATE, ActionType.UNKNOWN]
|
|
|
|
def test_action_extraction_read(self, analyzer):
|
|
"""Test action extraction for read operations."""
|
|
text = "As a user, I want to view my profile"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.action_type in [ActionType.READ, ActionType.UNKNOWN]
|
|
|
|
def test_action_extraction_delete(self, analyzer):
|
|
"""Test action extraction for delete operations."""
|
|
text = "As an admin, I want to delete the account"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.action_type in [ActionType.DELETE, ActionType.UNKNOWN]
|
|
|
|
def test_actor_type_user(self, analyzer):
|
|
"""Test actor type detection for user."""
|
|
text = "As a customer, I want to place an order"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.actor_type in [ActorType.USER, ActorType.UNKNOWN]
|
|
|
|
def test_actor_type_admin(self, analyzer):
|
|
"""Test actor type detection for admin."""
|
|
text = "As an administrator, I want to view logs"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.actor_type in [ActorType.ADMIN, ActorType.UNKNOWN]
|
|
|
|
def test_actor_type_system(self, analyzer):
|
|
"""Test actor type detection for system."""
|
|
text = "The system should validate the input"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.actor_type in [ActorType.SYSTEM, ActorType.UNKNOWN]
|
|
|
|
def test_condition_extraction(self, analyzer):
|
|
"""Test condition extraction from requirements."""
|
|
text = "When the user clicks submit, the form should be validated"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert result.condition is not None or "click" in result.raw_text.lower()
|
|
|
|
def test_variable_extraction(self, analyzer):
|
|
"""Test variable/parameter extraction."""
|
|
text = "As a user, I want to search for <product_name> so that I can find it"
|
|
result = analyzer.analyze(text)
|
|
|
|
assert "product_name" in result.variables
|
|
|
|
def test_to_dict_method(self, analyzer):
|
|
"""Test that to_dict returns a proper dictionary."""
|
|
text = "As a user, I want to login"
|
|
result = analyzer.analyze(text)
|
|
|
|
result_dict = result.to_dict()
|
|
|
|
assert isinstance(result_dict, dict)
|
|
assert "actor" in result_dict
|
|
assert "action" in result_dict
|
|
assert "actor_type" in result_dict
|
|
|
|
def test_empty_input(self, analyzer):
|
|
"""Test handling of empty input."""
|
|
result = analyzer.analyze("")
|
|
|
|
assert result.raw_text == ""
|
|
|
|
def test_ambiguity_detection_call(self, analyzer):
|
|
"""Test that ambiguity detection can be called."""
|
|
text = "As a user, I want to do something"
|
|
result = analyzer.analyze_ambiguity(text)
|
|
|
|
assert isinstance(result, list)
|