def test_get_by_index(self): index = ['some', 'index'] saved_export = SavedBasicExport(configuration=_mk_config(index=index)) saved_export.save() back = SavedBasicExport.by_index(index) self.assertEqual(1, len(back)) self.assertEqual(saved_export._id, back[0]._id)
def test_get_saved_and_delete_copies_multiple(self): index = ['multiple'] # make three exports with the last one being the most recently updated timestamp = datetime.datetime.utcnow() for i in range(3): saved_export = SavedBasicExport(configuration=_mk_config(index=index), last_updated=timestamp + datetime.timedelta(days=i)) saved_export.save() self.assertEqual(3, len(SavedBasicExport.by_index(index))) chosen_one = get_saved_export_and_delete_copies(index) # this relies on the variable being set last in the loop which is a bit unintuitive self.assertEqual(saved_export._id, chosen_one._id) saved_after_deletion = SavedBasicExport.by_index(index) self.assertEqual(1, len(saved_after_deletion)) self.assertEqual(chosen_one._id, saved_after_deletion[0]._id)
def test_get_by_index(self): index = ['some', 'index'] saved_export = SavedBasicExport(configuration=_mk_config(index=index)) saved_export.save() back = SavedBasicExport.by_index(index) self.assertEqual(1, len(back)) self.assertEqual(saved_export._id, back[0]._id)
def test_get_saved_and_delete_copies_multiple(self): index = ['multiple'] # make three exports with the last one being the most recently updated timestamp = datetime.datetime.utcnow() for i in range(3): saved_export = SavedBasicExport( configuration=_mk_config(index=index), last_updated=timestamp + datetime.timedelta(days=i)) saved_export.save() self.assertEqual(3, len(SavedBasicExport.by_index(index))) chosen_one = get_saved_export_and_delete_copies(index) # this relies on the variable being set last in the loop which is a bit unintuitive self.assertEqual(saved_export._id, chosen_one._id) saved_after_deletion = SavedBasicExport.by_index(index) self.assertEqual(1, len(saved_after_deletion)) self.assertEqual(chosen_one._id, saved_after_deletion[0]._id)
def get_saved_export_and_delete_copies(index): matching = SavedBasicExport.by_index(index) if not matching: return None if len(matching) == 1: return matching[0] else: # delete all matches besides the last updated match matching = sorted(matching, key=lambda x: x.last_updated) for match in matching[:-1]: match.delete() return matching[-1]
def get_saved_export_and_delete_copies(index): matching = SavedBasicExport.by_index(index) if not matching: return None if len(matching) == 1: return matching[0] else: # delete all matches besides the last updated match matching = sorted(matching, key=lambda x: x.last_updated) for match in matching[:-1]: match.delete() return matching[-1]