File size: 2,587 Bytes
9041389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
from typing import List
from unittest import mock
import pytest
import openai
from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam

import llm_handler.openai_handler as openai_handler


# Clear openai.api_key before each test
@pytest.fixture(autouse=True)
def test_env_setup():
    openai.api_key = None


# This allows us to clear the OPENAI_API_KEY before any test we want
@pytest.fixture()
def mock_settings_env_vars():
    with mock.patch.dict(os.environ, clear=True):
        yield


# Define some constants for our tests
_TEST_KEY: str = "TEST_KEY"
_TEST_MESSAGE: str = "Hello how are you?"
_TEST_MESSAGE_FOR_CHAT_COMPLETION: List[ChatCompletionMessageParam] = [
    {
        "role": "system", "content": "You are serving as a en endpoint to verify a test."
    }, {
        "role": "user", "content": "Respond with something to help us verify our code is working."
    }
]
_TEXT_EMBEDDING_SMALL_3_LENGTH: int = 1536


def test_init_without_key(mock_settings_env_vars):
    # Ensure key not set as env var and openai.api_key not set
    with pytest.raises(KeyError):
        os.environ[openai_handler.OpenAIHandler._ENV_KEY_NAME]
    assert openai.api_key == None
    # Ensure proper exception raised when instantiating handler without key as param or env var
    with pytest.raises(ValueError) as excinfo:
        openai_handler.OpenAIHandler()
    assert f'{openai_handler.OpenAIHandler._ENV_KEY_NAME} not set' in str(excinfo.value)
    assert openai.api_key == None


def test_init_with_key_as_param():
    # Ensure key is set as env var, key value is unique from _TEST_KEY, and openai.api_key not set
    assert not os.environ[openai_handler.OpenAIHandler._ENV_KEY_NAME] == _TEST_KEY
    assert openai.api_key == None
    # Ensure successful instantiation and openai.api_key properly set
    handler = openai_handler.OpenAIHandler(openai_api_key=_TEST_KEY)
    assert isinstance(handler, openai_handler.OpenAIHandler)
    assert openai.api_key == _TEST_KEY


def test_init_with_key_as_env_var(mock_settings_env_vars):
    # Ensure key not set as env var and openai.api_key not set
    with pytest.raises(KeyError):
        os.environ[openai_handler.OpenAIHandler._ENV_KEY_NAME]
    assert openai.api_key == None
    # Set key as env var
    os.environ.setdefault(openai_handler.OpenAIHandler._ENV_KEY_NAME, _TEST_KEY)
    # Ensure successful instantiation and openai.api_key properly set
    handler = openai_handler.OpenAIHandler()
    assert isinstance(handler, openai_handler.OpenAIHandler)
    assert openai.api_key == _TEST_KEY