Exemple #1
0
    def setUp(self) -> None:
        self.app = create_app('testing')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        self.client = self.app.test_client()

        # Create user
        self.user = User.from_json(
            {'username': '******',
             'email': '*****@*****.**',
             'password': '******',
             'confirmed': True,
             'first_name': 'test_first_name',
             'last_name': 'test_last_name'}
        )
        db.session.add(self.user)

        # Login user
        response = self.client.post(
            url_for('api.login'),
            headers=self.get_api_headers('test_user', 'new_password')
        )
        self.token = response.json['token']

        # Create wallet
        self.wallet = Wallet.from_json({'title': 'test_wallet',
                                        'currency': 'usd',
                                        'initial_balance': randint(0, 100),
                                        'owner_id': self.user.id})
        db.session.add(self.wallet)
        db.session.commit()

        # Create parent category
        self.parent_category = ParentCategory.from_json({'title': 'test_parent_category',
                                                         'budget': randint(0, 1000),
                                                         'is_income': choice([True, False]),
                                                         'wallet_id': self.wallet.id})
        db.session.add(self.parent_category)
        db.session.commit()

        # Create category
        self.category = Category.from_json({'title': 'test_category',
                                            'budget': randint(0, 1000),
                                            'has_bills': choice([True, False]),
                                            'parent_category_id': self.parent_category.id})
        db.session.add(self.category)
        db.session.commit()

        self.data = {'title': 'test_category2',
                     'budget': randint(0, 1000),
                     'has_bills': choice([True, False]),
                     'parent_category_id': self.parent_category.id}
Exemple #2
0
    def test_get_all_categories(self):
        """
        The test case for get_all_categories view.
        """
        # Create 3 categories
        for i in range(3):
            c = Category.from_json({'title': 'test_parent_category{}'.format(i),
                                    'budget': randint(0, 1000),
                                    'has_bills': choice([True, False]),
                                    'parent_category_id': self.parent_category.id})
            db.session.add(c)
        db.session.commit()

        response = self.client.get(
            url_for('api.get_all_categories'),
            headers=self.get_token_headers(self.token)
        )

        self.assertTrue(response.status_code == 200)
        self.assertTrue(len(response.json['categories']) == 4)