Skip to content

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

Moves the integration tests to their own folder, set up unit tests, and resolves rpc issues in #27

Paul Copplestonee5646897cb5f
diff --git a/libraries/supabase-js/package.json b/libraries/supabase-js/package.json
index efc059d8356..80884aa681e 100644
--- a/libraries/supabase-js/package.json
+++ b/libraries/supabase-js/package.json
@@ -5,13 +5,13 @@
"main": "./lib/index.js",
"scripts": {
"clean": "rimraf lib",
- "test": "npm run cover",
- "test:integration": "docker-compose -f ./test/docker-compose.yml up -d && sleep 10 && mocha --require babel-core/register --require babel-polyfill test/integration/**/*.js ; docker-compose -f ./test/docker-compose.yml down",
+ "test": "mocha --require babel-core/register --require babel-polyfill test/unit/**/*.js",
+ "test:integration:only": "mocha --require babel-core/register --require babel-polyfill test/integration/**/*.js",
+ "test:integration": "docker-compose -f ./test/integration/docker-compose.yml up -d && sleep 10 && mocha --require babel-core/register --require babel-polyfill test/integration/**/*.js ; docker-compose -f ./test/integration/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",
"test:examples": "node examples/",
- "cover": "nyc --check-coverage npm run test:only",
+ "cover": "nyc --check-coverage && npm test",
"build": "cross-env BABEL_ENV=production babel src --out-dir lib",
"prepublish": "npm run clean && npm run build",
"deploy": "npm version patch && npm publish --access=public"
@@ -47,6 +47,7 @@
"rimraf": "^2.6.2"
},
"dependencies": {
+ "@supabase/doctest-js": "^0.1.0",
"@supabase/postgrest-js": "^0.8.0",
"@supabase/realtime-js": "^0.1.6",
"superagent": "^5.2.1"
diff --git a/libraries/supabase-js/src/index.js b/libraries/supabase-js/src/index.js
index 2af76b6f012..7f1039abe53 100644
--- a/libraries/supabase-js/src/index.js
+++ b/libraries/supabase-js/src/index.js
@@ -36,7 +36,7 @@ class SupabaseClient {
}
rpc(functionName, functionParameters = null) {
- let rest = new PostgrestClient(this.restUrl, { apikey: this.supabaseKey })
+ let rest = new PostgrestClient(this.restUrl, { queryParams: { apikey: this.supabaseKey } })
return rest.rpc(functionName, functionParameters)
}
diff --git a/libraries/supabase-js/src/utils/ChangeMapper.js b/libraries/supabase-js/src/utils/ChangeMapper.js
index 40c33a9eab9..a0012301908 100644
--- a/libraries/supabase-js/src/utils/ChangeMapper.js
+++ b/libraries/supabase-js/src/utils/ChangeMapper.js
@@ -9,10 +9,8 @@
* @param {Object} options The map of various options that can be applied to the mapper
* @param {Array} options.skipTypes The array of types that should not be converted
*
- * @example
- * convertChangeData([{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], {first_name: 'Paul', age:'33'}, {})
- * //=>
- * { first_name: 'Paul', age: 33 }
+ * @example convertChangeData([{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], {first_name: 'Paul', age:'33'}, {})
+ * //=>{ first_name: 'Paul', age: 33 }
*/
export const convertChangeData = (columns, records, options = {}) => {
let result = {}
@@ -31,15 +29,10 @@ export const convertChangeData = (columns, records, options = {}) => {
* @param {Array} skipTypes An array of types that should not be converted
* @return {object} Useless information
*
- * @example
- * convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], [])
- * //=>
- * 33
- *
- * @example
- * convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], ['int4'])
- * //=>
- * "33"
+ * @example convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], [])
+ * //=> 33
+ * @example convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], ['int4'])
+ * //=> "33"
*/
export const convertColumn = (columnName, columns, records, skipTypes) => {
let column = columns.find(x => x.name == columnName)
@@ -53,20 +46,12 @@ export const convertColumn = (columnName, columns, records, skipTypes) => {
* @param {String} type A postgres column type
* @param {String} stringValue The cell value
*
- * @example
- * convertCell('bool', 'true')
- * //=>
- * true
- *
- * @example
- * convertCell('int8', '10')
- * //=>
- * 10
- *
- * @example
- * convertCell('_int4', '{1,2,3,4}')
- * //=>
- * [1,2,3,4]
+ * @example convertCell('bool', 'true')
+ * //=> true
+ * @example convertCell('int8', '10')
+ * //=> 10
+ * @example convertCell('_int4', '{1,2,3,4}')
+ * //=> [1,2,3,4]
*/
export const convertCell = (type, stringValue) => {
try {
@@ -137,13 +122,10 @@ export const convertCell = (type, stringValue) => {
}
}
-/**
- * Passes back the raw string value
- */
-const noop = stringValue => {
+export const noop = stringValue => {
return stringValue
}
-const toBoolean = stringValue => {
+export const toBoolean = stringValue => {
switch (stringValue) {
case 't':
return true
@@ -153,33 +135,36 @@ const toBoolean = stringValue => {
return null
}
}
-const toDate = stringValue => {
+export const toDate = stringValue => {
return new Date(stringValue)
}
-const toDateRange = stringValue => {
+export const toDateRange = stringValue => {
let arr = JSON.parse(stringValue)
return [new Date(arr[0]), new Date(arr[1])]
}
-const toFloat = stringValue => {
+export const toFloat = stringValue => {
return parseFloat(stringValue)
}
-const toInt = stringValue => {
+export const toInt = stringValue => {
return parseInt(stringValue)
}
-const toIntRange = stringValue => {
+export const toIntRange = stringValue => {
let arr = JSON.parse(stringValue)
return [parseInt(arr[0]), parseInt(arr[1])]
}
-const toJson = stringValue => {
+export const toJson = stringValue => {
return JSON.parse(stringValue)
}
-const toArray = (stringValue, type) => {
+/**
+ * Converts a Postgres Array into a native JS array
+ * @example toArray('{1,2,3,4}', 'int4')
+ * //=> [1,2,3,4]
+ */
+export const toArray = (stringValue, type) => {
// this takes off the '{' & '}'
let stringEnriched = stringValue.slice(1, stringValue.length - 1)
- console.log('this is the string Enriched ', stringEnriched)
-
// converts the string into an array
let stringArray = stringEnriched.split(',')
let array = stringArray.map(string => {
@@ -193,12 +178,9 @@ const toArray = (stringValue, type) => {
* Fixes timestamp to be ISO-8601. Swaps the space between the date and time for a 'T'
* See https://github.com/supabase/supabase/issues/18
* @returns {string}
- *
- * @example
- * toTimestampString('2019-09-10 00:00:00')
- * //=>
- * '2019-09-10T00:00:00'
+ * @example toTimestampString('2019-09-10 00:00:00')
+ * //=> '2019-09-10T00:00:00'
*/
-const toTimestampString = stringValue => {
+export const toTimestampString = stringValue => {
return stringValue.replace(' ', 'T')
}
\ No newline at end of file
diff --git a/libraries/supabase-js/test/index.js b/libraries/supabase-js/test/index.js
deleted file mode 100644
index 744ba68daf9..00000000000
--- a/libraries/supabase-js/test/index.js
+++ /dev/null
@@ -1,14 +0,0 @@
-import { assert } from 'chai';
-import defaultAwesomeFunction, { awesomeFunction } from '../src';
-
-describe('Awesome test.', () => {
- it('should test default awesome function', () => {
- const expectedVal = 'I am the Default Awesome Function, fellow comrade! - Dinesh';
- assert(defaultAwesomeFunction('Dinesh') === expectedVal, 'Default not awesome :(');
- });
-
- it('should test awesome function', () => {
- const expectedVal = 'I am just an Awesome Function';
- assert(awesomeFunction() === expectedVal, 'Named awesome :(');
- });
-});
diff --git a/libraries/supabase-js/test/db/00-schema.sql b/libraries/supabase-js/test/integration/db/00-schema.sql
similarity index 83%
rename from libraries/supabase-js/test/db/00-schema.sql
rename to libraries/supabase-js/test/integration/db/00-schema.sql
index c4b0b9a8406..fb13269db95 100644
--- a/libraries/supabase-js/test/db/00-schema.sql
+++ b/libraries/supabase-js/test/integration/db/00-schema.sql
@@ -39,3 +39,19 @@ CREATE TABLE public.messages (
ALTER TABLE public.messages REPLICA IDENTITY FULL; -- Send "previous data" to supabase
COMMENT ON COLUMN public.messages.data IS 'For unstructured data and prototyping.';
+CREATE OR REPLACE FUNCTION public.update_user_status(user_id bigint, new_user_status text)
+RETURNS jsonb AS $$
+BEGIN
+ UPDATE users SET status = new_user_status::public.user_status
+ WHERE id = user_id;
+ return json_build_object(
+ 'result',
+ true,
+ 'user_id',
+ user_id,
+ 'status',
+ new_user_status
+ );
+END;
+$$ LANGUAGE plpgsql;
+
diff --git a/libraries/supabase-js/test/db/01-dummy-data.sql b/libraries/supabase-js/test/integration/db/01-dummy-data.sql
similarity index 100%
rename from libraries/supabase-js/test/db/01-dummy-data.sql
rename to libraries/supabase-js/test/integration/db/01-dummy-data.sql
diff --git a/libraries/supabase-js/test/docker-compose.yml b/libraries/supabase-js/test/integration/docker-compose.yml
similarity index 100%
rename from libraries/supabase-js/test/docker-compose.yml
rename to libraries/supabase-js/test/integration/docker-compose.yml
diff --git a/libraries/supabase-js/test/integration/testRest.js b/libraries/supabase-js/test/integration/testRest.js
index 45930f52024..203cbf3e617 100644
--- a/libraries/supabase-js/test/integration/testRest.js
+++ b/libraries/supabase-js/test/integration/testRest.js
@@ -150,3 +150,17 @@ describe('test reading from the rest interface', () => {
assert(response.body.length === 3, 'should be all except id 3')
})
})
+
+
+describe('test rpc()', () => {
+ const supabase = createClient('http://localhost:8000', 'examplekey')
+
+ it('should return a value', async () => {
+ const response = await supabase.rpc('update_user_status', {
+ user_id: 2,
+ new_user_status: 'OFFLINE',
+ })
+ assert(response.body.result === true)
+ assert(response.body.status === 'OFFLINE')
+ })
+})
diff --git a/libraries/supabase-js/test/unit/index.js b/libraries/supabase-js/test/unit/index.js
new file mode 100644
index 00000000000..ab6e627c040
--- /dev/null
+++ b/libraries/supabase-js/test/unit/index.js
@@ -0,0 +1,8 @@
+import doctest from '@supabase/doctest-js'
+const { createClient } = require('../../src/index.js')
+
+describe('Doctests', () => {
+ // file paths are relative to root of directory
+ doctest('src/utils/ChangeMapper.js')
+ doctest('src/index.js', { instance: new createClient('https://test.supabase.co', 'abc-def') })
+})
\ No newline at end of file