Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save can raise exception if error #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ build
rethinkengine.egg-info
.coverage
docs/_build
dist
dist
bin
include
lib
man
.python
3 changes: 3 additions & 0 deletions .pep8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pep8]
ignore=E401
max-line-length=140
8 changes: 8 additions & 0 deletions rethinkengine/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ValidationError(Exception):
pass


class RqlOperationError(Exception):
pass


class Meta(object):
order_by = None
primary_key_field = 'id'
Expand Down Expand Up @@ -143,6 +147,10 @@ def save(self):
result = table.get(self.pk).update(doc).run(get_conn())
else:
result = table.insert(doc).run(get_conn())

if result.get('errors', False) == 1:
raise RqlOperationError(result['first_error'])

self._dirty = False
if 'generated_keys' in result:
self._data['pk'] = result['generated_keys'][0]
Expand Down
15 changes: 12 additions & 3 deletions rethinkengine/query_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def _build_cursor_obj(self):
order_by_r.append(r.asc(field))
self._cursor_obj = self._cursor_obj.order_by(*order_by_r)

if self._limit:
self._cursor_obj = self._cursor_obj.limit(self._limit)

if self._skip:
self._cursor_obj = self._cursor_obj.skip(self._skip)

if self._limit:
self._cursor_obj = self._cursor_obj.limit(self._limit)

self._iter_index = 0
self._cursor_iter = iter(self._cursor_obj.run(get_conn()))

Expand Down Expand Up @@ -160,6 +160,15 @@ def get_or_create(self, **kwargs):
created = True
return created, doc

def first(self, **kwargs):
self.filter(**kwargs)
self._limit = 1
try:
doc = self.next()
except StopIteration:
doc = None
return doc

def create(self, **kwargs):
doc = self._document(**kwargs)
doc.save()
Expand Down
10 changes: 10 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ class Foo(Document):
number = IntegerField(required=False)


class User(Document):
class Meta(object):
primary_key_field = 'email'

email = StringField()


def setUp():
connect(DB_NAME)
try:
Foo.table_create()
User.table_create()

except r.RqlRuntimeError:
pass


def tearDown():
try:
disconnect(DB_NAME)
Expand Down
17 changes: 14 additions & 3 deletions tests/document/test_document.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from .. import Foo
from rethinkengine import *
from .. import Foo, User
from rethinkengine import RqlOperationError

import rethinkdb as r
import unittest2 as unittest


class DocumentTestCase(unittest.TestCase):
def setUp(self):
Foo.objects.all().delete()
User.objects.all().delete()

def test_set_does_not_exist(self):
# Should not raise an error
Expand Down Expand Up @@ -73,3 +73,14 @@ def test_save_and_update(self):
# Retrieve doc and make sure pks are equal
f = Foo.objects.get(name='Jack')
self.assertEqual(f.pk, pk)

def test_rql_operation_error(self):
user1 = User(email='[email protected]')
user1.save()

user2 = User(email='[email protected]')
user2.save()

with self.assertRaises(RqlOperationError):
user3 = User(email='[email protected]')
user3.save()
18 changes: 18 additions & 0 deletions tests/query_set/test_first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from .. import Foo

import unittest2 as unittest


class GetTestCase(unittest.TestCase):
def setUp(self):
Foo.objects.delete()

Foo(name='foo').save()

def test_get_none(self):
f = Foo.objects.first(name='bar')
self.assertIsNone(f)

def test_get_one(self):
f = Foo.objects.first(name='foo')
self.assertIsInstance(f, Foo)
3 changes: 1 addition & 2 deletions tests/query_set/test_skip.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ def test_filter_and_skip(self):
self.assertEqual(len(result), 1)

def test_limit_and_skip(self):
result = Foo.objects.limit(3).skip(1)
result = Foo.objects.skip(3).limit(3)
self.assertEqual(len(result), 2)