Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rust test changes #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 95 additions & 33 deletions postgres/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,67 @@ fn get_connection_string() -> String {
)
}

#[test]
fn full_crud() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

client
.batch_execute(
"CREATE TEMPORARY TABLE foo(
id INT,
name TEXT,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to replace TEXT with VARCHAR here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the postgres UDx that Alex is working on makes VARCHAR an alias for TEXT, so this works. You can find the UDx in server/udx/supported/pgcompat. I could not get the generateseries() function in it to work, but I ran the 'install.sql' script in the 'ddl' folder.

price FLOAT,
isperson BOOL,
guid UUID,
num NUMERIC
)",
)
.unwrap();

let mut transaction = client.transaction().unwrap();
transaction
.batch_execute("INSERT INTO foo (id, name, price, isperson, guid, num) VALUES (1, 'steven', 2.0, true, '123e4567-e89b-12d3-a456-426655440000', 42)")
.unwrap();
transaction.commit().unwrap();

let stmt = client.prepare("SELECT name FROM foo").unwrap();
let rows = client.query(&stmt, &[]).unwrap();

assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, &str>(0), "steven");

let mut transaction = client.transaction().unwrap();
transaction
.execute("UPDATE foo SET name = 'john' WHERE id = 1", &[])
.unwrap();
transaction.commit().unwrap();

let stmt = client.prepare("SELECT name FROM foo").unwrap();
let rows = client.query(&stmt, &[]).unwrap();

assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, &str>(0), "john");

let mut transaction = client.transaction().unwrap();
transaction
.execute("DELETE FROM foo WHERE id = 1", &[])
.unwrap();
transaction.commit().unwrap();

let stmt = client.prepare("SELECT name FROM foo").unwrap();
let rows = client.query(&stmt, &[]).unwrap();
assert_eq!(rows.len(), 0);
}

#[test]
fn prepare() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

let stmt = client.prepare("SELECT 1::INT, $1::TEXT").unwrap();
assert_eq!(stmt.params(), &[Type::TEXT]);
assert_eq!(stmt.params(), &[Type::VARCHAR]);
assert_eq!(stmt.columns().len(), 2);
assert_eq!(stmt.columns()[0].type_(), &Type::INT4);
assert_eq!(stmt.columns()[1].type_(), &Type::TEXT);
assert_eq!(stmt.columns()[0].type_(), &Type::INT8);
assert_eq!(stmt.columns()[1].type_(), &Type::VARCHAR);
}

#[test]
Expand All @@ -49,7 +101,7 @@ fn query_prepared() {
fn query_unprepared() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

let rows = client.query("SELECT $1::TEXT", &[&"hello"]).unwrap();
let rows = client.query("SELECT 'hello'", &[]).unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, &str>(0), "hello");
}
Expand All @@ -59,34 +111,34 @@ fn transaction_commit() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
.simple_query("CREATE TEMPORARY TABLE foo (id INT)")
.unwrap();

let mut transaction = client.transaction().unwrap();

transaction
.execute("INSERT INTO foo DEFAULT VALUES", &[])
.execute("INSERT INTO foo (id) VALUES (1)", &[])
.unwrap();

transaction.commit().unwrap();

let rows = client.query("SELECT * FROM foo", &[]).unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);
assert_eq!(rows[0].get::<_, i64>(0), 1);
}

#[test]
fn transaction_rollback() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
.simple_query("CREATE TEMPORARY TABLE foo (id INT)")
.unwrap();

let mut transaction = client.transaction().unwrap();

transaction
.execute("INSERT INTO foo DEFAULT VALUES", &[])
.execute("INSERT INTO foo (id) VALUES (2)", &[])
.unwrap();

transaction.rollback().unwrap();
Expand All @@ -100,7 +152,7 @@ fn transaction_drop() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id SERIAL PRIMARY KEY)")
.simple_query("CREATE TEMPORARY TABLE foo (id INT)")
.unwrap();

let mut transaction = client.transaction().unwrap();
Expand All @@ -116,6 +168,7 @@ fn transaction_drop() {
}

#[test]
#[ignore]
fn transaction_drop_immediate_rollback() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();
let mut client2 = Client::connect(&get_connection_string(), NoTls).unwrap();
Expand Down Expand Up @@ -166,7 +219,7 @@ fn nested_transactions() {
.query("SELECT id FROM foo ORDER BY id", &[])
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);
assert_eq!(rows[0].get::<_, i64>(0), 1);

let mut transaction3 = transaction.transaction().unwrap();

Expand All @@ -186,9 +239,9 @@ fn nested_transactions() {

let rows = client.query("SELECT id FROM foo ORDER BY id", &[]).unwrap();
assert_eq!(rows.len(), 3);
assert_eq!(rows[0].get::<_, i32>(0), 1);
assert_eq!(rows[1].get::<_, i32>(0), 3);
assert_eq!(rows[2].get::<_, i32>(0), 4);
assert_eq!(rows[0].get::<_, i64>(0), 1);
assert_eq!(rows[1].get::<_, i64>(0), 3);
assert_eq!(rows[2].get::<_, i64>(0), 4);
}

#[test]
Expand Down Expand Up @@ -217,7 +270,7 @@ fn savepoints() {
.query("SELECT id FROM foo ORDER BY id", &[])
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 1);
assert_eq!(rows[0].get::<_, i64>(0), 1);

let mut savepoint2 = transaction.savepoint("savepoint2").unwrap();

Expand All @@ -237,31 +290,31 @@ fn savepoints() {

let rows = client.query("SELECT id FROM foo ORDER BY id", &[]).unwrap();
assert_eq!(rows.len(), 3);
assert_eq!(rows[0].get::<_, i32>(0), 1);
assert_eq!(rows[1].get::<_, i32>(0), 3);
assert_eq!(rows[2].get::<_, i32>(0), 4);
assert_eq!(rows[0].get::<_, i64>(0), 1);
assert_eq!(rows[1].get::<_, i64>(0), 3);
assert_eq!(rows[2].get::<_, i64>(0), 4);
}

#[test]
fn copy_in() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name TEXT)")
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)")
.unwrap();

let mut writer = client.copy_in("COPY foo FROM stdin").unwrap();
writer.write_all(b"1\tsteven\n2\ttimothy").unwrap();
writer.write_all(b"1|steven\n2|timothy").unwrap();
writer.finish().unwrap();

let rows = client
.query("SELECT id, name FROM foo ORDER BY id", &[])
.unwrap();

assert_eq!(rows.len(), 2);
assert_eq!(rows[0].get::<_, i32>(0), 1);
assert_eq!(rows[0].get::<_, i64>(0), 1);
assert_eq!(rows[0].get::<_, &str>(1), "steven");
assert_eq!(rows[1].get::<_, i32>(0), 2);
assert_eq!(rows[1].get::<_, i64>(0), 2);
assert_eq!(rows[1].get::<_, &str>(1), "timothy");
}

Expand All @@ -270,11 +323,11 @@ fn copy_in_abort() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name TEXT)")
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)")
.unwrap();

let mut writer = client.copy_in("COPY foo FROM stdin").unwrap();
writer.write_all(b"1\tsteven\n2\ttimothy").unwrap();
writer.write_all(b"1|steven\n2|timothy").unwrap();
drop(writer);

let rows = client
Expand All @@ -285,15 +338,16 @@ fn copy_in_abort() {
}

#[test]
#[ignore]
fn binary_copy_in() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

client
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name TEXT)")
.simple_query("CREATE TEMPORARY TABLE foo (id INT, name VARCHAR)")
.unwrap();

let writer = client.copy_in("COPY foo FROM stdin BINARY").unwrap();
let mut writer = BinaryCopyInWriter::new(writer, &[Type::INT4, Type::TEXT]);
let mut writer = BinaryCopyInWriter::new(writer, &[Type::INT4, Type::VARCHAR]);
writer.write(&[&1i32, &"steven"]).unwrap();
writer.write(&[&2i32, &"timothy"]).unwrap();
writer.finish().unwrap();
Expand All @@ -310,6 +364,7 @@ fn binary_copy_in() {
}

#[test]
#[ignore]
fn copy_out() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

Expand All @@ -331,6 +386,7 @@ fn copy_out() {
}

#[test]
#[ignore]
fn binary_copy_out() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

Expand Down Expand Up @@ -363,7 +419,9 @@ fn portal() {
client
.simple_query(
"CREATE TEMPORARY TABLE foo (id INT);
INSERT INTO foo (id) VALUES (1), (2), (3);",
INSERT INTO foo (id) VALUES (1);
INSERT INTO foo (id) VALUES (2);
INSERT INTO foo (id) VALUES (3);",
)
.unwrap();

Expand All @@ -373,14 +431,15 @@ fn portal() {
.bind("SELECT * FROM foo ORDER BY id", &[])
.unwrap();

let rows = transaction.query_portal(&portal, 2).unwrap();
assert_eq!(rows.len(), 2);
assert_eq!(rows[0].get::<_, i32>(0), 1);
assert_eq!(rows[1].get::<_, i32>(0), 2);
let rows = transaction.query_portal(&portal, 3).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the meaning of parameters in transaction.query_portal()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The integer is the number of rows. In postgres, a portal can return fewer than the whole set of rows but in Vertica we can only query all at once. So really, it's kind of pointless here.

assert_eq!(rows.len(), 3);
assert_eq!(rows[0].get::<_, i64>(0), 1);
assert_eq!(rows[1].get::<_, i64>(0), 2);
assert_eq!(rows[1].get::<_, i64>(0), 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this line be the following?

Suggested change
assert_eq!(rows[1].get::<_, i64>(0), 2);
assert_eq!(rows[2].get::<_, i64>(0), 3);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll fix that, thanks.


let rows = transaction.query_portal(&portal, 2).unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<_, i32>(0), 3);
assert_eq!(rows.len(), 3);
assert_eq!(rows[0].get::<_, i64>(0), 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the second call to transaction.query_portal(&portal, 2).unwrap() return 0 row?

}

#[test]
Expand All @@ -402,6 +461,7 @@ fn cancel_query() {
}

#[test]
#[ignore]
fn notifications_iter() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

Expand All @@ -422,6 +482,7 @@ fn notifications_iter() {
}

#[test]
#[ignore]
fn notifications_blocking_iter() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

Expand Down Expand Up @@ -455,6 +516,7 @@ fn notifications_blocking_iter() {
}

#[test]
#[ignore]
fn notifications_timeout_iter() {
let mut client = Client::connect(&get_connection_string(), NoTls).unwrap();

Expand Down