Skip to content

Commit df67aad

Browse files
committed
Reformatted tests accordingly
1 parent 14de74a commit df67aad

File tree

4 files changed

+81
-36
lines changed

4 files changed

+81
-36
lines changed

tests/asciidoc/test__main__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ def setUp(self):
3333
self.asciidoctor_cmd = "/usr/bin/asciidoctor"
3434
self.input_file = Path("input.adoc")
3535
self.output_file = Path("output.adoc")
36-
self.text_converter_file = RUBY_ASCIIDOC_DIR.joinpath("asciidoc_text_converter.rb")
37-
self.structure_dumper_file = RUBY_ASCIIDOC_DIR.joinpath("asciidoc_structure_dumper.rb")
36+
self.text_converter_file = RUBY_ASCIIDOC_DIR.joinpath(
37+
"asciidoc_text_converter.rb"
38+
)
39+
self.structure_dumper_file = RUBY_ASCIIDOC_DIR.joinpath(
40+
"asciidoc_structure_dumper.rb"
41+
)
3842

3943
def get_mock_parsed_args(self) -> Mock:
4044
mock_args = Mock()
@@ -74,7 +78,9 @@ def test_main_convert(self, mock_which, mock_run):
7478
@patch("lightspeed_rag_content.asciidoc.asciidoctor_converter.shutil.which")
7579
def test_main_convert_incorrect_cmd_error(self, mock_which, mock_run):
7680
mock_which.return_value = self.asciidoctor_cmd
77-
mock_run.side_effect = subprocess.CalledProcessError(cmd=self.asciidoctor_cmd, returncode=1)
81+
mock_run.side_effect = subprocess.CalledProcessError(
82+
cmd=self.asciidoctor_cmd, returncode=1
83+
)
7884
mock_args = self.get_mock_parsed_args()
7985

8086
with self.assertRaises(SystemExit) as e:
@@ -109,7 +115,9 @@ def test_main_get_structure(self, mock_which, mock_run):
109115

110116
@patch("lightspeed_rag_content.asciidoc.asciidoctor_converter.subprocess.run")
111117
def test_main_incorrect_asciidoctor_cmd(self, mock_run):
112-
mock_run.side_effect = subprocess.CalledProcessError(cmd=self.asciidoctor_cmd, returncode=1)
118+
mock_run.side_effect = subprocess.CalledProcessError(
119+
cmd=self.asciidoctor_cmd, returncode=1
120+
)
113121
mock_args = Mock()
114122
mock_args.input_file = self.input_file
115123

tests/asciidoc/test_asciidoc_conveter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ def setUp(self):
3636
[[]
3737
"""
3838

39-
self.text_converter_path = RUBY_ASCIIDOC_DIR.joinpath("asciidoc_text_converter.rb")
39+
self.text_converter_path = RUBY_ASCIIDOC_DIR.joinpath(
40+
"asciidoc_text_converter.rb"
41+
)
4042
self.input_file = Path("input.adoc")
4143
self.output_file = Path("output.txt")
4244
self.attributes_file = Path("attributes.yaml")
@@ -47,7 +49,9 @@ def setUp(self):
4749
def test_convert(self, mock_which, mock_run):
4850
mock_which.return_value = self.asciidoctor_cmd
4951
with patch("builtins.open", mock_open(read_data=self.valid_attributes_file)):
50-
adoc_text_converter = AsciidoctorConverter(attributes_file=self.attributes_file)
52+
adoc_text_converter = AsciidoctorConverter(
53+
attributes_file=self.attributes_file
54+
)
5155
adoc_text_converter.convert(self.input_file, self.output_file)
5256

5357
mock_run.assert_called_with(
@@ -123,7 +127,9 @@ def test_convert_new_output_file(self, mock_which, mock_run):
123127

124128
def test__get_converter_file(self):
125129
converter_file = AsciidoctorConverter._get_converter_file("text")
126-
self.assertEqual(converter_file, RUBY_ASCIIDOC_DIR.joinpath("asciidoc_text_converter.rb"))
130+
self.assertEqual(
131+
converter_file, RUBY_ASCIIDOC_DIR.joinpath("asciidoc_text_converter.rb")
132+
)
127133

128134
def test__get_converter_file_asciidoctor_built_in_format(self):
129135
converter_file = AsciidoctorConverter._get_converter_file("html5")
@@ -140,7 +146,9 @@ def test__get_asciidoctor_path_missing(self, mock_which):
140146
AsciidoctorConverter()
141147

142148
def test__get_attribute_list_valid_yaml(self):
143-
with patch("builtins.open", mock_open(read_data=self.valid_attributes_file)) as m:
149+
with patch(
150+
"builtins.open", mock_open(read_data=self.valid_attributes_file)
151+
) as m:
144152
AsciidoctorConverter._get_attribute_list("valid.yaml")
145153
m.assert_called_once()
146154

tests/test_document_processor.py

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,19 @@ def setUp(self):
4242
# Mock the _get_settings() method
4343
self.settings_obj = mock.MagicMock()
4444
self.patcher = mock.patch.object(
45-
document_processor.DocumentProcessor, "_get_settings")
45+
document_processor.DocumentProcessor, "_get_settings"
46+
)
4647
self._settings = self.patcher.start()
4748
self._settings.return_value = self.settings_obj
4849
self.addCleanup(self.patcher.stop)
4950

5051
self.doc_processor = document_processor.DocumentProcessor(
51-
self.chunk_size, self.chunk_overlap, self.model_name,
52-
self.embeddings_model_dir, self.num_workers)
52+
self.chunk_size,
53+
self.chunk_overlap,
54+
self.model_name,
55+
self.embeddings_model_dir,
56+
self.num_workers,
57+
)
5358

5459
def test__got_whitespace_false(self):
5560
text = "NoWhitespace"
@@ -72,7 +77,8 @@ def test__filter_out_invalid_nodes(self):
7277
fake_node_1.text = "NoWhitespace"
7378

7479
result = self.doc_processor._filter_out_invalid_nodes(
75-
[fake_node_0, fake_node_1])
80+
[fake_node_0, fake_node_1]
81+
)
7682

7783
# Only nodes with whitespaces should be returned
7884
self.assertEqual([fake_node_0], result)
@@ -85,7 +91,8 @@ def test__save_index(self, mock_vector_index):
8591

8692
fake_index.set_index_id.assert_called_once_with("fake-index")
8793
fake_index.storage_context.persist.assert_called_once_with(
88-
persist_dir="/fake/path")
94+
persist_dir="/fake/path"
95+
)
8996

9097
@mock.patch.object(document_processor.json, "dumps")
9198
@mock.patch("builtins.open", new_callable=mock.mock_open)
@@ -102,7 +109,7 @@ def test__save_metadata(self, mock_file, mock_dumps):
102109
"embedding-dimension": mock.ANY,
103110
"chunk": self.chunk_size,
104111
"overlap": self.chunk_overlap,
105-
"total-embedded-files": 0
112+
"total-embedded-files": 0,
106113
}
107114
mock_dumps.assert_called_once_with(expected_dict)
108115

@@ -114,7 +121,8 @@ def test_process(self, mock_dir_reader):
114121
fake_good_nodes = [mock.Mock(), mock.Mock()]
115122

116123
with mock.patch.object(
117-
self.doc_processor, '_filter_out_invalid_nodes') as mock_filter:
124+
self.doc_processor, "_filter_out_invalid_nodes"
125+
) as mock_filter:
118126
mock_filter.return_value = fake_good_nodes
119127
self.doc_processor.process("/fake/path/docs", fake_metadata)
120128

@@ -132,27 +140,45 @@ def test_save(self):
132140
mock_index.assert_called_once_with("fake-index", "/fake/output_dir")
133141
mock_md.assert_called_once_with("fake-index", "/fake/output_dir")
134142

135-
@mock.patch.dict(os.environ, {
136-
"POSTGRES_USER": "postgres",
137-
"POSTGRES_PASSWORD": "somesecret",
138-
"POSTGRES_HOST": "localhost",
139-
"POSTGRES_PORT": "15432",
140-
"POSTGRES_DATABASE": "postgres",
141-
})
142-
@mock.patch("lightspeed_rag_content.document_processor.HuggingFaceEmbedding", new=MockEmbedding)
143+
@mock.patch.dict(
144+
os.environ,
145+
{
146+
"POSTGRES_USER": "postgres",
147+
"POSTGRES_PASSWORD": "somesecret",
148+
"POSTGRES_HOST": "localhost",
149+
"POSTGRES_PORT": "15432",
150+
"POSTGRES_DATABASE": "postgres",
151+
},
152+
)
153+
@mock.patch(
154+
"lightspeed_rag_content.document_processor.HuggingFaceEmbedding",
155+
new=MockEmbedding,
156+
)
143157
def test_pgvector(self):
144158
self.patcher.stop() # Remove the mock on the _get_settings() method
145159
self.doc_processor = document_processor.DocumentProcessor(
146-
self.chunk_size, self.chunk_overlap, self.model_name,
147-
self.embeddings_model_dir, self.num_workers,
148-
"postgres")
160+
self.chunk_size,
161+
self.chunk_overlap,
162+
self.model_name,
163+
self.embeddings_model_dir,
164+
self.num_workers,
165+
"postgres",
166+
)
149167
self.assertIsNotNone(self.doc_processor)
150168

151-
@mock.patch("lightspeed_rag_content.document_processor.HuggingFaceEmbedding", new=MockEmbedding)
169+
@mock.patch(
170+
"lightspeed_rag_content.document_processor.HuggingFaceEmbedding",
171+
new=MockEmbedding,
172+
)
152173
def test_invalid_vector_store_type(self):
153174
self.patcher.stop() # Remove the mock on the _get_settings() method
154-
self.assertRaises(RuntimeError,
175+
self.assertRaises(
176+
RuntimeError,
155177
document_processor.DocumentProcessor,
156-
self.chunk_size, self.chunk_overlap, self.model_name,
157-
self.embeddings_model_dir, self.num_workers,
158-
"nonexisting")
178+
self.chunk_size,
179+
self.chunk_overlap,
180+
self.model_name,
181+
self.embeddings_model_dir,
182+
self.num_workers,
183+
"nonexisting",
184+
)

tests/test_metadata_processor.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def test_ping_url_exception(self, mock_get):
5757

5858
self.assertFalse(result)
5959

60-
@mock.patch("builtins.open", new_callable=mock.mock_open,
61-
read_data="# Road-Core title")
60+
@mock.patch(
61+
"builtins.open", new_callable=mock.mock_open, read_data="# Road-Core title"
62+
)
6263
def test_get_file_title(self, mock_file):
6364
result = self.md_processor.get_file_title(self.file_path)
6465

@@ -89,13 +90,15 @@ def test_populate(self, mock_url_func, mock_get_title, mock_ping_url):
8990
@mock.patch.object(metadata_processor.MetadataProcessor, "get_file_title")
9091
@mock.patch.object(metadata_processor.MetadataProcessor, "url_function")
9192
def test_populate_url_unreachable(
92-
self, mock_url_func, mock_get_title, mock_ping_url):
93+
self, mock_url_func, mock_get_title, mock_ping_url
94+
):
9395
mock_url_func.return_value = self.url
9496
mock_get_title.return_value = self.title
9597
mock_ping_url.return_value = False
9698

97-
with self.assertLogs("lightspeed_rag_content.metadata_processor",
98-
level="WARNING") as log:
99+
with self.assertLogs(
100+
"lightspeed_rag_content.metadata_processor", level="WARNING"
101+
) as log:
99102
result = self.md_processor.populate(self.file_path)
100103

101104
expected_result = {"docs_url": self.url, "title": self.title}

0 commit comments

Comments
 (0)