Skip to content

Latest commit

 

History

History
52 lines (46 loc) · 2.53 KB

README.md

File metadata and controls

52 lines (46 loc) · 2.53 KB

JSON Test Data Generator

Gem Version Build Status Code Climate Coverage Status

The JSON test data generator takes JSON schema as input and generates test data matching the specification. You can download it from RubyGems:

gem install json_test_data

Alternatively, it can be included in your Gemfile:

gem "json_test_data", "~> 1.2.0"

To use it, first require it, then generate the test data using:

data = JsonTestData.generate!(schema)

If you would prefer to return a Ruby object instead of a JSON object, simply include the :ruby option:

data = JsonTestData.generate!(schema, ruby: true)

The input that you put into it must be valid JSON schema. The top-level object must have a :type key indicating the type of object to be generated ("object", "array", "string", etc.).

Using Optional Handlers

In the case where you need to generate additional data or perform other tasks when generating your JSON test data, you can pass in an optional handler to the JsonTestData.generate! method. The handler class must expose two methods: manages_key? and get_data. A handler class might look like:

class Handler
  def manages_key?(key)
    %i(user_id).includes?(key)
  end

  def get_data(key, obj)
    case key
    when :user_id
      # From a json file with valid mapped user_ids
       JSON.parse(File.read(users.json)).collect{|users| user['id'] }).sample
    else
      raise 'invalid key'
    end
  end
end

This can then be passed into the generator like this:

data = JsonTestData.generate!(schema, handler: Handler.new)

Contribute

The JSON Test Data Generator is in its infancy and contributions are much appreciated. Please include passing tests with all pull requests, and file an issue report indicating what you'd like to work on. I'm also happy to get issue reports that do not come with pull requests, but can't guarantee I'll actually write whatever it is you want JSON Test Data to do. I strongly encourage pull requests adding documentation!