Skip to content

Commit 8c54138

Browse files
committed
Add Postgrest client integration tests
1 parent 7fcedd8 commit 8c54138

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

tests/client.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
use postgrest::Postgrest;
2+
3+
use std::error::Error;
4+
5+
const REST_URL: &str = "http://localhost:3000";
6+
7+
#[tokio::test]
8+
async fn basic_data() -> Result<(), Box<dyn Error>> {
9+
let client = Postgrest::new(REST_URL);
10+
let resp = client
11+
.from("users")
12+
.select("username")
13+
.eq("status", "OFFLINE")
14+
.execute()
15+
.await?;
16+
let body = resp.text().await?;
17+
let body = json::parse(&body)?;
18+
19+
assert_eq!(body[0]["username"], "kiwicopple");
20+
21+
Ok(())
22+
}
23+
24+
#[tokio::test]
25+
async fn relational_join() -> Result<(), Box<dyn Error>> {
26+
let client = Postgrest::new(REST_URL);
27+
let resp = client
28+
.from("channels")
29+
.select("slug, messages(message)")
30+
.eq("slug", "public")
31+
.execute()
32+
.await?;
33+
let body = resp.text().await?;
34+
let body = json::parse(&body)?;
35+
36+
assert_eq!(body[0]["messages"][0]["message"], "Hello World 👋");
37+
assert_eq!(body[0]["slug"], "public");
38+
39+
Ok(())
40+
}
41+
42+
#[tokio::test]
43+
async fn insert() -> Result<(), Box<dyn Error>> {
44+
let client = Postgrest::new(REST_URL);
45+
let resp = client
46+
.from("messages")
47+
.insert(r#"[{"message": "Test message 0", "channel_id": 1, "username": "kiwicopple"}]"#)
48+
.execute()
49+
.await?;
50+
let status = resp.status();
51+
52+
assert_eq!(status.as_u16(), 201);
53+
54+
Ok(())
55+
}
56+
57+
#[tokio::test]
58+
async fn upsert() -> Result<(), Box<dyn Error>> {
59+
let client = Postgrest::new(REST_URL);
60+
let resp = client
61+
.from("users")
62+
.upsert(
63+
r#"[{"username": "dragarcia", "status": "OFFLINE"},
64+
{"username": "supabot2", "status": "ONLINE"}]"#,
65+
)
66+
.execute()
67+
.await?;
68+
let body = resp.text().await?;
69+
let body = json::parse(&body)?;
70+
71+
assert_eq!(body[0]["username"], "dragarcia");
72+
assert_eq!(body[1]["username"], "supabot2");
73+
74+
Ok(())
75+
}
76+
77+
#[tokio::test]
78+
async fn upsert_existing() -> Result<(), Box<dyn Error>> {
79+
let client = Postgrest::new(REST_URL);
80+
let resp = client
81+
.from("users")
82+
.upsert(r#"{"username": "dragarcia", "status": "ONLINE"}"#)
83+
.execute()
84+
.await?;
85+
let body = resp.text().await?;
86+
let body = json::parse(&body)?;
87+
88+
assert_eq!(body[0]["username"], "dragarcia");
89+
assert_eq!(body[0]["status"], "ONLINE");
90+
91+
Ok(())
92+
}
93+
94+
#[tokio::test]
95+
async fn upsert_nonexisting() -> Result<(), Box<dyn Error>> {
96+
let client = Postgrest::new(REST_URL);
97+
let resp = client
98+
.from("users")
99+
.upsert(r#"{"username": "supabot3", "status": "ONLINE"}"#)
100+
.execute()
101+
.await?;
102+
let body = resp.text().await?;
103+
let body = json::parse(&body)?;
104+
105+
assert_eq!(body[0]["username"], "supabot3");
106+
assert_eq!(body[0]["status"], "ONLINE");
107+
108+
Ok(())
109+
}
110+
111+
#[tokio::test]
112+
async fn update() -> Result<(), Box<dyn Error>> {
113+
let client = Postgrest::new(REST_URL);
114+
let resp = client
115+
.from("users")
116+
.eq("status", "ONLINE")
117+
.update(r#"{"status": "ONLINE"}"#)
118+
.execute()
119+
.await?;
120+
let status = resp.status();
121+
let body = resp.text().await?;
122+
let body = json::parse(&body)?;
123+
124+
assert_eq!(status.as_u16(), 200);
125+
assert_eq!(body[0]["status"], "ONLINE");
126+
127+
Ok(())
128+
}
129+
130+
#[tokio::test]
131+
async fn delete() -> Result<(), Box<dyn Error>> {
132+
let client = Postgrest::new(REST_URL);
133+
let resp = client
134+
.from("messages")
135+
.neq("username", "supabot")
136+
.delete()
137+
.execute()
138+
.await?;
139+
let status = resp.status();
140+
141+
assert_eq!(status.as_u16(), 200);
142+
143+
Ok(())
144+
}
145+
146+
#[tokio::test]
147+
async fn rpc() -> Result<(), Box<dyn Error>> {
148+
let client = Postgrest::new(REST_URL);
149+
let resp = client
150+
.rpc("get_status", r#"{"name_param": "leroyjenkins"}"#)
151+
.execute()
152+
.await?;
153+
let body = resp.text().await?;
154+
let body = json::parse(&body)?;
155+
156+
assert!(body.is_null());
157+
158+
Ok(())
159+
}

0 commit comments

Comments
 (0)