-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_environment.py
94 lines (73 loc) · 2.94 KB
/
test_environment.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# This will test the environment to ensure that the .env file is set up
# correctly and that the OpenAI and Neo4j connections are working.
import os
import unittest
from dotenv import load_dotenv
load_dotenv()
class TestEnvironment(unittest.TestCase):
skip_env_variable_tests = True
skip_openai_test = True
skip_neo4j_test = True
def test_env_file_exists(self):
env_file_exists = os.path.exists('.env')
if env_file_exists:
TestEnvironment.skip_env_variable_tests = False
self.assertTrue(env_file_exists, ".env file not found.")
def env_variable_exists(self, variable_name):
self.assertIsNotNone(
os.getenv(variable_name),
f"{variable_name} not found in .env file")
def test_openai_variables(self):
if TestEnvironment.skip_env_variable_tests:
self.skipTest("Skipping OpenAI env variable test")
self.env_variable_exists('OPENAI_API_KEY')
TestEnvironment.skip_openai_test = False
def test_neo4j_variables(self):
if TestEnvironment.skip_env_variable_tests:
self.skipTest("Skipping Neo4j env variables test")
self.env_variable_exists('NEO4J_URI')
self.env_variable_exists('NEO4J_USERNAME')
self.env_variable_exists('NEO4J_PASSWORD')
TestEnvironment.skip_neo4j_test = False
def test_openai_connection(self):
if TestEnvironment.skip_openai_test:
self.skipTest("Skipping OpenAI test")
from openai import OpenAI, AuthenticationError
llm = OpenAI()
try:
models = llm.models.list()
except AuthenticationError as e:
models = None
self.assertIsNotNone(
models,
"OpenAI connection failed. Check the OPENAI_API_KEY key in .env file.")
def test_neo4j_connection(self):
if TestEnvironment.skip_neo4j_test:
self.skipTest("Skipping Neo4j connection test")
from neo4j import GraphDatabase
driver = GraphDatabase.driver(
os.getenv('NEO4J_URI'),
auth=(os.getenv('NEO4J_USERNAME'),
os.getenv('NEO4J_PASSWORD'))
)
try:
driver.verify_connectivity()
connected = True
except Exception as e:
connected = False
driver.close()
self.assertTrue(
connected,
"Neo4j connection failed. Check the NEO4J_URI, NEO4J_USERNAME, and NEO4J_PASSWORD values in .env file."
)
def suite():
suite = unittest.TestSuite()
suite.addTest(TestEnvironment('test_env_file_exists'))
suite.addTest(TestEnvironment('test_openai_variables'))
suite.addTest(TestEnvironment('test_neo4j_variables'))
suite.addTest(TestEnvironment('test_openai_connection'))
suite.addTest(TestEnvironment('test_neo4j_connection'))
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite())