-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_environment.py
More file actions
68 lines (53 loc) · 2.07 KB
/
test_environment.py
File metadata and controls
68 lines (53 loc) · 2.07 KB
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
# This will test the environment to ensure that the .env file is set up
# correctly and that the Neo4j connection is working.
import os
import unittest
from dotenv import load_dotenv
load_dotenv()
class TestEnvironment(unittest.TestCase):
skip_env_variable_tests = 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_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_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_neo4j_variables'))
suite.addTest(TestEnvironment('test_neo4j_connection'))
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite())