fn serialize_bulk_operations_with_different_types_writes_to_bytes()

in elasticsearch/src/root/bulk.rs [763:811]


    fn serialize_bulk_operations_with_different_types_writes_to_bytes() -> anyhow::Result<()> {
        #[derive(Serialize)]
        struct IndexDoc<'a> {
            foo: &'a str,
        }
        #[derive(Serialize)]
        struct CreateDoc<'a> {
            bar: &'a str,
        }
        #[derive(Serialize)]
        struct UpdateDoc<'a> {
            baz: &'a str,
        }

        let mut bytes = BytesMut::new();
        let mut ops = BulkOperations::new();

        ops.push(
            BulkOperation::index(IndexDoc { foo: "index" })
                .id("1")
                .pipeline("pipeline")
                .index("index_doc")
                .routing("routing"),
        )?;
        ops.push(BulkOperation::create(CreateDoc { bar: "create" }).id("2"))?;
        ops.push(BulkOperation::update("3", UpdateDoc { baz: "update" }))?;
        ops.push(BulkOperation::<()>::delete("4"))?;

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

        let mut expected = BytesMut::new();
        expected.put_slice(b"{\"index\":{\"_index\":\"index_doc\",\"_id\":\"1\",\"pipeline\":\"pipeline\",\"routing\":\"routing\"}}\n");
        expected.put_slice(b"{\"foo\":\"index\"}\n");
        expected.put_slice(b"{\"create\":{\"_id\":\"2\"}}\n");
        expected.put_slice(b"{\"bar\":\"create\"}\n");
        expected.put_slice(b"{\"update\":{\"_id\":\"3\"}}\n");
        expected.put_slice(b"{\"baz\":\"update\"}\n");
        expected.put_slice(b"{\"delete\":{\"_id\":\"4\"}}\n");

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