Skip to content

Commit

Permalink
add trivial pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguida committed Dec 9, 2023
1 parent 1f314cc commit c49faf6
Show file tree
Hide file tree
Showing 6 changed files with 1,573 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
target/
result/
__pycache__/

scratch

89 changes: 89 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,92 @@ async fn block_added_handler(plugin: Plugin<State>, v: serde_json::Value) -> Res
log::trace!("returning from block_added_handler");
Ok(())
}

// #[tokio::test]
// async fn test_list() {
// let builder = Builder::new(tokio::io::stdin(), tokio::io::stdout())
// .dynamic();
// let plugin = Plugin {
// /// The state gets cloned for each request
// state: S,
// /// "options" field of "init" message sent by cln
// options: Vec<ConfigOption>,
// /// "configuration" field of "init" message sent by cln
// configuration: Configuration,
// /// A signal that allows us to wait on the plugin's shutdown.
// wait_handle: tokio::sync::broadcast::Sender<()>,

// sender: tokio::sync::mpsc::Sender<serde_json::Value>,
// };
// println!("plugin = {:?}", plugin);
// }

// #[cfg(test)]
// mod tests {
// use super::*;
// use async_trait::async_trait;
// use mockall::mock;
// use std::sync::{Arc, Mutex};

// // Mocking a simplified version of Wallet and State
// #[derive(Clone)]
// struct Wallet {
// pub descriptor: String,
// pub change_descriptor: Option<String>,
// pub birthday: Option<u32>,
// pub gap: Option<u32>,
// pub network: Option<String>,
// }

// struct State {
// pub wallets: BTreeMap<String, Wallet>,
// }

// #[async_trait]
// pub trait StateHolder {
// async fn state(&self) -> Arc<Mutex<State>>;
// }

// mock! {
// Plugin {}

// #[async_trait]
// impl StateHolder for Plugin<State> {
// async fn state(&self) -> Arc<Mutex<State>>;
// }
// }

// #[tokio::test]
// async fn test_list_function() {
// // Setup mock state
// let mut wallets = BTreeMap::new();
// wallets.insert(
// "wallet1".to_string(),
// Wallet {
// descriptor: "descriptor1".to_string(),
// change_descriptor: Some("change1".to_string()),
// birthday: Some(123),
// gap: Some(5),
// network: Some("network1".to_string()),
// },
// );

// let plugin = MockPlugin::new(wallets);

// // Call the function
// let result = list(plugin).await.unwrap();

// // Assert the expected outcome
// let expected_json = json!({
// "wallet1": {
// "descriptor": "descriptor1",
// "change_descriptor": "change1",
// "birthday": 123,
// "gap": 5,
// "network": "network1"
// }
// });

// assert_eq!(result, expected_json);
// }
// }
42 changes: 42 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pathlib import Path
import subprocess

from pyln.testing.fixtures import * # noqa: F401,F403
from pyln.testing.utils import BITCOIND_CONFIG, TailableProc

SMAUG_PLUGIN = Path("~/.cargo/bin/smaug").expanduser()


def write_toml_config(filename, opts):
with open(filename, "w") as f:
for k, v in opts.items():
if isinstance(v, str):
f.write('{} = "{}"\n'.format(k, v))
else:
f.write("{} = {}\n".format(k, v))

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
# execute all other hooks to obtain the report object
outcome = yield
rep = outcome.get_result()

# set a report attribute for each phase of a call, which can
# be "setup", "call", "teardown"

setattr(item, "rep_" + rep.when, rep)


def pytest_configure(config):
config.addinivalue_line("markers", "developer: only run when developer is flagged on")


def pytest_runtest_setup(item):
for mark in item.iter_markers(name="developer"):
pass


@pytest.fixture(scope="function", autouse=True)
def log_name(request):
# Here logging is used, you can use whatever you want to use for logs
logging.info("Starting '{}'".format(request.node.name))
Loading

0 comments on commit c49faf6

Please sign in to comment.