> For the complete documentation index, see [llms.txt](https://pmse.gitbook.io/pmse-dhdk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pmse.gitbook.io/pmse-dhdk/4.-verification-and-validation/4.4-sample-test-case.md).

# 4.4 Sample Test Case

<table><thead><tr><th width="148">Test Case ID</th><th width="273">Title</th><th>Description</th></tr></thead><tbody><tr><td>TC001</td><td>Verify the chatbot's ability to respond accurately to a user query about GNA user manual.</td><td>This test ensures that the chatbot can accurately retrieve and present information about a specific resource available on the GNA platform when prompted by the user.</td></tr></tbody></table>

<mark style="color:blue;">**Preconditions**</mark>

* The chatbot is integrated and accessible via the GNA platform.
* The knowledge base (KB) of the system contains up-to-date information about all documentation resources of the GNA User Manual.
* The user is logged into the GNA platform.

### **Test Scenario**&#x20;

User query in input:

> <mark style="background-color:yellow;">"What is the MOPR layer in the GNA project?"</mark>

### **Test Steps**

{% stepper %}
{% step %}
Open the GNA platform and navigate to the chatbot section.
{% endstep %}

{% step %}
Enter the test query into the chatbot interface: *"What is the MOPR layer in the GNA project?"*
{% endstep %}

{% step %}
Wait for the chatbot to process the query and respond.
{% endstep %}
{% endstepper %}

<mark style="color:blue;">**Expected Outcome**</mark>

The chatbot should provide a response similar to:

> <mark style="background-color:yellow;">Il livello MOPR (Modulo di Progetto) è il livello fondamentale nel framework di raccolta dei dati del progetto GNA. Deve essere compilato per primo delineando l'intera area di esame per la raccolta dei dati. Questo livello funge da contenitore per tutti gli elementi successivi. La necessità di collegare l'intero lavoro a un singolo evento deriva dal fatto che questi compiti spesso coinvolgono incarichi esterni o interni che coinvolgono professionisti e/o funzionari, rendendo essenziale che siano facilmente riportabili.</mark>
>
> *<mark style="background-color:yellow;">Vedi ulteriori informazioni qui:</mark>* [<mark style="background-color:yellow;">La struttura del progetto</mark>](https://gna.cultura.gov.it/wiki/index.php/La_struttura_del_progetto) <mark style="background-color:yellow;">{link to specific section}.</mark>

<mark style="color:blue;">**Postconditions**</mark>

* The user receives a clear and accurate explanation of the MOPR layer.
* The chatbot logs the interaction for future reference and continuous improvement.

***

## Sample Test Case Implementation

The following sample Python implementation outlines functions to load and preprocess the dataset, split the text into manageable chunks, and store these chunks in a FAISS-based vector database for retrieval. The chatbot uses `open-mistral-nemo` Mistral LLM to generate responses, configured through a conversational retrieval chain with memory for context retention. A retry mechanism handles API invocation errors, while a Streamlit-based user interface allows users to interact with the chatbot, displaying both user inputs and AI-generated responses in a styled format.&#x20;

The application is built with extensibility in mind, using environment variables `dotenv`for Mistral API key and dynamic configurations for chunk processing and model usage.

The function `patch` from the `unittest.mock` module will be used to temporarily replace certain functions or objects with mock versions during testing. This allows the tests to **isolate dependencies** and **control their behavior**, ensuring that external factors (such as file existence, API calls, or delays) do not interfere with the tests.

```python
import unittest
from unittest.mock import patch, MagicMock
import pandas as pd
import dotenv
import os

# Mock dataset for testing
mock_data = {
    "body": ["Sample content for testing."],
    "title": ["Sample Title"],
    "description": ["Sample Description"],
    "url": ["http://example.com"],
}

class TestGNAChatbot(unittest.TestCase):
    @patch("os.getenv")
    def test_load_dataset_success(self, mock_getenv):
        """Test successful loading of the dataset."""
        # the following is just a mock Mistral API key
        mock_getenv.return_value = "mock_key_ZAWnA7Ijn4rS13qZ3fyh62klVo" 
        
        # Mock CSV reading
        with patch("pandas.read_csv", return_value=pd.DataFrame(mock_data)) as mock_read_csv:
            dataset = load_dataset("gna_kg_dataset.csv")
            self.assertIsNotNone(dataset, "Dataset should not be None")
            self.assertEqual(len(dataset), 1, "Dataset should contain one record")
            self.assertIn("body", dataset.columns, "Dataset should contain 'body' column")
            mock_read_csv.assert_called_once()

    def test_create_chunks_success(self):
        """Test chunk creation with valid dataset."""
        df = pd.DataFrame(mock_data)
        chunks = create_chunks(df, chunk_size=100, chunk_overlap=10)
        self.assertIsInstance(chunks, list, "Chunks should be a list")
        self.assertGreater(len(chunks), 0, "Chunks should not be empty")

    @patch("os.path.exists")
    @patch("os.makedirs")
    def test_create_or_get_vector_store(self, mock_makedirs, mock_exists):
        """Test vector store creation or retrieval."""
        mock_exists.side_effect = [False, True]  # Simulate vector store folder and file absence
        chunks = [{"page_content": "Sample content for vector store"}]

        with patch("langchain_community.vectorstores.FAISS.from_texts") as mock_faiss_create:
            mock_faiss_create.return_value = MagicMock()
            vector_store = create_or_get_vector_store(chunks, "fake_api_key")
            self.assertIsNotNone(vector_store, "Vector store should not be None")
            mock_faiss_create.assert_called_once()

    @patch("langchain_mistralai.ChatMistralAI")
    def test_create_mistral_llm(self, mock_chat_mistral):
        """Test Mistral LLM initialization."""
        mock_llm = MagicMock()
        mock_chat_mistral.return_value = mock_llm
        llm = create_mistral_llm(api_key="fake_key", model_name="test_model")
        self.assertIsNotNone(llm, "LLM should not be None")
        mock_chat_mistral.assert_called_once_with(model="test_model", temperature=0, max_retries=2)

    @patch("time.sleep", return_value=None)  # Avoid delays during testing
    @patch("langchain.chains.ConversationalRetrievalChain.invoke")
    def test_invoke_with_retry(self, mock_invoke, mock_sleep):
        """Test retry mechanism for invoke_with_retry."""
        mock_invoke.side_effect = [
            Exception("Simulated error"),
            {"question": "test", "response": "Success!"},
        ]
        conversation_chain = MagicMock()
        result = invoke_with_retry(conversation_chain, test_message="Test Message", retries=2, delay=1)
        self.assertEqual(result["response"], "Success!", "Should return the successful response")
        self.assertEqual(mock_invoke.call_count, 2, "Should attempt the API call twice")

if __name__ == "__main__":
    unittest.main()

```

#### <mark style="color:blue;">Key Features of the Test Case</mark>

1. **Dataset Testing**:
   * Validates successful loading of the dataset.
   * Ensures the dataset contains the required columns.
2. **Chunk Creation Testing**:
   * Confirms chunks are created successfully and are in the expected format.
3. **Vector Store Testing**:
   * Mocks FAISS vector store creation and retrieval.
   * Simulates conditions for both creating a new store and loading an existing one.
4. **Mistral LLM Initialization**:
   * Mocks and validates the initialization of the Mistral language model.
5. **Retry Mechanism**:
   * Tests the `invoke_with_retry` function with simulated API errors and success.

#### Notes:

* Mocking ensures that external dependencies like file I/O, API calls, and third-party libraries are not executed during testing.
* The suggestion is to use a separate environment or mock dataset for testing purposes to avoid affecting production data.
* Paths and configurations need to be aligned with the specific environment and the context of the workspace.

***

This test case ensures that the chatbot can effectively assist users in understanding key components of the GNA project's structure, enhancing user engagement and knowledge dissemination.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://pmse.gitbook.io/pmse-dhdk/4.-verification-and-validation/4.4-sample-test-case.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
