feat: complete pre-ai local completion v3
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from central.app.ai.questionnaire import build_questionnaire, group_questions, validate_answers
|
||||
|
||||
|
||||
def test_questionnaire_groups_by_scope_and_preserves_effects():
|
||||
rows = [
|
||||
{"QUESTION_ID": "Q1", "DATABASE": "ACME", "SCOPE": "COMPANY", "GROUP": "", "KEY": "entry_scope",
|
||||
"VALID_OPTIONS": ["INTRA", "INTER", "MIXED_OR_DYNAMIC"], "PRODUCTS_AFFECTED": 2, "CELLS": 3,
|
||||
"QUESTION": "Escopo?", "EXAMPLES": "P1; P2"},
|
||||
{"QUESTION_ID": "Q2", "DATABASE": "ACME", "SCOPE": "GROUP", "GROUP": "G1", "KEY": "product_role",
|
||||
"VALID_OPTIONS": ["RESALE"], "PRODUCTS_AFFECTED": 1, "CELLS": 1, "QUESTION": "Papel?"},
|
||||
]
|
||||
|
||||
result = build_questionnaire(rows)
|
||||
|
||||
assert [item["question_id"] for item in result] == ["Q1", "Q2"]
|
||||
assert result[0]["affected"] == {"products": 2, "cells": 3}
|
||||
assert "MIXED_OR_DYNAMIC" in result[0]["options"]
|
||||
assert result[1]["scope_key"] == "G1"
|
||||
|
||||
|
||||
def test_answers_are_validated_without_persisting_and_mixed_scope_is_explicit():
|
||||
questions = build_questionnaire([{
|
||||
"QUESTION_ID": "Q1", "DATABASE": "ACME", "SCOPE": "COMPANY", "GROUP": "", "KEY": "entry_scope",
|
||||
"VALID_OPTIONS": ["INTRA", "INTER", "MIXED_OR_DYNAMIC"], "QUESTION": "Escopo?",
|
||||
}])
|
||||
|
||||
answers = validate_answers(questions, {"Q1": "MIXED_OR_DYNAMIC"})
|
||||
|
||||
assert answers["entries"] == [{"scope": "COMPANY", "scope_key": "", "key": "entry_scope", "value": "MIXED_OR_DYNAMIC", "reason": None, "company_key": "ACME"}]
|
||||
assert answers["effects"][0]["behavior"] == "no_static_cfop_entrada"
|
||||
|
||||
with pytest.raises(ValueError, match="nothing was persisted"):
|
||||
validate_answers(questions, {"Q1": "OUTSIDE"})
|
||||
|
||||
|
||||
def test_store_ipi_profile_requires_explicit_choice_and_only_plans_rollback():
|
||||
questions = build_questionnaire([{
|
||||
"QUESTION_ID": "QIPI-ACME", "DATABASE": "ACME", "SCOPE": "COMPANY", "GROUP": "", "KEY": "store_ipi_profile",
|
||||
"VALID_OPTIONS": ["POPULATE_REFERENCE_RATE", "LEAVE_NOT_APPLICABLE"], "QUESTION": "IPI?",
|
||||
"PRODUCTS_AFFECTED": 4, "CELLS": 4,
|
||||
}])
|
||||
|
||||
result = validate_answers(questions, {"QIPI-ACME": "LEAVE_NOT_APPLICABLE"})
|
||||
|
||||
assert result["entries"][0]["value"] == "LEAVE_NOT_APPLICABLE"
|
||||
assert result["rollback_plan"] == [{"database": "ACME", "field": "IPI", "affected_cells": 4}]
|
||||
assert json.dumps(result)
|
||||
|
||||
|
||||
def test_equivalent_questions_are_grouped_with_aggregated_impact():
|
||||
questions = build_questionnaire([
|
||||
{"QUESTION_ID": "Q1", "DATABASE": "ACME", "SCOPE": "GROUP", "GROUP": "REFRIGERANTES", "KEY": "product_role", "VALID_OPTIONS": ["RESALE"], "CELLS": 10, "PRODUCTS_AFFECTED": 3, "QUESTION": "Qual o papel?"},
|
||||
{"QUESTION_ID": "Q2", "DATABASE": "ACME", "SCOPE": "GROUP", "GROUP": "REFRIGERANTES", "KEY": "product_role", "VALID_OPTIONS": ["RESALE"], "CELLS": 20, "PRODUCTS_AFFECTED": 5, "QUESTION": "Qual o papel?"},
|
||||
])
|
||||
groups = group_questions(questions)
|
||||
assert len(groups) == 1 and groups[0]["question_ids"] == ["Q1", "Q2"]
|
||||
assert groups[0]["affected"] == {"products": 8, "cells": 30}
|
||||
Reference in New Issue
Block a user