Add result_set binary test

This commit is contained in:
Daniel Akhterov 2019-08-09 22:33:52 -07:00
parent 6f8b2edead
commit 14c1a35789
3 changed files with 32 additions and 15 deletions

View File

@ -19,7 +19,12 @@ impl Decode for ColumnPacket {
let length = decoder.decode_length()?;
let seq_no = decoder.decode_int_u8();
let columns = decoder.decode_int_lenenc_unsigned();
let mut columns = decoder.decode_int_lenenc_unsigned();
// Treat 0 columns as None; this should never be a thing though
if columns.is_some() && columns.unwrap() == 0 {
columns = None;
}
Ok(ColumnPacket {
length,

View File

@ -3,7 +3,7 @@ use bytes::Bytes;
use failure::Error;
use std::convert::TryFrom;
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
// ColumnDefPacket doesn't have a packet header because
// it's nested inside a result set packet
pub struct ColumnDefPacket {

View File

@ -10,7 +10,7 @@ use crate::mariadb::{
#[derive(Debug, Default)]
pub struct ResultSet {
pub column_packet: ColumnPacket,
pub columns: Vec<ColumnDefPacket>,
pub column_defs: Option<Vec<ColumnDefPacket>>,
pub rows: Vec<ResultRow>,
}
@ -21,17 +21,23 @@ impl ResultSet {
) -> Result<Self, Error> {
let column_packet = ColumnPacket::decode(&mut ctx)?;
let columns = if let Some(columns) = column_packet.columns {
ctx.columns = column_packet.columns;
let column_defs = if let Some(columns) = column_packet.columns {
let mut column_defs = Vec::new();
for _ in 0..columns {
ctx.next_packet().await?;
column_defs.push(ColumnDefPacket::decode(&mut ctx)?);
}
column_defs
Some(column_defs)
} else {
Vec::new()
None
};
if column_defs.is_some() {
ctx.column_defs = column_defs.clone();
}
ctx.next_packet().await?;
let eof_packet = if !ctx
@ -47,8 +53,6 @@ impl ResultSet {
None
};
ctx.columns = column_packet.columns.clone();
let mut rows = Vec::new();
loop {
@ -101,7 +105,7 @@ impl ResultSet {
Ok(ResultSet {
column_packet,
columns,
column_defs,
rows,
})
}
@ -124,7 +128,7 @@ mod test {
async fn it_decodes_result_set_text_packet() -> Result<(), Error> {
// TODO: Use byte string as input for test; this is a valid return from a mariadb.
#[rustfmt::skip]
let buf = __bytes_builder!(
let buf: Bytes = __bytes_builder!(
// ------------------- //
// Column Count packet //
// ------------------- //
@ -341,9 +345,10 @@ mod test {
}
#[runtime::test]
fn it_decodes_result_set_binary_packet() -> Result<(), Error> {
async fn it_decodes_result_set_binary_packet() -> Result<(), Error> {
// TODO: Use byte string as input for test; this is a valid return from a mariadb.
#[rustfmt::skip]
let buf = __bytes_builder!(
let buf: Bytes = __bytes_builder!(
// ------------------- //
// Column Count packet //
// ------------------- //
@ -352,7 +357,7 @@ mod test {
// int<1> seq_no
1u8,
// int<lenenc> tag code or length
4u8,
1u8,
// ------------------------ //
// Column Definition packet //
@ -378,7 +383,7 @@ mod test {
// int<2> character set number
8u8, 0u8,
// int<4> max. column size
0x80, 0u8, 0u8, 0u8,
0x80u8, 0u8, 0u8, 0u8,
// int<1> Field types
0xFD_u8,
// int<2> Field detail flag
@ -414,7 +419,7 @@ mod test {
// byte<(number_of_columns + 9) / 8> NULL-Bitmap
0u8,
// byte<lenenc> encoded result
36u8, b"d83dd1c4-ada9-11e9-96bc-0242ac110003",
36u8, b"044d3f34-af65-11e9-a2e5-0242ac110003",
// ---------- //
// EOF Packet //
@ -430,5 +435,12 @@ mod test {
// int<2> server status
34u8, 0u8
);
let mut context = ConnContext::new();
let mut ctx = DeContext::new(&mut context, buf);
ResultSet::deserialize(ctx, ProtocolType::Binary).await?;
Ok(())
}
}