3

I am a newbie working with Django. When I run command python manage.py test, I keep receiving the following Error;

Tact/tact_api/lines/test_views.py", line 103, in 
test_api_can_create_an_event
self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)
AssertionError: 400 != 201

----------------------------------------------------------------------
Ran 8 tests in 0.105s

I would really appreciate any assistance in fixing this issue! A few of my own thoughts include;

1) There is probably a syntax error in my Event object post request. See test code below. 2) The issue could configuration related.

For reference, see below Test;

class EventViewTestCase(TestCase):
"""
Test suite for Event API views
"""
def setUp(self):
    """
    Define the event test client and other test variables.
    """
    user = User.objects.create(username="tactician")

    # initialize client and force authentication
    self.client = APIClient()
    self.client.force_authenticate(user=user)

    # create and post a new line using authorized user
    # since user model instance is not serializable, use its Id/PK
    self.line_data = {'owner': user.id, 'title': 'Title goes here'}
    self.res = self.client.post(reverse('line-list'), self.line_data, format='json')

    # create and post a new event linked to created line using authorized user
    line = Line.objects.get()
    self.event_data = {'owner': user.id, 'pk': line.id, 'title': 'Title goes here',
                       'desc': 'Desc. here', 'start': timezone.now(), 'end': timezone.now()}
    self.response = self.client.post(reverse('event-list'), self.event_data, format='json')

def test_api_can_create_an_event(self):
    """
    Test the api has event creation capability.
    """
    self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)

Serializers;

class EventSerializer(serializers.HyperlinkedModelSerializer):
"""
Convert Event model instance into native Python datatypes to be rendered as JSON.
"""
owner = serializers.ReadOnlyField(source='owner.username')

class Meta:
    model = Event
    fields = ('url', 'id', 'owner', 'created', 'modified', 'title', 'desc', 'start',
              'end', 'line')

def get_fields(self, *args, **kwargs):
    """
    create and return a new Event object (linked to a user owned Line) with validated data.

    update and return an existing Event object with validated data.
    """
    fields = super(EventSerializer, self).get_fields(*args, **kwargs)
    view = self.context['view']
    owner = view.request.user
    fields['line'].queryset = fields['line'].queryset.filter(owner=owner)
    return fields
K.Daniel
  • 41
  • 1
  • 6
  • I think more valueable info will be config and urlconf ,Pls add this to question, or give link to some external fiddle. – Take_Care_ Jan 17 '18 at 12:06
  • @Take_Care_ - Thanks. You can view the url config file at [link] (https://github.com/KenyattaDaniel/tact/blob/master/tact_api/config/urls.py), the app url file at [link] (https://github.com/KenyattaDaniel/tact/blob/master/tact_api/lines/urls.py) and the base config file at [link] (https://github.com/KenyattaDaniel/tact/blob/master/tact_api/config/settings/base.py). If you prefer this info pasted into the question, just let me know. – K.Daniel Jan 17 '18 at 12:40
  • @Take_Care_ - I was able to finally resolve this issue. Turns out that there was a slight syntax error in my test module. I changed self.event_data = {'pk': line.id...} to {'line': line.id...}. Additionally, in my serializers module I changed class EventSerializer(serializers.HyperlinkedModelSerializer): to class EventSerializer(serializers.ModelSerializer):. With these changes, my tests now run! – K.Daniel Jan 19 '18 at 22:50

1 Answers1

1

This issue is now resolved.

Firstly, in my test suite there was a slight syntax error in the event payload data I supplied to the post request.

self.event_data = {'owner': user.id, 'pk': line.id...}

Changed to; (line is the foreign key field name specified in my Event model.)

self.event_data = {'owner': user.id, 'line': line.id...}

Secondly, for my Event serializer I used the wrong base class;

class EventSerializer(serializers.HyperlinkedModelSerializer)

Changed to;

class EventSerializer(serializers.ModelSerializer) 
K.Daniel
  • 41
  • 1
  • 6