<commit_before>"""Testacases for functionality involving items."""
from tests.base_test import BaseTest
import json
from bucketlist.models import Item


class TestItem(BaseTest):
    """Test items routes."""

    def get_token(self):
        """Return authentication token."""
        self.user = {"email": "<EMAIL>",
                     "password": "password"}
        r = self.app.post("/api/v1/auth/login",
                          data=self.user)
        output = json.loads(r.data.decode())
        token = output.get("token")
        return {"token": token}

    def test_can_add_new_item(self):
        """Tests if a user can add a new item in a bucketlist."""
        self.item = {"name": "Go to Hawaii", "done": "y",
                     "buckeltist_id": 1}
        r = self.app.post("/api/v1/bucketlists/1/items/", data=self.item,
                          headers=self.get_token())
        self.assertEqual(r.status_code, 201)
        message = json.loads(r.data.decode())
        self.assertIn("created successfully", message["message"])

    def test_shows_message_when_item_already_exists(self):
        """Tests that a message is shown when an item already exists."""
        self.item = {"name": "Enjoy the beautiful beaches of Hawaii",
                     "done": "Y", "buckeltist_id": 1}
        r = self.app.post("/api/v1/bucketlists/1/items/", data=self.item,
                          headers=self.get_token())
        message = json.loads(r.data.decode())
        self.assertIn("already exists", message["message"])

    def test_user_can_update_a_bucketlist_item(self):
        """Tests that a user can update an existing bucketlist item."""
        self.item = {"name": "New Item", "bucketlist_id": 1, "done": "True"}
        r = self.app.put("/api/v1/bucketlists/1/items/1", data=self.item,
                         headers=self.get_token())
        self.assertEqual(r.status_code, 200)
        self.assertTrue(Item.query.filter_by(name="New Item").first())
        self.assertFalse(Item.query.filter_by(
            name="Enjoy the beautiful beaches of Hawaii").first())

    def test_message_when_user_updates_to__name_that_already_exists(self):
        """Tests for an error message.

        shown when a user tries to update to a item name which already exists.
        """
        self.item = {"name": "Go to Hawaii", "done": "y",
                     "buckeltist_id": 1}
        self.app.post("/api/v1/bucketlists/1/items/", data=self.item,
                      headers=self.get_token())
        self.item2 = {"name": "Enjoy the beautiful beaches of Hawaii",
                      "bucketlist_id": 1, "done": "True"}
        r = self.app.put("/api/v1/bucketlists/1/items/2", data=self.item2,
                         headers=self.get_token())
        message = json.loads(r.data.decode())
        self.assertIn("already exists", message["message"])

    def test_message_when_user_updates_an_item_that_doesnot_exists(self):
        """Tests for an error message shown when a user tries to update an item which doesnot exist.""" # noqa
        self.item = {"name": "Enjoy the beautiful beaches of Hawaii",
                     "bucketlist_id": 1, "done": "True"}
        r = self.app.put("/api/v1/bucketlists/1/items/2", data=self.item,
                         headers=self.get_token())
        message = json.loads(r.data.decode())
        self.assertIn("does not exist", message["message"])

    def test_message_when_user_updates_a_bucketlist_that_has_no_items(self):
        """Tests for an error message when a user tries to update a bucketlist which has no items.""" # noqa
        self.bucketlist = {"title": "Love",
                           "description": "I want to marry a princcess",
                           "created_by": '1'}<commit_msg>[tests] Fix failing test, use y/n for item status<commit_after>"""Testacases for functionality involving items."""
from tests.base_test import BaseTest
import json
from bucketlist.models import Item


class TestItem(BaseTest):
    """Test items routes."""

    def get_token(self):
        """Return authentication token."""
        self.user = {"email": "<EMAIL>",
                     "password": "password"}
        r = self.app.post("/api/v1/auth/login",
                          data=self.user)
        output = json.loads(r.data.decode())
        token = output.get("token")
        return {"token": token}

    def test_can_add_new_item(self):
        """Tests if a user can add a new item in a bucketlist."""
        self.item = {"name": "Go to Hawaii", "done": "y",
                     "buckeltist_id": 1}
        r = self.app.post("/api/v1/bucketlists/1/items/", data=self.item,
                          headers=self.get_token())
        self.assertEqual(r.status_code, 201)
        message = json.loads(r.data.decode())
        self.assertIn("created successfully", message["message"])

    def test_shows_message_when_item_already_exists(self):
        """Tests that a message is shown when an item already exists."""
        self.item = {"name": "Enjoy the beautiful beaches of Hawaii",
                     "done": "Y", "buckeltist_id": 1}
        r = self.app.post("/api/v1/bucketlists/1/items/", data=self.item,
                          headers=self.get_token())
        message = json.loads(r.data.decode())
        self.assertIn("already exists", message["message"])

    def test_user_can_update_a_bucketlist_item(self):
        """Tests that a user can update an existing bucketlist item."""
        self.item = {"name": "New Item", "bucketlist_id": 1, "done": "y"}
        r = self.app.put("/api/v1/bucketlists/1/items/1", data=self.item,
                         headers=self.get_token())
        self.assertEqual(r.status_code, 200)
        self.assertTrue(Item.query.filter_by(name="New Item").first())
        self.assertFalse(Item.query.filter_by(
            name="Enjoy the beautiful beaches of Hawaii").first())

    def test_message_when_user_updates_to__name_that_already_exists(self):
        """Tests for an error message.

        shown when a user tries to update to a item name which already exists.
        """
        self.item = {"name": "Go to Hawaii", "done": "y",
                     "buckeltist_id": 1}
        self.app.post("/api/v1/bucketlists/1/items/", data=self.item,
                      headers=self.get_token())
        self.item2 = {"name": "Enjoy the beautiful beaches of Hawaii",
                      "bucketlist_id": 1, "done": "y"}
        r = self.app.put("/api/v1/bucketlists/1/items/2", data=self.item2,
                         headers=self.get_token())
        message = json.loads(r.data.decode())
        self.assertIn("already exists", message["message"])

    def test_message_when_user_updates_an_item_that_doesnot_exists(self):
        """Tests for an error message shown when a user tries to update an item which doesnot exist.""" # noqa
        self.item = {"name": "Enjoy the beautiful beaches of Hawaii",
                     "bucketlist_id": 1, "done": "True"}
        r = self.app.put("/api/v1/bucketlists/1/items/2", data=self.item,
                         headers=self.get_token())
        message = json.loads(r.data.decode())
        self.assertIn("does not exist", message["message"])

    def test_message_when_user_updates_a_bucketlist_that_has_no_items(self):
        """Tests for an error message when a user tries to update a bucketlist which has no items.""" # noqa
        self.bucketlist = {"title": "Love",
                           "description": "I want to marry a princcess",
                           "created_by": '1'}
{'fraction_non_alphanumeric': 0.13534822601839686, 'fraction_numerical': 0.008147174770039421, 'mean_word_length': 2.3410886742756802, 'pattern_counts': {'":': 48, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2929335', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2168, 'n_words': 549}