Skip to content

Commit

Permalink
refactor: add client api test crate
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed Jan 23, 2024
1 parent 3f31150 commit 3468fa6
Show file tree
Hide file tree
Showing 46 changed files with 470 additions and 189 deletions.
116 changes: 99 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ gotrue = { path = "libs/gotrue" }
gotrue-entity = { path = "libs/gotrue-entity" }
infra = { path = "libs/infra" }
app-error = { workspace = true, features = ["sqlx_error", "actix_web_error", "tokio_error"] }
shared_entity = { path = "libs/shared-entity", features = ["cloud"] }
shared-entity = { path = "libs/shared-entity", features = ["cloud"] }
workspace-template = { workspace = true }
realtime-entity.workspace = true

Expand All @@ -91,6 +91,7 @@ once_cell = "1.19.0"
tempfile = "3.9.0"
assert-json-diff = "2.0.2"
scraper = "0.17.1"
client-api-test-util = { path = "libs/client-api-test-util" }
client-api = { path = "libs/client-api", features = ["collab-sync", "test_util"] }
opener = "0.6.1"
image = "0.23.14"
Expand Down Expand Up @@ -122,12 +123,14 @@ members = [
"libs/encrypt",
"libs/realtime-protocol",
"libs/websocket",
"libs/client-api-test-util", "libs/wasm-test",
]

[workspace.dependencies]
realtime-entity = { path = "libs/realtime-entity" }
realtime-protocol = { path = "libs/realtime-protocol" }
database-entity = { path = "libs/database-entity" }
shared-entity = { path = "libs/shared-entity" }
app-error = { path = "libs/app_error" }
serde_json = "1.0.111"
serde = { version = "1.0.195", features = ["derive"] }
Expand All @@ -140,6 +143,11 @@ tokio = { version = "1.35", features = ["sync"] }
yrs = "0.17.2"
bincode = "1.3.3"
websocket = { path = "libs/websocket" }
collab = { version = "0.1.0" }
collab-folder = { version = "0.1.0" }
tracing = { version = "0.1"}
collab-entity = { version = "0.1.0" }
gotrue = { path = "libs/gotrue" }

[profile.release]
lto = true
Expand Down
32 changes: 32 additions & 0 deletions libs/client-api-test-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "client-api-test-util"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bytes = "1.5.0"
mime = "0.3.17"
serde_json = "1.0.111"
tokio = { version = "1.0", features = ["sync"] }
tokio-stream = "0.1.14"
tracing.workspace = true
collab-folder.workspace = true
collab = { workspace = true, features = ["async-plugin"] }
client-api = { path = "../client-api", features = ["collab-sync", "test_util"] }
once_cell = "1.19.0"
tempfile = "3.9.0"
assert-json-diff = "2.0.2"
scraper = "0.17.1"
opener = "0.6.1"
image = "0.23.14"
database-entity.workspace = true
collab-entity.workspace = true
shared-entity.workspace = true
tracing-subscriber = { version = "0.3.18", features = ["registry", "env-filter", "ansi", "json"] }
uuid = "1.6.1"
lazy_static = "1.4.0"
dotenv = "0.15.0"
reqwest = "0.11.23"
gotrue.workspace = true
50 changes: 50 additions & 0 deletions libs/client-api-test-util/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use client_api::{Client, ClientConfiguration};
use dotenv::dotenv;
use lazy_static::lazy_static;
use std::borrow::Cow;
use std::env;
use tracing::warn;

lazy_static! {
pub static ref LOCALHOST_URL: Cow<'static, str> =
get_env_var("LOCALHOST_URL", "http://localhost:8000");
pub static ref LOCALHOST_WS: Cow<'static, str> =
get_env_var("LOCALHOST_WS", "ws://localhost:8000/ws");
pub static ref LOCALHOST_GOTRUE: Cow<'static, str> =
get_env_var("LOCALHOST_GOTRUE", "http://localhost:9999");
}

fn get_env_var<'default>(key: &str, default: &'default str) -> Cow<'default, str> {
dotenv().ok();
match env::var(key) {
Ok(value) => Cow::Owned(value),
Err(_) => {
warn!("could not read env var {}: using default: {}", key, default);
Cow::Borrowed(default)
},
}
}

/// Return a client that connects to the local host. It requires to run the server locally.
/// ```shell
/// ./build/run_local_server.sh
/// ```
pub fn localhost_client() -> Client {
Client::new(
&LOCALHOST_URL,
&LOCALHOST_WS,
&LOCALHOST_GOTRUE,
ClientConfiguration::default(),
)
}

pub async fn workspace_id_from_client(c: &Client) -> String {
c.get_workspaces()
.await
.unwrap()
.0
.first()
.unwrap()
.workspace_id
.to_string()
}
9 changes: 9 additions & 0 deletions libs/client-api-test-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod client;
mod log;
mod test_client;
mod user;

pub use client::*;
pub use log::*;
pub use test_client::*;
pub use user::*;
15 changes: 9 additions & 6 deletions tests/util/mod.rs → libs/client-api-test-util/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::sync::Once;
use tracing_subscriber::fmt::Subscriber;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;

pub(crate) mod test_client;
#[cfg(not(target_arch = "wasm32"))]
use {
std::sync::Once,
tracing_subscriber::{fmt::Subscriber, util::SubscriberInitExt, EnvFilter},
};

#[cfg(not(target_arch = "wasm32"))]
pub fn setup_log() {
static START: Once = Once::new();
START.call_once(|| {
Expand All @@ -20,3 +20,6 @@ pub fn setup_log() {
subscriber.try_init().unwrap();
});
}

#[cfg(target_arch = "wasm32")]
pub fn setup_log() {}
Loading

0 comments on commit 3468fa6

Please sign in to comment.