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_event_types(p): value = p.event.value ion_type = p.event.ion_type event_output = p.type.from_event(p.event) value_output = p.type.from_value(ion_type, value, p.event.annotations) to_event_output = value_output.to_event(p.event.event_type, p.event.field_name, p.event.depth) assert p.event == to_event_output if p.type is IonPyNull: # Null is a special case due to the non-extension of NoneType. assert not event_output assert not value_output assert is_null(event_output) assert is_null(value_output) assert ion_equals(event_output, value_output) else: # Make sure we don't change value equality symmetry. assert event_output == value assert value == event_output assert value_output == value assert value == value_output # Derive a new event from just the value because equality is stricter in some cases. value_event = e_scalar(ion_type, value) output_event = e_scalar(ion_type, event_output) assert value_event == output_event assert event_output.ion_type is ion_type assert p.event.annotations == event_output.ion_annotations assert value_output.ion_type is ion_type assert p.event.annotations == value_output.ion_annotations
def test_fractional_combinations(item): assert item.timestamp.microsecond == item.expected_microsecond assert item.timestamp.fractional_precision == item.expected_fractional_precision # Using ion_equals ensures that the Decimals are compared for Ion data model equivalence # (i.e. precision is significant). assert ion_equals(item.timestamp.fractional_seconds, item.expected_fractional_seconds)
def _assert_roundtrip(before, after): # All loaded Ion values extend _IonNature, even if they were dumped from primitives. This recursively # wraps each input value in _IonNature for comparison against the output. def _to_ion_nature(obj): out = obj if not isinstance(out, _IonNature): ion_type = _ion_type(out) out = _FROM_ION_TYPE[ion_type].from_value(ion_type, out) if isinstance(out, dict): update = {} for field, value in six.iteritems(out): update[field] = _to_ion_nature(value) update = IonPyDict.from_value(out.ion_type, update, out.ion_annotations) out = update elif isinstance(out, list): update = [] for value in out: update.append(_to_ion_nature(value)) update = IonPyList.from_value(out.ion_type, update, out.ion_annotations) out = update return out assert ion_equals(_to_ion_nature(before), after)
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 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
def _assert_symbol_aware_ion_equals(assertion, output): if ion_equals(assertion, output): return True if isinstance(assertion, SymbolToken): expected_token = assertion if assertion.text is None: assert assertion.sid is not None # System symbol IDs are mapped correctly in the text format. token = SYSTEM_SYMBOL_TABLE.get(assertion.sid) assert token is not None # User symbols with unknown text won't be successfully read. expected_token = token return expected_token == output else: try: return isnan(assertion) and isnan(output) except TypeError: return False
def test_event_types(p): value = p.event.value ion_type = p.event.ion_type event_output = p.type.from_event(p.event) value_output = p.type.from_value(ion_type, value, p.event.annotations) to_event_output = value_output.to_event(p.event.event_type, p.event.field_name, in_struct=True, depth=p.event.depth) if p.event.ion_type.is_container: # compensate for abuse of IonEvent.value, which is intended to be None for CONTAINER_START events, # but is populated and relied upon by the logic of this test code assert to_event_output.value is None to_event_output = to_event_output._replace(value=p.event.value) assert p.event == to_event_output if p.type is IonPyNull: # Null is a special case due to the non-extension of NoneType. assert not event_output assert not value_output assert is_null(event_output) assert is_null(value_output) assert ion_equals(event_output, value_output) else: # Make sure we don't change value equality symmetry. assert event_output == value assert value == event_output assert value_output == value assert value == value_output # Derive a new event from just the value because equality is stricter in some cases. value_event = e_scalar(ion_type, value) output_event = e_scalar(ion_type, event_output) assert value_event == output_event assert event_output.ion_type is ion_type assert p.event.annotations == event_output.ion_annotations assert value_output.ion_type is ion_type assert p.event.annotations == value_output.ion_annotations
def test_event_types(p): value = p.event.value ion_type = p.event.ion_type event_output = p.type.from_event(p.event) value_output = p.type.from_value(ion_type, value, p.event.annotations) to_event_output = value_output.to_event(p.event.event_type, p.event.field_name, p.event.depth) assert p.event == to_event_output if p.type is IonPyNull: # Null is a special case due to the non-extension of NoneType. assert not event_output assert not value_output assert is_null(event_output) assert is_null(value_output) assert ion_equals(event_output, value_output) else: # Make sure we don't change value equality symmetry. assert event_output == value assert value == event_output assert value_output == value assert value == value_output # Derive a new event from just the value because equality is stricter in some cases. value_event = e_scalar(ion_type, value) output_event = e_scalar(ion_type, event_output) assert value_event == output_event assert event_output.ion_event == p.event assert event_output.ion_type is ion_type assert p.event.annotations == event_output.ion_annotations assert value_output.ion_event is to_event_output assert value_output.ion_type is ion_type assert p.event.annotations == value_output.ion_annotations
def roundtrip(): assert ion_equals(value, _roundtrip(value, is_binary))
def assert_equals(): for a_roundtripped, b_roundtripped, message in _roundtrip_pairs(a, b): assert ion_equals(a_roundtripped, b_roundtripped, timestamps_instants_only), message
def assert_nonequals(): for a_roundtripped, b_roundtripped, message in _roundtrip_pairs(a, b): assert not ion_equals(a_roundtripped, b_roundtripped), message
def assert_equal(): assert ion_equals(a, b, timestamps_instants_only)
def assert_nonequal(): assert not ion_equals(a, b)
def assert_equivalent(): assert ion_equals(a, b, timestamp_instants_only) assert ion_equals(b, a, timestamp_instants_only)
def assert_not_equivalent(): assert not ion_equals(a, b) assert not ion_equals(b, a)
def compare_two_files(first_file, second_file): first = simpleion.load(FileIO(first_file)) second = simpleion.load(FileIO(second_file)) return ion_equals(first, second)