def test_estimator_persistance_without_factory(self):
        m = Estimator(estimator='new string', description='another object')
        assert m.object_file.storage.exists(m.object_file.path) == False
        assert m.is_file_persisted == False

        m.save()
        assert m.object_file.storage.exists(m.object_file.path) == True
        assert m.is_file_persisted == True
 def test_update_estimator_fail(self):
     m = Estimator(estimator='uneditable_object')
     m.estimator = 'object_edited_before_persistance'
     m.save()
     m.estimator = 'object_edited_after_persistance'
     with pytest.raises(ValidationError):
         m.save()
Example #3
0
 def persist_results(self, er):
     er._estimator_proxy = Estimator.get_or_create(
         er._estimator_proxy.estimator)
     er._X_test_proxy = DataSet.get_or_create(er._X_test_proxy.data)
     er._y_test_proxy = DataSet.get_or_create(er._y_test_proxy.data)
     er._y_predicted_proxy = DataSet.get_or_create(
         er._y_predicted_proxy.data)
     er.save()
    def test_create_from_file_with_factory(self):
        obj = "{'key': 'value'}"
        m = EstimatorFactory(estimator=obj)
        object_hash = m.object_hash
        file_path = m.object_file.name
        del m

        m = Estimator.create_from_file(file_path)
        assert m.estimator == obj
        assert m.object_hash == object_hash
        assert m.is_file_persisted == True
    def test_object_hash_with_factory(self):
        m = EstimatorFactory(estimator=object)
        assert m.estimator == object
        del m

        n = Estimator.objects.filter(estimator=object).first()
        # sklearn hash of a object = 'd9c9f286391652b89978a6961b52b674'
        assert n.object_hash == 'd9c9f286391652b89978a6961b52b674'
        # assert loaded after calling n.estimator
        assert n.estimator == object
        assert Estimator._compute_hash(
            object) == 'd9c9f286391652b89978a6961b52b674'
Example #6
0
    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.object_file.name
            no_obj.delete()
            del no_obj

            os.remove(
                os.path.join(
                    self.tmp_dir.name,
                    no_file.object_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))
Example #7
0
    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.object_file.name))

            # run tests
            all_estimators = Estimator.objects.empty_records()
            self.assertEqual(
                all_estimators[0].object_hash, no_file.object_hash)

            deletion = Estimator.delete_empty_records()
            self.assertEqual(deletion[0], 1)

            all_estimators = Estimator.objects.empty_records()
            self.assertEqual(len(all_estimators), 0)
Example #8
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.object_file.name
            no_obj.delete()
            del no_obj

            os.remove(
                os.path.join(
                    self.tmp_dir.name,
                    no_file.object_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)
 def test_wrong_hash_fail(self):
     m = Estimator(estimator='unique_object')
     m.object_hash = 'randomly set hash'
     with pytest.raises(ValidationError):
         m.save()
 def test_hash_without_estimator_fail(self):
     m = Estimator()
     m.object_hash = 'randomly set hash'
     with pytest.raises(ValidationError):
         m.save()
 def test_hashing_func(self):
     object_hash = Estimator._compute_hash('abcd')
     assert object_hash == '3062a9e3345c129799bd2c1603c2e966'