Example #1
0
    def test_parse_interval_internal(self):
        # Test the internal _parse_interval function
        testtuples = (
            (
                "P1M/1981-04-05T01:01:00",
                {
                    "end": (
                        ("1981", "04", "05", None, None, None, "date"),
                        ("01", "01", "00", None, "time"),
                        "datetime",
                    ),
                    "duration":
                    (None, "1", None, None, None, None, None, "duration"),
                },
            ),
            (
                "P1M/1981-04-05",
                {
                    "end": ("1981", "04", "05", None, None, None, "date"),
                    "duration":
                    (None, "1", None, None, None, None, None, "duration"),
                },
            ),
            (
                "P1,5Y/2018-03-06",
                {
                    "end": ("2018", "03", "06", None, None, None, "date"),
                    "duration":
                    ("1.5", None, None, None, None, None, None, "duration"),
                },
            ),
            (
                "P1.5Y/2018-03-06",
                {
                    "end": ("2018", "03", "06", None, None, None, "date"),
                    "duration":
                    ("1.5", None, None, None, None, None, None, "duration"),
                },
            ),
            (
                "PT1H/2014-11-12",
                {
                    "end": ("2014", "11", "12", None, None, None, "date"),
                    "duration":
                    (None, None, None, None, "1", None, None, "duration"),
                },
            ),
            (
                "PT4H54M6.5S/2014-11-12",
                {
                    "end": ("2014", "11", "12", None, None, None, "date"),
                    "duration":
                    (None, None, None, None, "4", "54", "6.5", "duration"),
                },
            ),
            # Make sure we truncate, not round
            # https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
            (
                "PT0.0000001S/2018-03-06",
                {
                    "end": ("2018", "03", "06", None, None, None, "date"),
                    "duration": (
                        None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        "0.0000001",
                        "duration",
                    ),
                },
            ),
            (
                "PT2.0000048S/2018-03-06",
                {
                    "end": ("2018", "03", "06", None, None, None, "date"),
                    "duration": (
                        None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        "2.0000048",
                        "duration",
                    ),
                },
            ),
            (
                "1981-04-05T01:01:00/P1M1DT1M",
                {
                    "start": (
                        ("1981", "04", "05", None, None, None, "date"),
                        ("01", "01", "00", None, "time"),
                        "datetime",
                    ),
                    "duration":
                    (None, "1", None, "1", None, "1", None, "duration"),
                },
            ),
            (
                "1981-04-05/P1M1D",
                {
                    "start": ("1981", "04", "05", None, None, None, "date"),
                    "duration":
                    (None, "1", None, "1", None, None, None, "duration"),
                },
            ),
            (
                "2018-03-06/P2,5M",
                {
                    "start": ("2018", "03", "06", None, None, None, "date"),
                    "duration":
                    (None, "2.5", None, None, None, None, None, "duration"),
                },
            ),
            (
                "2018-03-06/P2.5M",
                {
                    "start": ("2018", "03", "06", None, None, None, "date"),
                    "duration":
                    (None, "2.5", None, None, None, None, None, "duration"),
                },
            ),
            (
                "2014-11-12/PT1H",
                {
                    "start": ("2014", "11", "12", None, None, None, "date"),
                    "duration":
                    (None, None, None, None, "1", None, None, "duration"),
                },
            ),
            (
                "2014-11-12/PT4H54M6.5S",
                {
                    "start": ("2014", "11", "12", None, None, None, "date"),
                    "duration":
                    (None, None, None, None, "4", "54", "6.5", "duration"),
                },
            ),
            # Make sure we truncate, not round
            # https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
            (
                "2018-03-06/PT0.0000001S",
                {
                    "start": ("2018", "03", "06", None, None, None, "date"),
                    "duration": (
                        None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        "0.0000001",
                        "duration",
                    ),
                },
            ),
            (
                "2018-03-06/PT2.0000048S",
                {
                    "start": ("2018", "03", "06", None, None, None, "date"),
                    "duration": (
                        None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        "2.0000048",
                        "duration",
                    ),
                },
            ),
            (
                "1980-03-05T01:01:00/1981-04-05T01:01:00",
                {
                    "start": (
                        ("1980", "03", "05", None, None, None, "date"),
                        ("01", "01", "00", None, "time"),
                        "datetime",
                    ),
                    "end": (
                        ("1981", "04", "05", None, None, None, "date"),
                        ("01", "01", "00", None, "time"),
                        "datetime",
                    ),
                },
            ),
            (
                "1980-03-05T01:01:00/1981-04-05",
                {
                    "start": (
                        ("1980", "03", "05", None, None, None, "date"),
                        ("01", "01", "00", None, "time"),
                        "datetime",
                    ),
                    "end": ("1981", "04", "05", None, None, None, "date"),
                },
            ),
            (
                "1980-03-05/1981-04-05T01:01:00",
                {
                    "start": ("1980", "03", "05", None, None, None, "date"),
                    "end": (
                        ("1981", "04", "05", None, None, None, "date"),
                        ("01", "01", "00", None, "time"),
                        "datetime",
                    ),
                },
            ),
            (
                "1980-03-05/1981-04-05",
                {
                    "start": ("1980", "03", "05", None, None, None, "date"),
                    "end": ("1981", "04", "05", None, None, None, "date"),
                },
            ),
            (
                "1981-04-05/1980-03-05",
                {
                    "start": ("1981", "04", "05", None, None, None, "date"),
                    "end": ("1980", "03", "05", None, None, None, "date"),
                },
            ),
            # Make sure we truncate, not round
            # https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
            (
                "1980-03-05T01:01:00.0000001/"
                "1981-04-05T14:43:59.9999997",
                {
                    "start": (
                        ("1980", "03", "05", None, None, None, "date"),
                        ("01", "01", "00.0000001", None, "time"),
                        "datetime",
                    ),
                    "end": (
                        ("1981", "04", "05", None, None, None, "date"),
                        ("14", "43", "59.9999997", None, "time"),
                        "datetime",
                    ),
                },
            ),
        )

        for testtuple in testtuples:
            mockBuilder = mock.Mock()
            mockBuilder.build_interval.return_value = testtuple[1]

            result = _parse_interval(testtuple[0], mockBuilder)

            self.assertEqual(result, testtuple[1])
            mockBuilder.build_interval.assert_called_once_with(**testtuple[1])

        # Test different separators
        expectedargs = {
            "start": (
                ("1980", "03", "05", None, None, None, "date"),
                ("01", "01", "00", None, "time"),
                "datetime",
            ),
            "end": (
                ("1981", "04", "05", None, None, None, "date"),
                ("01", "01", "00", None, "time"),
                "datetime",
            ),
        }

        mockBuilder = mock.Mock()
        mockBuilder.build_interval.return_value = expectedargs

        result = _parse_interval(
            "1980-03-05T01:01:00--1981-04-05T01:01:00",
            mockBuilder,
            intervaldelimiter="--",
        )

        self.assertEqual(result, expectedargs)
        mockBuilder.build_interval.assert_called_once_with(**expectedargs)

        expectedargs = {
            "start": (
                ("1980", "03", "05", None, None, None, "date"),
                ("01", "01", "00", None, "time"),
                "datetime",
            ),
            "end": (
                ("1981", "04", "05", None, None, None, "date"),
                ("01", "01", "00", None, "time"),
                "datetime",
            ),
        }

        mockBuilder = mock.Mock()
        mockBuilder.build_interval.return_value = expectedargs

        _parse_interval("1980-03-05 01:01:00/1981-04-05 01:01:00",
                        mockBuilder,
                        datetimedelimiter=" ")

        self.assertEqual(result, expectedargs)
        mockBuilder.build_interval.assert_called_once_with(**expectedargs)
Example #2
0
    def test_parse_interval_internal(self):
        #Test the internal _parse_interval function
        testtuples = (('P1M/1981-04-05T01:01:00',
                       {'end': (('1981', '04', '05', None, None, None, 'date'),
                                ('01', '01', '00', None, 'time'), 'datetime'),
                        'duration': (None, '1', None, None, None, None, None,
                                     'duration')}),
                      ('P1M/1981-04-05',
                       {'end': ('1981', '04', '05', None, None, None, 'date'),
                        'duration': (None, '1', None, None, None, None, None,
                                     'duration')}),
                      ('P1,5Y/2018-03-06',
                       {'end': ('2018', '03', '06', None, None, None, 'date'),
                        'duration': ('1.5', None, None, None, None, None, None,
                                     'duration')}),
                      ('P1.5Y/2018-03-06',
                       {'end': ('2018', '03', '06', None, None, None, 'date'),
                        'duration': ('1.5', None, None, None, None, None, None,
                                     'duration')}),
                      ('PT1H/2014-11-12',
                       {'end': ('2014', '11', '12', None, None, None, 'date'),
                        'duration': (None, None, None, None, '1', None, None,
                                     'duration')}),
                      ('PT4H54M6.5S/2014-11-12',
                       {'end': ('2014', '11', '12', None, None, None, 'date'),
                        'duration': (None, None, None, None, '4', '54', '6.5',
                                     'duration')}),
                      #Make sure we truncate, not round
                      #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
                      ('PT0.0000001S/2018-03-06',
                       {'end': ('2018', '03', '06', None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, None, None,
                                     '0.0000001', 'duration')}),
                      ('PT2.0000048S/2018-03-06',
                       {'end': ('2018', '03', '06', None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, None, None,
                                     '2.0000048', 'duration')}),
                      ('1981-04-05T01:01:00/P1M1DT1M',
                       {'start': (('1981', '04', '05',
                                   None, None, None, 'date'),
                                  ('01', '01', '00', None, 'time'),
                                  'datetime'),
                        'duration': (None, '1', None,
                                     '1', None, '1', None, 'duration')}),
                      ('1981-04-05/P1M1D',
                       {'start': ('1981', '04', '05',
                                  None, None, None, 'date'),
                        'duration': (None, '1', None,
                                     '1', None, None, None, 'duration')}),
                      ('2018-03-06/P2,5M',
                       {'start': ('2018', '03', '06',
                                  None, None, None, 'date'),
                        'duration': (None, '2.5', None,
                                     None, None, None, None, 'duration')}),
                      ('2018-03-06/P2.5M',
                       {'start': ('2018', '03', '06',
                                  None, None, None, 'date'),
                        'duration': (None, '2.5', None,
                                     None, None, None, None, 'duration')}),
                      ('2014-11-12/PT1H',
                       {'start': ('2014', '11', '12',
                                  None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, '1', None, None, 'duration')}),
                      ('2014-11-12/PT4H54M6.5S',
                       {'start': ('2014', '11', '12',
                                  None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, '4', '54', '6.5', 'duration')}),
                      #Make sure we truncate, not round
                      #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
                      ('2018-03-06/PT0.0000001S',
                       {'start': ('2018', '03', '06',
                                  None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, None, None,
                                     '0.0000001', 'duration')}),
                      ('2018-03-06/PT2.0000048S',
                       {'start': ('2018', '03', '06',
                                  None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, None, None,
                                     '2.0000048', 'duration')}),
                      ('1980-03-05T01:01:00/1981-04-05T01:01:00',
                       {'start': (('1980', '03', '05',
                                   None, None, None, 'date'),
                                  ('01', '01', '00',
                                   None, 'time'), 'datetime'),
                        'end': (('1981', '04', '05',
                                 None, None, None, 'date'),
                                ('01', '01', '00',
                                 None, 'time'), 'datetime')}),
                      ('1980-03-05T01:01:00/1981-04-05',
                       {'start': (('1980', '03', '05',
                                   None, None, None, 'date'),
                                  ('01', '01', '00',
                                   None, 'time'), 'datetime'),
                        'end': ('1981', '04', '05',
                                None, None, None, 'date')}),
                      ('1980-03-05/1981-04-05T01:01:00',
                       {'start': ('1980', '03', '05',
                                  None, None, None, 'date'),
                        'end': (('1981', '04', '05',
                                 None, None, None, 'date'),
                                ('01', '01', '00',
                                 None, 'time'), 'datetime')}),
                      ('1980-03-05/1981-04-05',
                       {'start': ('1980', '03', '05',
                                  None, None, None, 'date'),
                        'end': ('1981', '04', '05',
                                None, None, None, 'date')}),
                      ('1981-04-05/1980-03-05',
                       {'start': ('1981', '04', '05',
                                  None, None, None, 'date'),
                        'end': ('1980', '03', '05',
                                None, None, None, 'date')}),
                      #Make sure we truncate, not round
                      #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
                      ('1980-03-05T01:01:00.0000001/'
                       '1981-04-05T14:43:59.9999997',
                       {'start': (('1980', '03', '05',
                                   None, None, None, 'date'),
                                  ('01', '01', '00.0000001',
                                   None, 'time'), 'datetime'),
                        'end': (('1981', '04', '05',
                                 None, None, None, 'date'),
                                ('14', '43', '59.9999997', None, 'time'),
                                'datetime')}))

        for testtuple in testtuples:
            mockBuilder = mock.Mock()
            mockBuilder.build_interval.return_value = testtuple[1]

            result = _parse_interval(testtuple[0], mockBuilder)

            self.assertEqual(result, testtuple[1])
            mockBuilder.build_interval.assert_called_once_with(**testtuple[1])

        #Test different separators
        expectedargs = {'start': (('1980', '03', '05',
                                   None, None, None,
                                   'date'),
                                  ('01', '01', '00',
                                   None, 'time'),
                                  'datetime'),
                        'end': (('1981', '04', '05',
                                 None, None, None,
                                 'date'),
                                ('01', '01', '00',
                                 None, 'time'),
                                'datetime')}

        mockBuilder = mock.Mock()
        mockBuilder.build_interval.return_value = expectedargs

        result = _parse_interval('1980-03-05T01:01:00--1981-04-05T01:01:00',
                                 mockBuilder,
                                 intervaldelimiter='--')

        self.assertEqual(result, expectedargs)
        mockBuilder.build_interval.assert_called_once_with(**expectedargs)

        expectedargs = {'start': (('1980', '03', '05',
                                   None, None, None,
                                   'date'),
                                  ('01', '01', '00',
                                   None, 'time'),
                                  'datetime'),
                        'end': (('1981', '04', '05',
                                 None, None, None,
                                 'date'),
                                ('01', '01', '00',
                                 None, 'time'),
                                'datetime')}

        mockBuilder = mock.Mock()
        mockBuilder.build_interval.return_value = expectedargs

        _parse_interval('1980-03-05 01:01:00/1981-04-05 01:01:00',
                        mockBuilder,
                        datetimedelimiter=' ')

        self.assertEqual(result, expectedargs)
        mockBuilder.build_interval.assert_called_once_with(**expectedargs)
    def test_parse_interval_internal(self):
        #Test the internal _parse_interval function
        testtuples = (('P1M/1981-04-05T01:01:00',
                       {'end': (('1981', '04', '05', None, None, None, 'date'),
                                ('01', '01', '00', None, 'time'), 'datetime'),
                        'duration': (None, '1', None, None, None, None, None,
                                     'duration')}),
                      ('P1M/1981-04-05',
                       {'end': ('1981', '04', '05', None, None, None, 'date'),
                        'duration': (None, '1', None, None, None, None, None,
                                     'duration')}),
                      ('P1.5Y/2018-03-06',
                       {'end': ('2018', '03', '06', None, None, None, 'date'),
                        'duration': ('1.5', None, None, None, None, None, None,
                                     'duration')}),
                      ('PT1H/2014-11-12',
                       {'end': ('2014', '11', '12', None, None, None, 'date'),
                        'duration': (None, None, None, None, '1', None, None,
                                     'duration')}),
                      ('PT4H54M6.5S/2014-11-12',
                       {'end': ('2014', '11', '12', None, None, None, 'date'),
                        'duration': (None, None, None, None, '4', '54', '6.5',
                                     'duration')}),
                      #Make sure we truncate, not round
                      #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
                      ('PT0.0000001S/2018-03-06',
                       {'end': ('2018', '03', '06', None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, None, None,
                                     '0.0000001', 'duration')}),
                      ('PT2.0000048S/2018-03-06',
                       {'end': ('2018', '03', '06', None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, None, None,
                                     '2.0000048', 'duration')}),
                      ('1981-04-05T01:01:00/P1M1DT1M',
                       {'start': (('1981', '04', '05',
                                   None, None, None, 'date'),
                                  ('01', '01', '00', None, 'time'),
                                  'datetime'),
                        'duration': (None, '1', None,
                                     '1', None, '1', None, 'duration')}),
                      ('1981-04-05/P1M1D',
                       {'start': ('1981', '04', '05',
                                  None, None, None, 'date'),
                        'duration': (None, '1', None,
                                     '1', None, None, None, 'duration')}),
                      ('2018-03-06/P2.5M',
                       {'start': ('2018', '03', '06',
                                  None, None, None, 'date'),
                        'duration': (None, '2.5', None,
                                     None, None, None, None, 'duration')}),
                      ('2014-11-12/PT1H',
                       {'start': ('2014', '11', '12',
                                  None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, '1', None, None, 'duration')}),
                      ('2014-11-12/PT4H54M6.5S',
                       {'start': ('2014', '11', '12',
                                  None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, '4', '54', '6.5', 'duration')}),
                      #Make sure we truncate, not round
                      #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
                      ('2018-03-06/PT0.0000001S',
                       {'start': ('2018', '03', '06',
                                  None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, None, None,
                                     '0.0000001', 'duration')}),
                      ('2018-03-06/PT2.0000048S',
                       {'start': ('2018', '03', '06',
                                  None, None, None, 'date'),
                        'duration': (None, None, None,
                                     None, None, None,
                                     '2.0000048', 'duration')}),
                      ('1980-03-05T01:01:00/1981-04-05T01:01:00',
                       {'start': (('1980', '03', '05',
                                   None, None, None, 'date'),
                                  ('01', '01', '00',
                                   None, 'time'), 'datetime'),
                        'end': (('1981', '04', '05',
                                 None, None, None, 'date'),
                                ('01', '01', '00',
                                 None, 'time'), 'datetime')}),
                      ('1980-03-05T01:01:00/1981-04-05',
                       {'start': (('1980', '03', '05',
                                   None, None, None, 'date'),
                                  ('01', '01', '00',
                                   None, 'time'), 'datetime'),
                        'end': ('1981', '04', '05',
                                None, None, None, 'date')}),
                      ('1980-03-05/1981-04-05T01:01:00',
                       {'start': ('1980', '03', '05',
                                  None, None, None, 'date'),
                        'end': (('1981', '04', '05',
                                 None, None, None, 'date'),
                                ('01', '01', '00',
                                 None, 'time'), 'datetime')}),
                      ('1980-03-05/1981-04-05',
                       {'start': ('1980', '03', '05',
                                  None, None, None, 'date'),
                        'end': ('1981', '04', '05',
                                None, None, None, 'date')}),
                      ('1981-04-05/1980-03-05',
                       {'start': ('1981', '04', '05',
                                  None, None, None, 'date'),
                        'end': ('1980', '03', '05',
                                None, None, None, 'date')}),
                      #Make sure we truncate, not round
                      #https://bitbucket.org/nielsenb/aniso8601/issues/10/sub-microsecond-precision-in-durations-is
                      ('1980-03-05T01:01:00.0000001/'
                       '1981-04-05T14:43:59.9999997',
                       {'start': (('1980', '03', '05',
                                   None, None, None, 'date'),
                                  ('01', '01', '00.0000001',
                                   None, 'time'), 'datetime'),
                        'end': (('1981', '04', '05',
                                 None, None, None, 'date'),
                                ('14', '43', '59.9999997', None, 'time'),
                                'datetime')}))

        for testtuple in testtuples:
            mockBuilder = mock.Mock()
            mockBuilder.build_interval.return_value = testtuple[1]

            result = _parse_interval(testtuple[0], mockBuilder)

            self.assertEqual(result, testtuple[1])
            mockBuilder.build_interval.assert_called_once_with(**testtuple[1])

        #Test different separators
        expectedargs = {'start': (('1980', '03', '05',
                                   None, None, None,
                                   'date'),
                                  ('01', '01', '00',
                                   None, 'time'),
                                  'datetime'),
                        'end': (('1981', '04', '05',
                                 None, None, None,
                                 'date'),
                                ('01', '01', '00',
                                 None, 'time'),
                                'datetime')}

        mockBuilder = mock.Mock()
        mockBuilder.build_interval.return_value = expectedargs

        result = _parse_interval('1980-03-05T01:01:00--1981-04-05T01:01:00',
                                 mockBuilder,
                                 intervaldelimiter='--')

        self.assertEqual(result, expectedargs)
        mockBuilder.build_interval.assert_called_once_with(**expectedargs)

        expectedargs = {'start': (('1980', '03', '05',
                                   None, None, None,
                                   'date'),
                                  ('01', '01', '00',
                                   None, 'time'),
                                  'datetime'),
                        'end': (('1981', '04', '05',
                                 None, None, None,
                                 'date'),
                                ('01', '01', '00',
                                 None, 'time'),
                                'datetime')}

        mockBuilder = mock.Mock()
        mockBuilder.build_interval.return_value = expectedargs

        _parse_interval('1980-03-05 01:01:00/1981-04-05 01:01:00',
                        mockBuilder,
                        datetimedelimiter=' ')

        self.assertEqual(result, expectedargs)
        mockBuilder.build_interval.assert_called_once_with(**expectedargs)