Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

started adding tests for rest and realtime

awaliasa6be88ca5cca
diff --git a/libraries/supabase-js/package.json b/libraries/supabase-js/package.json
index fd4c5c9bf68..76d9fbab221 100644
--- a/libraries/supabase-js/package.json
+++ b/libraries/supabase-js/package.json
@@ -6,6 +6,7 @@
"scripts": {
"clean": "rimraf lib",
"test": "npm run cover",
+ "test:integration": "docker-compose -f ./test/docker-compose.yml up -d && sleep 2 && mocha --require babel-core/register --require babel-polyfill test/integration/**/*.js ; docker-compose -f ./test/docker-compose.yml down",
"test:prod": "cross-env BABEL_ENV=production npm run test",
"test:only": "mocha --require babel-core/register --require babel-polyfill --recursive",
"test:watch": "npm test -- --watch",
@@ -44,6 +45,7 @@
"babel-preset-minify": "^0.3.0",
"chai": "^4.1.2",
"cross-env": "^5.2.1",
+ "jest-websocket-mock": "^2.0.1",
"mocha": "^6.1.3",
"nyc": "^13.3.0",
"rimraf": "^2.6.2"
diff --git a/libraries/supabase-js/src/Supabase.js b/libraries/supabase-js/src/Supabase.js
index 5ceee4f874c..857b7d493d7 100644
--- a/libraries/supabase-js/src/Supabase.js
+++ b/libraries/supabase-js/src/Supabase.js
@@ -48,10 +48,10 @@ class Supabase {
this.channel = this.socket.channel(channel)
this.socket.onOpen(() => {
- console.log('REALTIME CONNECTED')
+ console.debug(`${this.realtimeUrl}: REALTIME CONNECTED`)
})
this.socket.onClose(() => {
- console.log('REALTIME DISCONNECTED')
+ console.debug(`${this.realtimeUrl}: REALTIME DISCONNECTED`)
})
}
@@ -121,9 +121,9 @@ class Supabase {
if (this.channel.state !== 'joined') {
this.channel
.join()
- .receive('ok', resp => console.log('Joined Realtime successfully ', resp))
- .receive('error', resp => console.log('Unable to join ', resp))
- .receive('timeout', () => console.log('Networking issue. Still waiting...'))
+ .receive('ok', resp => console.debug(`${this.realtimeUrl}: Joined Realtime successfully `, resp))
+ .receive('error', resp => console.debug(`${this.realtimeUrl}: Unable to join `, resp))
+ .receive('timeout', () => console.debug(`${this.realtimeUrl}: Network timeout. Still waiting...`))
}
return this
diff --git a/libraries/supabase-js/test/db/00-schema.sql b/libraries/supabase-js/test/db/00-schema.sql
new file mode 100644
index 00000000000..c4b0b9a8406
--- /dev/null
+++ b/libraries/supabase-js/test/db/00-schema.sql
@@ -0,0 +1,41 @@
+
+-- Create the Replication publication
+CREATE PUBLICATION supabase_realtime FOR ALL TABLES;
+
+-- USERS
+CREATE TYPE public.user_status AS ENUM ('ONLINE', 'OFFLINE');
+CREATE TABLE public.users (
+ id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ inserted_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ data jsonb DEFAULT null,
+ username text,
+ status user_status DEFAULT 'ONLINE'::public.user_status
+);
+ALTER TABLE public.users REPLICA IDENTITY FULL; -- Send "previous data" to supabase
+COMMENT ON COLUMN public.users.data IS 'For unstructured data and prototyping.';
+
+-- CHANNELS
+CREATE TABLE public.channels (
+ id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ inserted_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ data jsonb DEFAULT null,
+ slug text
+);
+ALTER TABLE public.users REPLICA IDENTITY FULL; -- Send "previous data" to supabase
+COMMENT ON COLUMN public.channels.data IS 'For unstructured data and prototyping.';
+
+-- MESSAGES
+CREATE TABLE public.messages (
+ id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
+ inserted_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
+ data jsonb DEFAULT null,
+ message text,
+ user_id bigint REFERENCES users NOT NULL,
+ channel_id bigint REFERENCES channels NOT NULL
+);
+ALTER TABLE public.messages REPLICA IDENTITY FULL; -- Send "previous data" to supabase
+COMMENT ON COLUMN public.messages.data IS 'For unstructured data and prototyping.';
+
diff --git a/libraries/supabase-js/test/db/01-dummy-data.sql b/libraries/supabase-js/test/db/01-dummy-data.sql
new file mode 100644
index 00000000000..8d33f17700c
--- /dev/null
+++ b/libraries/supabase-js/test/db/01-dummy-data.sql
@@ -0,0 +1,19 @@
+INSERT INTO
+ public.users (username)
+VALUES
+ ('supabot'),
+ ('kiwicopple'),
+ ('awalias'),
+ ('dragarcia');
+
+INSERT INTO
+ public.channels (slug)
+VALUES
+ ('public'),
+ ('random');
+
+INSERT INTO
+ public.messages (message, channel_id, user_id)
+VALUES
+ ('Hello World 👋', 1, 1),
+ ('Perfection is attained, not when there is nothing more to add, but when there is nothing left to take away.', 2, 1);
\ No newline at end of file
diff --git a/libraries/supabase-js/test/docker-compose.yml b/libraries/supabase-js/test/docker-compose.yml
new file mode 100644
index 00000000000..fca3652225d
--- /dev/null
+++ b/libraries/supabase-js/test/docker-compose.yml
@@ -0,0 +1,31 @@
+version: '3'
+
+services:
+ supabase:
+ image: supabase/supabase-dev:0.1.4
+ ports:
+ - "8000:8000"
+ environment:
+ DB_HOST: db
+ DB_NAME: postgres
+ DB_USER: postgres
+ DB_PASSWORD: postgres
+ DB_PORT: 5432
+ DB_SCHEMA: public
+ depends_on:
+ - db
+ db:
+ image: postgres:12
+ ports:
+ - "5432:5432"
+ volumes:
+ - ./db:/docker-entrypoint-initdb.d/
+ command:
+ - "postgres"
+ - "-c"
+ - "wal_level=logical"
+ environment:
+ POSTGRES_DB: postgres
+ POSTGRES_USER: postgres
+ POSTGRES_PASSWORD: postgres
+ POSTGRES_PORT: 5432
diff --git a/libraries/supabase-js/test/integration/testRealtime.js b/libraries/supabase-js/test/integration/testRealtime.js
new file mode 100644
index 00000000000..91ce4208f14
--- /dev/null
+++ b/libraries/supabase-js/test/integration/testRealtime.js
@@ -0,0 +1,32 @@
+import { assert } from 'chai'
+import { createClient } from '../../src'
+
+// on()
+// subscribe()
+// unsubscribe()
+// removeSubscription()
+// getSubscriptions()
+describe('test subscribing to an insert', () => {
+ const supabase = createClient('http://localhost:8000', 'examplekey')
+
+ afterEach(function() {
+ for (const key in supabase.subscriptions) {
+ console.log(`removing: ${key}`)
+ console.log(supabase.subscriptions[key])
+ supabase.removeSubscription(supabase.subscriptions[key])
+ }
+ })
+
+ it('on() and subscribe()', async () => {
+ function callbackAction(record) {
+ assert(record.new.message === 'hello', 'insert made')
+ }
+ const mySubscription = await supabase
+ .from('messages')
+ .on('*', callbackAction)
+ .subscribe()
+ const response = await supabase
+ .from('messages')
+ .insert([{ message: 'hello', user_id: 1, channel_id: 1 }])
+ }).timeout(10000)
+})
diff --git a/libraries/supabase-js/test/integration/testRest.js b/libraries/supabase-js/test/integration/testRest.js
new file mode 100644
index 00000000000..4c34dffe424
--- /dev/null
+++ b/libraries/supabase-js/test/integration/testRest.js
@@ -0,0 +1,152 @@
+import { assert } from 'chai'
+import { createClient } from '../../src'
+
+// from()
+// select()
+// order()
+// range()
+// single()
+// filter()
+// match()
+// gt()
+// lt()
+// gte()
+// lte()
+// like()
+// ilike()
+// is()
+// in()
+// not()
+describe('test reading from the rest interface', () => {
+ const supabase = createClient('http://localhost:8000', 'examplekey')
+
+ it('from() and select()', async () => {
+ const response = await supabase.from('users').select()
+ assert(response.body[3].status === 'ONLINE', 'should return 4 records with all columns')
+ })
+
+ it('order()', async () => {
+ const response = await supabase
+ .from('users')
+ .select('*')
+ .order('id', false, false)
+ assert(response.body[0].id === 4, 'id should equal 4, since order is descending')
+ })
+
+ it('range()', async () => {
+ const response = await supabase
+ .from('users')
+ .select('*')
+ .range(0, 3)
+ assert(response.body[3].id === 4, 'last item should be id 4')
+ assert(response.body[0].id === 1, 'first item should be id 1')
+ })
+
+ it('single()', async () => {
+ const response = await supabase
+ .from('users')
+ .eq('id', '3')
+ .select('*')
+ .single()
+ assert(response.body.id === 3, 'single should return one result as an object not an array')
+ })
+
+ it('filter()', async () => {
+ const response = await supabase
+ .from('users')
+ .filter('id', 'lt', 3)
+ .filter('username', 'in', '["awalias","supabot"]')
+ .select('*')
+ assert(response.body.length === 2, 'should return two records, awalias, and supabot')
+ })
+
+ it('match()', async () => {
+ const response = await supabase
+ .from('users')
+ .match({ username: 'awalias' })
+ .select('*')
+ assert(response.body.length === 1, 'should return one record')
+ assert(response.body[0].username == 'awalias', 'should return one record, awalias')
+ })
+
+ it('eq()', async () => {
+ const response = await supabase
+ .from('users')
+ .eq('username', 'supabot')
+ .select('id')
+ assert(response.body[0].id === 1, 'id should equal 1')
+ })
+
+ it('lt()', async () => {
+ const response = await supabase
+ .from('users')
+ .lt('id', '3')
+ .select('*')
+ assert(response.body.length === 2, 'should be 2 members with id less than 3')
+ })
+
+ it('lte()', async () => {
+ const response = await supabase
+ .from('users')
+ .lte('id', '3')
+ .select('*')
+ assert(response.body.length === 3, 'should be 3 members with id less than or equal to 3')
+ })
+
+ it('gt()', async () => {
+ const response = await supabase
+ .from('users')
+ .gt('id', '3')
+ .select('*')
+ assert(response.body.length === 1, 'should be 1 members with id greater than 3')
+ })
+
+ it('gte()', async () => {
+ const response = await supabase
+ .from('users')
+ .gte('id', '3')
+ .select('*')
+ assert(response.body.length === 2, 'should be 2 members with id equal to or greater than 3')
+ })
+
+ it('like()', async () => {
+ const response = await supabase
+ .from('users')
+ .like('username', '%upAbo%')
+ .select('*')
+ assert(response.body.length === 0, 'should be 2 members with id equal to or greater than 3')
+ })
+
+ it('ilike()', async () => {
+ const response = await supabase
+ .from('users')
+ .ilike('username', '%upabo%')
+ .select('*')
+ assert(response.body.length === 1, 'should be 2 members with id equal to or greater than 3')
+ })
+
+ it('is()', async () => {
+ const response = await supabase
+ .from('users')
+ .is('data', null)
+ .select('*')
+ assert(response.body.length === 4, 'should be 4 members')
+ })
+
+ it('in()', async () => {
+ const response = await supabase
+ .from('users')
+ .in('id', "['2', '3']")
+ .select('*')
+ assert(response.body[0].id === 1, 'first item should be id 1')
+ assert(response.body[1].id === 4, 'last (and second) item should be id 4')
+ })
+
+ it('not()', async () => {
+ const response = await supabase
+ .from('users')
+ .not('id', '3')
+ .select('*')
+ assert(response.body.length === 3, 'should be all except id 3')
+ })
+})
diff --git a/web/docs/common/CommonFilters.mdx b/web/docs/common/CommonFilters.mdx
index 1a62ce3c46c..b79d8d5c413 100644
--- a/web/docs/common/CommonFilters.mdx
+++ b/web/docs/common/CommonFilters.mdx
@@ -6,7 +6,7 @@ import Collapsable from '../../src/components/Collapsable'
<div style={{display: props.verb == 'get' ? 'block' : 'none'}}>
<!-- prettier-ignore -->
-```js {3}
+```js {3}
supabase
.get(tableName)
.match(filterObject)
@@ -16,7 +16,7 @@ supabase
<div style={{display: props.verb == 'patch' ? 'block' : 'none'}}>
<!-- prettier-ignore -->
-```js {3}
+```js {3}
supabase
.patch(tableName, data)
.match(filterObject)
@@ -26,7 +26,7 @@ supabase
<div style={{display: props.verb == 'delete' ? 'block' : 'none'}}>
<!-- prettier-ignore -->
-```js {3}
+```js {3}
supabase
.delete(tableName)
.match(filterObject)
diff --git a/web/docs/common/filters/in.mdx b/web/docs/common/filters/in.mdx
index 15791d74b59..f5f2d8b1cdd 100644
--- a/web/docs/common/filters/in.mdx
+++ b/web/docs/common/filters/in.mdx
@@ -1,7 +1,7 @@
import Collapsable from '../../../src/components/Collapsable'
<!-- prettier-ignore -->
-```js {3}
+```js {3}
supabase
.from(tableName)
.in(columnName, filterArray)