-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support passing a handler to generate specific and related data
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
1 parent
96d9996
commit c67ec59
Showing
4 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |