示例#1
0
    def test_encode_thrift_to_json(self):
        x = structs_for_testing.TestStruct()
        x.field2 = True
        x.field4 = [2, 4, 6, 8]
        x.field7 = 1.2

        expected1 = \
    """{
  "field2": true,
  "field4": [
    2,
    4,
    6,
    8
  ],
  "field7": 1.2
}"""

        json_str1 = thrift_json_encoder.thrift_to_json(x)
        assert expected1 == json_str1

        x.field1 = 42
        x.field2 = False
        x.field3 = '"not default"'
        x.field4.append(10)
        x.field5 = set(['b', 'c', 'a'])
        x.field6 = structs_for_testing.InnerTestStruct()
        x.field6.foo = "bar"
        x.field6.color = structs_for_testing.Color.BLUE

        expected2 = \
    """{
  "field1": 42,
  "field2": false,
  "field3": "\\"not default\\"",
  "field4": [
    2,
    4,
    6,
    8,
    10
  ],
  "field5": [
    "a",
    "b",
    "c"
  ],
  "field6": {
    "color": 3,
    "foo": "bar"
  },
  "field7": 1.2
}"""

        json_str2 = thrift_json_encoder.thrift_to_json(x)
        print json_str2
        assert expected2 == json_str2
  def test_encode_thrift_to_json(self):
    x = structs_for_testing.TestStruct()
    x.field2 = True
    x.field4 = [2, 4, 6, 8]
    x.field7 = 1.2

    expected1 = \
"""{
  "field2": true,
  "field4": [
    2,
    4,
    6,
    8
  ],
  "field7": 1.2
}"""

    json_str1 = thrift_json_encoder.thrift_to_json(x)
    assert expected1 == json_str1

    x.field1 = 42
    x.field2 = False
    x.field3 = '"not default"'
    x.field4.append(10)
    x.field5 = set(['b', 'c', 'a'])
    x.field6 = structs_for_testing.InnerTestStruct()
    x.field6.foo = "bar"
    x.field6.color =  structs_for_testing.Color.BLUE

    expected2 = \
"""{
  "field1": 42,
  "field2": false,
  "field3": "\\"not default\\"",
  "field4": [
    2,
    4,
    6,
    8,
    10
  ],
  "field5": [
    "a",
    "b",
    "c"
  ],
  "field6": {
    "color": 3,
    "foo": "bar"
  },
  "field7": 1.2
}"""

    json_str2 = thrift_json_encoder.thrift_to_json(x)
    print(json_str2)
    assert expected2 == json_str2
def parseThriftToJSON(inputfile):
    parser = ThriftParser()
    print('Parsing file %s...' % inputfile)
    program = parser.parse_file(inputfile)
    print('Parse was success.')
    print('decoding to JSON.')
    thriftJson = thrift_json_encoder.thrift_to_json(program)
    # print '!!!!!!!!BEGIN JSON for ' + inputfile
    # print thriftJson
    # print '!!!!!!!!END JSON for ' + inputfile
    unpickled = jsonpickle.decode(thriftJson)
    print('JSON decode was success.')
    return unpickled
示例#4
0
def test_thrift_parser(generate_golden_data):
  """Tests that we can parse a complex file that tickles as many cases and corner cases
  as we can think of. We verify the result against golden data."""
  TEST_DATA_FILE = 'test_data/test_data.thrift'
  GOLDEN_DATA_FILE = TEST_DATA_FILE + '.golden'
  TEST_DATA_PATH = __name__

  test_data = pkgutil.get_data(TEST_DATA_PATH, TEST_DATA_FILE)
  golden_data = pkgutil.get_data(TEST_DATA_PATH, GOLDEN_DATA_FILE)
  parser = ThriftParser()
  print 'Parsing file %s...' % TEST_DATA_FILE,
  program = parser.parse_string(test_data)
  print 'OK.'
  res = thrift_json_encoder.thrift_to_json(program)

  if golden_data is not None:
    # Generate new golden data to the specified path. Use this only once you're
    # convinced that the generated data is correct and the old golden data is not.
    if generate_golden_data is not None:
      with open(generate_golden_data, 'w') as fd:
        fd.write(res)

    assert golden_data == res
示例#5
0
def test_thrift_parser(generate_golden_data):
  """Tests that we can parse a complex file that tickles as many cases and corner cases
  as we can think of. We verify the result against golden data."""
  TEST_DATA_FILE = 'test_data/test_data.thrift'
  GOLDEN_DATA_FILE = TEST_DATA_FILE + '.golden'
  TEST_DATA_PATH = 'twitter.thrift.descriptors'

  test_data = pkgutil.get_data(TEST_DATA_PATH, TEST_DATA_FILE)
  golden_data = pkgutil.get_data(TEST_DATA_PATH, GOLDEN_DATA_FILE)
  parser = ThriftParser()
  print('Parsing file %s...' % TEST_DATA_FILE)
  program = parser.parse_string(test_data)
  print('OK.')
  res = thrift_json_encoder.thrift_to_json(program)

  if golden_data is not None:
    # Generate new golden data to the specified path. Use this only once you're
    # convinced that the generated data is correct and the old golden data is not.
    if generate_golden_data is not None:
      with open(generate_golden_data, 'w') as fd:
        fd.write(res)

    assert golden_data == res