<commit_before>import unittest
from flask import json
from api import db
from api.BucketListAPI import app
from instance.config import application_config


class ItemTestCase(unittest.TestCase):
    def setUp(self):
        app.config.from_object(application_config['TestingEnv'])
        self.client = app.test_client()

        # Binds the app to current context
        with app.app_context():
            # Create all tables
            db.create_all()

        user = json.dumps({
            'email': '<EMAIL>',
            'password': '<PASSWORD>',
            'name': 'Patrick'
        })
        response = self.client.post('/auth/register', data=user)
        json_repr = json.loads(response.data.decode())
        self.token = json_repr['id']

    def test_add_item_with_no_name(self):
        """Should return 400 for missing item name"""
        item = json.dumps({'item': ''})
        response = self.client.post('/buckets/1/items', data=item,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('Missing Item name', response.data.decode())

    def test_add_item_when_bucket_doesnt_exist(self):
        """Should return 400 for missing bucket"""
        item = json.dumps({'item': 'Go to Nairobi'})
        response = self.client.post('/buckets/1/items', data=item,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('Bucket with id 1 not found', response.data.decode())

    def test_add_item_successfully(self):
        """Should return 201 for item added"""

        # First add the bucket
        bucket = json.dumps({
            'bucket': 'Travel',
            'desc': 'Visit places'
        })
        self.client.post('/bucket', data=bucket,
                                    headers={"Authorization": self.token})

        item = json.dumps({'item': 'Go to Nairobi'})
        response = self.client.post('/buckets/1/items', data=item,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 201)
        self.assertIn('Successfully Added item', response.data.decode())


    def test_add_duplicate_item(self):
        """Should return 400 for duplicate item"""

        # First add the item
        self.test_add_item_successfully()
        item = json.dumps({'item': 'Go to Nairobi'})
        response = self.client.post('/buckets/1/items', data=item,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('item name Already exists', response.data.decode())

    def tearDown(self):
        # Drop all tables
        with app.app_context():
            # Drop all tables
            db.session.remove()
            db.drop_all()


if __name__ == '__main__':
    unittest.main()
<commit_msg>Add tests for edit item<commit_after>import unittest
from flask import json
from api import db
from api.BucketListAPI import app
from instance.config import application_config


class ItemTestCase(unittest.TestCase):
    def setUp(self):
        app.config.from_object(application_config['TestingEnv'])
        self.client = app.test_client()

        # Binds the app to current context
        with app.app_context():
            # Create all tables
            db.create_all()

        user = json.dumps({
            'email': '<EMAIL>',
            'password': '<PASSWORD>',
            'name': 'Patrick'
        })
        response = self.client.post('/auth/register', data=user)
        json_repr = json.loads(response.data.decode())
        self.token = json_repr['id']

    def test_add_item_with_no_name(self):
        """Should return 400 for missing item name"""
        item = json.dumps({'item': ''})
        response = self.client.post('/buckets/1/items', data=item,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('Missing Item name', response.data.decode())

    def test_add_item_when_bucket_doesnt_exist(self):
        """Should return 400 for missing bucket"""
        item = json.dumps({'item': 'Go to Nairobi'})
        response = self.client.post('/buckets/1/items', data=item,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('Bucket with id 1 not found', response.data.decode())

    def test_add_item_successfully(self):
        """Should return 201 for item added"""

        # First add the bucket
        bucket = json.dumps({
            'bucket': 'Travel',
            'desc': 'Visit places'
        })
        self.client.post('/bucket', data=bucket,
                         headers={"Authorization": self.token})

        item = json.dumps({'item': 'Go to Nairobi'})
        response = self.client.post('/buckets/1/items', data=item,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 201)
        self.assertIn('Successfully Added item', response.data.decode())

    def test_add_duplicate_item(self):
        """Should return 400 for duplicate item"""

        # First add the item
        self.test_add_item_successfully()
        item = json.dumps({'item': 'Go to Nairobi'})
        response = self.client.post('/buckets/1/items', data=item,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('item name Already exists', response.data.decode())

    def test_edit_item_with_no_name(self):
        """Should return 400 for missing item name"""

        item = json.dumps({'item': ''})
        response = self.client.put('/buckets/1/items/1', data=item,
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('Missing Item name', response.data.decode())

    def test_edit_item_with_missing_bucket(self):
        """Should return 400 for missing bucket"""

        item = json.dumps({'item': 'Go to New York'})
        response = self.client.put('/buckets/1/items/1', data=item,
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('Bucket with id 1 not found', response.data.decode())

    def test_edit_item_with_missing_item(self):
        """Should return 400 for missing item"""

        # First add the bucket
        bucket = json.dumps({
            'bucket': 'Travel',
            'desc': 'Visit places'
        })
        self.client.post('/bucket', data=bucket,
                         headers={"Authorization": self.token})
        item = json.dumps({'item': 'Go to New York'})
        response = self.client.put('/buckets/1/items/1', data=item,
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('item with id 1 does not exist',
                      response.data.decode())

    def test_edit_item_succesfully(self):
        """Should return 400 for missing bucket"""

        self.test_add_item_successfully()
        item = json.dumps({'item': 'Go to Silicon Valley'})
        response = self.client.put('/buckets/1/items/1', data=item,
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 201)
        self.assertIn('Successfully updated item', response.data.decode())
        self.assertIn('Go to Silicon Valley', response.data.decode())

    def tearDown(self):
        # Drop all tables
        with app.app_context():
            # Drop all tables
            db.session.remove()
            db.drop_all()


if __name__ == '__main__':
    unittest.main()

{'fraction_non_alphanumeric': 0.12613962782565255, 'fraction_numerical': 0.011489946296990133, 'mean_word_length': 2.2212389380530975, 'pattern_counts': {'":': 15, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4695813', 'n_tokens_mistral': 2292, 'n_tokens_neox': 2158, 'n_words': 501}