<gh_stars>1-10
import unittest
from flask import json
from api import db
from api.shoppinglistApi import app
from instance.config import application_config


class ShoppingListTestCase(unittest.TestCase):
    def setUp(self):
        app.config.from_object(application_config['TestingEnv'])
        self.client = app.test_client()
        self.shoppinglist = {'shoppinglist': 'Go to Goborola'}
        

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

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

    def test_add_shoppinglist_without_name(self):
        """Should return 400 for missing shoppinglist name"""
        shoppinglist= json.dumps({
            'name': '',
            'desc': 'travel'
        })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('Missing name', response.data.decode())

    def test_add_shoppinglist_successfully(self):
        """Should return 201 for shoppinglist added"""
        shoppinglist = json.dumps({
            'name': 'Travel',
             'desc': 'Visit places'
         })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})

       
        self.assertEqual(response.status_code, 201)
        print(str(response.data.decode()))
        self.assertIn('travel', response.data.decode())

        


    def test_add_shoppinglist_with_existing_shoppinglist_name(self):
        """Should return 400 for existing shoppinglist name"""

        # First Add shoppinglist
        self.test_add_shoppinglist_successfully()
        shoppinglist = json.dumps({
            'name': 'Travel',
            'desc': 'travel'
        })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})
        (response.data.decode())
        self.assertEqual(response.status_code, 409)
        self.assertIn('Shoppinglist name Already exists', response.data.decode())
                                           
    def test_get_shoppinglist_when_DB_is_empty(self):
        """Should return no shoppinglist lists msg"""
        response = self.client.get('/shoppinglists',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        

    def test_get_shoppinglist(self):
        """Should return all shoppinglists"""

        # First add shoppinglist
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 200)
        self.assertIn('travel',
                      response.data.decode())

    def test_get_shoppinglist_search(self):
        """Should return 200 and shoppinglist"""

        # First add shoppinglist
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists?q=travel',
                                   headers={"Authorization": self.token})

        print(response.data.decode())
        self.assertEqual(response.status_code, 200)
        self.assertIn('travel',
                      response.data.decode())


    def test_get_single_shoppinglist(self):
        """Should return 200 and shoppinglists"""

        # First add shoppinglists
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists/1',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 200)
        self.assertIn('Travel',
                      response.data.decode())

    def test_get_single_shoppinglist_with_no_shoppinglist(self):
        """Should return 404 if no shoppinglist"""

        response = self.client.get('/shoppinglists/1',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('not found',
                      response.data.decode())

    def test_get_single_shoppinglist_not_existing(self):
        """Should return 400 for  doesnt exists"""

        # First add shoppinglists
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists/2',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('shoppinglist with id 2 not found',
                      response.data.decode())

    def test_get_single_shoppinglist(self):
        """Should return a single shoppinglist"""
        # First add shoppinglists
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists/1',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 200)
        self.assertIn('travel',
                      response.data.decode())
    def test_update_shoppinglist_which_doesnt_exist(self):
        """
        Should return 400 for shoppinglist
        does not exists
        """

        # First add shoppinglist
        self.test_add_shoppinglist_successfully()
        shoppinglist = json.dumps({
            'name': 'travel',
            'desc': 'Visit places'
        })
        response = self.client.put('/shoppinglists/2', data=shoppinglist,
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('does not exist', response.data.decode())

    def test_update_shoppinglist_without_name(self):
        """Should return 400 for missing shoppinglist name"""
        self.test_add_shoppinglist_successfully
        shoppinglist= json.dumps({
            'name': '',
            'desc': 'tattoo'
            
            
        })
        response = self.client.put('/shoppinglist/1', data=shoppinglist,
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('The request can not be linked to, Please check your endpoint url', response.data.decode())

    def test_update_shoppinglist_with_same_name(self):
        """Should return 409 for shoppinglist updates with same name"""

        shoppinglist = json.dumps({
            'name': 'Travel',
             'desc': 'Visit places'
         })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})

        shoppinglist = json.dumps({
            'name': 'London',
             'desc': 'Visit places'
         })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})


        shoppinglist1 = json.dumps({
            'name': 'Travel',
            'desc': 'Visit places'
        })
        response = self.client.put('/shoppinglists/2', data=shoppinglist1,
                                   headers={"Authorization": self.token})
       
        self.assertEqual(response.status_code, 409)
        self.assertIn('Shoppinglist name Already exists', response.data.decode())

    def test_update_shoppinglist_successfully(self):
        """Should return 200 for shoppinglists update succesfully"""
        # First add shoppinglist
        self.test_add_shoppinglist_successfully()
        shoppinglist = json.dumps({
            'name': 'Travel',
            'desc': 'Visit places'
        })
        response = self.client.put('/shoppinglists/1', data=shoppinglist,
                                   headers={"Authorization": self.token})
        
        self.assertEqual(response.status_code, 200)
        self.assertIn('shoppinglist updated', response.data.decode())


    def test_delete_shoppinglist_that_doesnt_exist(self):
        """Should return 201 for shoppinglist added"""

        response = self.client.delete(
            '/shoppinglists/1', headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('shoppinglist not found', response.data.decode())
                       
    def test_delete_shoppinglist_successfully(self):
        """Should return 200 for shoppinglist deleted"""

        # First add a shoppinglist
        self.test_add_shoppinglist_successfully()
        response = self.client.delete(
            '/shoppinglists/1', headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 200)
        self.assertIn('shoppinglist deleted', 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()
import unittest
from flask import json
from api import db
from api.shoppinglistApi import app
from instance.config import application_config


class ShoppingListTestCase(unittest.TestCase):
    def setUp(self):
        app.config.from_object(application_config['TestingEnv'])
        self.client = app.test_client()
        self.shoppinglist = {'shoppinglist': 'Go to Goborola'}
        

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

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

    def test_add_shoppinglist_without_name(self):
        """Should return 400 for missing shoppinglist name"""
        shoppinglist= json.dumps({
            'name': '',
            'desc': 'travel'
        })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('Missing name', response.data.decode())

    def test_add_shoppinglist_successfully(self):
        """Should return 201 for shoppinglist added"""
        shoppinglist = json.dumps({
            'name': 'Travel',
             'desc': 'Visit places'
         })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})

       
        self.assertEqual(response.status_code, 201)
        print(str(response.data.decode()))
        self.assertIn('travel', response.data.decode())

        


    def test_add_shoppinglist_with_existing_shoppinglist_name(self):
        """Should return 400 for existing shoppinglist name"""

        # First Add shoppinglist
        self.test_add_shoppinglist_successfully()
        shoppinglist = json.dumps({
            'name': 'Travel',
            'desc': 'travel'
        })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})
        (response.data.decode())
        self.assertEqual(response.status_code, 409)
        self.assertIn('Shoppinglist name Already exists', response.data.decode())
                                           
    def test_get_shoppinglist_when_DB_is_empty(self):
        """Should return no shoppinglist lists msg"""
        response = self.client.get('/shoppinglists',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        

    def test_get_shoppinglist(self):
        """Should return all shoppinglists"""

        # First add shoppinglist
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 200)
        self.assertIn('travel',
                      response.data.decode())

    def test_get_shoppinglist_search(self):
        """Should return 200 and shoppinglist"""

        # First add shoppinglist
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists?q=travel',
                                   headers={"Authorization": self.token})

        print(response.data.decode())
        self.assertEqual(response.status_code, 200)
        self.assertIn('travel',
                      response.data.decode())


    def test_get_single_shoppinglist(self):
        """Should return 200 and shoppinglists"""

        # First add shoppinglists
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists/1',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 200)
        self.assertIn('Travel',
                      response.data.decode())

    def test_get_single_shoppinglist_with_no_shoppinglist(self):
        """Should return 404 if no shoppinglist"""

        response = self.client.get('/shoppinglists/1',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('not found',
                      response.data.decode())

    def test_get_single_shoppinglist_not_existing(self):
        """Should return 400 for  doesnt exists"""

        # First add shoppinglists
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists/2',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('shoppinglist with id 2 not found',
                      response.data.decode())

    def test_get_single_shoppinglist(self):
        """Should return a single shoppinglist"""
        # First add shoppinglists
        self.test_add_shoppinglist_successfully()
        response = self.client.get('/shoppinglists/1',
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 200)
        self.assertIn('travel',
                      response.data.decode())
    def test_update_shoppinglist_which_doesnt_exist(self):
        """
        Should return 400 for shoppinglist
        does not exists
        """

        # First add shoppinglist
        self.test_add_shoppinglist_successfully()
        shoppinglist = json.dumps({
            'name': 'travel',
            'desc': 'Visit places'
        })
        response = self.client.put('/shoppinglists/2', data=shoppinglist,
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 400)
        self.assertIn('does not exist', response.data.decode())

    def test_update_shoppinglist_without_name(self):
        """Should return 400 for missing shoppinglist name"""
        self.test_add_shoppinglist_successfully
        shoppinglist= json.dumps({
            'name': '',
            'desc': 'tattoo'
            
            
        })
        response = self.client.put('/shoppinglist/1', data=shoppinglist,
                                   headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('The request can not be linked to, Please check your endpoint url', response.data.decode())

    def test_update_shoppinglist_with_same_name(self):
        """Should return 409 for shoppinglist updates with same name"""

        shoppinglist = json.dumps({
            'name': 'Travel',
             'desc': 'Visit places'
         })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})

        shoppinglist = json.dumps({
            'name': 'London',
             'desc': 'Visit places'
         })
        response = self.client.post('/shoppinglists', data=shoppinglist,
                                    headers={"Authorization": self.token})


        shoppinglist1 = json.dumps({
            'name': 'Travel',
            'desc': 'Visit places'
        })
        response = self.client.put('/shoppinglists/2', data=shoppinglist1,
                                   headers={"Authorization": self.token})
       
        self.assertEqual(response.status_code, 409)
        self.assertIn('Shoppinglist name Already exists', response.data.decode())

    def test_update_shoppinglist_successfully(self):
        """Should return 200 for shoppinglists update succesfully"""
        # First add shoppinglist
        self.test_add_shoppinglist_successfully()
        shoppinglist = json.dumps({
            'name': 'Travel',
            'desc': 'Visit places'
        })
        response = self.client.put('/shoppinglists/1', data=shoppinglist,
                                   headers={"Authorization": self.token})
        
        self.assertEqual(response.status_code, 200)
        self.assertIn('shoppinglist updated', response.data.decode())


    def test_delete_shoppinglist_that_doesnt_exist(self):
        """Should return 201 for shoppinglist added"""

        response = self.client.delete(
            '/shoppinglists/1', headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 404)
        self.assertIn('shoppinglist not found', response.data.decode())
                       
    def test_delete_shoppinglist_successfully(self):
        """Should return 200 for shoppinglist deleted"""

        # First add a shoppinglist
        self.test_add_shoppinglist_successfully()
        response = self.client.delete(
            '/shoppinglists/1', headers={"Authorization": self.token})
        self.assertEqual(response.status_code, 200)
        self.assertIn('shoppinglist deleted', 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()
# Binds the app to current context # Create all tables Should return 400 for missing shoppinglist name Should return 201 for shoppinglist added Should return 400 for existing shoppinglist name # First Add shoppinglist Should return no shoppinglist lists msg Should return all shoppinglists # First add shoppinglist Should return 200 and shoppinglist # First add shoppinglist Should return 200 and shoppinglists # First add shoppinglists Should return 404 if no shoppinglist Should return 400 for  doesnt exists # First add shoppinglists Should return a single shoppinglist # First add shoppinglists Should return 400 for shoppinglist         does not exists # First add shoppinglist Should return 400 for missing shoppinglist name Should return 409 for shoppinglist updates with same name Should return 200 for shoppinglists update succesfully # First add shoppinglist Should return 201 for shoppinglist added Should return 200 for shoppinglist deleted # First add a shoppinglist # Drop all tables # Drop all tables