Exemplo n.º 1
0
    def test_get_category(self):
        category = create_category()
        create_subcategory(category.id)

        response = self.client.get(f'/categories/{category.id}')
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data)
        self.assertEqual(json_response, category.serialize())
Exemplo n.º 2
0
    def test_get_categories_with_subcategories(self):
        category = create_category()
        create_subcategory(category.id)
        expected_response = [category.serialize(with_subcategories=True)]

        response = self.client.get('/categories?withSubcategories=true')
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data)
        self.assertEqual(json_response, expected_response)
Exemplo n.º 3
0
    def test_get_subcategories(self):
        subcategory1 = create_subcategory(self.category.id)
        subcategory2 = create_subcategory(self.category.id)

        response = self.client.get('/subcategories')
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data)
        self.assertEqual(json_response, [
            subcategory1.serialize(),
            subcategory2.serialize(),
        ])
Exemplo n.º 4
0
    def test_put_subcategory(self, mock_auth):
        category_id = self.category.id
        subcategory = create_subcategory(category_id)

        data = {
            'title': 'A New Title',
            'help_text': 'Some new help text',
        }

        response = self.client.put(
            f'/subcategories/{subcategory.id}',
            json=data,
            headers=auth_headers(),
        )
        self.assertEqual(response.status_code, 200)
        mock_auth.assert_called_once()

        subcategory = Subcategory.query.get(subcategory.id)
        self.assertEqual(subcategory.category_id, category_id)
        self.assertEqual(subcategory.title, 'A New Title')
        self.assertEqual(subcategory.help_text, 'Some new help text')
        self.assertTrue(subcategory.active)
        self.assertIsNone(subcategory.deactivated_at)

        json_response = json.loads(response.data)
        self.assertEqual(json_response, subcategory.serialize())
Exemplo n.º 5
0
    def test_put_subcategory_deactivate(self, mock_auth):
        subcategory = create_subcategory(self.category.id)

        data = {'active': False}
        response = self.client.put(
            f'/subcategories/{subcategory.id}',
            json=data,
            headers=auth_headers(),
        )
        self.assertEqual(response.status_code, 200)
        mock_auth.assert_called_once()

        subcategory = Subcategory.query.first()
        self.assertFalse(subcategory.active)
        self.assertTrue(isinstance(subcategory.deactivated_at, datetime.datetime))

        # Subcategory cannot be reactivated
        deactivated_at = subcategory.deactivated_at

        data = {'active': True}
        response = self.client.put(
            f'/subcategories/{subcategory.id}',
            json=data,
            headers=auth_headers(),
        )
        subcategory = Subcategory.query.first()
        self.assertFalse(subcategory.active)
        self.assertEqual(subcategory.deactivated_at, deactivated_at)
Exemplo n.º 6
0
    def test_get_subcategory_with_criteria(self):
        subcategory = create_subcategory(self.category.id)

        response = self.client.get(f'/subcategories/{subcategory.id}?withCriteria=true')
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.data)
        self.assertEqual(json_response, subcategory.serialize(with_criteria=True))
Exemplo n.º 7
0
 def setUp(self):
     self.client = app.test_client()
     self.category = create_category()
     self.subcategory = create_subcategory(self.category.id)
     self.state1_code = 'NY'
     self.state2_code = 'AZ'
     create_state(code=self.state1_code)
     create_state(code=self.state2_code)
Exemplo n.º 8
0
 def setUp(self):
     self.state = create_state()
     self.category = create_category()
     self.subcategory = create_subcategory(self.category.id)
     self.criterion = create_criterion(self.subcategory.id)
     self.score = Score(
         criterion_id=self.criterion.id,
         state=self.state.code,
         meets_criterion=True,
     ).save()
Exemplo n.º 9
0
 def setUp(self):
     self.state_code = 'NY'
     create_state(code=self.state_code)
     self.category = create_category()
     self.subcategory = create_subcategory(self.category.id)
     self.link = Link(
         subcategory_id=self.subcategory.id,
         state=self.state_code,
         text='Section 20 of Statute 39-B',
         url='ny.gov/link/to/statute',
     ).save()
Exemplo n.º 10
0
 def test_serialize_with_subcategories(self):
     subcategory1 = create_subcategory(self.category.id)
     subcategory2 = create_subcategory(self.category.id)
     self.assertEqual(
         {
             'id':
             self.category.id,
             'title':
             'Definition of Domestic Violence',
             'help_text':
             "This is how a state legally defines the term 'domestic violence'",
             'active':
             True,
             'deactivated_at':
             None,
             'subcategories': [
                 subcategory1.serialize(),
                 subcategory2.serialize(),
             ]
         }, self.category.serialize(with_subcategories=True))
Exemplo n.º 11
0
    def test_put_subcategory_cannot_change_category(self, mock_auth):
        category_id = self.category.id
        subcategory = create_subcategory(category_id)

        data = {'category_id': subcategory.category_id + 1}
        response = self.client.put(
            f'/subcategories/{subcategory.id}',
            json=data,
            headers=auth_headers(),
        )
        self.assertEqual(response.status_code, 400)
        mock_auth.assert_called_once()

        json_response = json.loads(response.data)
        self.assertEqual(json_response['description'], strings.cannot_change_category)
Exemplo n.º 12
0
    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()
Exemplo n.º 13
0
 def setUp(self):
     self.client = app.test_client()
     self.category = create_category()
     self.subcategory = create_subcategory(self.category.id)
Exemplo n.º 14
0
    def setUp(self):
        self.client = app.test_client()

        self.state = State(
            code='NY',
            name='New York',
            innovative_idea='Innovative idea',
            honorable_mention='Honorable mention',
        ).save()
        other_state = create_state(code='AZ')

        category1 = create_category()
        category2 = create_category()
        subcategory = create_subcategory(category1.id)
        criterion1 = create_criterion(subcategory.id)
        criterion2 = create_criterion(subcategory.id)

        self.link1 = create_link(subcategory.id, self.state.code)
        self.link2 = create_link(subcategory.id, self.state.code)

        self.state_grade1 = create_state_grade(self.state.code)
        self.state_grade2 = create_state_grade(self.state.code)

        # state_grade1 is more recent than state_grade2
        self.state_grade1.created_at = datetime.utcnow()
        self.state_grade2.created_at = datetime.utcnow() - timedelta(5)

        self.state_category_grade1 = create_state_category_grade(
            self.state.code, category1.id)
        self.state_category_grade2 = create_state_category_grade(
            self.state.code, category1.id)
        self.state_category_grade3 = create_state_category_grade(
            self.state.code, category2.id)

        # state_category_grade1 is more recent than state_category_grade2
        self.state_category_grade1.created_at = datetime.utcnow()
        self.state_category_grade2.created_at = datetime.utcnow() - timedelta(
            5)

        self.score1 = Score(
            criterion_id=criterion1.id,
            state=self.state.code,
            meets_criterion=True,
        )
        self.score2 = Score(
            criterion_id=criterion2.id,
            state=self.state.code,
            meets_criterion=False,
        )
        self.score3 = Score(
            criterion_id=criterion2.id,
            state=self.state.code,
            meets_criterion=True,
        )
        # score2 is more recent than score3
        self.score3.created_at = datetime.utcnow() - timedelta(5)
        self.score4 = Score(
            criterion_id=criterion2.id,
            state=other_state.code,
            meets_criterion=True,
        )

        Score.save_all([self.score1, self.score2, self.score3, self.score4])
        self.maxDiff = None
Exemplo n.º 15
0
 def setUp(self):
     self.client = app.test_client()
     self.category = create_category()
     self.subcategory = create_subcategory(self.category.id)
     self.criterion = create_criterion(self.subcategory.id)
     self.state = create_state()