Skip to content

Commit 0351291

Browse files
committed
Add PostgREST stack & some simple unit tests
1 parent 8df9fc4 commit 0351291

File tree

7 files changed

+148
-2
lines changed

7 files changed

+148
-2
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v2
2020

21+
- name: Build the stack
22+
run: docker-compose -f ./tests/db/docker-compose.yml up -d
23+
24+
# HACK: Maybe find a more reliable way to do this
25+
- name: Sleep for 30 seconds
26+
uses: jakejarvis/wait-action@master
27+
with:
28+
time: "30s"
29+
2130
- name: Set up toolchain
2231
uses: actions-rs/toolchain@v1
2332
with:

examples/test.rs renamed to examples/minimum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use postgrest::Postgrest;
22

33
#[tokio::main]
44
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5-
let client = Postgrest::new("https://hacks.soedirgo.dev/postgrest");
5+
let client = Postgrest::new("http://localhost:3000");
66
let resp = client.from("todos").select("*").execute().await?;
77
println!("{}", resp.text().await?);
88
Ok(())

src/builder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub struct Builder {
2121
url: String,
2222
schema: Option<String>,
2323
queries: Vec<(String, String)>,
24-
// TODO: Maybe change to HeaderMap in the future
2524
headers: HeaderMap,
2625
body: Option<String>,
2726
is_rpc: bool,

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,23 @@ impl Postgrest {
3030
Builder::new(&url, self.schema.clone()).rpc(params)
3131
}
3232
}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
use super::*;
37+
38+
const REST_URL: &str = "https://localhost:3000";
39+
40+
#[test]
41+
fn initialize() {
42+
assert_eq!(Postgrest::new(REST_URL).url, REST_URL);
43+
}
44+
45+
#[test]
46+
fn switch_schema() {
47+
assert_eq!(
48+
Postgrest::new(REST_URL).schema("private").schema,
49+
Some("private".to_string())
50+
);
51+
}
52+
}

tests/db/00-schema.sql

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
-- Create the Replication publication
3+
CREATE PUBLICATION supabase_realtime FOR ALL TABLES;
4+
5+
-- Create a second schema
6+
CREATE SCHEMA personal;
7+
8+
-- USERS
9+
CREATE TYPE public.user_status AS ENUM ('ONLINE', 'OFFLINE');
10+
CREATE TABLE public.users (
11+
username text primary key,
12+
inserted_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
13+
updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
14+
data jsonb DEFAULT null,
15+
age_range int4range DEFAULT null,
16+
status user_status DEFAULT 'ONLINE'::public.user_status,
17+
catchphrase tsvector DEFAULT null
18+
);
19+
ALTER TABLE public.users REPLICA IDENTITY FULL; -- Send "previous data" to supabase
20+
COMMENT ON COLUMN public.users.data IS 'For unstructured data and prototyping.';
21+
22+
-- CHANNELS
23+
CREATE TABLE public.channels (
24+
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
25+
inserted_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
26+
updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
27+
data jsonb DEFAULT null,
28+
slug text
29+
);
30+
ALTER TABLE public.users REPLICA IDENTITY FULL; -- Send "previous data" to supabase
31+
COMMENT ON COLUMN public.channels.data IS 'For unstructured data and prototyping.';
32+
33+
-- MESSAGES
34+
CREATE TABLE public.messages (
35+
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
36+
inserted_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
37+
updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
38+
data jsonb DEFAULT null,
39+
message text,
40+
username text REFERENCES users NOT NULL,
41+
channel_id bigint REFERENCES channels NOT NULL
42+
);
43+
ALTER TABLE public.messages REPLICA IDENTITY FULL; -- Send "previous data" to supabase
44+
COMMENT ON COLUMN public.messages.data IS 'For unstructured data and prototyping.';
45+
46+
-- STORED FUNCTION
47+
CREATE FUNCTION public.get_status(name_param text)
48+
RETURNS user_status AS $$
49+
SELECT status from users WHERE username=name_param;
50+
$$ LANGUAGE SQL IMMUTABLE;
51+
52+
-- SECOND SCHEMA USERS
53+
CREATE TYPE personal.user_status AS ENUM ('ONLINE', 'OFFLINE');
54+
CREATE TABLE personal.users(
55+
username text primary key,
56+
inserted_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
57+
updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
58+
data jsonb DEFAULT null,
59+
age_range int4range DEFAULT null,
60+
status user_status DEFAULT 'ONLINE'::public.user_status
61+
);
62+
63+
-- SECOND SCHEMA STORED FUNCTION
64+
CREATE FUNCTION personal.get_status(name_param text)
65+
RETURNS user_status AS $$
66+
SELECT status from users WHERE username=name_param;
67+
$$ LANGUAGE SQL IMMUTABLE;

tests/db/01-dummy-data.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
INSERT INTO
2+
public.users (username, status, age_range, catchphrase)
3+
VALUES
4+
('supabot', 'ONLINE', '[1,2)'::int4range, 'fat cat'::tsvector),
5+
('kiwicopple', 'OFFLINE', '[25,35)'::int4range, 'cat bat'::tsvector),
6+
('awailas', 'ONLINE', '[25,35)'::int4range, 'bat rat'::tsvector),
7+
('dragarcia', 'ONLINE', '[20,30)'::int4range, 'rat fat'::tsvector);
8+
9+
INSERT INTO
10+
public.channels (slug)
11+
VALUES
12+
('public'),
13+
('random');
14+
15+
INSERT INTO
16+
public.messages (message, channel_id, username)
17+
VALUES
18+
('Hello World 👋', 1, 'supabot'),
19+
('Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.', 2, 'supabot');
20+
21+
INSERT INTO
22+
personal.users (username, status, age_range)
23+
VALUES
24+
('supabot', 'ONLINE', '[1,2)'::int4range),
25+
('kiwicopple', 'OFFLINE', '[25,35)'::int4range),
26+
('awailas', 'ONLINE', '[25,35)'::int4range),
27+
('dragarcia', 'ONLINE', '[20,30)'::int4range),
28+
('leroyjenkins', 'ONLINE', '[20,40)'::int4range);

tests/db/docker-compose.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: "3"
2+
services:
3+
rest:
4+
image: postgrest/postgrest
5+
ports:
6+
- "3000:3000"
7+
environment:
8+
PGRST_DB_URI: postgres://postgres:postgres@db:5432/postgres
9+
PGRST_DB_SCHEMA: public, personal
10+
PGRST_DB_ANON_ROLE: postgres
11+
depends_on:
12+
- db
13+
db:
14+
image: postgres:12
15+
ports:
16+
- "5432:5432"
17+
volumes:
18+
- .:/docker-entrypoint-initdb.d/
19+
environment:
20+
POSTGRES_DB: postgres
21+
POSTGRES_USER: postgres
22+
POSTGRES_PASSWORD: postgres
23+
POSTGRES_PORT: 5432

0 commit comments

Comments
 (0)