コード例 #1
0
    def test_08_api_fail_wrong_user(self):
        """(8) Test API fail, logged in as different user"""
        msgt(self.test_08_api_fail_wrong_user.__doc__)

        new_client = APIClient()
        other_user_obj, _created = get_user_model().objects.get_or_create(
            username='******')
        new_client.force_login(other_user_obj)

        dataset_info = DataSetInfo.objects.get(id=4)
        AnalysisPlanUtil.create_plan(dataset_info.object_id, self.user_obj)
        analysis_plan = AnalysisPlan.objects.first()

        # Send the dp_statistics for validation
        #
        request_plan = dict(analysis_plan_id=analysis_plan.object_id,
                            dp_statistics=[self.general_stat_spec])

        response = new_client.post('/api/validation/',
                                   data=request_plan,
                                   format='json')

        self.assertEqual(response.status_code, 400)
        self.assertFalse(response.json()['success'])
        self.assertEqual(response.json()['message'],
                         astatic.ERR_MSG_NO_ANALYSIS_PLAN)
コード例 #2
0
    def setUp(self):
        # test client
        self.client = APIClient()

        self.user_obj, _created = get_user_model().objects.get_or_create(
            username='******')

        self.client.force_login(self.user_obj)

        dataset_info = DataSetInfo.objects.get(id=4)

        plan_info = AnalysisPlanUtil.create_plan(dataset_info.object_id,
                                                 self.user_obj)
        self.assertTrue(plan_info.success)
        orig_plan = plan_info.data

        # Retrieve it
        #
        self.analysis_plan = AnalysisPlan.objects.first()
        self.assertEqual(orig_plan.object_id, self.analysis_plan.object_id)

        self.general_stat_spec = {
            "statistic": astatic.DP_MEAN,
            "variable": "EyeHeight",
            "epsilon": 1,
            "delta": 0,
            "cl": astatic.CL_95,
            "error": "",
            "missing_values_handling": astatic.MISSING_VAL_INSERT_FIXED,
            "handle_as_fixed": False,
            "fixed_value": "5.0",
            "locked": False,
            "label": "EyeHeight"
        }
コード例 #3
0
    def test_10_create_plan(self):
        """(10) Create AnalysisPlan directly"""
        msgt(self.test_10_create_plan.__doc__)

        dataset_info = DataSetInfo.objects.get(id=4)

        plan_util = AnalysisPlanUtil.create_plan(dataset_info.object_id,
                                                 self.user_obj)

        # did plan creation work?
        self.assertTrue(plan_util.success)

        # look at the plan data/defaults
        the_plan = plan_util.data

        # should have same user and dataset
        self.assertEqual(the_plan.analyst.object_id, self.user_obj.object_id)

        # check default settings
        self.assertEqual(the_plan.dataset.object_id, dataset_info.object_id)
        self.assertFalse(the_plan.is_complete)
        self.assertEqual(the_plan.user_step,
                         AnalysisPlan.AnalystSteps.STEP_0700_VARIABLES_CONFIRMED)
        self.assertEqual(the_plan.variable_info,
                         dataset_info.depositor_setup_info.variable_info)
        self.assertEqual(the_plan.dp_statistics, None)
コード例 #4
0
    def test_80_update_plan(self):
        """(80) Update AnalysisPlan"""
        msgt(self.test_80_update_plan.__doc__)

        dataset_info = DataSetInfo.objects.get(id=4)

        plan_util = AnalysisPlanUtil.create_plan(dataset_info.object_id,
                                                 self.user_obj)

        # did plan creation work?
        self.assertTrue(plan_util.success)

        #
        # Update the plan!
        #
        plan_object_id = plan_util.data.object_id
        payload = json.dumps(dict(dp_statistics=dict(hi='there')))
        response = self.client.patch(f'/api/analyze/{plan_object_id}/',
                                    payload,
                                    content_type='application/json')

        self.assertEqual(response.status_code, 200)
        jresp = response.json()

        self.assertEqual(jresp['dp_statistics'], dict(hi='there'))
コード例 #5
0
    def test_100_update_plan_bad_fields(self):
        """(100) Update AnalysisPlan, fail w/ bad fields"""
        msgt(self.test_100_update_plan_bad_fields.__doc__)

        dataset_info = DataSetInfo.objects.get(id=4)

        plan_util = AnalysisPlanUtil.create_plan(dataset_info.object_id,
                                                 self.user_obj)

        # did plan creation work?
        self.assertTrue(plan_util.success)

        #
        # Update the plan!
        #
        plan_object_id = plan_util.data.object_id

        payload = json.dumps(dict(name='haha'))
        response = self.client.patch(f'/api/analyze/{plan_object_id}/',
                                    payload,
                                    content_type='application/json')

        self.assertEqual(response.status_code, 400)

        jresp = response.json()
        self.assertTrue(jresp['message'].find(astatic.ERR_MSG_FIELDS_NOT_UPDATEABLE) > -1)
コード例 #6
0
    def test_20_fail_no_dataset_id(self):
        """(20) Fail b/c no dataset id"""
        msgt(self.test_20_fail_no_dataset_id.__doc__)

        plan_util = AnalysisPlanUtil.create_plan(None,
                                                 self.user_obj)
        self.assertFalse(plan_util.success)
        self.assertEqual(plan_util.message, astatic.ERR_MSG_DATASET_ID_REQUIRED)
コード例 #7
0
    def test_40_fail_bad_dataset_id(self):
        """(40) Fail b/c bad dataset id"""
        msgt(self.test_40_fail_bad_dataset_id.__doc__)

        nonsense_dataset_id = uuid.uuid4()
        plan_util = AnalysisPlanUtil.create_plan(nonsense_dataset_id,
                                                 self.user_obj)
        self.assertFalse(plan_util.success)
        self.assertEqual(plan_util.message, astatic.ERR_MSG_NO_DATASET)
コード例 #8
0
    def test_30_fail_no_user(self):
        """(30) Fail b/c no user"""
        msgt(self.test_30_fail_no_user.__doc__)

        dataset_info = DataSetInfo.objects.get(id=4)
        plan_util = AnalysisPlanUtil.create_plan(dataset_info.object_id,
                                                 None)
        self.assertFalse(plan_util.success)
        self.assertEqual(plan_util.message, astatic.ERR_MSG_USER_REQUIRED)
コード例 #9
0
    def run_preliminary_steps(self):
        """Run preliminary steps before validation"""

        # Retrieve the Analysis Plan
        #
        ap_info = AnalysisPlanUtil.retrieve_analysis(self.analysis_plan_id, self.opendp_user)
        if not ap_info.success:
            self.add_err_msg(ap_info.message)
            return False

        self.analysis_plan = ap_info.data

        # Check the dp_statistics spec
        #
        if self.compute_mode:
            # In compute mode, run the stats saved in the plan!
            #
            self.dp_statistics = self.analysis_plan.dp_statistics
            if not self.dp_statistics:
                user_msg = 'The AnalysisPlan does not contain "dp_statistics"'
                self.add_err_msg(user_msg)
                return False
            #
        elif not self.dp_statistics:
            #
            user_msg = 'There are no statistics to validate'
            self.add_err_msg(user_msg)
            return False

        dataset_size_info = self.analysis_plan.dataset.get_dataset_size()
        if dataset_size_info.success:
            self.dataset_size = dataset_size_info.data
        else:
            self.add_err_msg('Dataset size is not available')
            return False

        # Make sure the total epsilon is valid
        #
        self.max_epsilon = self.analysis_plan.dataset.get_depositor_setup_info().epsilon
        self.max_delta = self.analysis_plan.dataset.get_depositor_setup_info().delta

        epsilon_ok, _err_msg_or_None = self.is_epsilon_valid(self.max_epsilon)
        if not epsilon_ok:
            user_msg = f'{astatic.ERR_MSG_BAD_TOTAL_EPSILON}: {self.max_epsilon}'
            self.add_err_msg(user_msg)
            return False

        try:
            validate_not_negative(self.max_delta)
        except ValidationError as _err_obj:
            user_msg = f'{astatic.ERR_MSG_BAD_TOTAL_DELTA}: {self.max_delta}'
            self.add_err_msg(user_msg)
            return False


        return True
コード例 #10
0
    def test_05_api_fail_not_logged_in(self):
        """(5) Test API fail, not logged in"""
        msgt(self.test_05_api_fail_not_logged_in.__doc__)

        client = APIClient()
        dataset_info = DataSetInfo.objects.get(id=4)
        AnalysisPlanUtil.create_plan(dataset_info.object_id, self.user_obj)
        analysis_plan = AnalysisPlan.objects.first()

        # Send the dp_statistics for validation
        #
        request_plan = dict(analysis_plan_id=analysis_plan.object_id,
                            dp_statistics=[self.general_stat_spec])

        response = client.post('/api/validation/',
                               data=request_plan,
                               format='json')
        #print('response.status_code', response.status_code)
        #print('json', response.json())
        self.assertEqual(response.status_code, 403)
コード例 #11
0
    def retrieve_new_plan(self):
        """
        Convenience method to create a new plan
        """

        # Create a plan
        dataset_info = DataSetInfo.objects.get(id=4)
        plan_info = AnalysisPlanUtil.create_plan(dataset_info.object_id,
                                                 self.user_obj)
        self.assertTrue(plan_info.success)
        orig_plan = plan_info.data

        # Retrieve it
        analysis_plan = AnalysisPlan.objects.first()
        self.assertEqual(orig_plan.object_id, analysis_plan.object_id)

        return analysis_plan
コード例 #12
0
    def test_50_depositor_setup_incomplete(self):
        """(50) Fail b/c DepositorSetupInfo is incomplete"""
        msgt(self.test_50_depositor_setup_incomplete.__doc__)

        dataset_info = DataSetInfo.objects.get(id=4)
        dataset_info.depositor_setup_info.variable_info = None
        dataset_info.depositor_setup_info.save()

        self.assertEqual(dataset_info.depositor_setup_info.variable_info, None)


        plan_util = AnalysisPlanUtil.create_plan(dataset_info.object_id,
                                                 self.user_obj)

        # Plan should fail with error message
        self.assertFalse(plan_util.success)
        self.assertEqual(plan_util.message, astatic.ERR_MSG_SETUP_INCOMPLETE)
コード例 #13
0
    def create(self, request, *args, **kwargs):
        """
        Create an AnalysisPlan object with default values
        """
        # Is this a object_id a valid UUID?
        #
        ois = DatasetObjectIdSerializer(data=request.data)
        if not ois.is_valid():
            #print(ois.errors)
            if 'object_id' in ois.errors:
                user_msg = '"object_id" error: %s' % (
                    ois.errors['object_id'][0])
            else:
                user_msg = 'Not a valid "object_id"'
            return Response(get_json_error(user_msg),
                            status=status.HTTP_400_BAD_REQUEST)

        # Is there a related DataSetInfo where the logged in user is the
        #   DataSetInfo creator?
        #
        dsi_info = ois.get_dataset_info_with_user_check(request.user)
        if not dsi_info.success:
            return Response(get_json_error(dsi_info.message),
                            status=status.HTTP_404_NOT_FOUND)

        # Use the AnalysisPlanUtil to create an AnalysisPlan
        #   with default values
        #
        plan_util = AnalysisPlanUtil.create_plan(ois.get_object_id(),
                                                 request.user)

        # Did AnalysisPlan creation work?
        if plan_util.success:
            # Yes, it worked!
            new_plan = plan_util.data  # "data" holds the AnalysisPlan object
            serializer = AnalysisPlanSerializer(new_plan)  # serialize the data

            # Return it
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        # Nope! Error encountered!
        return Response(get_json_error(plan_util.message),
                        status=plan_util.data)
コード例 #14
0
    def setUp(self):
        # test client
        self.client = APIClient()

        self.user_obj, _created = get_user_model().objects.get_or_create(username='******')

        self.client.force_login(self.user_obj)

        dataset_info = DataSetInfo.objects.get(id=4)
        self.add_source_file(dataset_info, 'Fatigue_data.tab', True)

        plan_info = AnalysisPlanUtil.create_plan(dataset_info.object_id, self.user_obj)
        self.assertTrue(plan_info.success)
        orig_plan = plan_info.data

        # Retrieve it
        #
        self.analysis_plan = AnalysisPlan.objects.first()
        self.assertEqual(orig_plan.object_id, self.analysis_plan.object_id)

        self.analysis_plan.variable_info['EyeHeight']['min'] = -8.01
        self.analysis_plan.variable_info['EyeHeight']['max'] = 5

        self.analysis_plan.variable_info['TypingSpeed']['min'] = 3
        self.analysis_plan.variable_info['TypingSpeed']['max'] = 30

        # analysis_plan.variable_info = variable_info_mod
        self.analysis_plan.save()

        self.general_stat_specs = [
            {
                "statistic": astatic.DP_MEAN,
                "variable": "EyeHeight",
                "epsilon": .25,
                "delta": 0,
                "cl": astatic.CL_95,
                "error": "",
                "missing_values_handling": astatic.MISSING_VAL_INSERT_FIXED,
                "handle_as_fixed": False,
                "fixed_value": "1",
                "locked": False,
                "label": "EyeHeight",
                "variable_info": {
                    "min": 0,
                    "max": 100,
                    "type": pstatic.VAR_TYPE_FLOAT
                }
            },
            {
                "statistic": astatic.DP_MEAN,
                "variable": "TypingSpeed",
                "epsilon": .25,
                "delta": 0,
                "cl": astatic.CL_99,
                "error": "",
                "missing_values_handling": astatic.MISSING_VAL_INSERT_FIXED,
                "handle_as_fixed": False,
                "fixed_value": "9",
                "locked": False,
                "label": "TypingSpeed",
                "variable_info": {
                    "min": 0,
                    "max": 100,
                    "type": pstatic.VAR_TYPE_FLOAT
                }
            },
            {
                'variable': 'Subject',
                'col_index': 0,
                'statistic': astatic.DP_HISTOGRAM,
                'dataset_size': 183,
                'epsilon': 0.5,
                'delta': 0.0,
                'cl': astatic.CL_95,
                'missing_values_handling': astatic.MISSING_VAL_INSERT_FIXED,
                'fixed_value': 5,
                'variable_info': {
                    'min': 0,
                    'max': 5,
                    'categories': ['"ac"', '"kj"', '"ys"', '"bh1"', '"bh2"', '"jm"', '"mh"', '"cw"',
                                   '"jp"', '"rh"', '"aq"', '"ph"', '"le"', '"mn"', '"ls2"', '"no"', '"af"'],
                    'type': pstatic.VAR_TYPE_FLOAT
                }
             }
        ]