Skip to content

Commit 362ced0

Browse files
committed
Improved bit tests
1 parent 017e0ed commit 362ced0

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/postgres_ext/bit.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,26 @@ mod tests {
5353
client.execute("CREATE EXTENSION IF NOT EXISTS vector", &[])?;
5454
client.execute("DROP TABLE IF EXISTS postgres_bit_items", &[])?;
5555
client.execute(
56-
"CREATE TABLE postgres_bit_items (id bigserial PRIMARY KEY, embedding bit(8))",
56+
"CREATE TABLE postgres_bit_items (id bigserial PRIMARY KEY, embedding bit(3))",
5757
&[],
5858
)?;
5959

60-
let vec = Bit::from_bytes(&[0b10101010]);
61-
let vec2 = Bit::from_bytes(&[0b01010101]);
60+
let vec = Bit::new(&[true, false, true]);
61+
let vec2 = Bit::new(&[false, true, false]);
6262
client.execute(
6363
"INSERT INTO postgres_bit_items (embedding) VALUES ($1), ($2), (NULL)",
6464
&[&vec, &vec2],
6565
)?;
6666

67-
let query_vec = Bit::from_bytes(&[0b10101010]);
67+
let query_vec = Bit::new(&[true, false, true]);
6868
let row = client.query_one(
6969
"SELECT embedding FROM postgres_bit_items ORDER BY embedding <~> $1 LIMIT 1",
7070
&[&query_vec],
7171
)?;
7272
let res_vec: Bit = row.get(0);
7373
assert_eq!(vec, res_vec);
74-
assert_eq!(8, res_vec.len());
75-
assert_eq!(&[0b10101010], res_vec.as_bytes());
74+
assert_eq!(3, res_vec.len());
75+
assert_eq!(&[0b10100000], res_vec.as_bytes());
7676

7777
let null_row = client.query_one(
7878
"SELECT embedding FROM postgres_bit_items WHERE embedding IS NULL LIMIT 1",
@@ -87,15 +87,15 @@ mod tests {
8787
&[],
8888
)?;
8989
let text_res: String = text_row.get(0);
90-
assert_eq!("10101010", text_res);
90+
assert_eq!("101", text_res);
9191

9292
// copy
9393
let bit_type = Type::BIT;
9494
let writer = client
9595
.copy_in("COPY postgres_bit_items (embedding) FROM STDIN WITH (FORMAT BINARY)")?;
9696
let mut writer = BinaryCopyInWriter::new(writer, &[bit_type]);
97-
writer.write(&[&Bit::from_bytes(&[0b10101010])])?;
98-
writer.write(&[&Bit::from_bytes(&[0b01010101])])?;
97+
writer.write(&[&Bit::new(&[true, false, true])])?;
98+
writer.write(&[&Bit::new(&[false, true, false])])?;
9999
writer.finish()?;
100100

101101
Ok(())

src/sqlx_ext/bit.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,27 @@ mod tests {
5757
sqlx::query("DROP TABLE IF EXISTS sqlx_bit_items")
5858
.execute(&pool)
5959
.await?;
60-
sqlx::query("CREATE TABLE sqlx_bit_items (id bigserial PRIMARY KEY, embedding bit(8))")
60+
sqlx::query("CREATE TABLE sqlx_bit_items (id bigserial PRIMARY KEY, embedding bit(3))")
6161
.execute(&pool)
6262
.await?;
6363

64-
let vec = Bit::from_bytes(&[0b10101010]);
65-
let vec2 = Bit::from_bytes(&[0b01010101]);
64+
let vec = Bit::new(&[true, false, true]);
65+
let vec2 = Bit::new(&[false, true, false]);
6666
sqlx::query("INSERT INTO sqlx_bit_items (embedding) VALUES ($1), ($2), (NULL)")
6767
.bind(&vec)
6868
.bind(&vec2)
6969
.execute(&pool)
7070
.await?;
7171

72-
let query_vec = Bit::from_bytes(&[0b10101010]);
72+
let query_vec = Bit::new(&[true, false, true]);
7373
let row =
7474
sqlx::query("SELECT embedding FROM sqlx_bit_items ORDER BY embedding <~> $1 LIMIT 1")
7575
.bind(query_vec)
7676
.fetch_one(&pool)
7777
.await?;
7878
let res_vec: Bit = row.try_get("embedding").unwrap();
7979
assert_eq!(vec, res_vec);
80-
assert_eq!(&[0b10101010], res_vec.as_bytes());
80+
assert_eq!(&[0b10100000], res_vec.as_bytes());
8181

8282
let null_row =
8383
sqlx::query("SELECT embedding FROM sqlx_bit_items WHERE embedding IS NULL LIMIT 1")
@@ -92,9 +92,9 @@ mod tests {
9292
.fetch_one(&pool)
9393
.await?;
9494
let text_res: String = text_row.try_get("embedding").unwrap();
95-
assert_eq!("10101010", text_res);
95+
assert_eq!("101", text_res);
9696

97-
sqlx::query("ALTER TABLE sqlx_bit_items ADD COLUMN factors bit(8)[]")
97+
sqlx::query("ALTER TABLE sqlx_bit_items ADD COLUMN factors bit(3)[]")
9898
.execute(&pool)
9999
.await?;
100100

0 commit comments

Comments
 (0)