def test_get_or_create(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): m = Estimator.get_or_create('new_original_object') m.save() self.assertEquals(m.estimator, 'new_original_object') n = Estimator.get_or_create('new_original_object') self.assertEquals(m, n)
def test_estimator_persistance(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): m = Estimator(estimator=object, description='another object') self.assertEqual(os.path.exists(m.estimator_file.path), False) self.assertEqual(m.is_estimator_persisted, False) m.save() self.assertEqual(os.path.exists(m.estimator_file.path), True) self.assertEqual(m.is_estimator_persisted, True)
def test_create_from_file(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): obj = "{'key': 'value'}" m = Estimator(estimator=obj) m.save() estimator_hash = m.estimator_hash file_path = m.estimator_file.name del m m = Estimator.create_from_file(file_path) self.assertEquals(m.estimator, obj) self.assertEquals(m.estimator_hash, estimator_hash)
def test_estimator_hash(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): m = Estimator(estimator=object, description='object test') m.save() self.assertEquals(m.estimator, object) del m n = Estimator.objects.get(description='object test') # sklearn hash of a object = 'd9c9f286391652b89978a6961b52b674' self.assertEqual(n.estimator_hash, 'd9c9f286391652b89978a6961b52b674') # assert loaded after calling n.estimator self.assertEquals(n.estimator, object) self.assertEquals(Estimator._compute_hash(object), 'd9c9f286391652b89978a6961b52b674')
def test_update_estimator_fail(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): m = Estimator(estimator='uneditable_object') m.estimator = 'object_edited_before_persistance' m.save() m.estimator = 'object_edited_after_persistance' self.assertRaises(ValidationError, m.save)
def test_estimator_hash_diff_fail(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): m = Estimator() m.estimator_hash = 'randomly set hash' self.assertRaises(ValidationError, m.save) m = Estimator(estimator='unique_object') m.estimator_hash = 'randomly set hash' self.assertRaises(ValidationError, m.save)
def test_hashing_func(self): estimator_hash = Estimator._compute_hash('abcd') self.assertEqual(estimator_hash, '3062a9e3345c129799bd2c1603c2e966')
def test_load_records(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): kept = Estimator(estimator='object to be kept3') kept.save() no_obj = Estimator(estimator='object to be deleted3') no_obj.save() no_file = Estimator(estimator='file to be deleted3') no_file.save() filename = no_obj.estimator_file.name no_obj.delete() del no_obj os.remove( os.path.join(self.tmp_dir.name, no_file.estimator_file.name)) del no_file # run tests unreferenced_files = Estimator.objects.unreferenced_files() total_count = Estimator.objects.count() self.assertEqual(unreferenced_files, {filename}) num = Estimator.load_unreferenced_files() self.assertEqual(num, 1) new_unreferenced_files = Estimator.objects.unreferenced_files() new_total_count = Estimator.objects.count() self.assertEqual(new_total_count, total_count + 1) # Note this does not reflect the original file. Instead it makes a duplicate file. self.assertEqual(len(unreferenced_files), len(new_unreferenced_files))
def test_empty_records(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): kept = Estimator(estimator='object to be kept2') kept.save() no_obj = Estimator(estimator='object to be deleted2') no_obj.save() no_file = Estimator(estimator='file to be deleted2') no_file.save() no_obj.delete() del no_obj os.remove( os.path.join(self.tmp_dir.name, no_file.estimator_file.name)) # run tests all_estimators = Estimator.objects.empty_records() self.assertEqual(all_estimators[0].estimator_hash, no_file.estimator_hash) deletion = Estimator.delete_empty_records() self.assertEqual(deletion[0], 1) all_estimators = Estimator.objects.empty_records() self.assertEqual(len(all_estimators), 0)
def test_unreferenced_files(self): with self.settings(MEDIA_ROOT=self.tmp_dir.name): kept = Estimator(estimator='object to be kept1') kept.save() no_obj = Estimator(estimator='object to be deleted1') no_obj.save() no_file = Estimator(estimator='file to be deleted1') no_file.save() filename = no_obj.estimator_file.name no_obj.delete() del no_obj os.remove( os.path.join(self.tmp_dir.name, no_file.estimator_file.name)) del no_file # run tests all_files = Estimator.objects.unreferenced_files() self.assertEqual(all_files, {filename}) num = Estimator.delete_unreferenced_files() self.assertEqual(num, 1) all_files = Estimator.objects.unreferenced_files() self.assertEqual(len(all_files), 0)