Skip to content

Commit

Permalink
Support passing a handler to generate specific and related data
Browse files Browse the repository at this point in the history
This allows passing a custom handler so that you can generate
related model data for example a users list and a task list
you will want to ensure that the user_id generated in the task list
will exist in the users list.

Its a basic implementation and defaults the current behavoir if either
a handler isn't passed or the handler doesn't manage the key
  • Loading branch information
jnormington committed Sep 9, 2017
1 parent 96d9996 commit c67ec59
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
16 changes: 16 additions & 0 deletions fixtures/basic_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"created_at": {
"type": "string",
"format": "date-time"
},
"count": {
"type": "number"
},
"user_count": {
"type": "number"
}
}
}
2 changes: 1 addition & 1 deletion lib/json_test_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module JsonTestData
def self.generate!(schema, opts={})
schema = JsonSchema.new(schema).generate_example
schema = JsonSchema.new(schema, opts[:handler]).generate_example
opts[:ruby] ? JSON.parse(schema, symbolize_names: true) : schema
end
end
15 changes: 9 additions & 6 deletions lib/json_test_data/json_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

module JsonTestData
class JsonSchema
attr_accessor :schema
attr_accessor :schema, :handler

def initialize(schema)
def initialize(schema, handler = nil)
@schema = JSON.parse(schema, symbolize_names: true)
@handler = handler
end

def generate_example
generate(schema).to_json
end

private
def generate_data(obj)
def generate_data(obj, key)
return handler.get_data(key, obj) if handler && handler.manages_key?(key)

case obj.fetch(:type)
when "number"
JsonTestData::Number.create(obj)
Expand All @@ -27,13 +30,13 @@ def generate_data(obj)
end
end

def generate(obj)
def generate(obj, key = nil)
if is_object?(obj)
generate_object(obj)
elsif is_array?(obj)
generate_array(obj)
else
generate_data(obj)
generate_data(obj, key)
end
end

Expand Down Expand Up @@ -67,7 +70,7 @@ def generate_object(object)
object.fetch(:properties).each do |k, v|
obj[k] = nil unless v.has_key?(:type)

obj[k] = generate(v)
obj[k] = generate(v, k)
end
end

Expand Down
55 changes: 55 additions & 0 deletions spec/json_test_data/custom_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'spec_helper'

class FakeHandler
def manages_key?(key); end
def get_data(key, obj); end
end

RSpec.describe 'Schema custom handler' do
let(:schema) { File.read('./fixtures/basic_schema.json') }
let(:result) { JSON.parse(subject.generate_example) }

subject { JsonTestData::JsonSchema.new(schema, handler) }

before do
allow(JsonTestData::Number).to receive(:create).and_return(9000)
end

describe 'when custom_handler is not passed' do
let(:handler) { nil }

it 'doesnt error' do
expect{result}.not_to raise_error
end

it 'still generates data' do
expect(result['count']).to eq 9000
end
end

describe 'when handler passed' do
let(:handler) { FakeHandler.new }

before do
allow(handler).to receive(:manages_key?).and_return(false)
end

context 'and it manages key' do
it 'gets data from the handler' do
expect(handler).to receive(:manages_key?).with(:count).and_return(true)
expect(handler).to receive(:get_data).with(:count, { type: 'number' }).and_return(5555)
expect(result['count']).to eq 5555
end
end

context "and it doesn't manage key" do
it 'returns number test data' do
expect(handler).to receive(:manages_key?).with(:count).and_return(false)
allow(handler).to receive(:get_data).and_return(5555)

expect(result['count']).to eq 9000
expect(result['user_count']).to eq 9000
end
end
end
end

0 comments on commit c67ec59

Please sign in to comment.