Skip to content

Commit

Permalink
new merge bag support
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Mar 14, 2016
1 parent 12946bd commit 959b377
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/budy/models/bag.py
Expand Up @@ -146,6 +146,18 @@ def add_product_s(self, product, quantity = 1.0):
)
self.add_line_s(_bag_line)

def add_update_line_s(self, bag_line):
self.add_product_s(
bag_line.product,
quantity = bag_line.quantity
)

def merge_s(self, bag_id):
bag = Bag.get(id = bag_id)
for line in bag.lines:
line = line.clone()
self.add_update_line_s(line)

def _calculate(self):
lines = self.lines if hasattr(self, "lines") else []
self.total = sum(line.total for line in lines)
40 changes: 40 additions & 0 deletions src/budy/test/bag.py
Expand Up @@ -114,3 +114,43 @@ def test_basic(self):

self.assertEqual(bag.total, 0.0)
self.assertEqual(len(bag.lines), 0)

def test_merge(self):
product = budy.Product.new(
short_description = "product",
gender = "Male",
price = 10.0,
form = False
)
product.save()

bag = budy.Bag.new(form = False)
bag.save()

bag_line = budy.BagLine.new(
quantity = 2.0,
form = False
)
bag_line.product = product
bag_line.save()
bag.add_line_s(bag_line)

self.assertEqual(bag_line.quantity, 2.0)
self.assertEqual(bag_line.total, 20.0)
self.assertEqual(bag.total, 20.0)
self.assertEqual(len(bag.lines), 1)

extra = budy.Bag.new(form = False)
extra.save()

self.assertEqual(len(extra.lines), 0)

extra.merge_s(bag.id)

self.assertEqual(extra.total, 20.0)
self.assertEqual(len(extra.lines), 1)

extra.merge_s(bag.id)

self.assertEqual(extra.total, 40.0)
self.assertEqual(len(extra.lines), 1)

0 comments on commit 959b377

Please sign in to comment.