def test_get_categories(self): category1 = Category( title='Definition of Domestic Violence', help_text= "This is how a state legally defines the term 'domestic violence'", ) category2 = Category( title='Worker Protections', help_text= ('This category defines whether the state protects the jobs of victims of ' 'domestic violence'), ) category2.deactivate() Category.save_all([category1, category2]) response = self.client.get('/categories') 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': category1.id, 'title': 'Definition of Domestic Violence', 'help_text': "This is how a state legally defines the term 'domestic violence'", 'active': True, 'deactivated_at': None, }) category_2_expected = { 'id': category2.id, 'title': 'Worker Protections', 'help_text': 'This category defines whether the state protects the jobs of victims of ' 'domestic violence', 'active': False, } # Assert that the expected results are a subset of the actual results self.assertTrue( category_2_expected.items() <= json_response[1].items()) self.assertTrue(isinstance(json_response[1]['deactivated_at'], str))
class CategoryTestCase(unittest.TestCase): def setUp(self): self.client = app.test_client() self.category = Category( title='Definition of Domestic Violence', help_text= "This is how a state legally defines the term 'domestic violence'", ).save() def tearDown(self): clear_database(db) def test_init(self): self.assertEqual(self.category.title, 'Definition of Domestic Violence') self.assertEqual( self.category.help_text, "This is how a state legally defines the term 'domestic violence'", ) self.assertTrue(self.category.active) def test_serialize(self): 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, }, self.category.serialize()) 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)) def test_deactivate(self): self.category.deactivate() self.assertFalse(self.category.active) self.assertTrue( isinstance(self.category.deactivated_at, datetime.datetime)) self.assertTrue( self.category.deactivated_at < datetime.datetime.utcnow())