【问题标题】:python schema validation issuepython模式验证问题
【发布时间】:2019-01-27 18:00:55
【问题描述】:

我正在尝试编写一个 pytest 案例,以说明下面的示例中首先存在 Clinical_in。我已经在 pycharm 中编写了这个,并且可以运行一个测试来说明在“clinical_in”中有“code_tbl”并且等于“code”,但似乎无法找到一种方法来编写一个测试用例来说明linical_in 首先存在因为如果不是,就没有前进的理由

我如何通过阅读来获取文件

json = json.load(open(resources_path + 'clinical_in.json'))

这是我的 json 示例

{
  "clinical_in": {
    "code_tbl": "code",
    "prov_tbl": "prov",
    "patient_tbl": {
      "patient_tbl":"patient_table",
      "filter": {
        "case1":{"data_typ":"Med", "claim_typ":["r", "s"]}
      }
    }
}

【问题讨论】:

  • assert 'clinical_in' in json.keys()?

标签: python-3.x validation schema pytest


【解决方案1】:

首先我们需要确保 json 的顺序正确。你应该使用 OrderDict 然后我们可以写一个断言

import json
from collections import OrderedDict
json_file = """{
  "clinical_in": {
    "code_tbl": "code",
    "prov_tbl": "prov",
    "patient_tbl": {
      "patient_tbl":"patient_table",
      "filter": {
        "case1": {"data_typ":"Med", "claim_typ":["r", "s"]}
      }
      }
    }
}"""
data = json.loads(json_file, object_pairs_hook=OrderedDict)
assert list(data.keys())[0] == 'clinical_in'

【讨论】:

  • 我把它放进去,我得到这个错误 E json.decoder.JSONDecodeError: Extra data: line 1 column 17 (char 16)
  • 我也刚刚做了一些修改,但现在我得到了 TypeError: string indices must be integers
  • 可以提供线路吗?
  • 这是我最新的 import json from collections import OrderedDict def test_clinical_schema(): json_data_diag = """ {"code_tbl": "icd","prov_tbl": "provider","patient_hub ": {"patient_hub_tbl":"pat_provider_diag","filter": {"case1":{"data_typ_cd":"DX", "claim_typ_cd":["P", "I"]} }}}""" 数据= json.loads(json_data_diag, object_pairs_hook=OrderedDict) print (data.keys) assert list(OrderedDict.keys(data)) == ["clinical_input"] 我收到这个断言错误 E - ['code_tbl', 'prov_tbl' , 'patient_hub'] E + ['clinical_input']
  • 它再次让我回馈临床输入的内容......不是关键
猜你喜欢
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 2012-10-04
  • 2018-05-09
相关资源
最近更新 更多