From f4d52ab7bf5eae5f12f8dee300543bc827ed473b Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 4 Apr 2012 01:59:52 +0200 Subject: Add basic tests for dates. --- fastimport/dates.py | 9 ++++++--- fastimport/parser.py | 8 +++++--- fastimport/tests/__init__.py | 1 + fastimport/tests/test_dates.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 fastimport/tests/test_dates.py diff --git a/fastimport/dates.py b/fastimport/dates.py index c467ff7..c0cf400 100644 --- a/fastimport/dates.py +++ b/fastimport/dates.py @@ -35,18 +35,21 @@ def parse_raw(s, lineno=0): """ timestamp_str, timezone_str = s.split(' ', 1) timestamp = float(timestamp_str) - timezone = _parse_tz(timezone_str, lineno) + try: + timezone = parse_tz(timezone_str) + except ValueError: + raise errors.InvalidTimezone(lineno, timezone_str) return timestamp, timezone -def _parse_tz(tz, lineno): +def parse_tz(tz): """Parse a timezone specification in the [+|-]HHMM format. :return: the timezone offset in seconds. """ # from git_repository.py in bzr-git if len(tz) != 5: - raise errors.InvalidTimezone(lineno, tz) + raise ValueError(tz) sign = {'+': +1, '-': -1}[tz[0]] hours = int(tz[1:3]) minutes = int(tz[3:]) diff --git a/fastimport/parser.py b/fastimport/parser.py index ca8e294..88eecfa 100644 --- a/fastimport/parser.py +++ b/fastimport/parser.py @@ -162,9 +162,11 @@ The grammar is: import re import sys -import commands -import dates -import errors +from fastimport import ( + commands, + dates, + errors, + ) ## Stream parsing ## diff --git a/fastimport/tests/__init__.py b/fastimport/tests/__init__.py index 49fbbac..1d3a09e 100644 --- a/fastimport/tests/__init__.py +++ b/fastimport/tests/__init__.py @@ -23,6 +23,7 @@ import unittest def test_suite(): names = [ 'test_commands', + 'test_dates', 'test_errors', 'test_filter_processor', 'test_helpers', diff --git a/fastimport/tests/test_dates.py b/fastimport/tests/test_dates.py new file mode 100644 index 0000000..109318c --- /dev/null +++ b/fastimport/tests/test_dates.py @@ -0,0 +1,31 @@ +# Copyright (C) 2012 Canonical Ltd +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""Test parsing of dates.""" + +from testtools import TestCase + +from fastimport import ( + dates, + ) + +class ParseTzTests(TestCase): + + def test_parse_tz_utc(self): + self.assertEquals(0, dates.parse_tz("+0000")) + self.assertEquals(0, dates.parse_tz("-0000")) + + def test_parse_tz_cet(self): + self.assertEquals(3600, dates.parse_tz("+0100")) -- cgit v1.2.1