QA_VAM / utils.py
JanviMl's picture
Create utils.py
44eab5b verified
import json
import yaml
import random
import time
import os
from typing import Dict, List, Any
# Mock LLM responses for demo purposes
# In production, replace with actual OpenAI API calls
PROMPT_TEMPLATES = {
"bdd_generation": """
Convert the following user story into a comprehensive Gherkin BDD scenario for insurance billing domain:
User Story: {user_story}
Generate a detailed BDD scenario with:
- Clear Given/When/Then steps
- Realistic insurance billing context
- Edge cases and validations
- Data examples where applicable
Format as proper Gherkin syntax.
""",
"test_script_generation": """
Convert the following BDD scenario into a Python pytest test script:
BDD Scenario:
{bdd_scenario}
Generate a complete pytest test script with:
- Proper test class structure
- Setup and teardown methods
- Realistic assertions
- Mock data and API calls
- Error handling
- Comments explaining the test logic
Story Context: {story_title}
""",
"defect_summary": """
Analyze the following test failure log and BDD scenario to create a comprehensive defect summary:
BDD Scenario:
{bdd_scenario}
Failure Log:
{failure_log}
Generate a professional defect report with:
- Clear defect title
- Severity level (Critical/High/Medium/Low)
- Steps to reproduce
- Expected vs actual behavior
- Root cause analysis
- Suggested fix
- Impact assessment
"""
}
def simulate_llm_call(prompt: str, context: Dict[str, Any] = None) -> str:
"""
Simulate LLM API call with realistic responses
In production, replace with actual OpenAI/Transformers call
"""
time.sleep(random.uniform(1, 3)) # Simulate API latency
if "BDD scenario" in prompt or "Gherkin" in prompt:
return generate_mock_bdd(context.get("user_story", ""))
elif "pytest" in prompt or "test script" in prompt:
return generate_mock_test_script(context.get("story_title", "Test"))
elif "defect" in prompt or "failure" in prompt:
return generate_mock_defect_summary(context.get("failure_log", ""))
else:
return "Mock LLM response generated successfully."
def generate_bdd_scenario(user_story: str) -> str:
"""
Generate BDD scenario from user story using LLM
"""
mock_scenarios = {
"invoice": """
Feature: Invoice Generation
As a policyholder
I want invoices generated automatically
So that I can track my insurance charges
Scenario: Successful invoice generation within 24 hours
Given I am a policyholder with an active policy
And my policy was issued today
When the invoice generation job runs
Then an invoice should be created within 24 hours
And the invoice should include policy details
And the invoice should be sent to my registered email
And the invoice status should be "Generated"
Scenario: Invoice generation failure handling
Given I am a policyholder with an active policy
And the billing system is experiencing issues
When the invoice generation job runs
Then the system should retry up to 3 times
And if still failing, an alert should be sent to administrators
And the policyholder should be notified of the delay
""",
"payment": """
Feature: Credit Card Payment Processing
As a policyholder
I want to pay my premium using credit card
So that my policy remains active
Scenario: Successful credit card payment
Given I am on the payment page
And I have a valid credit card
When I enter card number "4111111111111111"
And I enter CVV "123"
And I enter expiry date "12/25"
And I click "Pay Now"
Then the payment should be processed successfully
And I should receive a payment confirmation
And my policy status should be updated to "Active"
Scenario: Invalid CVV validation
Given I am on the payment page
When I enter card number "4111111111111111"
And I enter CVV "12" (invalid 2-digit CVV)
And I enter expiry date "12/25"
And I click "Pay Now"
Then I should see error message "Invalid CVV format"
And the payment should not be processed
And the form should highlight the CVV field
""",
"default": """
Feature: Insurance System Functionality
As a user
I want the system to work as expected
So that I can complete my insurance tasks
Scenario: Basic system operation
Given I am a registered user
And I have access to the insurance system
When I perform the required action
Then the system should respond appropriately
And the result should meet business requirements
And the user experience should be smooth
"""
}
# Simple keyword matching for demo
if "invoice" in user_story.lower():
return mock_scenarios["invoice"]
elif "payment" in user_story.lower() or "credit card" in user_story.lower():
return mock_scenarios["payment"]
else:
return mock_scenarios["default"]
def generate_test_script(bdd_scenario: str, story_title: str) -> str:
"""
Generate pytest test script from BDD scenario
"""
script_templates = {
"invoice": '''
import pytest
from unittest.mock import Mock, patch
from datetime import datetime, timedelta
import json
class TestInvoiceGeneration:
"""Test cases for invoice generation functionality"""
def setup_method(self):
"""Setup test environment before each test"""
self.mock_policy = {
"id": "POL-12345",
"holder_name": "John Doe",
"premium": 1200.00,
"issue_date": datetime.now().isoformat(),
"email": "john.doe@email.com"
}
self.invoice_service = Mock()
@patch('billing.scheduler.InvoiceGenerator')
def test_invoice_generated_within_24_hours(self, mock_generator):
"""Test that invoice is generated within 24 hours of policy issuance"""
# Given
mock_generator.return_value.generate_invoice.return_value = {
"invoice_id": "INV-001",
"policy_id": "POL-12345",
"amount": 1200.00,
"status": "Generated",
"created_at": datetime.now().isoformat()
}
# When
result = self.invoice_service.process_new_policy(self.mock_policy)
# Then
assert result["status"] == "Generated"
assert result["invoice_id"] is not None
mock_generator.return_value.generate_invoice.assert_called_once()
@patch('billing.email.EmailService')
def test_invoice_email_sent_to_policyholder(self, mock_email):
"""Test that invoice email is sent to registered email"""
# Given
mock_email.return_value.send_invoice.return_value = True
# When
result = self.invoice_service.send_invoice_email(
self.mock_policy["email"],
"INV-001"
)
# Then
assert result is True
mock_email.return_value.send_invoice.assert_called_once_with(
self.mock_policy["email"],
"INV-001"
)
def test_invoice_retry_mechanism(self):
"""Test retry mechanism when invoice generation fails"""
# Given
self.invoice_service.max_retries = 3
self.invoice_service.generate_invoice.side_effect = [
Exception("DB Connection Error"),
Exception("Service Unavailable"),
{"invoice_id": "INV-001", "status": "Generated"}
]
# When
result = self.invoice_service.process_with_retry(self.mock_policy)
# Then
assert result["status"] == "Generated"
assert self.invoice_service.generate_invoice.call_count == 3
''',
"payment": '''
import pytest
from unittest.mock import Mock, patch
import json
class TestCreditCardPayment:
"""Test cases for credit card payment processing"""
def setup_method(self):
"""Setup test environment before each test"""
self.payment_service = Mock()
self.valid_card = {
"number": "4111111111111111",
"cvv": "123",
"expiry": "12/25",
"amount": 1200.00
}
@patch('payment.processor.PaymentGateway')
def test_successful_credit_card_payment(self, mock_gateway):
"""Test successful credit card payment processing"""
# Given
mock_gateway.return_value.process_payment.return_value = {
"transaction_id": "TXN-001",
"status": "SUCCESS",
"amount": 1200.00,
"timestamp": "2024-01-15T10:30:00Z"
}
# When
result = self.payment_service.process_payment(self.valid_card)
# Then
assert result["status"] == "SUCCESS"
assert result["transaction_id"] is not None
assert result["amount"] == 1200.00
def test_invalid_cvv_validation(self):
"""Test CVV validation for invalid format"""
# Given
invalid_card = self.valid_card.copy()
invalid_card["cvv"] = "12" # Invalid 2-digit CVV
# When
result = self.payment_service.validate_card(invalid_card)
# Then
assert result["valid"] is False
assert "Invalid CVV format" in result["errors"]
assert "cvv" in result["error_fields"]
@patch('payment.processor.PaymentGateway')
def test_payment_failure_handling(self, mock_gateway):
"""Test handling of payment failures"""
# Given
mock_gateway.return_value.process_payment.side_effect = Exception("Payment declined")
# When
result = self.payment_service.process_payment(self.valid_card)
# Then
assert result["status"] == "FAILED"
assert "Payment declined" in result["error_message"]
def test_card_expiry_validation(self):
"""Test credit card expiry date validation"""
# Given
expired_card = self.valid_card.copy()
expired_card["expiry"] = "12/20" # Expired date
# When
result = self.payment_service.validate_card(expired_card)
# Then
assert result["valid"] is False
assert "Card has expired" in result["errors"]
''',
"default": '''
import pytest
from unittest.mock import Mock, patch
class TestInsuranceSystemFunctionality:
"""Test cases for general insurance system functionality"""
def setup_method(self):
"""Setup test environment before each test"""
self.system_service = Mock()
self.test_user = {
"id": "USER-001",
"name": "Test User",
"email": "test@example.com",
"role": "policyholder"
}
def test_user_authentication(self):
"""Test user authentication functionality"""
# Given
credentials = {"username": "testuser", "password": "password123"}
# When
result = self.system_service.authenticate(credentials)
# Then
assert result["authenticated"] is True
assert result["user_id"] is not None
def test_system_response_time(self):
"""Test that system responds within acceptable time limits"""
# Given
start_time = time.time()
# When
result = self.system_service.process_request(self.test_user)
end_time = time.time()
# Then
response_time = end_time - start_time
assert response_time < 5.0 # Response should be under 5 seconds
assert result["status"] == "success"
def test_error_handling(self):
"""Test system error handling"""
# Given
self.system_service.process_request.side_effect = Exception("System error")
# When
result = self.system_service.handle_error(self.test_user)
# Then
assert result["status"] == "error"
assert "error_message" in result
'''
}
if "invoice" in bdd_scenario.lower():
return script_templates["invoice"]
elif "payment" in bdd_scenario.lower() or "credit card" in bdd_scenario.lower():
return script_templates["payment"]
else:
return script_templates["default"]
def simulate_test_execution(story_title: str) -> Dict[str, str]:
"""
Simulate test execution with realistic pass/fail scenarios
"""
# Simulate random test outcomes for demo
outcomes = [
{
"status": "PASS",
"log": """
=== Test Execution Report ===
Tests run: 4
Passed: 4
Failed: 0
Skipped: 0
test_invoice_generated_within_24_hours ... PASSED
test_invoice_email_sent_to_policyholder ... PASSED
test_invoice_retry_mechanism ... PASSED
test_system_response_time ... PASSED
All tests completed successfully!
Execution time: 2.34 seconds
"""
},
{
"status": "FAIL",
"log": """
=== Test Execution Report ===
Tests run: 4
Passed: 2
Failed: 2
Skipped: 0
test_invoice_generated_within_24_hours ... FAILED
test_invoice_email_sent_to_policyholder ... PASSED
test_invoice_retry_mechanism ... FAILED
test_system_response_time ... PASSED
FAILURES:
test_invoice_generated_within_24_hours:
AssertionError: Invoice not generated within 24 hours
Expected: Invoice status = 'Generated'
Actual: Invoice status = 'Pending'
test_invoice_retry_mechanism:
TimeoutError: Retry mechanism failed after 3 attempts
Last error: Database connection timeout after 30 seconds
Execution time: 45.67 seconds
"""
}
]
# Randomly choose outcome (70% pass, 30% fail for demo)
return random.choices(outcomes, weights=[70, 30])[0]
def generate_defect_summary(failure_log: str, bdd_scenario: str) -> str:
"""
Generate intelligent defect summary from failure logs
"""
defect_summaries = [
"""
🐛 **Defect Report: Invoice Generation Timeout**
**Severity:** High
**Priority:** P1
**Component:** Billing System - Invoice Generation
**Description:**
Invoice generation process is not completing within the expected 24-hour SLA, causing policy holders to not receive their billing information on time.
**Steps to Reproduce:**
1. Create a new insurance policy
2. Trigger invoice generation process
3. Wait for 24+ hours
4. Observe that invoice status remains 'Pending'
**Expected Behavior:**
- Invoice should be generated within 24 hours
- Invoice status should change to 'Generated'
- Email notification should be sent to policyholder
**Actual Behavior:**
- Invoice generation takes more than 24 hours
- Status remains 'Pending' indefinitely
- No email notification sent
**Root Cause Analysis:**
- Database connection timeout occurring during invoice generation
- Retry mechanism not functioning properly
- Scheduler job may be overloaded or misconfigured
**Suggested Fix:**
1. Optimize database queries for invoice generation
2. Implement proper connection pooling
3. Fix retry mechanism with exponential backoff
4. Scale invoice generation service horizontally
**Impact Assessment:**
- Customer satisfaction: High impact (delayed billing)
- Business process: Medium impact (manual intervention required)
- Financial: Low impact (no direct revenue loss)
**Test Evidence:**
- Test log shows consistent timeout after 30 seconds
- Retry mechanism fails after 3 attempts
- Database connection pool exhausted
""",
"""
🐛 **Defect Report: Credit Card CVV Validation Bypass**
**Severity:** Critical
**Priority:** P0
**Component:** Payment Processing - Card Validation
**Description:**
The credit card payment form is accepting invalid CVV formats, allowing potentially fraudulent transactions to proceed.
**Steps to Reproduce:**
1. Navigate to payment form
2. Enter valid credit card number
3. Enter 2-digit CVV (invalid format)
4. Enter valid expiry date
5. Submit payment form
6. Observe that payment is processed
**Expected Behavior:**
- CVV validation should reject 2-digit CVV codes
- Error message should display "Invalid CVV format"
- Payment should not proceed
- CVV field should be highlighted in red
**Actual Behavior:**
- 2-digit CVV is accepted as valid
- No error message displayed
- Payment processing continues
- No visual indication of error
**Root Cause Analysis:**
- CVV validation regex pattern is incorrect
- Frontend validation is bypassed
- Backend validation is not properly implemented
- Security vulnerability in payment processing
**Suggested Fix:**
1. Implement proper CVV validation (3-4 digits)
2. Add both frontend and backend validation
3. Update validation regex patterns
4. Implement proper error handling and display
**Impact Assessment:**
- Security: Critical impact (potential fraud)
- Compliance: High impact (PCI DSS violation)
- Financial: High impact (chargeback risk)
- Reputation: High impact (customer trust)
**Test Evidence:**
- Payment processed with CVV "12" (2 digits)
- No validation error thrown
- Transaction completed successfully
- Security audit trail missing proper validation
"""
]
# Return appropriate defect summary based on content
if "invoice" in failure_log.lower() or "timeout" in failure_log.lower():
return defect_summaries[0]
elif "cvv" in failure_log.lower() or "payment" in failure_log.lower():
return defect_summaries[1]
else:
return defect_summaries[0] # Default
def load_dummy_stories() -> List[Dict[str, Any]]:
"""Load dummy user stories from JSON file"""
try:
with open('dummy_user_stories.json', 'r') as f:
return json.load(f)
except FileNotFoundError:
return []
def load_test_logs() -> List[Dict[str, Any]]:
"""Load test log samples from JSON file"""
try:
with open('test_log_samples.json', 'r') as f:
return json.load(f)
except FileNotFoundError:
return []
def generate_mock_bdd(user_story: str) -> str:
"""Generate mock BDD scenario (helper function)"""
return generate_bdd_scenario(user_story)
def generate_mock_test_script(story_title: str) -> str:
"""Generate mock test script (helper function)"""
return generate_test_script("", story_title)
def generate_mock_defect_summary(failure_log: str) -> str:
"""Generate mock defect summary (helper function)"""
return generate_defect_summary(failure_log, "")
# Configuration and constants
MAX_RETRIES = 3
API_TIMEOUT = 30
SUPPORTED_DOMAINS = ["insurance", "billing", "payment", "claims"]