Skip to content

Commit b67149a

Browse files
committed
move tests to tests dir
cargo clippy --all-targets --all-features --fix Signed-off-by: js0.site <jssite@googlegroups.com>
1 parent a0785e4 commit b67149a

6 files changed

Lines changed: 124 additions & 95 deletions

File tree

Cargo.toml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@
1515
[package]
1616
name = "opengemini"
1717
version = "0.0.1"
18-
edition = "2021"
18+
edition = "2024"
1919
description = "Rust client for OpenGemini"
2020
license = "Apache-2.0"
2121
repository = "https://github.com/openGemini/opengemini-client-rust"
2222
homepage = "https://github.com/openGemini/opengemini-client-rust"
2323

2424
[dependencies]
25-
reqwest = { version = "0.12.7", features = ["json", "__rustls", "blocking", "gzip"] }
26-
serde = { version = "1.0.208", features = ["derive"] }
27-
serde_json = "1.0.125"
28-
thiserror = "1.0.64"
29-
tokio = "1.40.0"
25+
log = "0.4.28"
26+
reqwest = { version = "0.12.24", features = [
27+
"json",
28+
"__rustls",
29+
"blocking",
30+
"gzip",
31+
] }
32+
serde = { version = "1.0.228", features = ["derive"] }
33+
serde_json = "1.0.145"
34+
thiserror = "2.0.17"
35+
tokio = "1.48.0"
3036
zigzag = "0.1.0"
37+
38+
[dev-dependencies]
39+
static_init = "1.0.4"

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
mod config;
16-
mod error;
15+
pub mod config;
16+
pub mod error;
1717
mod opengemini_client;
1818
mod url_const;
1919

src/opengemini_client.rs

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ impl Client {
5050
}
5151

5252
pub fn ping(&self, idx: usize) -> Result<bool, ClientError> {
53-
if idx >= self.endpoints.len() as usize {
53+
if idx >= self.endpoints.len() {
5454
return Err(ClientError::ValueError("Index out of range".to_string()));
5555
}
5656
let url: String = self.endpoints[idx].url.clone() + URL_PING;
5757
match reqwest::blocking::get(url) {
5858
Ok(response) => {
5959
if response.status().is_success() {
60-
return Ok(true);
60+
Ok(true)
6161
} else {
62-
return Ok(false);
62+
Ok(false)
6363
}
6464
}
6565
Err(_) => Err(ClientError::ConnectionError),
@@ -82,10 +82,11 @@ impl Client {
8282
todo!();
8383
}
8484

85-
fn get_server_url(&self) -> Option<String> {
85+
pub fn get_server_url(&self) -> Option<String> {
8686
let current_index = self.prev_idx.load(Ordering::SeqCst);
8787
let endpoint_count = self.endpoints.len() as i32;
88-
let url = if endpoint_count > 0 {
88+
89+
if endpoint_count > 0 {
8990
let url = self.endpoints[current_index as usize % endpoint_count as usize]
9091
.url
9192
.clone();
@@ -94,8 +95,7 @@ impl Client {
9495
Some(url)
9596
} else {
9697
None
97-
};
98-
url
98+
}
9999
}
100100
}
101101

@@ -111,79 +111,3 @@ pub fn build_endpoints(addresses: Vec<Address>) -> Vec<Endpoint> {
111111
})
112112
.collect()
113113
}
114-
115-
#[cfg(test)]
116-
mod tests {
117-
use std::time::Duration;
118-
119-
use crate::config::{AuthConfig, BatchConfig};
120-
121-
use super::*;
122-
123-
fn create_test_config() -> Config {
124-
Config {
125-
address: vec![Address {
126-
host: "127.0.0.1".to_string(),
127-
port: 8086,
128-
}],
129-
batch_config: BatchConfig {
130-
batch_interval: Duration::from_secs(30),
131-
batch_size: 100,
132-
},
133-
timeout: Duration::from_secs(30),
134-
connect_timeout: Duration::from_secs(10),
135-
gzip_enabled: true,
136-
auth_config: AuthConfig {
137-
username: "user".to_string(),
138-
password: "password".to_string(),
139-
token: None,
140-
auth_type: 1,
141-
},
142-
}
143-
}
144-
145-
#[test]
146-
fn test_get_server_url() {
147-
let addresses = vec![
148-
Address {
149-
host: "127.0.0.1".to_string(),
150-
port: 8086,
151-
},
152-
Address {
153-
host: "127.0.0.2".to_string(),
154-
port: 8087,
155-
},
156-
];
157-
let mut config = create_test_config();
158-
159-
config.address = addresses;
160-
161-
let client = Client::new(&config);
162-
163-
let url1 = client.get_server_url();
164-
let url2 = client.get_server_url();
165-
166-
assert!(url1.is_some());
167-
assert!(url2.is_some());
168-
assert_ne!(url1, url2);
169-
}
170-
171-
/// Tests the `ping` method of the `Client` struct.
172-
///
173-
/// This test sets up a `Client` with a single address and checks if the `ping` method
174-
/// returns `Ok(true)` when the server is reachable.
175-
///
176-
/// Before running this test, make sure to start the server using the following Docker command:
177-
/// ```sh
178-
/// docker run -p 8086:8086 --name opengemini --rm opengeminidb/opengemini-server
179-
/// ```
180-
#[test]
181-
fn test_ping_success() {
182-
let config = create_test_config();
183-
let client = Client::new(&config);
184-
185-
let result = client.ping(0);
186-
assert!(result.is_ok());
187-
assert_eq!(result.unwrap(), true);
188-
}
189-
}

src/url_const.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
pub const URL_PING: &'static str = "/ping";
16-
pub const URL_QUERY: &'static str = "/query";
17-
pub const URL_STATUS: &'static str = "/status";
18-
pub const URL_WRITE_OUTPUT: &'static str = "/write";
15+
pub const URL_PING: &str = "/ping";
16+
pub const URL_QUERY: &str = "/query";
17+
pub const URL_STATUS: &str = "/status";
18+
pub const URL_WRITE_OUTPUT: &str = "/write";

test.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
DIR=$(realpath $0) && DIR=${DIR%/*}
5+
cd $DIR
6+
# set -x
7+
8+
PORT_LI="8086 8087"
9+
10+
stop() {
11+
for i in $PORT_LI; do
12+
docker stop opengemini-rust-client-test-$i || true
13+
done
14+
}
15+
16+
trap stop EXIT
17+
18+
boot() {
19+
for i in $PORT_LI; do
20+
docker run --rm -d -p $i:8086 --name opengemini-rust-client-test-$i opengeminidb/opengemini-server:latest || stop
21+
done
22+
for i in $PORT_LI; do
23+
while ! nc -z localhost $i; do
24+
echo "wait for port $i"
25+
sleep 1
26+
done
27+
done
28+
}
29+
30+
boot
31+
sleep 3
32+
33+
RUST_LOG=debug RUST_BACKTRACE=1 cargo test --all-features -- --nocapture

tests/client.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use opengemini::Client;
2+
use opengemini::config::{Address, AuthConfig, BatchConfig, Config};
3+
use std::time::Duration;
4+
5+
#[static_init::dynamic]
6+
static CONFIG: Config = Config {
7+
address: vec![Address {
8+
host: "127.0.0.1".to_string(),
9+
port: 8086,
10+
}],
11+
batch_config: BatchConfig {
12+
batch_interval: Duration::from_secs(30),
13+
batch_size: 100,
14+
},
15+
timeout: Duration::from_secs(30),
16+
connect_timeout: Duration::from_secs(10),
17+
gzip_enabled: true,
18+
auth_config: AuthConfig {
19+
username: "user".to_string(),
20+
password: "password".to_string(),
21+
token: None,
22+
auth_type: 1,
23+
},
24+
};
25+
26+
#[test]
27+
fn test_get_server_url() {
28+
let mut config = CONFIG.clone();
29+
config.address.push(Address {
30+
host: "127.0.0.1".to_string(),
31+
port: 8087,
32+
});
33+
34+
let client = Client::new(&config);
35+
36+
let url1 = client.get_server_url();
37+
let url2 = client.get_server_url();
38+
39+
assert!(url1.is_some());
40+
assert!(url2.is_some());
41+
assert_ne!(url1, url2);
42+
}
43+
44+
/// Tests the `ping` method of the `Client` struct.
45+
///
46+
/// This test sets up a `Client` with a single address and checks if the `ping` method
47+
/// returns `Ok(true)` when the server is reachable.
48+
///
49+
/// Before running this test, make sure to start the server using the following Docker command:
50+
/// ```sh
51+
/// docker run -p 8086:8086 --name opengemini --rm opengeminidb/opengemini-server
52+
/// ```
53+
#[test]
54+
fn test_ping_success() {
55+
let client = Client::new(&CONFIG);
56+
57+
let result = client.ping(0);
58+
if result.is_err() {
59+
log::info!("{:?}", &result);
60+
}
61+
assert!(result.is_ok());
62+
assert!(result.unwrap());
63+
}

0 commit comments

Comments
 (0)