Ejemplo n.º 1
0
    def test_patch_exception_raised_when_operation_type_missing(self):
        invalid_operation = {
            'value': 'mock',
        }

        patch = Patch([])
        with self.assertRaises(PatchException):
            patch.get_operation(invalid_operation)
Ejemplo n.º 2
0
    def test_patch_exception_raised_when_operation_type_missing(self):
        invalid_operation = {
            'value': 'mock',
        }

        patch = Patch([])
        with self.assertRaises(PatchException):
            patch.get_operation(invalid_operation)
Ejemplo n.º 3
0
    def test_no_error_when_author_id_value_and_type_matches(self):
        Author.objects.create(name='Jeff')

        test_author_name_diff = [{'op': 'test', 'path': '/0/id', 'value': 1}]

        patch = Patch(test_author_name_diff)
        authors = Author.objects.all()
        patch.apply(authors)
Ejemplo n.º 4
0
    def test_add_exception_instance_returned_for_operation_add(self):
        operation = {
            'op': 'add',
            'path': '/0',
            'value': 'mock',
        }

        patch = Patch([])
        add_operation = patch.get_operation(operation)
        self.assertIsInstance(add_operation, AddOperation)
Ejemplo n.º 5
0
    def test_exception_thrown_when_author_id_value_matches_but_type_does_not(
            self):
        Author.objects.create(name='Jeff')

        test_author_name_diff = [{'op': 'test', 'path': '/0/id', 'value': '1'}]

        patch = Patch(test_author_name_diff)
        authors = Author.objects.all()
        with self.assertRaises(PatchException):
            patch.apply(authors)
Ejemplo n.º 6
0
    def test_add_exception_instance_returned_for_operation_add(self):
        operation = {
            'op': 'add',
            'path': '/0',
            'value': 'mock',
        }

        patch = Patch([])
        add_operation = patch.get_operation(operation)
        self.assertIsInstance(add_operation, AddOperation)
Ejemplo n.º 7
0
    def test_author_is_removed(self):
        author = Author.objects.create(name='Bob')
        Author.objects.create(name='Jeff')

        delete_author_diff = [{'op': 'remove', 'path': '/'}]

        patch = Patch(delete_author_diff)
        patch.apply(author)

        author_count = Author.objects.all().count()
        self.assertEqual(author_count, 1)
Ejemplo n.º 8
0
    def test_exception_thrown_when_path_does_not_exist(self):
        test_author_name_diff = [{
            'op': 'test',
            'path': '/0/name',
            'value': 'Jeff'
        }]

        patch = Patch(test_author_name_diff)
        authors = Author.objects.all()
        with self.assertRaises(PointerException):
            patch.apply(authors)
Ejemplo n.º 9
0
    def test_nested_book_is_removed_in_list(self):
        author = Author.objects.create(name='Jeff')
        Book.objects.create(author=author, title='Book One')

        delete_book_diff = [{'op': 'remove', 'path': '/0/books/0'}]

        patch = Patch(delete_book_diff)
        authors = list(Author.objects.all())
        patch.apply(authors)

        book_count = Book.objects.all().count()
        self.assertEqual(book_count, 0)
Ejemplo n.º 10
0
    def test_exception_thrown_when_path_does_not_exist(self):
        test_author_name_diff = [
            {
                'op': 'test',
                'path': '/0/name',
                'value': 'Jeff'
            }
        ]

        patch = Patch(test_author_name_diff)
        authors = Author.objects.all()
        with self.assertRaises(PointerException):
            patch.apply(authors)
Ejemplo n.º 11
0
    def test_no_error_when_author_id_value_and_type_matches(self):
        Author.objects.create(name='Jeff')

        test_author_name_diff = [
            {
                'op': 'test',
                'path': '/0/id',
                'value': 1
            }
        ]

        patch = Patch(test_author_name_diff)
        authors = Author.objects.all()
        patch.apply(authors)
Ejemplo n.º 12
0
    def test_author_name_is_replaced_in_single_object(self):
        author = Author.objects.create(name='Bob')

        update_author_diff = [{
            'op': 'replace',
            'path': '/name',
            'value': 'Jeff',
        }]

        patch = Patch(update_author_diff)
        patch.apply(author)

        author_lookup = Author.objects.get(pk=author.pk)
        self.assertEqual(author_lookup.name, 'Jeff')
Ejemplo n.º 13
0
    def test_exception_thrown_when_author_name_does_not_match(self):
        Author.objects.create(name='Jeff')

        test_author_name_diff = [
            {
                'op': 'test',
                'path': '/0/name',
                'value': 'Bob'
            }
        ]

        patch = Patch(test_author_name_diff)
        authors = Author.objects.all()
        with self.assertRaises(PatchException):
            patch.apply(authors)
Ejemplo n.º 14
0
    def test_author_name_is_replaced_in_list(self):
        Author.objects.create(name='Bob')

        update_author_diff = [{
            'op': 'replace',
            'path': '/0/name',
            'value': 'Jeff',
        }]

        patch = Patch(update_author_diff)
        authors = Author.objects.all()
        patch.apply(authors)

        author = Author.objects.first()
        self.assertEqual(author.name, 'Jeff')
Ejemplo n.º 15
0
    def test_author_is_removed(self):
        author = Author.objects.create(name='Bob')
        Author.objects.create(name='Jeff')

        delete_author_diff = [
            {
                'op': 'remove',
                'path': '/'
            }
        ]

        patch = Patch(delete_author_diff)
        patch.apply(author)

        author_count = Author.objects.all().count()
        self.assertEqual(author_count, 1)
Ejemplo n.º 16
0
    def test_author_is_created_in_single_object(self):
        add_author_diff = [{
            'op': 'add',
            'path': '/',
            'value': {
                'id': 1,
                'name': 'Jane'
            }
        }]

        patch = Patch(add_author_diff)
        authors = Author.objects.none()
        patch.apply(authors)

        author = Author.objects.get(pk=1)
        self.assertEqual(author.name, 'Jane')
Ejemplo n.º 17
0
    def test_author_name_is_replaced_in_single_object(self):
        author = Author.objects.create(name='Bob')

        update_author_diff = [
            {
                'op': 'replace',
                'path': '/name',
                'value': 'Jeff',
            }
        ]

        patch = Patch(update_author_diff)
        patch.apply(author)

        author_lookup = Author.objects.get(pk=author.pk)
        self.assertEqual(author_lookup.name, 'Jeff')
Ejemplo n.º 18
0
    def test_exception_throw_when_adding_invalid_path(self):
        add_author_diff = [{
            'op': 'add',
            'path': '/0/books/0',
            'value': {
                'id': 1,
                'title': 'Book one',
                'author': 1
            }
        }]

        patch = Patch(add_author_diff)
        authors = Author.objects.all()

        with self.assertRaises(PointerException):
            patch.apply(authors)
Ejemplo n.º 19
0
    def test_author_name_is_replaced_in_list(self):
        Author.objects.create(name='Bob')

        update_author_diff = [
            {
                'op': 'replace',
                'path': '/0/name',
                'value': 'Jeff',
            }
        ]

        patch = Patch(update_author_diff)
        authors = Author.objects.all()
        patch.apply(authors)

        author = Author.objects.first()
        self.assertEqual(author.name, 'Jeff')
Ejemplo n.º 20
0
    def test_nested_book_two_name_is_replaced_in_list(self):
        author = Author.objects.create(name='Bob')
        Book.objects.create(author=author, title='Book One')
        Book.objects.create(author=author, title='Book two')

        update_book_diff = [{
            'op': 'replace',
            'path': '/0/books/1/title',
            'value': 'Book Two',
        }]

        patch = Patch(update_book_diff)
        authors = Author.objects.all()
        patch.apply(authors)

        books = Book.objects.filter(author=author).all()
        self.assertEqual(books[1].title, 'Book Two')
Ejemplo n.º 21
0
    def test_nested_book_is_removed_in_list(self):
        author = Author.objects.create(name='Jeff')
        Book.objects.create(author=author, title='Book One')

        delete_book_diff = [
            {
                'op': 'remove',
                'path': '/0/books/0'
            }
        ]

        patch = Patch(delete_book_diff)
        authors = list(Author.objects.all())
        patch.apply(authors)

        book_count = Book.objects.all().count()
        self.assertEqual(book_count, 0)
Ejemplo n.º 22
0
    def test_nested_book_is_created_in_list(self):
        author = Author.objects.create(name='Jane')
        add_book_diff = [{
            'op': 'add',
            'path': '/0/books/0',
            'value': {
                'id': 1,
                'title': 'Book one',
                'author': 1
            }
        }]
        patch = Patch(add_book_diff)
        authors = Author.objects.all()
        patch.apply(authors)

        books = Book.objects.filter(author=author).all()
        self.assertEqual(books[0].title, 'Book one')
Ejemplo n.º 23
0
    def test_exception_throw_when_adding_invalid_path(self):
        add_author_diff = [
            {
                'op': 'add',
                'path': '/0/books/0',
                'value': {
                    'id': 1,
                    'title': 'Book one',
                    'author': 1
                }
            }
        ]

        patch = Patch(add_author_diff)
        authors = Author.objects.all()

        with self.assertRaises(PointerException):
            patch.apply(authors)
Ejemplo n.º 24
0
    def test_author_is_created_in_single_object(self):
        add_author_diff = [
            {
                'op': 'add',
                'path': '/',
                'value': {
                    'id': 1,
                    'name': 'Jane'
                }
            }
        ]

        patch = Patch(add_author_diff)
        authors = Author.objects.none()
        patch.apply(authors)

        author = Author.objects.get(pk=1)
        self.assertEqual(author.name, 'Jane')
Ejemplo n.º 25
0
    def test_nested_book_two_name_is_replaced_in_list(self):
        author = Author.objects.create(name='Bob')
        Book.objects.create(author=author, title='Book One')
        Book.objects.create(author=author, title='Book two')

        update_book_diff = [
            {
                'op': 'replace',
                'path': '/0/books/1/title',
                'value': 'Book Two',
            }
        ]

        patch = Patch(update_book_diff)
        authors = Author.objects.all()
        patch.apply(authors)

        books = Book.objects.filter(author=author).all()
        self.assertEqual(books[1].title, 'Book Two')
Ejemplo n.º 26
0
    def test_nested_book_is_created_in_list(self):
        author = Author.objects.create(name='Jane')
        add_book_diff = [
            {
                'op': 'add',
                'path': '/0/books/0',
                'value': {
                    'id': 1,
                    'title': 'Book one',
                    'author': 1
                }
            }
        ]
        patch = Patch(add_book_diff)
        authors = Author.objects.all()
        patch.apply(authors)

        books = Book.objects.filter(author=author).all()
        self.assertEqual(books[0].title, 'Book one')
Ejemplo n.º 27
0
    def test_authors_are_removed_from_queryset(self):
        Author.objects.create(name='Bob')
        Author.objects.create(name='Jeff')
        Author.objects.create(name='Jane')

        delete_all_authors_diff = [{
            'op': 'remove',
            'path': '/0'
        }, {
            'op': 'remove',
            'path': '/0'
        }]

        patch = Patch(delete_all_authors_diff)
        authors = Author.objects.all()
        patch.apply(authors)

        author_count = Author.objects.all().count()
        self.assertEqual(author_count, 1)
Ejemplo n.º 28
0
    def test_authors_are_removed_from_queryset(self):
        Author.objects.create(name='Bob')
        Author.objects.create(name='Jeff')
        Author.objects.create(name='Jane')

        delete_all_authors_diff = [
            {
                'op': 'remove',
                'path': '/0'
            },
            {
                'op': 'remove',
                'path': '/0'
            }
        ]

        patch = Patch(delete_all_authors_diff)
        authors = Author.objects.all()
        patch.apply(authors)

        author_count = Author.objects.all().count()
        self.assertEqual(author_count, 1)
Ejemplo n.º 29
0
 def test_add_operation_class_returned_for_operation_add_(self):
     patch = Patch([])
     add_operation_class = patch.get_operation_class('add')
     self.assertEqual(add_operation_class, AddOperation)
Ejemplo n.º 30
0
 def test_patch_exception_raised_when_operation_is_unsupported(self):
     patch = Patch([])
     with self.assertRaises(PatchException):
         patch.get_operation_class('mock')
Ejemplo n.º 31
0
 def test_patch_exception_raised_when_operation_is_unsupported(self):
     patch = Patch([])
     with self.assertRaises(PatchException):
         patch.get_operation_class('mock')
Ejemplo n.º 32
0
 def test_add_operation_class_returned_for_operation_add_(self):
     patch = Patch([])
     add_operation_class = patch.get_operation_class('add')
     self.assertEqual(add_operation_class, AddOperation)