Scalacheck json和案例类

时间:2012-09-11 18:02:18

标签: scala specs2 scalacheck

我正在编写一个服务,它接受一个case类并将其序列化为json,然后我将发送给运行Elastic Search的实例。

我希望scalacheck生成几个案例类,随机丢失数据,如下所示:

val searchDescAndBrand = SearchEntry("", "Ac Adapters", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)
val searchBrand = SearchEntry("", ", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)
val searchPartNumberAndBrand = SearchEntry("02DUYT", "", "Sony", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", 0L)

你明白了,要么填写值,要么留空(最后一个是Long类型。

这是一个简单的部分,问题是生成的json不只是省略“提交”,而是省略了整个部分,例如:

    """
  |{
  |  "from" : 0,
  |  "size" : 10,
  |  "query" : {
  |    "bool" : {
  |      "must" : [
  |        {"match" : {
  |          "description" : {
  |            "query" : "Ac Adapters",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "brand" : {
  |            "query" : "Sony",
  |            "type" : "phrase"
  |          }
  |        }}
  |      ]
  |    }
  |  }
  |}
  |
""".stripMargin)

如果我有一个带有数据的前3个字段的case类,json将是:

    """
  |{
  |  "from" : 0,
  |  "size" : 10,
  |  "query" : {
  |    "bool" : {
  |      "must" : [
  |        {"match" : {
  |          "part_number" : {
  |            "query" : "02D875",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "description" : {
  |            "query" : "Ac Adapters",
  |            "type" : "phrase"
  |          }
  |        }},
  |        {"match" : {
  |          "brand" : {
  |            "query" : "Sony",
  |            "type" : "phrase"
  |          }
  |        }}
  |      ]
  |    }
  |  }
  |}
  |
""".stripMargin)

因此,简而言之,拥有一个值意味着添加

{"match" : {
  |          "<specific name here, based on which value we have>" : {
  |            "query" : "<value from scalacheck>",
  |            "type" : "phrase"
  |          }
  |        }}

结果。

你会如何处理这样一个用例?

0 个答案:

没有答案