From b6ecfdf7b8f0b05b3e78fe3aafaf26d9c00c3259 Mon Sep 17 00:00:00 2001 From: Saul Shanabrook Date: Tue, 16 Apr 2013 10:49:31 -0300 Subject: Add FuzzyDate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaƫl Barrois --- factory/fuzzy.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'factory/fuzzy.py') diff --git a/factory/fuzzy.py b/factory/fuzzy.py index 186b4a7..fea7b05 100644 --- a/factory/fuzzy.py +++ b/factory/fuzzy.py @@ -25,6 +25,7 @@ import random +import datetime from . import declarations @@ -84,3 +85,22 @@ class FuzzyInteger(BaseFuzzyAttribute): def fuzz(self): return random.randint(self.low, self.high) + + +class FuzzyDate(BaseFuzzyAttribute): + """Random date within a given date range.""" + def __init__(self, start_date, end_date=None, **kwargs): + super(FuzzyDate, self).__init__(**kwargs) + if end_date is None: + end_date = datetime.date.today() + + if start_date > end_date: + raise ValueError( + "FuzzyDate boundaries should have start <= end; got %r > %r." + % (start_date, end_date)) + + self.start_date = start_date.toordinal() + self.end_date = end_date.toordinal() + + def fuzz(self): + return datetime.date.fromordinal(random.randint(self.start_date, self.end_date)) -- cgit v1.2.3