def test_save_duplicate(self): server_hostname = "simple.example.com" sync_info = IdentitySyncInfo(server_hostname=server_hostname) sync_info.last_sync = datetime.now(tzutc()) sync_info.save() self.assertEquals(len(IdentitySyncInfo.objects()), 1 ) dup = IdentitySyncInfo(server_hostname=server_hostname) dup.last_sync = datetime.now(tzutc()) caught = False try: dup.save() except: caught = True data = IdentitySyncInfo.objects() self.assertEquals(len(data), 1) self.assertTrue(caught)
def save_last_sync(server_hostname, timestamp): key = sanitize_key_for_mongo(server_hostname) sync = IdentitySyncInfo.objects(server_hostname=key).first() if not sync: sync = IdentitySyncInfo(server_hostname=key) sync.last_sync = timestamp try: sync.save() _LOG.info("Last sync saved: %s" % (sync)) except Exception, e: _LOG.exception(e) return False
def test_save_last_sync(self): server_hostname = "a.b.c.example.com" sync_info = IdentitySyncInfo(server_hostname=server_hostname) sync_info.last_sync = datetime.now(tzutc()) sync_info.save() key = utils.sanitize_key_for_mongo(server_hostname) lookup = IdentitySyncInfo.objects(server_hostname=key) self.assertIsNotNone(lookup) self.assertEquals(len(lookup), 1) created = sync_info.last_sync found = lookup[0].last_sync self.assertEquals(created.year, found.year) self.assertEquals(created.month, found.month) self.assertEquals(created.day, found.day) self.assertEquals(created.hour, found.hour) self.assertEquals(created.minute, found.minute) self.assertEquals(created.second, found.second)
def get_last_sync_timestamp(server_hostname): key = sanitize_key_for_mongo(server_hostname) sync = IdentitySyncInfo.objects(server_hostname=key).first() if not sync: return None return sync.last_sync