def test_dump_load_text(p): # test dump out = BytesIO() dump(p.obj, out, binary=False, sequence_as_stream=p.stream) res = out.getvalue() if not p.has_symbols: assert (b'$ion_1_0 ' + p.expected) == res else: # The payload contains a LST. The value comes last, so compare the end bytes. assert p.expected == res[len(res) - len(p.expected):] # test load out.seek(0) res = load(out, single_value=(not p.stream)) def equals(): if ion_equals(p.obj, res): return True if isinstance(p.obj, SymbolToken): expected_token = p.obj if p.obj.text is None: assert p.obj.sid is not None # System symbol IDs are mapped correctly in the text format. token = SYSTEM_SYMBOL_TABLE.get(p.obj.sid) assert token is not None # User symbols with unknown text won't be successfully read. expected_token = token return expected_token == res else: try: return isnan(p.obj) and isnan(res) except TypeError: return False if not equals(): assert ion_equals(p.obj, res) # Redundant, but provides better error message.
def test_roundtrip(p): obj, is_binary, indent = p out = BytesIO() dump(obj, out, binary=is_binary, indent=indent) out.seek(0) res = load(out) _assert_roundtrip(obj, res)
def test_roundtrip(p): obj, is_binary = p out = BytesIO() dump(obj, out, binary=is_binary) out.seek(0) res = load(out) _assert_roundtrip(obj, res)
def test_unknown_object_type_fails(is_binary): class Dummy: pass out = BytesIO() with raises(TypeError): dump(Dummy(), out, binary=is_binary)
def test_writing_simpleion_dump(): # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-and-writing-ion-data data = u'{hello: "world"}' value = simpleion.loads(data) ion = BytesIO() simpleion.dump(value, ion, binary=True) assert b'\xe0\x01\x00\xea\xec\x81\x83\xde\x88\x87\xb6\x85hello\xde\x87\x8a\x85world' == ion.getvalue( )
def test_dump_load_binary(p): # test dump out = BytesIO() dump(p.obj, out, binary=True, sequence_as_stream=p.stream) res = out.getvalue() if not p.has_symbols: assert (_IVM + p.expected) == res else: # The payload contains a LST. The value comes last, so compare the end bytes. assert p.expected == res[len(res) - len(p.expected):] # test load out.seek(0) res = load(out, single_value=(not p.stream)) assert ion_equals(p.obj, res)
def __execute_with(self, ion_implementation, error_location, args): stderr = ion_implementation.execute(*args) if len(stderr) != 0: # Any output to stderr is likely caused by an uncaught error in the implementation under test. This forces a # failure to avoid false negatives. error_file = FileIO(error_location, 'wb') try: error = { TestFile.ERROR_TYPE_FIELD: TestFile.ERROR_TYPE_STATE_SYMBOL, TestFile.ERROR_MESSAGE_FIELD: 'Implementation %s produced stderr output "%s" for command %r.' % ( ion_implementation.identifier, stderr.decode(), args ), TestFile.ERROR_LOCATION_FIELD: self.path } simpleion.dump(error, error_file, binary=False) finally: error_file.close()
def _flush(self): if len(self._events) > 0: print("Flushing stored events") self._header.sequence = self._sequence_counter self._sequence_counter += 1 header = self._header.pack_header() batch = header[EVENT_LIST] # Prepend a device context event to every batch and fudge the values # so that it appears as part of this batch. self._device_context[TIMESTAMP] = self._events[0][TIMESTAMP] self._device_context[APP_SESSION_ID] = self._events[0][ APP_SESSION_ID] self._device_context[USAGE_SESSION_ID] = self._events[0][ USAGE_SESSION_ID] self._device_context[PAGE_SESSION_ID] = self._events[0][ PAGE_SESSION_ID] batch.append(self._device_context) for event in self._events: event[CONTEXT_EVENT_ID] = self._device_context_id batch.append(event) # Add the end of file event and use the timestamp of the last event # leave the Session and context values set to null batch.append( EndOfFileEvent(self._events[-1][TIMESTAMP]).pack_event()) # Build a filename according to the specification filename = datetime.utcnow().strftime( "%Y%m%d-%H%M%S%f") + '_' + self._hw_client_id + '.10n' filename = os.path.join(self._path, filename) with open(filename, "wb") as write_file: simpleion.dump(header, fp=write_file, imports=[self._ion_symbols], binary=True) self._batches.append(filename) self._flush_time += timedelta(seconds=self._send_period) # Clear the stored events self._events.clear()
def _simple_dumps(obj, *args, **kw): buf = BytesIO() dump(obj, buf, *args, **kw) return buf.getvalue()
def _roundtrip(value, is_binary): out = BytesIO() dump(value, out, binary=is_binary) out.seek(0) roundtripped = load(out) return roundtripped
def dumps(val): f = StringIO() dump(val, f) return f.getvalue()
def test_single_value_with_stream_fails(is_binary): out = BytesIO() dump(['foo', 123], out, binary=is_binary, sequence_as_stream=True) out.seek(0) with raises(IonException): load(out, single_value=True)
def write_results(results, results_file, impls): """ Writes test results from `results`, which complies with the following schema-by-example. { good: { 'test_file_1.ion': { 'ion-c_abcd123': { result: PASS }, 'ion-java_def4567': { result: PASS } }, 'test_file_2.ion': { 'ion-c_abcd123': { result: FAIL, read_error: ErrorReport::[{ error_type: READ, message: "ion_reader_text.c:999 Line 1 index 3: Repeated underscore in numeric value.", location: "test_file_2.ion" }] }, 'ion-java_def4567': { result: PASS } } }, bad: { 'test_file_3.ion': { 'ion-c_abcd123' : { result: FAIL, errors: [] }, 'ion-java_def4567': { result: PASS } } }, equivs: { 'test_file_4.ion': { 'ion-c_abcd123': { result: FAIL, read_compare: { errors: [], failures: ComparisonReport::[{ result: NOT_EQUAL, lhs: { location: "ion-c_abcd123.ion", event: { event_type: SCALAR, ion_type: INT, value_text: "1", value_binary: [0x21, 0x01], depth:1 }, event_index: 2 }, rhs: { location: "test_file_4.ion", event: { event_type: SCALAR, ion_type: INT, value_text: "2", value_binary: [0x21, 0x02], depth:1 }, event_index:2 }, message: "1 vs. 2" }] } }, 'ion-java_def4567': { result: FAIL, write_error: ErrorReport::[{ error_type: WRITE, message: "IonManagedBinaryWriter.java:999 UnsupportedOperationException", location: "test_file_4.ion" }] } } } } """ # NOTE: A lot of this is a hack necessitated by the fact that ion-python does not yet support pretty-printing Ion # text. Once it does, the only thing this method needs to do is 'dump' to results_file with pretty-printing enabled. if '.' in results_file: results_file_raw = results_file[0:results_file.rfind('.')] + '_raw.ion' else: results_file_raw = results_file + '_raw.ion' results_out = FileIO(results_file_raw, mode='wb') try: simpleion.dump(results, results_out, binary=False) finally: results_out.close() ionc = list(filter(lambda x: 'ion-c' in x.identifier, impls))[0] ionc.execute('process', '--output', results_file, results_file_raw)