def test_generate_month_list_same_year(self): date0 = Mongotools.TimeObject( datetime.datetime(year=2011, month=2, day=1)) date1 = Mongotools.TimeObject( datetime.datetime(year=2011, month=5, day=1)) backup = Backup.BackupHelper(date0, date1) month_list = [(2011, k) for k in xrange(2, 6)] self.assertEqual(list(backup.iterate_months()), month_list)
def test_iterate_timestamp_ranges(self): date0 = Mongotools.TimeObject( datetime.datetime(year=2011, month=2, day=1)) date1 = Mongotools.TimeObject( datetime.datetime(year=2011, month=5, day=1)) backup = Backup.BackupHelper(date0, date1) expected_dateranges = [(1296518400000, 1298937599999), (1298937600000, 1301615999999), (1301616000000, 1304207999999), (1304208000000, 1306886399999)] calculated_dateranges = list(backup.iterate_timestamp_ranges()) self.assertEqual(expected_dateranges, calculated_dateranges)
def test_iterate_monthly_date_ranges(self): date0 = Mongotools.TimeObject( datetime.datetime(year=2011, month=2, day=1)) date1 = Mongotools.TimeObject( datetime.datetime(year=2011, month=5, day=1)) backup = Backup.BackupHelper(date0, date1) expected_dateranges = [ (datetime.datetime(2011, 2, 1), datetime.datetime(2011, 2, 28, 23, 59, 59, 999999)), (datetime.datetime(2011, 3, 1), datetime.datetime(2011, 3, 31, 23, 59, 59, 999999)), (datetime.datetime(2011, 4, 1), datetime.datetime(2011, 4, 30, 23, 59, 59, 999999)), (datetime.datetime(2011, 5, 1), datetime.datetime(2011, 5, 31, 23, 59, 59, 999999)) ] calculated_dateranges = list(backup.iterate_monthly_date_ranges()) self.assertEqual(calculated_dateranges, expected_dateranges)
def get_startTimeObject(self): if self.startMonth: month = int(self.startMonth[0:2]) year = int(self.startMonth[2:]) timezone_utc = Utilities.tz.tzutc() startDatetime = datetime.datetime(day=01, month=month, year=year, tzinfo=timezone_utc) else: startDatetime = None return Mongotools.TimeObject(startDatetime)
def __init__(self, host='localhost', db='tmp', col='tmp', port=27017, startTimeObject=None): self.connection = pymongo.Connection(host=host, port=port) self.col = self.connection[db][col] num_objects_to_backup = self.col.count() if num_objects_to_backup > 0: if startTimeObject: self.first_date = startTimeObject else: self.first_date = Mongotools.TimeObject( list(self.col.find().sort( "timestamp", pymongo.ASCENDING).limit(1))[0]["timestamp"]) self.last_date = Mongotools.TimeObject( list(self.col.find().sort( "timestamp", pymongo.DESCENDING).limit(1))[0]["timestamp"]) else: raise EmptyCollectionError("This collection is empty") self.date_operations = BackupHelper(self.first_date, self.last_date)
def iterate_timestamp_ranges(self): for (date0, date1) in self.iterate_monthly_date_ranges(): tst0 = Mongotools.TimeObject(date0).get_mongotimestamp() tst1 = Mongotools.TimeObject(date1).get_mongotimestamp() yield (tst0, tst1)
def test_convert_datetime2tst(self): for timestamp, dtime in zip(self.mongotsts, self.datetimes): tst = Mongotools.TimeObject(dtime) self.assertEqual(tst.get_mongotimestamp(), timestamp)
def test_convert_tst2datetime(self): for timestamp, dtime in zip(self.mongotsts, self.datetimes): tst = Mongotools.TimeObject(timestamp) self.assertEqual(tst.get_datetime(), dtime)