<commit_before>from datetime import datetime
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from .models import Trait, Wine, Winery


def add_trait(name='test'):
    """
    Create and return a new trait with the given name.
    """
    return Trait.objects.create(name=name)


def add_user(username='test', email='<EMAIL>', password='<PASSWORD>'):
    """
    Create and return a new user with the given username and email.
    """
    user = User.objects.create_user(
        username=username, email=email, password=password)
    user.save()
    return user


def add_wine(winery, name='test', vintage=2015):
    """
    Create and return a new wine with the given winery, name, and vintage.
    """
    return Wine.objects.create(winery=winery, name=name, vintage=vintage)


def add_winery(name='test'):
    """
    Create and return a new winery with the given name.
    """
    return Winery.objects.create(name=name)


class NoteTests(APITestCase):
    def setUp(self):
        add_user()
        winery = add_winery()
        add_wine(winery)


    def send_post_request(self):
        """
        Send POST request to create note and return data and response.
        """
        url = reverse('note-list')
        wine_url = reverse('wine-detail', kwargs={'pk': 1})

        data = {'wine': wine_url, 'tasted': datetime.now(),'rating': 5}
        response = self.client.post(url, data, format='json')
        
        return (data, response)


    def test_create_note_while_authenticated(self):
        """
        Ensure that we can create a new note after logging in.
        """
        self.client.login(username='test', password='<PASSWORD>')

        data, response = self.send_post_request()

        # Make sure note was created with expected data.
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        response_tasted = datetime.strptime(
            response.data['tasted'], '%Y-%m-%dT%H:%M:%S.%fZ')
        self.assertEqual(response_tasted.date(), data['tasted'].date())
        self.assertEqual(response.data['rating'], data['rating'])
        new_note_url = reverse('note-detail', kwargs={'pk': 1})
        self.assertTrue(new_note_url in response.data['url'])


    def test_create_note_while_unauthenticated(self):
        """
        Ensure that we cannot create a new note without logging in.
        """
        data, response = self.send_post_request()

        # Make sure authentication error was returned.
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertTrue('taster' not in response.data)
        self.assertTrue('tasted' not in response.data)
        self.assertTrue('rating' not in response.data)


class TraitTests(APITestCase):
    def setUp(self):
        add_user()


    def send_post_request(self):
        """
        Send POST request to create trait and return data and response.
        """
        url = reverse('trait-list')

        data = {'name': 'test'}
        response = self.client.post(url, data, format='json')

        return (data, response)


    def test_create_trait_while_authenticated(self):
        """
        Ensure that we can create a new trait after logging in.
        """
        self.client.login(username='test', password='<PASSWORD>')

        data, response = self.send_post_request()

        # Make sure trait was created with expected data.
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['name'], data['name'])
        new_trait_url = reverse('trait-detail', kwargs={'pk': 1})
        self.assertTrue(new_trait_url in response.data['url'])


    def test_create_trait_while_unauthenticated(self):
        """
        Ensure that we cannot create a new trait without logging in.
        """
        data, response = self.send_post_request()

        # Make sure authentication error was returned.
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertTrue('name' not in response.data)


    def test_view_trait_details_while_authenticated(self):
        """
        Ensure that we can view trait details after logging in.
        """
        add_trait()
        self.client.login(username='test', password='<PASSWORD>')

        # Send GET request for trait details.
        url = reverse('trait-detail', kwargs={'pk': 1})
        response = self.client.get(url)

        # Make sure correct trait details were returned.
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(url in response.data['url'])
        self.assertEqual(response.data['name'], 'test')


class WineTests(APITestCase):
    def setUp(self):
        self.user = add_user()
        self.winery = add_winery()


    def send_post_request(self):
        """
        Send POST request to create wine and return data and response.
        """
        url = reverse('wine-list')
        winery_url = reverse('winery-detail', kwargs={'pk': 1})

        data = {'winery': winery_url, 'name': 'test', 'vintage': 2015}
        response = self.client.post(url, data, format='json')

        return (data, response)<commit_msg>Add test for viewing trait detail without authenticating.<commit_after>from datetime import datetime
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from .models import Trait, Wine, Winery


def add_trait(name='test'):
    """
    Create and return a new trait with the given name.
    """
    return Trait.objects.create(name=name)


def add_user(username='test', email='<EMAIL>', password='<PASSWORD>'):
    """
    Create and return a new user with the given username and email.
    """
    user = User.objects.create_user(
        username=username, email=email, password=password)
    user.save()
    return user


def add_wine(winery, name='test', vintage=2015):
    """
    Create and return a new wine with the given winery, name, and vintage.
    """
    return Wine.objects.create(winery=winery, name=name, vintage=vintage)


def add_winery(name='test'):
    """
    Create and return a new winery with the given name.
    """
    return Winery.objects.create(name=name)


class NoteTests(APITestCase):
    def setUp(self):
        add_user()
        winery = add_winery()
        add_wine(winery)


    def send_post_request(self):
        """
        Send POST request to create note and return data and response.
        """
        url = reverse('note-list')
        wine_url = reverse('wine-detail', kwargs={'pk': 1})

        data = {'wine': wine_url, 'tasted': datetime.now(),'rating': 5}
        response = self.client.post(url, data, format='json')
        
        return (data, response)


    def test_create_note_while_authenticated(self):
        """
        Ensure that we can create a new note after logging in.
        """
        self.client.login(username='test', password='<PASSWORD>')

        data, response = self.send_post_request()

        # Make sure note was created with expected data.
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        response_tasted = datetime.strptime(
            response.data['tasted'], '%Y-%m-%dT%H:%M:%S.%fZ')
        self.assertEqual(response_tasted.date(), data['tasted'].date())
        self.assertEqual(response.data['rating'], data['rating'])
        new_note_url = reverse('note-detail', kwargs={'pk': 1})
        self.assertTrue(new_note_url in response.data['url'])


    def test_create_note_while_unauthenticated(self):
        """
        Ensure that we cannot create a new note without logging in.
        """
        data, response = self.send_post_request()

        # Make sure authentication error was returned.
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertTrue('taster' not in response.data)
        self.assertTrue('tasted' not in response.data)
        self.assertTrue('rating' not in response.data)


class TraitTests(APITestCase):
    def setUp(self):
        add_user()


    def send_post_request(self):
        """
        Send POST request to create trait and return data and response.
        """
        url = reverse('trait-list')

        data = {'name': 'test'}
        response = self.client.post(url, data, format='json')

        return (data, response)


    def test_create_trait_while_authenticated(self):
        """
        Ensure that we can create a new trait after logging in.
        """
        self.client.login(username='test', password='<PASSWORD>')

        data, response = self.send_post_request()

        # Make sure trait was created with expected data.
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['name'], data['name'])
        new_trait_url = reverse('trait-detail', kwargs={'pk': 1})
        self.assertTrue(new_trait_url in response.data['url'])


    def test_create_trait_while_unauthenticated(self):
        """
        Ensure that we cannot create a new trait without logging in.
        """
        data, response = self.send_post_request()

        # Make sure authentication error was returned.
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertTrue('name' not in response.data)


    def test_view_trait_details_while_authenticated(self):
        """
        Ensure that we can view trait details after logging in.
        """
        add_trait()
        self.client.login(username='test', password='<PASSWORD>')

        # Send GET request for trait details.
        url = reverse('trait-detail', kwargs={'pk': 1})
        response = self.client.get(url)

        # Make sure correct trait details were returned.
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(url in response.data['url'])
        self.assertEqual(response.data['name'], 'test')


    def test_view_trait_details_while_unauthenticated(self):
        """
        Ensure that we cannot view trait details without logging in.
        """
        add_trait()

        # Send GET request for trait details.
        url = reverse('trait-detail', kwargs={'pk': 1})
        response = self.client.get(url)

        # Make sure authentication error was returned.
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertTrue('url' not in response.data)
        self.assertTrue('name' not in response.data)


class WineTests(APITestCase):
    def setUp(self):
        self.user = add_user()
        self.winery = add_winery()


    def send_post_request(self):
        """
        Send POST request to create wine and return data and response.
        """
        url = reverse('wine-list')
        winery_url = reverse('winery-detail', kwargs={'pk': 1})

        data = {'winery': winery_url, 'name': 'test', 'vintage': 2015}
        response = self.client.post(url, data, format='json')

        return (data, response)
{'fraction_non_alphanumeric': 0.11592777976403289, 'fraction_numerical': 0.005541651769753307, 'mean_word_length': 2.8410573292138688, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '209174', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3179, 'n_words': 842}