def update_or_create_criterion(data, criterion=None):
    '''
    Takes a dict of data where the keys are fields of the criterion model.
    Valid keys are subcategory_id, title, recommendation_text, help_text, adverse,
    and active. The 'active' key only uses a False value.

    Once created, a criterion's subcategory cannot be changed.
    '''
    subcategory_id = data.get('subcategory_id')
    if criterion is None:
        criterion = Criterion(subcategory_id=subcategory_id)
    elif subcategory_id is not None and subcategory_id != criterion.subcategory_id:
        raise ValueError(strings.cannot_change_subcategory)

    if 'title' in data:
        criterion.title = data['title']
    if 'recommendation_text' in data:
        criterion.recommendation_text = data['recommendation_text']
    if 'help_text' in data:
        criterion.help_text = data['help_text']
    if 'adverse' in data:
        criterion.adverse = data['adverse']

    # You cannot reactivate a criterion after deactivating it
    if 'active' in data and not data['active']:
        criterion.deactivate()

    return criterion.save()
示例#2
0
    def test_get_criteria(self):
        criterion1 = Criterion(
            subcategory_id=self.subcategory.id,
            title='Includes economic abuse framework',
            recommendation_text=
            ("The state's definition of domestic violence should include a framework of "
             'economic abuse'),
            help_text=
            ('This means that the state acknowledges the role that economic control and abuse '
             'can play in domestic violence'),
            adverse=False,
        )

        criterion2 = Criterion(
            subcategory_id=self.subcategory.id,
            title='Uses coercive control framework',
            recommendation_text=
            ("The state's definition of domestic violence should use a framework of coercive "
             'control'),
            help_text=
            ('This means that the state acknowledges the role that coercion can play in '
             'domestic violence'),
            adverse=True,
        )

        criterion2.deactivate()
        Criterion.save_all([criterion1, criterion2])

        response = self.client.get('/criteria')
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.data.decode('utf-8'))

        self.assertEqual(len(json_response), 2)
        self.assertEqual(
            json_response[0], {
                'id':
                criterion1.id,
                'subcategory_id':
                criterion1.subcategory_id,
                'title':
                'Includes economic abuse framework',
                'recommendation_text':
                "The state's definition of domestic violence should include a framework of "
                'economic abuse',
                'help_text':
                'This means that the state acknowledges the role that economic control and abuse '
                'can play in domestic violence',
                'active':
                True,
                'deactivated_at':
                None,
                'adverse':
                False,
            })

        criterion2_expected = {
            'id':
            criterion2.id,
            'subcategory_id':
            criterion2.subcategory_id,
            'title':
            'Uses coercive control framework',
            'recommendation_text':
            "The state's definition of domestic violence should use a framework of coercive "
            'control',
            'help_text':
            'This means that the state acknowledges the role that coercion can play in '
            'domestic violence',
            'active':
            False,
            'adverse':
            True,
        }

        # Assert that the expected results are a subset of the actual results
        self.assertTrue(
            criterion2_expected.items() <= json_response[1].items())
        self.assertTrue(isinstance(json_response[1]['deactivated_at'], str))
示例#3
0
class CriterionTestCase(unittest.TestCase):
    def setUp(self):
        self.client = app.test_client()

        self.category = create_category()
        self.subcategory = create_subcategory(self.category.id)
        self.criterion = Criterion(
            subcategory_id=self.subcategory.id,
            title='Includes economic abuse framework',
            recommendation_text=(
                "The state's definition of domestic violence should include a framework of "
                'economic abuse'
            ),
            help_text=(
                'This means that the state acknowledges the role that economic control and abuse '
                'can play in domestic violence'
            ),
            adverse=False,
        ).save()

    def tearDown(self):
        clear_database(db)

    def test_init(self):
        self.assertEqual(self.criterion.subcategory_id, self.subcategory.id)
        self.assertEqual(self.criterion.title, 'Includes economic abuse framework')
        self.assertEqual(
            self.criterion.recommendation_text,
            "The state's definition of domestic violence should include a framework of economic "
            'abuse',
        )
        self.assertEqual(
            self.criterion.help_text,
            'This means that the state acknowledges the role that economic control and abuse can '
            'play in domestic violence',
        ),
        self.assertTrue(self.criterion.active)
        self.assertFalse(self.criterion.adverse)

    def test_init_default_adverse_value(self):
        criterion = Criterion(subcategory_id=self.subcategory.id)
        self.assertFalse(criterion.adverse)

    def test_init_invalid_subcategory(self):
        with self.assertRaises(ValueError) as e:
            Criterion(
                subcategory_id=0,
                title='Includes economic abuse framework',
                recommendation_text=(
                    "The state's definition of domestic violence should include a framework of "
                    'economic abuse'
                ),
                help_text=(
                    'This means that the state acknowledges the role that economic control and '
                    'abuse can play in domestic violence'
                ),
                adverse=False,
            )
        self.assertEqual(str(e.exception), subcategory_not_found)

    def test_serialize(self):
        self.assertEqual(
            {
                'id': self.criterion.id,
                'subcategory_id': self.subcategory.id,
                'title': 'Includes economic abuse framework',
                'recommendation_text':
                    "The state's definition of domestic violence should include a framework of "
                    'economic abuse',
                'help_text':
                    'This means that the state acknowledges the role that economic control and '
                    'abuse can play in domestic violence',
                'active': True,
                'deactivated_at': None,
                'adverse': False,
            },
            self.criterion.serialize()
        )

    def test_deactivate(self):
        self.criterion.deactivate()

        self.assertFalse(self.criterion.active)
        self.assertTrue(isinstance(self.criterion.deactivated_at, datetime.datetime))
        self.assertTrue(self.criterion.deactivated_at < datetime.datetime.utcnow())