Skip to content

Commit 7fcedd8

Browse files
committed
Add multi_schema tests
1 parent 3c84cd4 commit 7fcedd8

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

tests/multi_schema.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#[macro_use]
2+
extern crate json; // array!, object!, value!
3+
4+
use postgrest::Postgrest;
5+
6+
use std::error::Error;
7+
8+
const REST_URL: &str = "http://localhost:3000";
9+
10+
#[tokio::test]
11+
async fn read_other_schema() -> Result<(), Box<dyn Error>> {
12+
let client = Postgrest::new(REST_URL);
13+
let resp = client
14+
.from("users")
15+
.select("username")
16+
.eq("username", "leroyjenkins")
17+
.execute()
18+
.await?;
19+
let body = resp.text().await?;
20+
let body = json::parse(&body)?;
21+
22+
assert_eq!(body, array![]);
23+
24+
let other_client = Postgrest::new(REST_URL).schema("personal");
25+
let other_resp = other_client
26+
.from("users")
27+
.select("username")
28+
.eq("username", "leroyjenkins")
29+
.execute()
30+
.await?;
31+
let other_body = other_resp.text().await?;
32+
let other_body = json::parse(&other_body)?;
33+
34+
assert_eq!(other_body, array![{"username": "leroyjenkins"}]);
35+
36+
Ok(())
37+
}
38+
39+
#[tokio::test]
40+
async fn write_other_schema() -> Result<(), Box<dyn Error>> {
41+
let client = Postgrest::new(REST_URL);
42+
let resp = client
43+
.from("users")
44+
.select("status")
45+
.eq("username", "dragarcia")
46+
.execute()
47+
.await?;
48+
let body = resp.text().await?;
49+
let body = json::parse(&body)?;
50+
51+
assert_eq!(body[0]["status"], "ONLINE");
52+
53+
let other_client = Postgrest::new(REST_URL).schema("personal");
54+
let other_resp = other_client
55+
.from("users")
56+
.update("{\"status\": \"OFFLINE\"}")
57+
.eq("username", "dragarcia")
58+
.execute()
59+
.await?;
60+
let other_body = other_resp.text().await?;
61+
let other_body = json::parse(&other_body)?;
62+
63+
assert_eq!(other_body[0]["status"], "OFFLINE");
64+
65+
Ok(())
66+
}
67+
68+
#[tokio::test]
69+
async fn read_nonexisting_schema() -> Result<(), Box<dyn Error>> {
70+
let client = Postgrest::new(REST_URL).schema("private");
71+
let resp = client.from("channels").select("*").execute().await?;
72+
let body = resp.text().await?;
73+
let body = json::parse(&body)?;
74+
75+
assert_eq!(
76+
body["message"],
77+
"The schema must be one of the following: public, personal"
78+
);
79+
80+
Ok(())
81+
}
82+
83+
#[tokio::test]
84+
async fn write_nonexisting_schema() -> Result<(), Box<dyn Error>> {
85+
let client = Postgrest::new(REST_URL).schema("private");
86+
let resp = client
87+
.from("channels")
88+
.update("{\"slug\": \"private\"}")
89+
.eq("slug", "random")
90+
.execute()
91+
.await?;
92+
let body = resp.text().await?;
93+
let body = json::parse(&body)?;
94+
95+
assert_eq!(
96+
body["message"],
97+
"The schema must be one of the following: public, personal"
98+
);
99+
100+
Ok(())
101+
}
102+
103+
#[tokio::test]
104+
async fn other_schema_rpc() -> Result<(), Box<dyn Error>> {
105+
let client = Postgrest::new(REST_URL).schema("personal");
106+
let resp = client
107+
.rpc("get_status", "{\"name_param\": \"leroyjenkins\"}")
108+
.execute()
109+
.await?;
110+
let body = resp.text().await?;
111+
let body = json::parse(&body)?;
112+
113+
assert_eq!(body, "ONLINE");
114+
115+
Ok(())
116+
}
117+
118+
#[tokio::test]
119+
async fn nonexisting_rpc_in_schema() -> Result<(), Box<dyn Error>> {
120+
let client = Postgrest::new(REST_URL).schema("personal");
121+
let resp = client
122+
.rpc("nonexistent_procedure", "{\"param\": 0}")
123+
.execute()
124+
.await?;
125+
let body = resp.text().await?;
126+
let body = json::parse(&body)?;
127+
128+
assert_eq!(
129+
body["message"],
130+
"function personal.nonexistent_procedure(param => text) does not exist"
131+
);
132+
133+
Ok(())
134+
}
135+
136+
#[tokio::test]
137+
async fn nonexisting_schema_for_rpc() -> Result<(), Box<dyn Error>> {
138+
let client = Postgrest::new(REST_URL).schema("private");
139+
let resp = client
140+
.rpc("get_status", "{\"name_param\": \"leroyjenkins\"}")
141+
.execute()
142+
.await?;
143+
let body = resp.text().await?;
144+
let body = json::parse(&body)?;
145+
146+
assert_eq!(
147+
body["message"],
148+
"The schema must be one of the following: public, personal"
149+
);
150+
151+
Ok(())
152+
}

0 commit comments

Comments
 (0)