def test_serialization(): # Let's get some temporal objects ready, which we can use to show serialization examples ti1 = TIntInst(10, unix_dt(2011, 1, 1)) ti2 = TIntInst(20, unix_dt(2019, 1, 1)) tseti = TIntInstSet({ti1, ti2}) tf1 = TFloatInst(1.0, unix_dt(2011, 1, 1)) tf2 = TFloatInst(2.5, unix_dt(2011, 1, 2)) tseqf = TFloatSeq({tf1, tf2}) tseqsetf = TFloatSeqSet({tseqf}) # Example serialization of these objects si = SerializerInt() assert (si.write(ti1) == "10@2011-01-01T00:00:00+0000") sf = SerializerFloat() assert (sf.write(tseqf) == "[1@2011-01-01T00:00:00+0000, 2.5@2011-01-02T00:00:00+0000)") assert (sf.write(tseqsetf) == "{[1@2011-01-01T00:00:00+0000, 2.5@2011-01-02T00:00:00+0000)}") # For sets, ordering might vary, so we need to check accordingly serialized = si.write(tseti) assert len(serialized) == 58 assert serialized[0] == "{" assert serialized[-1] == "}" assert {"10@2011-01-01T00:00:00+0000", "20@2019-01-01T00:00:00+0000"} == set(serialized[1:-1].split(", ")) assert {tseqf} == tseqsetf.sequences
def test_constructor(): tseqf = get_sample_tsequence() assert tseqf.instants == { TFloatInst(1.25, unix_dt(2011, 1, 1)), TFloatInst(6.25, unix_dt(2011, 1, 2)), } assert (tseqf.lower_inc, tseqf.upper_inc) == (False, True)
def test_deserialization(): di = DeserializerInt("10@2011-01-01") ti = di.nextTInstant() assert (ti.getValue, ti.getTimestamp) == (10, unix_dt(2011, 1, 1)) df = DeserializerFloat("{1.0@2011-01-01, 2.5@2011-01-02}") tset = df.nextTInstantSet() actual = {(tf.getValue, tf.getTimestamp) for tf in tset.instants} expected = {(1.0, unix_dt(2011, 1, 1)), (2.5, unix_dt(2011, 1, 2))} assert actual == expected dg = DeserializerGeom( "[POINT(0 0)@2012-01-01 08:00:00+00, POINT(2 0)@2012-01-01 08:10:00+00, POINT(2 -1.98)@2012-01-01 08:15:00+00]" ) tseq = dg.nextTSequence() assert (tseq.lower_inc, tseq.upper_inc) == (True, True) actual = {(tg.getValue.toWKT(), tg.getTimestamp) for tg in tseq.instants} expected = {('POINT (0 0)', unix_dt(2012, 1, 1, 8)), ('POINT (2 0)', unix_dt(2012, 1, 1, 8, 10)), ('POINT (2 -1.98)', unix_dt(2012, 1, 1, 8, 15))} assert actual == expected df = DeserializerFloat("{[1.0@2011-01-01, 2.5@2011-01-02)}") tseqset = df.nextTSequenceSet() tf1 = TFloatInst(1.0, unix_dt(2011, 1, 1)) tf2 = TFloatInst(2.5, unix_dt(2011, 1, 2)) expected = TFloatSeqSet({TFloatSeq({tf1, tf2})}) assert tseqset == expected
def test_str(): tseqf = get_sample_tsequence() assert str(tseqf) == '(1.25@2011-01-01T00:00:00+0000, 6.25@2011-01-02T00:00:00+0000]' assert repr(tseqf) == '(1.25@2011-01-01T00:00:00+0000, 6.25@2011-01-02T00:00:00+0000]' tseqf = TFloatSeq({TFloatInst(10, unix_dt(2020, 9, 10)), TFloatInst(20, unix_dt(2019, 9, 10))}, False, True, Interpolation.Stepwise) assert str(tseqf) == "Interp=Stepwise;(20@2019-09-10T00:00:00+0000, 10@2020-09-10T00:00:00+0000]" assert repr(tseqf) == "Interp=Stepwise;(20@2019-09-10T00:00:00+0000, 10@2020-09-10T00:00:00+0000]"
def test_different_constructors_with_continuous_base_type(actual): assert actual.duration == TemporalDuration.Sequence assert actual.duration.name == 'Sequence' assert actual.interpolation == Interpolation.Linear assert actual.interpolation.name == 'Linear' assert len(actual.instants) == 2 assert actual.startInstant == TFloatInst(20, unix_dt(2019, 9, 10)) assert actual.endInstant == TFloatInst(10, unix_dt(2020, 9, 10)) assert actual.lower_inc == False assert actual.upper_inc == True assert str(actual) == "(20@2019-09-10T00:00:00+0000, 10@2020-09-10T00:00:00+0000]" assert repr(actual) == "(20@2019-09-10T00:00:00+0000, 10@2020-09-10T00:00:00+0000]"
def test_constructor_different_base_types(): tb = TBoolInst(True, unix_dt(2011, 1, 1)) ti = TIntInst(10, unix_dt(2011, 1, 1)) tf = TFloatInst(1.25, unix_dt(2011, 1, 1)) tt = TTextInst("testing", unix_dt(2011, 1, 1)) tg = TGeomPointInst(GeomPoint(10.0, 15.0), unix_dt(2011, 1, 1)) assert (tb.getValue, ti.getTimestamp) == (True, unix_dt(2011, 1, 1)) assert (ti.getValue, ti.getTimestamp) == (10, unix_dt(2011, 1, 1)) assert (tf.getValue, ti.getTimestamp) == (1.25, unix_dt(2011, 1, 1)) assert (tt.getValue, tt.getTimestamp) == ("testing", unix_dt(2011, 1, 1)) assert (tg.getValue.toWKT(), tg.getTimestamp) == ("POINT (10 15)", unix_dt(2011, 1, 1))
def get_sample_tsequence(): tf1 = TFloatInst(1.25, unix_dt(2011, 1, 1)) tf2 = TFloatInst(6.25, unix_dt(2011, 1, 2)) return TFloatSeq({tf1, tf2}, False, True)
assert actual.interpolation == Interpolation.Stepwise assert actual.interpolation.name == 'Stepwise' assert len(actual.instants) == 2 assert actual.startInstant == TIntInst(20, unix_dt(2019, 9, 10)) assert actual.endInstant == TIntInst(10, unix_dt(2020, 9, 10)) assert actual.lower_inc == True assert actual.upper_inc == False assert str(actual) == "[20@2019-09-10T00:00:00+0000, 10@2020-09-10T00:00:00+0000)" assert repr(actual) == "[20@2019-09-10T00:00:00+0000, 10@2020-09-10T00:00:00+0000)" @pytest.mark.parametrize("actual", [ TFloatSeq({TFloatInst(10, unix_dt(2020, 9, 10)), TFloatInst(20, unix_dt(2019, 9, 10))}, False, True), TFloatSeq({"10@2020-09-10 01:00:00+01", "20@2019-09-10 01:00:00+01"}, False, True), TFloatSeq({TFloatInst(10, unix_dt(2020, 9, 10)), TFloatInst(20, unix_dt(2019, 9, 10))}, False, True, Interpolation.Linear), TFloatSeq({"10@2020-09-10 01:00:00+01", "20@2019-09-10 01:00:00+01"}, False, True, Interpolation.Linear), TFloatSeq("(10@2020-09-10 01:00:00+01, 20@2019-09-10 01:00:00+01]"), TFloatSeq("Interp=Linear;(10@2020-09-10 01:00:00+01, 20@2019-09-10 01:00:00+01]"), ]) def test_different_constructors_with_continuous_base_type(actual): assert actual.duration == TemporalDuration.Sequence assert actual.duration.name == 'Sequence' assert actual.interpolation == Interpolation.Linear assert actual.interpolation.name == 'Linear' assert len(actual.instants) == 2 assert actual.startInstant == TFloatInst(20, unix_dt(2019, 9, 10))
def get_sample_float_tseq_2(): instant_1 = TFloatInst(20, unix_dt(2011, 1, 3)) instant_2 = TFloatInst(30, unix_dt(2011, 1, 4)) return TFloatSeq({instant_1, instant_2})
def get_sample_float_tseq_1(): instant_1 = TFloatInst(10, unix_dt(2011, 1, 1)) instant_2 = TFloatInst(40, unix_dt(2011, 1, 2)) return TFloatSeq({instant_1, instant_2})