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
Example #2
0
def test_parse_various_files():
  """Tests that we can parse, without choking, test files that are part of the original
  thrift parser's test suite. We just check that parsing succeeds, and don't verify the
  results."""
  TEST_DATA_FILES = [
    "AnnotationTest.thrift", "ConstantsDemo.thrift", "DenseLinkingTest.thrift",
    "OptionalRequiredTest.thrift", "StressTest.thrift", "DebugProtoTest.thrift",
    "DocTest.thrift", "ManyTypedefs.thrift", "SmallTest.thrift", "ThriftTest.thrift"
  ]
  TEST_DATA_DIR = 'test_data'
  TEST_DATA_PATH = __name__

  parser = ThriftParser()
  for test_data_file in TEST_DATA_FILES:
    test_data = pkgutil.get_data(TEST_DATA_PATH, os.path.join(TEST_DATA_DIR, test_data_file))
    print 'Parsing file %s...' % test_data_file,
    program = parser.parse_string(test_data)
    print 'OK.'
Example #3
0
def test_parse_various_files():
  """Tests that we can parse, without choking, test files that are part of the original
  thrift parser's test suite. We just check that parsing succeeds, and don't verify the
  results."""
  TEST_DATA_FILES = [
    "AnnotationTest.thrift", "ConstantsDemo.thrift", "DenseLinkingTest.thrift",
    "OptionalRequiredTest.thrift", "StressTest.thrift", "DebugProtoTest.thrift",
    "DocTest.thrift", "ManyTypedefs.thrift", "SmallTest.thrift", "ThriftTest.thrift"
  ]
  TEST_DATA_DIR = 'test_data'
  TEST_DATA_PATH = 'twitter.thrift.descriptors'

  parser = ThriftParser()
  for test_data_file in TEST_DATA_FILES:
    test_data = pkgutil.get_data(TEST_DATA_PATH, os.path.join(TEST_DATA_DIR, test_data_file))
    print('Parsing file %s...' % test_data_file, end='')
    # TODO: The parser may fail silently and return a partial program. Fix this.
    program = parser.parse_string(test_data)
    print('OK.')
Example #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
Example #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
Example #6
0
def _parse_with_expected_error(test_data, expected_error_msg):
  parser = ThriftParser()
  with pytest.raises(ThriftParserError) as exception_info:
    program = parser.parse_string(test_data)
  assert str(exception_info.value) == expected_error_msg
Example #7
0
def _parse_with_expected_error(test_data, expected_error_msg):
  parser = ThriftParser()
  with pytest.raises(ThriftParserError) as exception_info:
    program = parser.parse_string(test_data)
  assert str(exception_info.value) == expected_error_msg