JSON::Fuzz::GeneratorでJSON Schemaからテストデータを生成する

JSON::Fuzz::Generatorを利用して
JSON Schemaからテストデータを生成します

## <i class="fa fa-cloud-download" style="font-size:1em;"></i> インストール
~~~bash
$ gem install json-fuzz-generator
~~~

## <i class="fa fa-flask" style="font-size:1em;"></i> サンプル
### <i class="fa fa-file-text" style="font-size:1em;"></i> JSON Schema
~~~json
{
  "links": [
    {
      "description": "全ての人物の一覧を取得します。",
      "href": "/api/v1/people",
      "method": "GET",
      "rel": "instances",
      "title": "list"
    },
    {
      "description": "人物を取得します。",
      "href": "/api/v1/people/:id",
      "method": "GET",
      "rel": "self",
      "title": "get"
    },
    {
      "description": "人物を登録します。",
      "href": "/api/v1/people/:id",
      "method": "POST",
      "rel": "self",
      "schema": {
        "properties": {
          "name": {
            "description": "人物の名前",
            "example": "tanaka",
            "type": "string"
          },
          "age": {
            "description": "人物の年齢",
            "example": 34,
            "type": "integer"
          }
        },
        "required": [
          "name",
          "age"
        ]
      },
      "title": "create"
    },
    {
      "description": "人物を更新します。",
      "href": "/api/v1/people/:id",
      "method": "PATCH",
      "rel": "self",
      "schema": {
        "properties": {
          "name": {
            "description": "人物の名前",
            "example": "tanaka",
            "type": "string"
          },
          "age": {
            "description": "人物の年齢",
            "example": 34,
            "type": "integer"
          }
        },
        "required": [
          "name",
          "age"
        ]
      },
      "title": "update"
    },
    {
      "description": "人物を削除します。",
      "href": "/api/v1/people/:id",
      "method": "DELETE",
      "rel": "self",
      "title": "delete"
    }
  ],
  "properties": {
    "id": {
      "description": "人物のid",
      "example": 1,
      "type": "integer"
    },
    "name": {
      "description": "人物の名前",
      "example": "tanaka",
      "type": "string"
    },
    "age": {
      "description": "人物の年齢",
      "example": 34,
      "type": "integer"
    }
  },
  "required": [
    "id",
    "name",
    "age"
  ],
  "title": "Person"
}
~~~

### <i class="fa fa-smile-o" style="font-size:1em;"></i> 正常系データの生成
* プログラム

~~~ruby
require "json-fuzz-generator"

puts JSON::Fuzz::Generator.default_param('schema.json')
~~~

* 実行

~~~bash
$ ruby sample_normal.rb
{"id"=>15, "name"=>"hoge", "age"=>86}
~~~

### <i class="fa fa-frown-o" style="font-size:1em;"></i> 異常系データの生成
* プログラム

~~~ruby
require "json-fuzz-generator"

puts JSON::Fuzz::Generator.generate('schema.json')
~~~

* 実行

~~~bash
$ ruby sample_abnormal.rb
{"id"=>"a", "name"=>"hoge", "age"=>24}
{"id"=>"1", "name"=>"hoge", "age"=>91}
{"id"=>0.1, "name"=>"hoge", "age"=>26}
{"id"=>["sample", "array"], "name"=>"hoge", "age"=>48}
{"id"=>true, "name"=>"hoge", "age"=>45}
{"id"=>nil, "name"=>"hoge", "age"=>88}
{"id"=>0.6223376305782494, "name"=>"hoge", "age"=>7}
{"id"=>{}, "name"=>"hoge", "age"=>95}
{"id"=>"hoge", "name"=>"hoge", "age"=>34}
{"id"=>64, "name"=>["sample", "array"], "age"=>74}
{"id"=>46, "name"=>false, "age"=>9}
{"id"=>57, "name"=>42, "age"=>2}
{"id"=>45, "name"=>nil, "age"=>54}
{"id"=>48, "name"=>0.1365808521323717, "age"=>44}
{"id"=>41, "name"=>{}, "age"=>52}
{"id"=>94, "name"=>"hoge", "age"=>"a"}
{"id"=>74, "name"=>"hoge", "age"=>"1"}
{"id"=>24, "name"=>"hoge", "age"=>0.1}
{"id"=>94, "name"=>"hoge", "age"=>["sample", "array"]}
{"id"=>53, "name"=>"hoge", "age"=>false}
{"id"=>68, "name"=>"hoge", "age"=>nil}
{"id"=>87, "name"=>"hoge", "age"=>0.2790809146382933}
{"id"=>27, "name"=>"hoge", "age"=>{}}
{"id"=>51, "name"=>"hoge", "age"=>"hoge"}
{"name"=>"hoge", "age"=>58}
{"id"=>61, "age"=>63}
{"id"=>97, "name"=>"hoge"}
~~~

## <i class="fa fa-bookmark-o" style="font-size:1em;"></i> 外部資料
* <i class="fa fa-external-link" style="font-size:1em;"></i> [json-fuzz-generator - GitHub](https://github.com/deme0607/json-fuzz-generator)
* <i class="fa fa-external-link" style="font-size:1em;"></i> [YAPC::Asia Tokyo 2014 に 参加しました & 発表しました  - Three Cheers for the Web](http://deme0607.hatenablog.com/entry/2014/09/29/000726)