Skip to content

Commit

Permalink
Fixed the case of values returned by the map function.
Browse files Browse the repository at this point in the history
When doing multiple insert, SQLAlchemy was inserting an empty row when
rows is an empty list. Fixed this by not calling insert when there are
no rows.
  • Loading branch information
anandology committed Dec 30, 2011
1 parent bfefb2c commit 2374661
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 5 additions & 5 deletions datastore/store.py
Expand Up @@ -236,11 +236,11 @@ def update_view(self, docs, session):
q = t.delete().where(t.c._id.in_(ids))
session.execute(q)

bind_params = dict((c.name, sa.bindparam(c)) for c in t.columns)
q = t.insert().values(**bind_params)

rows = self.map_docs(docs)
session.execute(q, list(rows))
rows = list(self.map_docs(docs))
if rows:
bind_params = dict((c.name, sa.bindparam(c)) for c in t.columns)
q = t.insert().values(**bind_params)
session.execute(q, rows)

def _parse_order_by(self, order_by):
if isinstance(order_by, list):
Expand Down
22 changes: 22 additions & 0 deletions datastore/tests/test_store.py
Expand Up @@ -61,6 +61,28 @@ def create_views(self):

rows = self.ds.query("lname", lname="bar")
assert [row['_key'] for row in rows] == ['bar']

def test_view_with_no_rows(self):
class NoneView(View):
def get_table(self, metadata):
return sa.Table("none_view", metadata)

def map(self, doc):
return []

class NoneStore(Datastore):
def create_views(self):
return {
"none": NoneView()
}

self.ds = NoneStore("sqlite:///:memory:")

self.ds.put("foo", {"name": "Foo"})
self.ds.put("bar", {"name": "Bar"})

rows = self.ds.query("none")
assert len(rows) == 0

class TestView:
def test_parse_order_by(self):
Expand Down

0 comments on commit 2374661

Please sign in to comment.