Permalink
Browse files

Fix POST on rows resource and tests

  • Loading branch information...
bjornharrtell committed Jul 11, 2012
1 parent 8dfb7b9 commit 5fba7f7fb41624998e621976b37ea543f497ff08
View
@@ -3,7 +3,5 @@ node_js:
- 0.6
- 0.8
before_script:
- - psql -c 'CREATE DATABASE test;' -U postgres
- - psql -c 'CREATE SCHEMA test;' -U postgres
- - psql -c 'CREATE TABLE test (id SERIAL PRIMARY KEY, name VARCHAR);' -U postgres
+ - psql -f ./test/sql/init.sql -U postgres
- ./bin/postgresql-http-server --user postgres --database test &
@@ -27,7 +27,7 @@ module.exports = (server) ->
db.query config
app.post path, (req, res) ->
- parsedRow = parseRow req.body
+ parsedRow = db.parseRow req.body
table = parseTable req
sql = "INSERT INTO #{table} (#{parsedRow.fields}) VALUES (#{parsedRow.params}) RETURNING id"
View
@@ -7,7 +7,7 @@
"bin": "bin/postgresql-http-server",
"main": "lib/cli.coffee",
"scripts": {
- "test": "mocha --require coffee-script --reporter dot test/**/*"
+ "test": "mocha --recursive --require coffee-script --reporter dot test/specs/**/*"
},
"preferGlobal": "true",
"repository": {
View
@@ -0,0 +1,4 @@
+#!/bin/sh
+# This script is intended to run the tests locally
+psql -f ./test/sql/init.sql -d test
+npm test
@@ -1,24 +1,15 @@
-http = require 'http'
assert = require 'assert'
+test = require('../utils').test
+
describe 'Root resource', ->
it 'should answer a GET request with an object created from db instance info', (done) ->
-
- options =
- host: 'localhost',
- port: 3000
- path: '/',
+ test
+ path: '/'
method: 'GET'
-
- req = http.request options, (res) ->
- res.on 'data', (chunk) ->
- data = JSON.parse chunk
-
+ callback: (res, data) ->
assert data.version is null, 'version should be string'
assert typeof data.version_human is 'string', 'human should be string'
assert typeof data.description is 'string', 'description should be string'
assert data.children[0] is 'db', "should have child db"
-
done()
-
- req.end()
@@ -0,0 +1,32 @@
+assert = require 'assert'
+
+test = require('../utils').test
+
+describe 'Rows resource', ->
+ it 'should answer first GET with empty recordset', (done) ->
+ test
+ path: '/db/test/schemas/testschema/tables/testtable/rows'
+ method: 'GET'
+ callback: (res, data) ->
+ assert data.length is 0, '#{data.length} should be 0'
+ done()
+
+ it 'should answer a POST with status 2001', (done) ->
+ test
+ path: '/db/test/schemas/testschema/tables/testtable/rows',
+ method: 'POST'
+ headers:
+ 'Content-Type': 'application/json'
+ body:
+ name: 'boo'
+ callback: (res, data) ->
+ assert res.statusCode is 201, "#{res.statusCode} should 200"
+ done()
+
+ it 'should answer second GET with single record in recordset', (done) ->
+ test
+ path: '/db/test/schemas/testschema/tables/testtable/rows'
+ method: 'GET'
+ callback: (res, data) ->
+ assert data.length is 1, '#{data.length} should be 0'
+ done()
View
@@ -0,0 +1,11 @@
+http = require 'http'
+
+exports.test = (options) ->
+ options.host = 'localhost'
+ options.port = 3000
+ req = http.request options, (res) ->
+ res.on 'data', (chunk) ->
+ options.callback res, JSON.parse chunk
+ if options.body?
+ req.write JSON.stringify options.body
+ req.end()
View
@@ -0,0 +1,3 @@
+DROP SCHEMA IF EXISTS testschema CASCADE;
+CREATE SCHEMA testschema;
+CREATE TABLE testschema.testtable (id SERIAL PRIMARY KEY, name varchar);

0 comments on commit 5fba7f7

Please sign in to comment.