From 971578ca345c3d3bae7fd93b87c41d43483b6f05 Mon Sep 17 00:00:00 2001 From: Andreas Pelme Date: Sun, 2 Mar 2014 12:40:30 +0100 Subject: Support for running the test suite with py.test * Get rid of runtests.py * Moved test code from rest_framework/tests and rest_framework/runtests to tests * Invoke py.test from setup.py * Invoke py.test from Travis * Invoke py.test from tox * Changed setUpClass to be just plain setUp in test_permissions.py * Updated contribution guideline to show how to invoke py.test --- tests/test_serializer_bulk_update.py | 278 +++++++++++++++++++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 tests/test_serializer_bulk_update.py (limited to 'tests/test_serializer_bulk_update.py') diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py new file mode 100644 index 00000000..8b0ded1a --- /dev/null +++ b/tests/test_serializer_bulk_update.py @@ -0,0 +1,278 @@ +""" +Tests to cover bulk create and update using serializers. +""" +from __future__ import unicode_literals +from django.test import TestCase +from rest_framework import serializers + + +class BulkCreateSerializerTests(TestCase): + """ + Creating multiple instances using serializers. + """ + + def setUp(self): + class BookSerializer(serializers.Serializer): + id = serializers.IntegerField() + title = serializers.CharField(max_length=100) + author = serializers.CharField(max_length=100) + + self.BookSerializer = BookSerializer + + def test_bulk_create_success(self): + """ + Correct bulk update serialization should return the input data. + """ + + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 1, + 'title': 'If this is a man', + 'author': 'Primo Levi' + }, { + 'id': 2, + 'title': 'The wind-up bird chronicle', + 'author': 'Haruki Murakami' + } + ] + + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(serializer.object, data) + + def test_bulk_create_errors(self): + """ + Correct bulk update serialization should return the input data. + """ + + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 1, + 'title': 'If this is a man', + 'author': 'Primo Levi' + }, { + 'id': 'foo', + 'title': 'The wind-up bird chronicle', + 'author': 'Haruki Murakami' + } + ] + expected_errors = [ + {}, + {}, + {'id': ['Enter a whole number.']} + ] + + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + self.assertEqual(serializer.errors, expected_errors) + + def test_invalid_list_datatype(self): + """ + Data containing list of incorrect data type should return errors. + """ + data = ['foo', 'bar', 'baz'] + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + + expected_errors = [ + {'non_field_errors': ['Invalid data']}, + {'non_field_errors': ['Invalid data']}, + {'non_field_errors': ['Invalid data']} + ] + + self.assertEqual(serializer.errors, expected_errors) + + def test_invalid_single_datatype(self): + """ + Data containing a single incorrect data type should return errors. + """ + data = 123 + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + + expected_errors = {'non_field_errors': ['Expected a list of items.']} + + self.assertEqual(serializer.errors, expected_errors) + + def test_invalid_single_object(self): + """ + Data containing only a single object, instead of a list of objects + should return errors. + """ + data = { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + } + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + + expected_errors = {'non_field_errors': ['Expected a list of items.']} + + self.assertEqual(serializer.errors, expected_errors) + + +class BulkUpdateSerializerTests(TestCase): + """ + Updating multiple instances using serializers. + """ + + def setUp(self): + class Book(object): + """ + A data type that can be persisted to a mock storage backend + with `.save()` and `.delete()`. + """ + object_map = {} + + def __init__(self, id, title, author): + self.id = id + self.title = title + self.author = author + + def save(self): + Book.object_map[self.id] = self + + def delete(self): + del Book.object_map[self.id] + + class BookSerializer(serializers.Serializer): + id = serializers.IntegerField() + title = serializers.CharField(max_length=100) + author = serializers.CharField(max_length=100) + + def restore_object(self, attrs, instance=None): + if instance: + instance.id = attrs['id'] + instance.title = attrs['title'] + instance.author = attrs['author'] + return instance + return Book(**attrs) + + self.Book = Book + self.BookSerializer = BookSerializer + + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 1, + 'title': 'If this is a man', + 'author': 'Primo Levi' + }, { + 'id': 2, + 'title': 'The wind-up bird chronicle', + 'author': 'Haruki Murakami' + } + ] + + for item in data: + book = Book(item['id'], item['title'], item['author']) + book.save() + + def books(self): + """ + Return all the objects in the mock storage backend. + """ + return self.Book.object_map.values() + + def test_bulk_update_success(self): + """ + Correct bulk update serialization should return the input data. + """ + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 2, + 'title': 'Kafka on the shore', + 'author': 'Haruki Murakami' + } + ] + serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(serializer.data, data) + serializer.save() + new_data = self.BookSerializer(self.books(), many=True).data + + self.assertEqual(data, new_data) + + def test_bulk_update_and_create(self): + """ + Bulk update serialization may also include created items. + """ + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 3, + 'title': 'Kafka on the shore', + 'author': 'Haruki Murakami' + } + ] + serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(serializer.data, data) + serializer.save() + new_data = self.BookSerializer(self.books(), many=True).data + self.assertEqual(data, new_data) + + def test_bulk_update_invalid_create(self): + """ + Bulk update serialization without allow_add_remove may not create items. + """ + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 3, + 'title': 'Kafka on the shore', + 'author': 'Haruki Murakami' + } + ] + expected_errors = [ + {}, + {'non_field_errors': ['Cannot create a new item, only existing items may be updated.']} + ] + serializer = self.BookSerializer(self.books(), data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + self.assertEqual(serializer.errors, expected_errors) + + def test_bulk_update_error(self): + """ + Incorrect bulk update serialization should return error data. + """ + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 'foo', + 'title': 'Kafka on the shore', + 'author': 'Haruki Murakami' + } + ] + expected_errors = [ + {}, + {'id': ['Enter a whole number.']} + ] + serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) + self.assertEqual(serializer.is_valid(), False) + self.assertEqual(serializer.errors, expected_errors) -- cgit v1.2.3 From bf09c32de8f9d528f83e9cb7a2773d1f4c9ab563 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 19 Aug 2014 13:28:07 +0100 Subject: Code linting and added runtests.py --- tests/test_serializer_bulk_update.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/test_serializer_bulk_update.py') diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index 8b0ded1a..67a8ed0d 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -83,9 +83,9 @@ class BulkCreateSerializerTests(TestCase): self.assertEqual(serializer.is_valid(), False) expected_errors = [ - {'non_field_errors': ['Invalid data']}, - {'non_field_errors': ['Invalid data']}, - {'non_field_errors': ['Invalid data']} + {'non_field_errors': ['Invalid data']}, + {'non_field_errors': ['Invalid data']}, + {'non_field_errors': ['Invalid data']} ] self.assertEqual(serializer.errors, expected_errors) -- cgit v1.2.3 From c1036c17533a3091401ff90f825571f0e6125eca Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 3 Sep 2014 16:34:09 +0100 Subject: More test passing --- tests/test_serializer_bulk_update.py | 556 +++++++++++++++++------------------ 1 file changed, 278 insertions(+), 278 deletions(-) (limited to 'tests/test_serializer_bulk_update.py') diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index 67a8ed0d..3341ce59 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -1,278 +1,278 @@ -""" -Tests to cover bulk create and update using serializers. -""" -from __future__ import unicode_literals -from django.test import TestCase -from rest_framework import serializers - - -class BulkCreateSerializerTests(TestCase): - """ - Creating multiple instances using serializers. - """ - - def setUp(self): - class BookSerializer(serializers.Serializer): - id = serializers.IntegerField() - title = serializers.CharField(max_length=100) - author = serializers.CharField(max_length=100) - - self.BookSerializer = BookSerializer - - def test_bulk_create_success(self): - """ - Correct bulk update serialization should return the input data. - """ - - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 1, - 'title': 'If this is a man', - 'author': 'Primo Levi' - }, { - 'id': 2, - 'title': 'The wind-up bird chronicle', - 'author': 'Haruki Murakami' - } - ] - - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, data) - - def test_bulk_create_errors(self): - """ - Correct bulk update serialization should return the input data. - """ - - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 1, - 'title': 'If this is a man', - 'author': 'Primo Levi' - }, { - 'id': 'foo', - 'title': 'The wind-up bird chronicle', - 'author': 'Haruki Murakami' - } - ] - expected_errors = [ - {}, - {}, - {'id': ['Enter a whole number.']} - ] - - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, expected_errors) - - def test_invalid_list_datatype(self): - """ - Data containing list of incorrect data type should return errors. - """ - data = ['foo', 'bar', 'baz'] - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) - - expected_errors = [ - {'non_field_errors': ['Invalid data']}, - {'non_field_errors': ['Invalid data']}, - {'non_field_errors': ['Invalid data']} - ] - - self.assertEqual(serializer.errors, expected_errors) - - def test_invalid_single_datatype(self): - """ - Data containing a single incorrect data type should return errors. - """ - data = 123 - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) - - expected_errors = {'non_field_errors': ['Expected a list of items.']} - - self.assertEqual(serializer.errors, expected_errors) - - def test_invalid_single_object(self): - """ - Data containing only a single object, instead of a list of objects - should return errors. - """ - data = { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - } - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) - - expected_errors = {'non_field_errors': ['Expected a list of items.']} - - self.assertEqual(serializer.errors, expected_errors) - - -class BulkUpdateSerializerTests(TestCase): - """ - Updating multiple instances using serializers. - """ - - def setUp(self): - class Book(object): - """ - A data type that can be persisted to a mock storage backend - with `.save()` and `.delete()`. - """ - object_map = {} - - def __init__(self, id, title, author): - self.id = id - self.title = title - self.author = author - - def save(self): - Book.object_map[self.id] = self - - def delete(self): - del Book.object_map[self.id] - - class BookSerializer(serializers.Serializer): - id = serializers.IntegerField() - title = serializers.CharField(max_length=100) - author = serializers.CharField(max_length=100) - - def restore_object(self, attrs, instance=None): - if instance: - instance.id = attrs['id'] - instance.title = attrs['title'] - instance.author = attrs['author'] - return instance - return Book(**attrs) - - self.Book = Book - self.BookSerializer = BookSerializer - - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 1, - 'title': 'If this is a man', - 'author': 'Primo Levi' - }, { - 'id': 2, - 'title': 'The wind-up bird chronicle', - 'author': 'Haruki Murakami' - } - ] - - for item in data: - book = Book(item['id'], item['title'], item['author']) - book.save() - - def books(self): - """ - Return all the objects in the mock storage backend. - """ - return self.Book.object_map.values() - - def test_bulk_update_success(self): - """ - Correct bulk update serialization should return the input data. - """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 2, - 'title': 'Kafka on the shore', - 'author': 'Haruki Murakami' - } - ] - serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.data, data) - serializer.save() - new_data = self.BookSerializer(self.books(), many=True).data - - self.assertEqual(data, new_data) - - def test_bulk_update_and_create(self): - """ - Bulk update serialization may also include created items. - """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 3, - 'title': 'Kafka on the shore', - 'author': 'Haruki Murakami' - } - ] - serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.data, data) - serializer.save() - new_data = self.BookSerializer(self.books(), many=True).data - self.assertEqual(data, new_data) - - def test_bulk_update_invalid_create(self): - """ - Bulk update serialization without allow_add_remove may not create items. - """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 3, - 'title': 'Kafka on the shore', - 'author': 'Haruki Murakami' - } - ] - expected_errors = [ - {}, - {'non_field_errors': ['Cannot create a new item, only existing items may be updated.']} - ] - serializer = self.BookSerializer(self.books(), data=data, many=True) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, expected_errors) - - def test_bulk_update_error(self): - """ - Incorrect bulk update serialization should return error data. - """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 'foo', - 'title': 'Kafka on the shore', - 'author': 'Haruki Murakami' - } - ] - expected_errors = [ - {}, - {'id': ['Enter a whole number.']} - ] - serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, expected_errors) +# """ +# Tests to cover bulk create and update using serializers. +# """ +# from __future__ import unicode_literals +# from django.test import TestCase +# from rest_framework import serializers + + +# class BulkCreateSerializerTests(TestCase): +# """ +# Creating multiple instances using serializers. +# """ + +# def setUp(self): +# class BookSerializer(serializers.Serializer): +# id = serializers.IntegerField() +# title = serializers.CharField(max_length=100) +# author = serializers.CharField(max_length=100) + +# self.BookSerializer = BookSerializer + +# def test_bulk_create_success(self): +# """ +# Correct bulk update serialization should return the input data. +# """ + +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 1, +# 'title': 'If this is a man', +# 'author': 'Primo Levi' +# }, { +# 'id': 2, +# 'title': 'The wind-up bird chronicle', +# 'author': 'Haruki Murakami' +# } +# ] + +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, data) + +# def test_bulk_create_errors(self): +# """ +# Correct bulk update serialization should return the input data. +# """ + +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 1, +# 'title': 'If this is a man', +# 'author': 'Primo Levi' +# }, { +# 'id': 'foo', +# 'title': 'The wind-up bird chronicle', +# 'author': 'Haruki Murakami' +# } +# ] +# expected_errors = [ +# {}, +# {}, +# {'id': ['Enter a whole number.']} +# ] + +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, expected_errors) + +# def test_invalid_list_datatype(self): +# """ +# Data containing list of incorrect data type should return errors. +# """ +# data = ['foo', 'bar', 'baz'] +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) + +# expected_errors = [ +# {'non_field_errors': ['Invalid data']}, +# {'non_field_errors': ['Invalid data']}, +# {'non_field_errors': ['Invalid data']} +# ] + +# self.assertEqual(serializer.errors, expected_errors) + +# def test_invalid_single_datatype(self): +# """ +# Data containing a single incorrect data type should return errors. +# """ +# data = 123 +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) + +# expected_errors = {'non_field_errors': ['Expected a list of items.']} + +# self.assertEqual(serializer.errors, expected_errors) + +# def test_invalid_single_object(self): +# """ +# Data containing only a single object, instead of a list of objects +# should return errors. +# """ +# data = { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# } +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) + +# expected_errors = {'non_field_errors': ['Expected a list of items.']} + +# self.assertEqual(serializer.errors, expected_errors) + + +# class BulkUpdateSerializerTests(TestCase): +# """ +# Updating multiple instances using serializers. +# """ + +# def setUp(self): +# class Book(object): +# """ +# A data type that can be persisted to a mock storage backend +# with `.save()` and `.delete()`. +# """ +# object_map = {} + +# def __init__(self, id, title, author): +# self.id = id +# self.title = title +# self.author = author + +# def save(self): +# Book.object_map[self.id] = self + +# def delete(self): +# del Book.object_map[self.id] + +# class BookSerializer(serializers.Serializer): +# id = serializers.IntegerField() +# title = serializers.CharField(max_length=100) +# author = serializers.CharField(max_length=100) + +# def restore_object(self, attrs, instance=None): +# if instance: +# instance.id = attrs['id'] +# instance.title = attrs['title'] +# instance.author = attrs['author'] +# return instance +# return Book(**attrs) + +# self.Book = Book +# self.BookSerializer = BookSerializer + +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 1, +# 'title': 'If this is a man', +# 'author': 'Primo Levi' +# }, { +# 'id': 2, +# 'title': 'The wind-up bird chronicle', +# 'author': 'Haruki Murakami' +# } +# ] + +# for item in data: +# book = Book(item['id'], item['title'], item['author']) +# book.save() + +# def books(self): +# """ +# Return all the objects in the mock storage backend. +# """ +# return self.Book.object_map.values() + +# def test_bulk_update_success(self): +# """ +# Correct bulk update serialization should return the input data. +# """ +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 2, +# 'title': 'Kafka on the shore', +# 'author': 'Haruki Murakami' +# } +# ] +# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.data, data) +# serializer.save() +# new_data = self.BookSerializer(self.books(), many=True).data + +# self.assertEqual(data, new_data) + +# def test_bulk_update_and_create(self): +# """ +# Bulk update serialization may also include created items. +# """ +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 3, +# 'title': 'Kafka on the shore', +# 'author': 'Haruki Murakami' +# } +# ] +# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.data, data) +# serializer.save() +# new_data = self.BookSerializer(self.books(), many=True).data +# self.assertEqual(data, new_data) + +# def test_bulk_update_invalid_create(self): +# """ +# Bulk update serialization without allow_add_remove may not create items. +# """ +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 3, +# 'title': 'Kafka on the shore', +# 'author': 'Haruki Murakami' +# } +# ] +# expected_errors = [ +# {}, +# {'non_field_errors': ['Cannot create a new item, only existing items may be updated.']} +# ] +# serializer = self.BookSerializer(self.books(), data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, expected_errors) + +# def test_bulk_update_error(self): +# """ +# Incorrect bulk update serialization should return error data. +# """ +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 'foo', +# 'title': 'Kafka on the shore', +# 'author': 'Haruki Murakami' +# } +# ] +# expected_errors = [ +# {}, +# {'id': ['Enter a whole number.']} +# ] +# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, expected_errors) -- cgit v1.2.3 From ed541864e637681e1aca3a808be1f26202b4c271 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 6 Nov 2014 10:34:59 +0000 Subject: Support for bulk create. Closes #1965. --- tests/test_serializer_bulk_update.py | 240 +++++++++++++++++------------------ 1 file changed, 120 insertions(+), 120 deletions(-) (limited to 'tests/test_serializer_bulk_update.py') diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index 3341ce59..85b6b2fa 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -1,123 +1,123 @@ -# """ -# Tests to cover bulk create and update using serializers. -# """ -# from __future__ import unicode_literals -# from django.test import TestCase -# from rest_framework import serializers - - -# class BulkCreateSerializerTests(TestCase): -# """ -# Creating multiple instances using serializers. -# """ - -# def setUp(self): -# class BookSerializer(serializers.Serializer): -# id = serializers.IntegerField() -# title = serializers.CharField(max_length=100) -# author = serializers.CharField(max_length=100) - -# self.BookSerializer = BookSerializer - -# def test_bulk_create_success(self): -# """ -# Correct bulk update serialization should return the input data. -# """ - -# data = [ -# { -# 'id': 0, -# 'title': 'The electric kool-aid acid test', -# 'author': 'Tom Wolfe' -# }, { -# 'id': 1, -# 'title': 'If this is a man', -# 'author': 'Primo Levi' -# }, { -# 'id': 2, -# 'title': 'The wind-up bird chronicle', -# 'author': 'Haruki Murakami' -# } -# ] - -# serializer = self.BookSerializer(data=data, many=True) -# self.assertEqual(serializer.is_valid(), True) -# self.assertEqual(serializer.object, data) - -# def test_bulk_create_errors(self): -# """ -# Correct bulk update serialization should return the input data. -# """ - -# data = [ -# { -# 'id': 0, -# 'title': 'The electric kool-aid acid test', -# 'author': 'Tom Wolfe' -# }, { -# 'id': 1, -# 'title': 'If this is a man', -# 'author': 'Primo Levi' -# }, { -# 'id': 'foo', -# 'title': 'The wind-up bird chronicle', -# 'author': 'Haruki Murakami' -# } -# ] -# expected_errors = [ -# {}, -# {}, -# {'id': ['Enter a whole number.']} -# ] - -# serializer = self.BookSerializer(data=data, many=True) -# self.assertEqual(serializer.is_valid(), False) -# self.assertEqual(serializer.errors, expected_errors) - -# def test_invalid_list_datatype(self): -# """ -# Data containing list of incorrect data type should return errors. -# """ -# data = ['foo', 'bar', 'baz'] -# serializer = self.BookSerializer(data=data, many=True) -# self.assertEqual(serializer.is_valid(), False) - -# expected_errors = [ -# {'non_field_errors': ['Invalid data']}, -# {'non_field_errors': ['Invalid data']}, -# {'non_field_errors': ['Invalid data']} -# ] - -# self.assertEqual(serializer.errors, expected_errors) - -# def test_invalid_single_datatype(self): -# """ -# Data containing a single incorrect data type should return errors. -# """ -# data = 123 -# serializer = self.BookSerializer(data=data, many=True) -# self.assertEqual(serializer.is_valid(), False) - -# expected_errors = {'non_field_errors': ['Expected a list of items.']} - -# self.assertEqual(serializer.errors, expected_errors) - -# def test_invalid_single_object(self): -# """ -# Data containing only a single object, instead of a list of objects -# should return errors. -# """ -# data = { -# 'id': 0, -# 'title': 'The electric kool-aid acid test', -# 'author': 'Tom Wolfe' -# } -# serializer = self.BookSerializer(data=data, many=True) -# self.assertEqual(serializer.is_valid(), False) - -# expected_errors = {'non_field_errors': ['Expected a list of items.']} - -# self.assertEqual(serializer.errors, expected_errors) +""" +Tests to cover bulk create and update using serializers. +""" +from __future__ import unicode_literals +from django.test import TestCase +from rest_framework import serializers + + +class BulkCreateSerializerTests(TestCase): + """ + Creating multiple instances using serializers. + """ + + def setUp(self): + class BookSerializer(serializers.Serializer): + id = serializers.IntegerField() + title = serializers.CharField(max_length=100) + author = serializers.CharField(max_length=100) + + self.BookSerializer = BookSerializer + + def test_bulk_create_success(self): + """ + Correct bulk update serialization should return the input data. + """ + + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 1, + 'title': 'If this is a man', + 'author': 'Primo Levi' + }, { + 'id': 2, + 'title': 'The wind-up bird chronicle', + 'author': 'Haruki Murakami' + } + ] + + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), True) + self.assertEqual(serializer.validated_data, data) + + def test_bulk_create_errors(self): + """ + Incorrect bulk create serialization should return errors. + """ + + data = [ + { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + }, { + 'id': 1, + 'title': 'If this is a man', + 'author': 'Primo Levi' + }, { + 'id': 'foo', + 'title': 'The wind-up bird chronicle', + 'author': 'Haruki Murakami' + } + ] + expected_errors = [ + {}, + {}, + {'id': ['A valid integer is required.']} + ] + + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + self.assertEqual(serializer.errors, expected_errors) + + def test_invalid_list_datatype(self): + """ + Data containing list of incorrect data type should return errors. + """ + data = ['foo', 'bar', 'baz'] + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + + expected_errors = [ + {'non_field_errors': ['Invalid data. Expected a dictionary, but got unicode.']}, + {'non_field_errors': ['Invalid data. Expected a dictionary, but got unicode.']}, + {'non_field_errors': ['Invalid data. Expected a dictionary, but got unicode.']} + ] + + self.assertEqual(serializer.errors, expected_errors) + + def test_invalid_single_datatype(self): + """ + Data containing a single incorrect data type should return errors. + """ + data = 123 + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + + expected_errors = {'non_field_errors': ['Expected a list of items but got type `int`.']} + + self.assertEqual(serializer.errors, expected_errors) + + def test_invalid_single_object(self): + """ + Data containing only a single object, instead of a list of objects + should return errors. + """ + data = { + 'id': 0, + 'title': 'The electric kool-aid acid test', + 'author': 'Tom Wolfe' + } + serializer = self.BookSerializer(data=data, many=True) + self.assertEqual(serializer.is_valid(), False) + + expected_errors = {'non_field_errors': ['Expected a list of items but got type `dict`.']} + + self.assertEqual(serializer.errors, expected_errors) # class BulkUpdateSerializerTests(TestCase): -- cgit v1.2.3 From 9923a6ce9013693ea1723e7895b3cab638d719fd Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 6 Nov 2014 11:51:10 +0000 Subject: Fix tests for py2/3 compat --- tests/test_serializer_bulk_update.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'tests/test_serializer_bulk_update.py') diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index 85b6b2fa..2259ee31 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -3,6 +3,7 @@ Tests to cover bulk create and update using serializers. """ from __future__ import unicode_literals from django.test import TestCase +from django.utils import six from rest_framework import serializers @@ -82,10 +83,12 @@ class BulkCreateSerializerTests(TestCase): serializer = self.BookSerializer(data=data, many=True) self.assertEqual(serializer.is_valid(), False) + text_type_string = six.text_type.__name__ + message = 'Invalid data. Expected a dictionary, but got %s.' % text_type_string expected_errors = [ - {'non_field_errors': ['Invalid data. Expected a dictionary, but got unicode.']}, - {'non_field_errors': ['Invalid data. Expected a dictionary, but got unicode.']}, - {'non_field_errors': ['Invalid data. Expected a dictionary, but got unicode.']} + {'non_field_errors': [message]}, + {'non_field_errors': [message]}, + {'non_field_errors': [message]} ] self.assertEqual(serializer.errors, expected_errors) -- cgit v1.2.3 From c8764de7881f419c9269913ec3654fc1d904ab2e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 28 Nov 2014 13:04:42 +0000 Subject: Drop defunct tests --- tests/test_serializer_bulk_update.py | 158 ----------------------------------- 1 file changed, 158 deletions(-) (limited to 'tests/test_serializer_bulk_update.py') diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index 2259ee31..fb881a75 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -121,161 +121,3 @@ class BulkCreateSerializerTests(TestCase): expected_errors = {'non_field_errors': ['Expected a list of items but got type `dict`.']} self.assertEqual(serializer.errors, expected_errors) - - -# class BulkUpdateSerializerTests(TestCase): -# """ -# Updating multiple instances using serializers. -# """ - -# def setUp(self): -# class Book(object): -# """ -# A data type that can be persisted to a mock storage backend -# with `.save()` and `.delete()`. -# """ -# object_map = {} - -# def __init__(self, id, title, author): -# self.id = id -# self.title = title -# self.author = author - -# def save(self): -# Book.object_map[self.id] = self - -# def delete(self): -# del Book.object_map[self.id] - -# class BookSerializer(serializers.Serializer): -# id = serializers.IntegerField() -# title = serializers.CharField(max_length=100) -# author = serializers.CharField(max_length=100) - -# def restore_object(self, attrs, instance=None): -# if instance: -# instance.id = attrs['id'] -# instance.title = attrs['title'] -# instance.author = attrs['author'] -# return instance -# return Book(**attrs) - -# self.Book = Book -# self.BookSerializer = BookSerializer - -# data = [ -# { -# 'id': 0, -# 'title': 'The electric kool-aid acid test', -# 'author': 'Tom Wolfe' -# }, { -# 'id': 1, -# 'title': 'If this is a man', -# 'author': 'Primo Levi' -# }, { -# 'id': 2, -# 'title': 'The wind-up bird chronicle', -# 'author': 'Haruki Murakami' -# } -# ] - -# for item in data: -# book = Book(item['id'], item['title'], item['author']) -# book.save() - -# def books(self): -# """ -# Return all the objects in the mock storage backend. -# """ -# return self.Book.object_map.values() - -# def test_bulk_update_success(self): -# """ -# Correct bulk update serialization should return the input data. -# """ -# data = [ -# { -# 'id': 0, -# 'title': 'The electric kool-aid acid test', -# 'author': 'Tom Wolfe' -# }, { -# 'id': 2, -# 'title': 'Kafka on the shore', -# 'author': 'Haruki Murakami' -# } -# ] -# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) -# self.assertEqual(serializer.is_valid(), True) -# self.assertEqual(serializer.data, data) -# serializer.save() -# new_data = self.BookSerializer(self.books(), many=True).data - -# self.assertEqual(data, new_data) - -# def test_bulk_update_and_create(self): -# """ -# Bulk update serialization may also include created items. -# """ -# data = [ -# { -# 'id': 0, -# 'title': 'The electric kool-aid acid test', -# 'author': 'Tom Wolfe' -# }, { -# 'id': 3, -# 'title': 'Kafka on the shore', -# 'author': 'Haruki Murakami' -# } -# ] -# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) -# self.assertEqual(serializer.is_valid(), True) -# self.assertEqual(serializer.data, data) -# serializer.save() -# new_data = self.BookSerializer(self.books(), many=True).data -# self.assertEqual(data, new_data) - -# def test_bulk_update_invalid_create(self): -# """ -# Bulk update serialization without allow_add_remove may not create items. -# """ -# data = [ -# { -# 'id': 0, -# 'title': 'The electric kool-aid acid test', -# 'author': 'Tom Wolfe' -# }, { -# 'id': 3, -# 'title': 'Kafka on the shore', -# 'author': 'Haruki Murakami' -# } -# ] -# expected_errors = [ -# {}, -# {'non_field_errors': ['Cannot create a new item, only existing items may be updated.']} -# ] -# serializer = self.BookSerializer(self.books(), data=data, many=True) -# self.assertEqual(serializer.is_valid(), False) -# self.assertEqual(serializer.errors, expected_errors) - -# def test_bulk_update_error(self): -# """ -# Incorrect bulk update serialization should return error data. -# """ -# data = [ -# { -# 'id': 0, -# 'title': 'The electric kool-aid acid test', -# 'author': 'Tom Wolfe' -# }, { -# 'id': 'foo', -# 'title': 'Kafka on the shore', -# 'author': 'Haruki Murakami' -# } -# ] -# expected_errors = [ -# {}, -# {'id': ['Enter a whole number.']} -# ] -# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) -# self.assertEqual(serializer.is_valid(), False) -# self.assertEqual(serializer.errors, expected_errors) -- cgit v1.2.3 From 7f8d314101c4e6e059b00ac12658f0e1055da8f7 Mon Sep 17 00:00:00 2001 From: Craig Blaszczyk Date: Thu, 8 Jan 2015 17:16:47 +0000 Subject: update tests to expect new error messages --- tests/test_serializer_bulk_update.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/test_serializer_bulk_update.py') diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index fb881a75..bc955b2e 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -101,7 +101,7 @@ class BulkCreateSerializerTests(TestCase): serializer = self.BookSerializer(data=data, many=True) self.assertEqual(serializer.is_valid(), False) - expected_errors = {'non_field_errors': ['Expected a list of items but got type `int`.']} + expected_errors = {'non_field_errors': ['Expected a list of items but got type "int".']} self.assertEqual(serializer.errors, expected_errors) @@ -118,6 +118,6 @@ class BulkCreateSerializerTests(TestCase): serializer = self.BookSerializer(data=data, many=True) self.assertEqual(serializer.is_valid(), False) - expected_errors = {'non_field_errors': ['Expected a list of items but got type `dict`.']} + expected_errors = {'non_field_errors': ['Expected a list of items but got type "dict".']} self.assertEqual(serializer.errors, expected_errors) -- cgit v1.2.3