diff options
author | Saul Shanabrook <s.shanabrook@gmail.com> | 2013-04-16 10:49:31 -0300 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-04-21 19:38:29 +0200 |
commit | b6ecfdf7b8f0b05b3e78fe3aafaf26d9c00c3259 (patch) | |
tree | e2b212ba77490b55fe6dc04ebc3f171b4d7ecc45 /factory | |
parent | 876845102c4a217496d0f6435bfe1e3726d31fe4 (diff) | |
download | factory-boy-b6ecfdf7b8f0b05b3e78fe3aafaf26d9c00c3259.tar factory-boy-b6ecfdf7b8f0b05b3e78fe3aafaf26d9c00c3259.tar.gz |
Add FuzzyDate
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'factory')
-rw-r--r-- | factory/fuzzy.py | 20 |
1 files changed, 20 insertions, 0 deletions
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)) |