Skip to content

Commit 5afc984

Browse files
committed
Add serde versions of non-serde tests
1 parent 8061986 commit 5afc984

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

src/builder.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,19 @@ mod tests {
796796
HeaderValue::from_static("return=representation,resolution=merge-duplicates")
797797
);
798798
}
799+
800+
#[test]
801+
#[cfg(feature = "serde")]
802+
fn upsert_assert_prefer_header_serde() {
803+
let client = Client::new();
804+
let builder = Builder::new(TABLE_URL, None, HeaderMap::new(), client)
805+
.upsert(&())
806+
.unwrap();
807+
assert_eq!(
808+
builder.headers.get("Prefer").unwrap(),
809+
HeaderValue::from_static("return=representation,resolution=merge-duplicates")
810+
);
811+
}
799812

800813
#[test]
801814
fn not_rpc_should_not_have_flag() {

tests/client.rs

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ async fn insert() -> Result<(), Box<dyn Error>> {
9090
Ok(())
9191
}
9292

93+
#[tokio::test]
94+
#[cfg(feature = "serde")]
95+
async fn insert_serde() -> Result<(), Box<dyn Error>> {
96+
#[derive(serde::Serialize)]
97+
struct Message {
98+
message: String,
99+
channel_id: i32,
100+
username: String,
101+
}
102+
103+
let client = Postgrest::new(REST_URL);
104+
let resp = client
105+
.from("messages")
106+
.insert(&[Message {
107+
message: "Test message 1".to_string(),
108+
channel_id: 1,
109+
username: "kiwicopple".to_string(),
110+
}])?
111+
.execute()
112+
.await?;
113+
let status = resp.status();
114+
115+
assert_eq!(status.as_u16(), 201);
116+
117+
Ok(())
118+
}
119+
93120
#[tokio::test]
94121
#[cfg(not(feature = "serde"))]
95122
async fn upsert() -> Result<(), Box<dyn Error>> {
@@ -111,6 +138,39 @@ async fn upsert() -> Result<(), Box<dyn Error>> {
111138
Ok(())
112139
}
113140

141+
#[tokio::test]
142+
#[cfg(feature = "serde")]
143+
async fn upsert_serde() -> Result<(), Box<dyn Error>> {
144+
#[derive(serde::Serialize)]
145+
struct User {
146+
username: String,
147+
status: String,
148+
}
149+
150+
let client = Postgrest::new(REST_URL);
151+
let resp = client
152+
.from("users")
153+
.upsert(&[
154+
User {
155+
username: "dragarcia".to_string(),
156+
status: "OFFLINE".to_string(),
157+
},
158+
User {
159+
username: "supabot2".to_string(),
160+
status: "ONLINE".to_string(),
161+
},
162+
])?
163+
.execute()
164+
.await?;
165+
let body = resp.text().await?;
166+
let body = json::parse(&body)?;
167+
168+
assert_eq!(body[0]["username"], "dragarcia");
169+
assert_eq!(body[1]["username"], "supabot2");
170+
171+
Ok(())
172+
}
173+
114174
#[tokio::test]
115175
#[cfg(not(feature = "serde"))]
116176
async fn upsert_existing() -> Result<(), Box<dyn Error>> {
@@ -130,6 +190,34 @@ async fn upsert_existing() -> Result<(), Box<dyn Error>> {
130190
Ok(())
131191
}
132192

193+
#[tokio::test]
194+
#[cfg(feature = "serde")]
195+
async fn upsert_existing_serde() -> Result<(), Box<dyn Error>> {
196+
#[derive(serde::Serialize)]
197+
struct User {
198+
username: String,
199+
status: String,
200+
}
201+
202+
let client = Postgrest::new(REST_URL);
203+
let resp = client
204+
.from("users")
205+
.upsert(&User {
206+
username: "dragarcia".to_string(),
207+
status: "ONLINE".to_string(),
208+
})?
209+
.on_conflict("username")
210+
.execute()
211+
.await?;
212+
let body = resp.text().await?;
213+
let body = json::parse(&body)?;
214+
215+
assert_eq!(body[0]["username"], "dragarcia");
216+
assert_eq!(body[0]["status"], "ONLINE");
217+
218+
Ok(())
219+
}
220+
133221
#[tokio::test]
134222
#[cfg(not(feature = "serde"))]
135223
async fn upsert_nonexisting() -> Result<(), Box<dyn Error>> {
@@ -148,6 +236,33 @@ async fn upsert_nonexisting() -> Result<(), Box<dyn Error>> {
148236
Ok(())
149237
}
150238

239+
#[tokio::test]
240+
#[cfg(feature = "serde")]
241+
async fn upsert_nonexisting_serde() -> Result<(), Box<dyn Error>> {
242+
#[derive(serde::Serialize)]
243+
struct User {
244+
username: String,
245+
status: String,
246+
}
247+
248+
let client = Postgrest::new(REST_URL);
249+
let resp = client
250+
.from("users")
251+
.upsert(&User {
252+
username: "supabot3".to_string(),
253+
status: "ONLINE".to_string(),
254+
})?
255+
.execute()
256+
.await?;
257+
let body = resp.text().await?;
258+
let body = json::parse(&body)?;
259+
260+
assert_eq!(body[0]["username"], "supabot3");
261+
assert_eq!(body[0]["status"], "ONLINE");
262+
263+
Ok(())
264+
}
265+
151266
#[tokio::test]
152267
#[cfg(not(feature = "serde"))]
153268
async fn update() -> Result<(), Box<dyn Error>> {
@@ -168,6 +283,33 @@ async fn update() -> Result<(), Box<dyn Error>> {
168283
Ok(())
169284
}
170285

286+
#[tokio::test]
287+
#[cfg(feature = "serde")]
288+
async fn update_serde() -> Result<(), Box<dyn Error>> {
289+
#[derive(serde::Serialize)]
290+
struct User {
291+
status: String,
292+
}
293+
294+
let client = Postgrest::new(REST_URL);
295+
let resp = client
296+
.from("users")
297+
.eq("status", "ONLINE")
298+
.update(&User {
299+
status: "ONLINE".to_string(),
300+
})?
301+
.execute()
302+
.await?;
303+
let status = resp.status();
304+
let body = resp.text().await?;
305+
let body = json::parse(&body)?;
306+
307+
assert_eq!(status.as_u16(), 200);
308+
assert_eq!(body[0]["status"], "ONLINE");
309+
310+
Ok(())
311+
}
312+
171313
#[tokio::test]
172314
async fn delete() -> Result<(), Box<dyn Error>> {
173315
let client = Postgrest::new(REST_URL);

tests/multi_schema.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,43 @@ async fn write_other_schema() -> Result<(), Box<dyn Error>> {
6666
Ok(())
6767
}
6868

69+
#[tokio::test]
70+
#[cfg(feature = "serde")]
71+
async fn write_other_schema_serde() -> Result<(), Box<dyn Error>> {
72+
#[derive(serde::Serialize)]
73+
struct User {
74+
status: String,
75+
}
76+
77+
let client = Postgrest::new(REST_URL);
78+
let resp = client
79+
.from("users")
80+
.select("status")
81+
.eq("username", "supabot")
82+
.execute()
83+
.await?;
84+
let body = resp.text().await?;
85+
let body = json::parse(&body)?;
86+
87+
assert_eq!(body[0]["status"], "ONLINE");
88+
89+
let other_client = Postgrest::new(REST_URL).schema("personal");
90+
let other_resp = other_client
91+
.from("users")
92+
.update(&User {
93+
status: "OFFLINE".to_string(),
94+
})?
95+
.eq("username", "supabot")
96+
.execute()
97+
.await?;
98+
let other_body = other_resp.text().await?;
99+
let other_body = json::parse(&other_body)?;
100+
101+
assert_eq!(other_body[0]["status"], "OFFLINE");
102+
103+
Ok(())
104+
}
105+
69106
#[tokio::test]
70107
async fn read_nonexisting_schema() -> Result<(), Box<dyn Error>> {
71108
let client = Postgrest::new(REST_URL).schema("private");
@@ -102,6 +139,34 @@ async fn write_nonexisting_schema() -> Result<(), Box<dyn Error>> {
102139
Ok(())
103140
}
104141

142+
#[tokio::test]
143+
#[cfg(feature = "serde")]
144+
async fn write_nonexisting_schema_serde() -> Result<(), Box<dyn Error>> {
145+
#[derive(serde::Serialize)]
146+
struct Channel {
147+
slug: String,
148+
}
149+
150+
let client = Postgrest::new(REST_URL).schema("private");
151+
let resp = client
152+
.from("channels")
153+
.update(&Channel {
154+
slug: "private".to_string(),
155+
})?
156+
.eq("slug", "random")
157+
.execute()
158+
.await?;
159+
let body = resp.text().await?;
160+
let body = json::parse(&body)?;
161+
162+
assert_eq!(
163+
body["message"],
164+
"The schema must be one of the following: public, personal"
165+
);
166+
167+
Ok(())
168+
}
169+
105170
#[tokio::test]
106171
async fn other_schema_rpc() -> Result<(), Box<dyn Error>> {
107172
let client = Postgrest::new(REST_URL).schema("personal");

0 commit comments

Comments
 (0)