Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 2ef424c

Browse files
committed
add create dump test.
1 parent 8d3b24f commit 2ef424c

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

sqld/tests/common/http.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ impl Response {
1616
Ok(v)
1717
}
1818

19+
pub async fn json_value(self) -> anyhow::Result<serde_json::Value> {
20+
self.json().await
21+
}
22+
23+
pub async fn body_string(self) -> anyhow::Result<String> {
24+
let bytes = hyper::body::to_bytes(self.0.into_body()).await?;
25+
Ok(String::from_utf8(bytes.to_vec())?)
26+
}
27+
1928
pub fn status(&self) -> hyper::http::StatusCode {
2029
self.0.status()
2130
}

sqld/tests/namespaces/load_dump.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::convert::Infallible;
22

33
use hyper::{service::make_service_fn, Response, Body, StatusCode};
4+
use insta::assert_snapshot;
45
use libsql::{Database, Value};
56
use serde_json::json;
67
use tempfile::tempdir;
@@ -157,9 +158,10 @@ fn load_namespace_from_no_txn() {
157158

158159
sim.client("client", async move {
159160
let client = Client::new();
160-
let resp = client.post("http://primary:9090/v1/namespaces/foo/create", json!({ "dump_url": format!("file:{}", tmp_path.join("dump.sql").display())})).await.unwrap();
161+
let resp = client.post("http://primary:9090/v1/namespaces/foo/create", json!({ "dump_url": format!("file:{}", tmp_path.join("dump.sql").display())})).await?;
161162
// the dump is malformed
162163
assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "{}", resp.json::<serde_json::Value>().await.unwrap_or_default());
164+
assert_snapshot!(resp.json_value().await.unwrap());
163165

164166
// namespace doesn't exist
165167
let foo = Database::open_remote_with_connector("http://foo.primary:8080", "", TurmoilConnector)?;
@@ -171,3 +173,34 @@ fn load_namespace_from_no_txn() {
171173

172174
sim.run().unwrap();
173175
}
176+
177+
#[test]
178+
fn export_dump() {
179+
let mut sim = Builder::new().build();
180+
let tmp = tempdir().unwrap();
181+
182+
make_primary(&mut sim, tmp.path().to_path_buf());
183+
184+
sim.client("client", async move {
185+
let client = Client::new();
186+
let resp = client.post("http://primary:9090/v1/namespaces/foo/create", json!({})).await?;
187+
assert_eq!(resp.status(), StatusCode::OK);
188+
assert_snapshot!(resp.json_value().await.unwrap());
189+
190+
let foo = Database::open_remote_with_connector("http://foo.primary:8080", "", TurmoilConnector)?;
191+
let foo_conn = foo.connect()?;
192+
foo_conn.execute("create table test (x)", ()).await?;
193+
foo_conn.execute("insert into test values (42)", ()).await?;
194+
foo_conn.execute("insert into test values (\"foo\")", ()).await?;
195+
foo_conn.execute("insert into test values (\"bar\")", ()).await?;
196+
197+
198+
let resp = client.get("http://foo.primary:8080/dump").await?;
199+
assert_eq!(resp.status(), StatusCode::OK);
200+
assert_snapshot!(resp.body_string().await?);
201+
202+
Ok(())
203+
});
204+
205+
sim.run().unwrap();
206+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: sqld/tests/namespaces/load_dump.rs
3+
expression: resp.body_string().await?
4+
---
5+
PRAGMA foreign_keys=OFF;
6+
BEGIN TRANSACTION;
7+
CREATE TABLE IF NOT EXISTS libsql_wasm_func_table (name text PRIMARY KEY, body text) WITHOUT ROWID;
8+
CREATE TABLE IF NOT EXISTS test (x);
9+
INSERT INTO test VALUES(42);
10+
INSERT INTO test VALUES('foo');
11+
INSERT INTO test VALUES('bar');
12+
COMMIT;
13+

0 commit comments

Comments
 (0)