Example #1
0
 def test_deserialize_both_open(self):
     serialized = (int8_pack(4))
     deserialized = DateRangeType.deserialize(serialized, 5)
     self.assertEqual(
         deserialized,
         util.DateRange(lower_bound=util.OPEN_BOUND,
                        upper_bound=util.OPEN_BOUND))
Example #2
0
 def test_deserialize_single_value(self):
     serialized = (int8_pack(0) + int64_pack(self.timestamp) + int8_pack(3))
     self.assertEqual(
         DateRangeType.deserialize(serialized, 5),
         util.DateRange(value=util.DateRangeBound(value=datetime.datetime(
             2017, 2, 1, 15, 42, 12, 404000),
                                                  precision='HOUR')))
Example #3
0
 def test_serialize_open_low(self):
     serialized = (int8_pack(2) + int64_pack(self.timestamp) + int8_pack(3))
     deserialized = DateRangeType.deserialize(serialized, 5)
     self.assertEqual(
         deserialized,
         util.DateRange(lower_bound=util.DateRangeBound(
             value=datetime.datetime(2017, 2, 1, 15), precision='HOUR'),
                        upper_bound=util.OPEN_BOUND))
Example #4
0
 def test_serialize_single_value(self):
     serialized = (int8_pack(0) + int64_pack(self.timestamp) + int8_pack(5))
     deserialized = DateRangeType.deserialize(serialized, 5)
     self.assertEqual(
         deserialized,
         util.DateRange(value=util.DateRangeBound(value=datetime.datetime(
             2017, 2, 1, 15, 42, 12),
                                                  precision='SECOND')))
Example #5
0
 def test_deserialize_open_low(self):
     serialized = (int8_pack(3) + int64_pack(self.timestamp) + int8_pack(4))
     deserialized = DateRangeType.deserialize(serialized, 5)
     self.assertEqual(
         deserialized,
         util.DateRange(lower_bound=util.OPEN_BOUND,
                        upper_bound=util.DateRangeBound(
                            value=datetime.datetime(2017, 2, 1, 15, 42, 20,
                                                    1000),
                            precision='MINUTE')))
Example #6
0
 def test_deserialize_closed_range(self):
     serialized = (int8_pack(1) + int64_pack(self.timestamp) +
                   int8_pack(2) + int64_pack(self.timestamp) + int8_pack(6))
     self.assertEqual(
         DateRangeType.deserialize(serialized, 5),
         util.DateRange(lower_bound=util.DateRangeBound(
             value=datetime.datetime(2017, 2, 1, 0, 0), precision='DAY'),
                        upper_bound=util.DateRangeBound(
                            value=datetime.datetime(2017, 2, 1, 15, 42, 12,
                                                    404000),
                            precision='MILLISECOND')))
Example #7
0
 def test_serialize_closed_range(self):
     serialized = (int8_pack(1) + int64_pack(self.timestamp) +
                   int8_pack(5) + int64_pack(self.timestamp) + int8_pack(0))
     deserialized = DateRangeType.deserialize(serialized, 5)
     self.assertEqual(
         deserialized,
         util.DateRange(lower_bound=util.DateRangeBound(
             value=datetime.datetime(2017, 2, 1, 15, 42, 12),
             precision='SECOND'),
                        upper_bound=util.DateRangeBound(
                            value=datetime.datetime(2017, 12, 31),
                            precision='YEAR')))
Example #8
0
    def test_serialize_zero_datetime(self):
        """
        Test serialization where timestamp = 0

        Companion test for test_deserialize_zero_datetime

        @since 2.0.0
        @jira_ticket PYTHON-729
        @expected_result serialization doesn't raise an error

        @test_category data_types
        """
        DateRangeType.serialize(
            util.DateRange(lower_bound=(datetime.datetime(1970, 1, 1), 'YEAR'),
                           upper_bound=(datetime.datetime(1970, 1,
                                                          1), 'YEAR')), 5)
Example #9
0
    def deserialize(cls, byts, protocol_version):
        # <type>[<time0><precision0>[<time1><precision1>]]
        type_ = int8_unpack(byts[0:1])

        if type_ in (BoundKind.to_int(BoundKind.BOTH_OPEN_RANGE),
                     BoundKind.to_int(BoundKind.SINGLE_DATE_OPEN)):
            time0 = precision0 = None
        else:
            time0 = int64_unpack(byts[1:9])
            precision0 = int8_unpack(byts[9:10])

        if type_ == BoundKind.to_int(BoundKind.CLOSED_RANGE):
            time1 = int64_unpack(byts[10:18])
            precision1 = int8_unpack(byts[18:19])
        else:
            time1 = precision1 = None

        if time0 is not None:
            date_range_bound0 = util.DateRangeBound(
                time0,
                cls._decode_precision(precision0)
            )
        if time1 is not None:
            date_range_bound1 = util.DateRangeBound(
                time1,
                cls._decode_precision(precision1)
            )

        if type_ == BoundKind.to_int(BoundKind.SINGLE_DATE):
            return util.DateRange(value=date_range_bound0)
        if type_ == BoundKind.to_int(BoundKind.CLOSED_RANGE):
            return util.DateRange(lower_bound=date_range_bound0,
                                  upper_bound=date_range_bound1)
        if type_ == BoundKind.to_int(BoundKind.OPEN_RANGE_HIGH):
            return util.DateRange(lower_bound=date_range_bound0,
                                  upper_bound=util.OPEN_BOUND)
        if type_ == BoundKind.to_int(BoundKind.OPEN_RANGE_LOW):
            return util.DateRange(lower_bound=util.OPEN_BOUND,
                                  upper_bound=date_range_bound0)
        if type_ == BoundKind.to_int(BoundKind.BOTH_OPEN_RANGE):
            return util.DateRange(lower_bound=util.OPEN_BOUND,
                                  upper_bound=util.OPEN_BOUND)
        if type_ == BoundKind.to_int(BoundKind.SINGLE_DATE_OPEN):
            return util.DateRange(value=util.OPEN_BOUND)
        raise ValueError('Could not deserialize %r' % (byts,))
Example #10
0
 def test_serialize_both_open(self):
     serialized = DateRangeType.serialize(
         util.DateRange(lower_bound=util.OPEN_BOUND,
                        upper_bound=util.OPEN_BOUND), 5)
     self.assertEqual(int8_pack(4), serialized)
Example #11
0
 def test_serialize_single_open(self):
     serialized = DateRangeType.serialize(
         util.DateRange(value=util.OPEN_BOUND, ), 5)
     self.assertEqual(int8_pack(5), serialized)
Example #12
0
 def test_deserialize_single_open(self):
     self.assertEqual(util.DateRange(value=util.OPEN_BOUND),
                      DateRangeType.deserialize(int8_pack(5), 5))