Ruby
JSON
jsonschema

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

More than 2 years have passed since last update.

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

インストール

$ gem install json-fuzz-generator

サンプル

JSON Schema

{
  "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"
}

正常系データの生成

  • プログラム
require "json-fuzz-generator"

puts JSON::Fuzz::Generator.default_param('schema.json')
  • 実行
$ ruby sample_normal.rb
{"id"=>15, "name"=>"hoge", "age"=>86}

異常系データの生成

  • プログラム
require "json-fuzz-generator"

puts JSON::Fuzz::Generator.generate('schema.json')
  • 実行
$ 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"}

外部資料