Exemple #1
0
    def test_command_parsing_ok(self, executor, ndjson_bytes):
        """Sanity test that hits most elements of parsing."""

        BulkAPI.from_byte_stream(ndjson_bytes,
                                 executor=executor,
                                 observer=Observer())

        executor.configure.assert_called_with(
            config=Any.instance_of(Configuration))

        executor.execute_batch.assert_has_calls([
            call(
                command_type=CommandType.UPSERT,
                data_type=DataType.USER,
                batch=[Any.instance_of(DataCommand)],
                default_config={},
            ),
            call(
                command_type=CommandType.UPSERT,
                data_type=DataType.GROUP,
                batch=[Any.instance_of(DataCommand)],
                default_config={},
            ),
            call(
                command_type=CommandType.CREATE,
                data_type=DataType.GROUP_MEMBERSHIP,
                batch=[Any.instance_of(DataCommand)],
                default_config={"on_duplicate": "continue"},
            ),
        ])
Exemple #2
0
    def test_from_byte_stream(self, nd_json_byte_stream, executor,
                              CommandProcessor):
        BulkAPI.from_byte_stream(nd_json_byte_stream, executor)

        CommandProcessor.assert_called_once_with(
            executor=executor, observer=Any.instance_of(Observer))

        self._assert_process_called_with_generator_of_commands(
            CommandProcessor)
Exemple #3
0
    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
Exemple #4
0
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 = BulkAPI.from_byte_stream(request.body_file,
                                       executor=BulkExecutor(request.db))

    if results is None:
        return Response(status=204)

    # 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 when called

    try:
        results = chain([next(results)], results)
    except StopIteration:
        results = []

    # An NDJSON response is required
    return Response(
        app_iter=((json.dumps(result) + "\n").encode("utf-8")
                  for result in results),
        status=200,
        content_type="application/x-ndjson",
    )