Skip to content

Commit

Permalink
Merge be74a5d into 8753847
Browse files Browse the repository at this point in the history
  • Loading branch information
limbo committed Apr 8, 2015
2 parents 8753847 + be74a5d commit dfb4ce6
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 41 deletions.
18 changes: 16 additions & 2 deletions src/add-tests.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
async = require 'async'
_ = require 'underscore'
csonschema = require 'csonschema'

Test = require './test'

parseSchema = (source) ->
if source.contains('$schema')
#jsonschema
# @response.schema = JSON.parse @response.schema
JSON.parse source
else
csonschema.parse source
# @response.schema = csonschema.parse @response.schema

parseHeaders = (raml) ->
return {} unless raml

Expand Down Expand Up @@ -68,8 +78,12 @@ addTests = (raml, tests, parent, callback) ->

# Update test.response
test.response.status = status
test.response.schema = res?.body?['application/json']?.schema

test.response.schema = []
if (raml.schemas)
test.response.schema = raml.schemas
if (res?.body?['application/json']?.schema)
test.response.schema.push parseSchema res.body['application/json'].schema

callback()
, (err) ->
return callback(err) if err
Expand Down
12 changes: 1 addition & 11 deletions src/test.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
chai = require 'chai'
csonschema = require 'csonschema'
request = require 'request'
_ = require 'underscore'
async = require 'async'
Expand Down Expand Up @@ -57,15 +56,6 @@ class Test
callback()
], callback

parseSchema: (source) =>
if source.contains('$schema')
#jsonschema
# @response.schema = JSON.parse @response.schema
JSON.parse source
else
csonschema.parse source
# @response.schema = csonschema.parse @response.schema

assertResponse: (error, response, body) =>
assert.isNull error
assert.isNotNull response, 'Response'
Expand All @@ -79,7 +69,7 @@ class Test

# Body
if @response.schema
schema = @parseSchema @response.schema
schema = @response.schema
validateJson = _.partial JSON.parse, body
body = '[empty]' if body is ''
assert.doesNotThrow validateJson, JSON.SyntaxError, """
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/inline_and_included_schemas.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#%RAML 0.8

title: World Music API
baseUri: http://example.api.com/{version}
version: v1

schemas:
- type1: !include ./type1.json
- type2: !include ./type2.json

/machines:
get:
responses:
200:
body:
application/json:
schema: |
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"properties": {
"type": {"$ref": "type2"},
"name": "string"
}
}
17 changes: 17 additions & 0 deletions test/fixtures/ref_other_schemas.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#%RAML 0.8

title: World Music API
baseUri: http://example.api.com/{version}
version: v1

schemas:
- type1: !include ./type1.json
- type2: !include ./type2.json

/machines:
get:
responses:
200:
body:
application/json:
schema: type2
10 changes: 10 additions & 0 deletions test/fixtures/type1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-03/schema",
"type": "object",
"description": "A type",
"properties": {
"type": "string",
"name": "string",
"chick": { "type": "string"}
}
}
9 changes: 9 additions & 0 deletions test/fixtures/type2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-03/schema",
"type": "object",
"description": "Another type",
"properties": {
"chick": { "type": "string"}
}
}

102 changes: 92 additions & 10 deletions test/unit/add-tests-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ describe '#addTests', ->
res = tests[0].response

assert.equal res.status, 200
assert.equal res.schema, """[
type: 'string'
name: 'string'
]
"""
schema = res.schema[0]
assert.equal schema.items.properties.type.type, 'string'
assert.equal schema.items.properties.name.type, 'string'
assert.isNull res.headers
assert.isNull res.body

Expand Down Expand Up @@ -105,11 +102,96 @@ describe '#addTests', ->
res = tests[1].response

assert.equal res.status, 201
assert.equal res.schema, """
type: 'string'
name: 'string'
schema = res.schema[0]
assert.equal schema.properties.type.type, 'string'
assert.equal schema.properties.name.type, 'string'
assert.isNull res.headers
assert.isNull res.body

"""
describe 'when raml includes multiple referencing schemas', ->

tests = []
callback = ''

before (done) ->

ramlParser.loadFile("#{__dirname}/../fixtures/ref_other_schemas.raml")
.then (data) ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, callback
, done
after ->
tests = []

it 'should run callback', ->
assert.ok callback.called

it 'should added 1 test', ->
assert.lengthOf tests, 1

it 'should set test.name', ->
assert.equal tests[0].name, 'GET /machines -> 200'

it 'should setup test.request', ->
req = tests[0].request

assert.equal req.path, '/machines'
assert.deepEqual req.params, {}
assert.deepEqual req.query, {}
assert.deepEqual req.body, {}
assert.equal req.method, 'GET'

it 'should setup test.response', ->
res = tests[0].response

assert.equal res.status, 200
assert.lengthOf res.schema, 3
assert.isNull res.headers
assert.isNull res.body

describe 'when raml has inline and included schemas', ->

tests = []
callback = ''

before (done) ->

ramlParser.loadFile("#{__dirname}/../fixtures/inline_and_included_schemas.raml")
.then (data) ->
callback = sinon.stub()
callback.returns(done())

addTests data, tests, callback
, done
after ->
tests = []

it 'should run callback', ->
assert.ok callback.called

it 'should added 1 test', ->
assert.lengthOf tests, 1

it 'should set test.name', ->
assert.equal tests[0].name, 'GET /machines -> 200'

it 'should setup test.request', ->
req = tests[0].request

assert.equal req.path, '/machines'
assert.deepEqual req.params, {}
assert.deepEqual req.query, {}
assert.deepEqual req.body, {}
assert.equal req.method, 'GET'

it 'should setup test.response', ->
res = tests[0].response

assert.equal res.status, 200
# console.log res.schema
assert.lengthOf res.schema, 3
assert.isNull res.headers
assert.isNull res.body

Expand Down
22 changes: 4 additions & 18 deletions test/unit/test-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ describe 'Test', ->
test.request.body =
body: 'value'
test.response.status = 201
test.response.schema = """
type: 'string'
name: 'string'
"""
test.response.schema = [{ type: 'object', properties: { type: 'string', name: 'string'}}]

machine =
type: 'foo'
Expand Down Expand Up @@ -85,10 +82,6 @@ describe 'Test', ->
response = test.response
# Unchanged properties
assert.equal response.status, 201
assert.deepEqual response.schema, """
type: 'string'
name: 'string'
"""

# changed properties
# assert.equal response.headers, 201
Expand Down Expand Up @@ -116,10 +109,7 @@ describe 'Test', ->
test.request.body =
body: 'value'
test.response.status = 200
test.response.schema = """
type: 'string'
name: 'string'
"""
test.response.schema = [{ type: 'object', properties: { type: 'string', name: 'string'}}]

machine =
type: 'foo'
Expand Down Expand Up @@ -158,10 +148,6 @@ describe 'Test', ->
response = test.response
# Unchanged properties
assert.equal response.status, 200
assert.deepEqual response.schema, """
type: 'string'
name: 'string'
"""
assert.deepEqual response.body, machine


Expand Down Expand Up @@ -196,14 +182,14 @@ describe 'Test', ->

test = new Test()
test.response.status = 201
test.response.schema = JSON.stringify
test.response.schema = {
$schema: 'http://json-schema.org/draft-04/schema#'
type: 'object'
properties:
type:
type: 'string'
name:
type: 'string'
type: 'string'}

describe 'when against valid response', ->

Expand Down

0 comments on commit dfb4ce6

Please sign in to comment.