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

Fix python tests #37

Merged
merged 9 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `TraverseConfig::in_bounds` takes a slice instead of a `&Vec` now.
- Bumped upstream dependencies.
- Fixed broken python tests. FFI requires tokio now.
- Work around some broken python & FFI tests

### Removed
Expand Down
16 changes: 7 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,15 @@ thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["macros", "rt", "sync"] }
tokio-stream = "0.1.14"
tracing = "0.1.40"
uniffi = { version = "0.26.1", features = ["cli"], optional = true }
uniffi = { version = "0.26.1", features = ["cli", "tokio"], optional = true }

[dependencies.replicator]
#path = "../replicator/replicator"
git = "https://github.com/cowlicks/replicator.git"
# TODO onyl used in the tests, but duplicating this for dev-dependencies is annoying
features = ["utils"]

[dependencies.hypercore]
#version = "0.13.0"
#path = "../core"
git = "https://github.com/cowlicks/hypercore.git"
branch = "replication"
version = "0.14.0"
default-features = false
features = ["sparse", "tokio"]
features = ["sparse", "tokio", "replication"]

[build-dependencies]
prost-build = "0.12.1"
Expand All @@ -68,3 +62,7 @@ random-access-memory = "3.0.0"
once_cell = "1.19.0"
tempfile = "3.10.0"
serde_json = "1.0.114"

[dev-dependencies.replicator]
git = "https://github.com/cowlicks/replicator.git"
features = ["utils"]
6 changes: 3 additions & 3 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct Hyperbee {
rust_hyperbee: Shared<RustHyperbee>,
}

#[uniffi::export]
#[uniffi::export(async_runtime = "tokio")]
impl Hyperbee {
/// The number of blocks in the hypercore.
/// The first block is always the header block so:
Expand Down Expand Up @@ -101,7 +101,7 @@ async fn hyperbee_from_ram() -> Result<Hyperbee, HyperbeeError> {
}

/// Helper for creating a Hyperbee from the provided path to a storage directory
#[uniffi::export]
#[uniffi::export(async_runtime = "tokio")]
async fn hyperbee_from_storage_dir(path_to_storage_dir: &str) -> Result<Hyperbee, HyperbeeError> {
let rust_hyperbee = RustHyperbee::from_storage_dir(path_to_storage_dir).await?;
Ok(Hyperbee {
Expand All @@ -114,7 +114,7 @@ struct Prefixed {
rust_prefixed: Shared<RustPrefixed>,
}

#[uniffi::export]
#[uniffi::export(async_runtime = "tokio")]
impl Prefixed {
/// Get the value associated with the provided key plus this [`Prefixed`]'s instance's `prefix`
async fn get(&self, key: &[u8]) -> Result<Option<Gotten>, HyperbeeError> {
Expand Down
4 changes: 2 additions & 2 deletions src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async fn make_child_key_index_iter(
}

impl TraverseConfig {
fn in_bounds(&self, value: &Vec<u8>) -> bool {
fn in_bounds(&self, value: &[u8]) -> bool {
// TODO impl Ord for LimitValue and remove the expects
match self
.min_value
Expand Down Expand Up @@ -308,7 +308,7 @@ async fn get_child_stream<'a>(
Ok(Traverse::new(child, config))
}

impl<'a> Stream for Traverse<'a> {
impl Stream for Traverse<'_> {
type Item = TreeItem;
#[tracing::instrument(skip(self, cx))]
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Expand Down
7 changes: 1 addition & 6 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,7 @@ mod test {
);
Ok(())
}
}
#[cfg(feature = "debug")]
#[cfg(test)]
mod test {
use super::*;

#[cfg(feature = "debug")]
#[tokio::test]
async fn height_zero() -> Result<(), HyperbeeError> {
let tree = Tree::from_ram().await?;
Expand Down
12 changes: 8 additions & 4 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,16 @@ macro_rules! write_range_to_hb {
pub(crate) use write_range_to_hb;

pub fn check_cmd_output(out: Output) -> Result<Output> {
eprint!("{}", String::from_utf8_lossy(&out.stdout));
if out.status.code() != Some(0) {
if !out.stdout.is_empty() {
eprintln!("stdout:\n{}", String::from_utf8_lossy(&out.stdout));
}
if !out.stderr.is_empty() {
eprintln!("stderr:\n{}", String::from_utf8_lossy(&out.stderr));
}
return Err(Box::new(Error::TestError(format!(
"comand output status was not zero. Got:\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
"command output status was not zero. Got:{:?}",
out.status.code()
))));
}
Ok(out)
Expand Down
134 changes: 63 additions & 71 deletions tests/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ use common::{
};
use hyperbee::Hyperbee;

#[tokio::test]
async fn test_sub_prefix() -> Result<()> {
let x = match require_python() {
Err(e) => {
println!("{}", e);
return Err(e);
}
Ok(x) => x,
};
dbg!(&x);
let out = run_python(
"
#[cfg(test)]
mod python {
use super::*;
#[tokio::test]
async fn test_sub_prefix() -> Result<()> {
require_python()?;
let out = run_python(
"
async def main():
prefix = b'myprefix'
key = b'keykey'
Expand All @@ -37,18 +33,16 @@ async def main():
x = await sub_hb.get(key)
assert(x.value == val_with_pref)
",
)?;
dbg!(&out);
assert_eq!(out.status.code(), Some(0));
Ok(())
}
)?;
assert_eq!(out.status.code(), Some(0));
Ok(())
}

#[tokio::test]
async fn hello_world_get_set_del() -> Result<()> {
let x = require_python()?;
dbg!(&x);
let out = run_python(
"
#[tokio::test]
async fn hello_world_get_set_del() -> Result<()> {
require_python()?;
let out = run_python(
"
async def main():
hb = await hyperbee_from_ram()
x = await hb.put(b'hello', b'world')
Expand All @@ -61,23 +55,21 @@ async def main():
x = await hb.delete(b'hello')
assert(x == 1)
",
)?;
dbg!(&out);
assert_eq!(out.status.code(), Some(0));
Ok(())
}

#[tokio::test]
#[ignore]
async fn optionals() -> Result<()> {
let _x = require_python()?;
let storage_dir = tempfile::tempdir()?;
{
let hb = Hyperbee::from_storage_dir(&storage_dir).await?;
let _ = hb.put(b"hello", None).await?;
)?;
assert_eq!(out.status.code(), Some(0));
Ok(())
}
let code = format!(
"

#[tokio::test]
async fn optionals() -> Result<()> {
let _x = require_python()?;
let storage_dir = tempfile::tempdir()?;
{
let hb = Hyperbee::from_storage_dir(&storage_dir).await?;
let _ = hb.put(b"hello", None).await?;
}
let code = format!(
"
async def main():
import json
hb = await hyperbee_from_storage_dir('{}')
Expand All @@ -88,19 +80,19 @@ async def main():
res = await hb.get(b'skipval')
assert(res.value is None)
",
storage_dir.path().display()
);
storage_dir.path().display()
);

let output = run_python(&code)?;
dbg!(&output);
assert_eq!(output.status.code(), Some(0));
Ok(())
}
#[tokio::test]
async fn check_version() -> Result<()> {
let _x = require_python()?;
let out = run_python(
"
let output = run_python(&code)?;
assert_eq!(output.status.code(), Some(0));
Ok(())
}

#[tokio::test]
async fn check_version() -> Result<()> {
let _x = require_python()?;
let out = run_python(
"
async def main():
hb = await hyperbee_from_ram()
x = await hb.version()
Expand All @@ -109,20 +101,19 @@ async def main():
x = await hb.version()
assert(x == 2)
",
)?;
assert_eq!(out.status.code(), Some(0));
Ok(())
}
)?;
assert_eq!(out.status.code(), Some(0));
Ok(())
}

#[tokio::test]
#[ignore]
async fn zero_to_one_hundred() -> Result<()> {
let _x = require_python()?;
let storage_dir = tempfile::tempdir()?;
let hb = Hyperbee::from_storage_dir(&storage_dir).await?;
let keys = write_range_to_hb!(&hb);
let code = format!(
"
#[tokio::test]
async fn zero_to_one_hundred() -> Result<()> {
let _x = require_python()?;
let storage_dir = tempfile::tempdir()?;
let hb = Hyperbee::from_storage_dir(&storage_dir).await?;
let keys = write_range_to_hb!(&hb);
let code = format!(
"
async def main():
import json
hb = await hyperbee_from_storage_dir('{}')
Expand All @@ -131,12 +122,13 @@ async def main():
values = [res.value.decode('utf8') for res in results]
print(json.dumps(values))
",
storage_dir.path().display()
);
storage_dir.path().display()
);

let output = run_python(&code)?;
let res = parse_json_result(&output)?;
assert_eq!(res, keys);
assert_eq!(output.status.code(), Some(0));
Ok(())
let output = run_python(&code)?;
let res = parse_json_result(&output)?;
assert_eq!(res, keys);
assert_eq!(output.status.code(), Some(0));
Ok(())
}
}
Loading