11import contextlib
2- import json
32import unittest
43
54from pysolr import SolrCoreAdmin , SolrError
@@ -46,31 +45,27 @@ def test_status(self):
4645 """Test the status endpoint returns details for all cores and specific cores."""
4746
4847 # Status of all cores
49- raw_all = self .solr_admin .status ()
50- all_data = json .loads (raw_all )
48+ result = self .solr_admin .status ()
5149
52- self .assertIn ("core0" , all_data ["status" ])
50+ self .assertIn ("core0" , result ["status" ])
5351
5452 # Status of a specific core
55- raw_single = self .solr_admin .status (core = "core0" )
56- single_data = json .loads (raw_single )
53+ result = self .solr_admin .status (core = "core0" )
5754
58- self .assertEqual (single_data ["status" ]["core0" ]["name" ], "core0" )
55+ self .assertEqual (result ["status" ]["core0" ]["name" ], "core0" )
5956
6057 def test_create (self ):
6158 """Test creating a core returns a successful response."""
62- raw_response = self .solr_admin .create ("demo_core1" )
63- data = json .loads (raw_response )
59+ result = self .solr_admin .create ("demo_core1" )
6460
65- self .assertEqual (data ["responseHeader" ]["status" ], 0 )
66- self .assertEqual (data ["core" ], "demo_core1" )
61+ self .assertEqual (result ["responseHeader" ]["status" ], 0 )
62+ self .assertEqual (result ["core" ], "demo_core1" )
6763
6864 def test_reload (self ):
6965 """Test reloading a core returns a successful response."""
70- raw_response = self .solr_admin .reload ("core0" )
71- data = json .loads (raw_response )
66+ result = self .solr_admin .reload ("core0" )
7267
73- self .assertEqual (data ["responseHeader" ]["status" ], 0 )
68+ self .assertEqual (result ["responseHeader" ]["status" ], 0 )
7469
7570 def test_rename (self ):
7671 """Test renaming a core succeeds and the new name appears in the status."""
@@ -79,16 +74,14 @@ def test_rename(self):
7974 self .solr_admin .create ("demo_core1" )
8075
8176 # Rename the core to a new name
82- raw_response = self .solr_admin .rename ("demo_core1" , "demo_core2" )
83- data = json .loads (raw_response )
77+ result = self .solr_admin .rename ("demo_core1" , "demo_core2" )
8478
85- self .assertEqual (data ["responseHeader" ]["status" ], 0 )
79+ self .assertEqual (result ["responseHeader" ]["status" ], 0 )
8680
8781 # Verify that the renamed core appears in the status response
88- raw_response2 = self .solr_admin .status (core = "demo_core2" )
89- data2 = json .loads (raw_response2 )
82+ result_2 = self .solr_admin .status (core = "demo_core2" )
9083
91- self .assertEqual (data2 ["status" ]["demo_core2" ]["name" ], "demo_core2" )
84+ self .assertEqual (result_2 ["status" ]["demo_core2" ]["name" ], "demo_core2" )
9285
9386 def test_swap (self ):
9487 """
@@ -107,10 +100,9 @@ def test_swap(self):
107100 self .solr_admin .create ("demo_core2" )
108101
109102 # Perform swap
110- raw_swap = self .solr_admin .swap ("demo_core1" , "demo_core2" )
111- swap_data = json .loads (raw_swap )
103+ result = self .solr_admin .swap ("demo_core1" , "demo_core2" )
112104
113- self .assertEqual (swap_data ["responseHeader" ]["status" ], 0 )
105+ self .assertEqual (result ["responseHeader" ]["status" ], 0 )
114106
115107 def test_unload (self ):
116108 """
@@ -121,21 +113,19 @@ def test_unload(self):
121113 """
122114 self .solr_admin .create ("demo_core1" )
123115
124- raw_response = self .solr_admin .unload ("demo_core1" )
125- data = json .loads (raw_response )
116+ result = self .solr_admin .unload ("demo_core1" )
126117
127- self .assertEqual (data ["responseHeader" ]["status" ], 0 )
118+ self .assertEqual (result ["responseHeader" ]["status" ], 0 )
128119
129120 def test_load (self ):
130121 self .assertRaises (NotImplementedError , self .solr_admin .load , "wheatley" )
131122
132123 def test_status__nonexistent_core_returns_empty_response (self ):
133124 """Test that requesting status for a missing core returns an empty response."""
134- raw_response = self .solr_admin .status (core = "not_exists" )
135- data = json .loads (raw_response )
125+ result = self .solr_admin .status (core = "not_exists" )
136126
137- self .assertNotIn ("name" , data ["status" ]["not_exists" ])
138- self .assertNotIn ("instanceDir" , data ["status" ]["not_exists" ])
127+ self .assertNotIn ("name" , result ["status" ]["not_exists" ])
128+ self .assertNotIn ("instanceDir" , result ["status" ]["not_exists" ])
139129
140130 def test_create__existing_core_raises_error (self ):
141131 """Test creating a core that already exists returns a 500 error."""
@@ -144,23 +134,21 @@ def test_create__existing_core_raises_error(self):
144134 self .solr_admin .create ("demo_core1" )
145135
146136 # Creating the same core again should return a 500 error response
147- raw_response = self .solr_admin .create ("demo_core1" )
148- data = json .loads (raw_response )
137+ result = self .solr_admin .create ("demo_core1" )
149138
150- self .assertEqual (data ["responseHeader" ]["status" ], 500 )
139+ self .assertEqual (result ["responseHeader" ]["status" ], 500 )
151140 self .assertEqual (
152- data ["error" ]["msg" ], "Core with name 'demo_core1' already exists."
141+ result ["error" ]["msg" ], "Core with name 'demo_core1' already exists."
153142 )
154143
155144 def test_reload__nonexistent_core_raises_error (self ):
156145 """Test that reloading a non-existent core returns a 400 error."""
157- raw_response = self .solr_admin .reload ("not_exists" )
158- data = json .loads (raw_response )
146+ result = self .solr_admin .reload ("not_exists" )
159147
160148 # Solr returns a 400 error for missing cores
161- self .assertEqual (data ["responseHeader" ]["status" ], 400 )
162- self .assertIn ("No such core" , data ["error" ]["msg" ])
163- self .assertIn ("not_exists" , data ["error" ]["msg" ])
149+ self .assertEqual (result ["responseHeader" ]["status" ], 400 )
150+ self .assertIn ("No such core" , result ["error" ]["msg" ])
151+ self .assertIn ("not_exists" , result ["error" ]["msg" ])
164152
165153 def test_rename__nonexistent_core_no_effect (self ):
166154 """
@@ -175,12 +163,11 @@ def test_rename__nonexistent_core_no_effect(self):
175163 self .solr_admin .rename ("not_exists" , "demo_core99" )
176164
177165 # Check the status of the target core to verify the rename had no effect
178- raw_response = self .solr_admin .status (core = "demo_core99" )
179- data = json .loads (raw_response )
166+ result = self .solr_admin .status (core = "demo_core99" )
180167
181168 # The target core should not exist because the rename operation was ignored
182- self .assertNotIn ("name" , data ["status" ]["demo_core99" ])
183- self .assertNotIn ("instanceDir" , data ["status" ]["demo_core99" ])
169+ self .assertNotIn ("name" , result ["status" ]["demo_core99" ])
170+ self .assertNotIn ("instanceDir" , result ["status" ]["demo_core99" ])
184171
185172 def test_swap__missing_source_core_returns_error (self ):
186173 """Test swapping when the source core is missing returns a 400 error."""
@@ -189,13 +176,12 @@ def test_swap__missing_source_core_returns_error(self):
189176 self .solr_admin .create ("demo_core2" )
190177
191178 # Attempt to swap a missing source core with an existing target core
192- raw_response = self .solr_admin .swap ("not_exists" , "demo_core2" )
193- data = json .loads (raw_response )
179+ result = self .solr_admin .swap ("not_exists" , "demo_core2" )
194180
195181 # Solr returns a 400 error when the source core does not exist
196- self .assertEqual (data ["responseHeader" ]["status" ], 400 )
197- self .assertIn ("No such core" , data ["error" ]["msg" ])
198- self .assertIn ("not_exists" , data ["error" ]["msg" ])
182+ self .assertEqual (result ["responseHeader" ]["status" ], 400 )
183+ self .assertIn ("No such core" , result ["error" ]["msg" ])
184+ self .assertIn ("not_exists" , result ["error" ]["msg" ])
199185
200186 def test_swap__missing_target_core_returns_error (self ):
201187 """Test swapping when the target core is missing returns a 400 error."""
@@ -204,22 +190,20 @@ def test_swap__missing_target_core_returns_error(self):
204190 self .solr_admin .create ("demo_core1" )
205191
206192 # Attempt to swap with a missing target core
207- raw_response = self .solr_admin .swap ("demo_core1" , "not_exists" )
208- data = json .loads (raw_response )
193+ result = self .solr_admin .swap ("demo_core1" , "not_exists" )
209194
210195 # Solr returns a 400 error when the target core does not exist
211- self .assertEqual (data ["responseHeader" ]["status" ], 400 )
212- self .assertIn ("No such core" , data ["error" ]["msg" ])
213- self .assertIn ("not_exists" , data ["error" ]["msg" ])
196+ self .assertEqual (result ["responseHeader" ]["status" ], 400 )
197+ self .assertIn ("No such core" , result ["error" ]["msg" ])
198+ self .assertIn ("not_exists" , result ["error" ]["msg" ])
214199
215200 def test_unload__nonexistent_core_returns_error (self ):
216201 """Test unloading a non-existent core returns a 400 error response."""
217202
218203 # Attempt to unload a core that does not exist
219- raw_response = self .solr_admin .unload ("not_exists" )
220- data = json .loads (raw_response )
204+ result = self .solr_admin .unload ("not_exists" )
221205
222206 # Solr returns a 400 error for unloading a missing core
223- self .assertEqual (data ["responseHeader" ]["status" ], 400 )
224- self .assertIn ("Cannot unload non-existent core" , data ["error" ]["msg" ])
225- self .assertIn ("not_exists" , data ["error" ]["msg" ])
207+ self .assertEqual (result ["responseHeader" ]["status" ], 400 )
208+ self .assertIn ("Cannot unload non-existent core" , result ["error" ]["msg" ])
209+ self .assertIn ("not_exists" , result ["error" ]["msg" ])
0 commit comments