fn serialize_bulk_operations_with_same_type_writes_to_bytes()

in elasticsearch/src/root/bulk.rs [689:760]


    fn serialize_bulk_operations_with_same_type_writes_to_bytes() -> anyhow::Result<()> {
        let mut bytes = BytesMut::new();
        let mut ops: Vec<BulkOperation<Value>> = Vec::with_capacity(4);

        ops.push(
            BulkOperation::index(json!({ "foo": "index" }))
                .id("1")
                .pipeline("pipeline")
                .routing("routing")
                .if_seq_no(1)
                .if_primary_term(2)
                .version(3)
                .version_type(VersionType::Internal)
                .into(),
        );
        ops.push(
            BulkOperation::create(json!({ "bar": "create" }))
                .id("2")
                .pipeline("pipeline")
                .routing("routing")
                .index("create_index")
                .into(),
        );
        ops.push(
            BulkOperation::update("3", json!({ "baz": "update_1" }))
                .source(false)
                .into(),
        );
        ops.push(
            BulkOperation::update("4", json!({ "baz": "update_2" }))
                .source("baz")
                .into(),
        );
        ops.push(
            BulkOperation::update("5", json!({ "baz": "update_3" }))
                .source(vec!["baz"])
                .into(),
        );
        ops.push(
            BulkOperation::update("6", json!({ "baz": "update_4" }))
                .source((vec!["baz"], vec!["bar"]))
                .into(),
        );
        ops.push(BulkOperation::delete("7").into());

        let body = NdBody::new(ops);
        body.write(&mut bytes)?;

        let mut expected = BytesMut::new();
        expected.put_slice(b"{\"index\":{\"_id\":\"1\",\"pipeline\":\"pipeline\",\"if_seq_no\":1,\"if_primary_term\":2,\"routing\":\"routing\",\"version\":3,\"version_type\":\"internal\"}}\n");
        expected.put_slice(b"{\"foo\":\"index\"}\n");
        expected.put_slice(b"{\"create\":{\"_index\":\"create_index\",\"_id\":\"2\",\"pipeline\":\"pipeline\",\"routing\":\"routing\"}}\n");
        expected.put_slice(b"{\"bar\":\"create\"}\n");
        expected.put_slice(b"{\"update\":{\"_id\":\"3\",\"_source\":false}}\n");
        expected.put_slice(b"{\"baz\":\"update_1\"}\n");
        expected.put_slice(b"{\"update\":{\"_id\":\"4\",\"_source\":\"baz\"}}\n");
        expected.put_slice(b"{\"baz\":\"update_2\"}\n");
        expected.put_slice(b"{\"update\":{\"_id\":\"5\",\"_source\":[\"baz\"]}}\n");
        expected.put_slice(b"{\"baz\":\"update_3\"}\n");
        expected.put_slice(b"{\"update\":{\"_id\":\"6\",\"_source\":{\"includes\":[\"baz\"],\"excludes\":[\"bar\"]}}}\n");
        expected.put_slice(b"{\"baz\":\"update_4\"}\n");
        expected.put_slice(b"{\"delete\":{\"_id\":\"7\"}}\n");

        assert_eq!(
            compare(&expected[..], &bytes[..]),
            Ordering::Equal,
            "expected {} but found {}",
            str::from_utf8(&expected[..]).unwrap(),
            str::from_utf8(&bytes[..]).unwrap()
        );
        Ok(())
    }