def bulk(request): """ Perform a bulk request which can modify multiple records in on go. This end-point can: * Upsert users * Upsert groups * Add users to groups This end-point is intended to be called using the classes provided by `h.h_api.bulk_api`. """ results = FakeBulkAPI.from_byte_stream(request.body_file, executor=AutomaticReportExecutor()) # No return view is required if results is None: return Response(status=204) # An NDJSON response is required if results: # When we get an iterator we must force the first return value to be # created to be sure input validation has occurred. Otherwise we might # raise errors outside of the view results = chain([next(results)], results) return Response(app_iter=results, status=200, content_type="application/x-ndjson")
def executor(): executor = create_autospec(Executor, instance=True) fake_report_executor = AutomaticReportExecutor() executor.execute_batch.side_effect = fake_report_executor.execute_batch return executor
def test_execute_batch_returns_an_appropriate_type(self, command_type): results = AutomaticReportExecutor().execute_batch( command_type, sentinel.data_type, sentinel.config, batch=[sentinel.command]) assert results == [Any.instance_of(Report)]
def test_execute_batch_generates_fake_ids(self): results = AutomaticReportExecutor().execute_batch( sentinel.command_type, sentinel.data_type, sentinel.config, batch=[sentinel.command, sentinel.command, sentinel.command], ) assert results == Any.list.comprised_of( Any.instance_of(Report)).of_size(3) assert len({report.id for report in results}) == 3
def to_stream(cls, handle, commands): """ Check a series of commands for correctness and stream to NDJSON. :param handle: File-like object to write NDJSON to :param commands: Iterator of commands to process """ CommandProcessor( executor=AutomaticReportExecutor(), observer=SerialisingObserver(handle), # We want to know as soon as we can if we've passed in rubbish batch_size=1, ).process(commands)
def test_round_tripping(self, commands, collecting_observer): """Check that sending and decoding results in the same data.""" original_raw = [command.raw for command in commands] BulkAPI.from_byte_stream( BytesIO(BulkAPI.to_string(commands).encode("utf-8")), executor=AutomaticReportExecutor(), observer=collecting_observer, ) final_raw = [command.raw for command in collecting_observer.commands] assert original_raw == final_raw