2025-01-06 17:03:41 -06:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from pyramid.httpexceptions import HTTPFound
|
|
|
|
|
|
|
|
from sideshow.testing import WebTestCase
|
|
|
|
from sideshow.web.views import products as mod
|
|
|
|
from sideshow.batch.neworder import NewOrderBatchHandler
|
|
|
|
|
|
|
|
|
|
|
|
class TestIncludeme(WebTestCase):
|
|
|
|
|
|
|
|
def test_coverage(self):
|
|
|
|
mod.includeme(self.pyramid_config)
|
|
|
|
|
|
|
|
|
2025-01-09 12:13:58 -06:00
|
|
|
class TestLocalProductView(WebTestCase):
|
|
|
|
|
|
|
|
def make_view(self):
|
|
|
|
return mod.LocalProductView(self.request)
|
|
|
|
|
|
|
|
def test_configure_grid(self):
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
grid = view.make_grid(model_class=model.LocalProduct)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertNotIn("scancode", grid.linked_columns)
|
|
|
|
self.assertNotIn("brand_name", grid.linked_columns)
|
|
|
|
self.assertNotIn("description", grid.linked_columns)
|
2025-01-09 12:13:58 -06:00
|
|
|
view.configure_grid(grid)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertIn("scancode", grid.linked_columns)
|
|
|
|
self.assertIn("brand_name", grid.linked_columns)
|
|
|
|
self.assertIn("description", grid.linked_columns)
|
2025-01-09 12:13:58 -06:00
|
|
|
|
|
|
|
def test_configure_form(self):
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# creating
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(view, "creating", new=True):
|
2025-01-09 12:13:58 -06:00
|
|
|
form = view.make_form(model_class=model.LocalProduct)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertIn("external_id", form)
|
2025-01-09 12:13:58 -06:00
|
|
|
view.configure_form(form)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertNotIn("external_id", form)
|
2025-01-09 12:13:58 -06:00
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-01-09 12:13:58 -06:00
|
|
|
self.session.add(user)
|
|
|
|
product = model.LocalProduct()
|
|
|
|
self.session.add(product)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# viewing
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(view, "viewing", new=True):
|
2025-01-09 12:13:58 -06:00
|
|
|
form = view.make_form(model_instance=product)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertNotIn("external_id", form.readonly_fields)
|
|
|
|
self.assertNotIn("local_products.view.orders", form.grid_vue_context)
|
2025-01-09 12:13:58 -06:00
|
|
|
view.configure_form(form)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertIn("external_id", form.readonly_fields)
|
|
|
|
self.assertIn("local_products.view.orders", form.grid_vue_context)
|
2025-01-09 12:13:58 -06:00
|
|
|
|
|
|
|
def test_make_orders_grid(self):
|
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
view = self.make_view()
|
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-01-09 12:13:58 -06:00
|
|
|
self.session.add(user)
|
|
|
|
order = model.Order(order_id=42, customer_id=42, created_by=user)
|
|
|
|
product = model.LocalProduct()
|
|
|
|
self.session.add(product)
|
2025-08-31 12:59:28 -05:00
|
|
|
item = model.OrderItem(
|
|
|
|
local_product=product,
|
|
|
|
order_qty=1,
|
|
|
|
order_uom=enum.ORDER_UOM_UNIT,
|
|
|
|
status_code=enum.ORDER_ITEM_STATUS_INITIATED,
|
|
|
|
)
|
2025-01-09 12:13:58 -06:00
|
|
|
order.items.append(item)
|
|
|
|
self.session.add(order)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# no view perm
|
|
|
|
grid = view.make_orders_grid(product)
|
|
|
|
self.assertEqual(len(grid.actions), 0)
|
|
|
|
|
|
|
|
# with view perm
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(self.request, "is_root", new=True):
|
2025-01-09 12:13:58 -06:00
|
|
|
grid = view.make_orders_grid(product)
|
|
|
|
self.assertEqual(len(grid.actions), 1)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertEqual(grid.actions[0].key, "view")
|
2025-01-09 12:13:58 -06:00
|
|
|
|
|
|
|
def test_make_new_order_batches_grid(self):
|
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
handler = NewOrderBatchHandler(self.config)
|
|
|
|
view = self.make_view()
|
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-01-09 12:13:58 -06:00
|
|
|
self.session.add(user)
|
|
|
|
batch = handler.make_batch(self.session, created_by=user)
|
|
|
|
self.session.add(batch)
|
|
|
|
product = model.LocalProduct()
|
|
|
|
self.session.add(product)
|
2025-08-31 12:59:28 -05:00
|
|
|
row = handler.make_row(
|
|
|
|
local_product=product, order_qty=1, order_uom=enum.ORDER_UOM_UNIT
|
|
|
|
)
|
2025-01-09 12:13:58 -06:00
|
|
|
handler.add_row(batch, row)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# no view perm
|
|
|
|
grid = view.make_new_order_batches_grid(product)
|
|
|
|
self.assertEqual(len(grid.actions), 0)
|
|
|
|
|
|
|
|
# with view perm
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(self.request, "is_root", new=True):
|
2025-01-09 12:13:58 -06:00
|
|
|
grid = view.make_new_order_batches_grid(product)
|
|
|
|
self.assertEqual(len(grid.actions), 1)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertEqual(grid.actions[0].key, "view")
|
2025-01-09 12:13:58 -06:00
|
|
|
|
|
|
|
|
2025-01-06 17:03:41 -06:00
|
|
|
class TestPendingProductView(WebTestCase):
|
|
|
|
|
|
|
|
def make_view(self):
|
|
|
|
return mod.PendingProductView(self.request)
|
|
|
|
|
|
|
|
def test_configure_grid(self):
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
# nb. mostly just getting coverage here
|
|
|
|
grid = view.make_grid(model_class=model.PendingProduct)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertNotIn("scancode", grid.linked_columns)
|
|
|
|
self.assertNotIn("brand_name", grid.linked_columns)
|
|
|
|
self.assertNotIn("description", grid.linked_columns)
|
2025-01-06 17:03:41 -06:00
|
|
|
view.configure_grid(grid)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertIn("scancode", grid.linked_columns)
|
|
|
|
self.assertIn("brand_name", grid.linked_columns)
|
|
|
|
self.assertIn("description", grid.linked_columns)
|
2025-01-06 17:03:41 -06:00
|
|
|
|
2025-02-21 18:13:57 -06:00
|
|
|
def test_grid_row_class(self):
|
|
|
|
enum = self.app.enum
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
product = model.PendingProduct()
|
|
|
|
|
|
|
|
# null by default
|
|
|
|
self.assertIsNone(view.grid_row_class(product, {}, 1))
|
|
|
|
|
|
|
|
# warning for ignored
|
|
|
|
product.status = enum.PendingProductStatus.IGNORED
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertEqual(view.grid_row_class(product, {}, 1), "has-background-warning")
|
2025-02-21 18:13:57 -06:00
|
|
|
|
2025-01-06 17:03:41 -06:00
|
|
|
def test_configure_form(self):
|
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# creating
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(view, "creating", new=True):
|
2025-01-06 17:03:41 -06:00
|
|
|
form = view.make_form(model_class=model.PendingProduct)
|
|
|
|
view.configure_form(form)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertNotIn("created", form)
|
|
|
|
self.assertNotIn("created_by", form)
|
2025-01-06 17:03:41 -06:00
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(user)
|
2025-08-31 12:59:28 -05:00
|
|
|
product = model.PendingProduct(
|
|
|
|
status=enum.PendingProductStatus.PENDING, created_by=user
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(product)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# viewing
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(view, "viewing", new=True):
|
2025-01-06 17:03:41 -06:00
|
|
|
form = view.make_form(model_instance=product)
|
|
|
|
view.configure_form(form)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertIn("status", form)
|
|
|
|
self.assertIn("created", form)
|
|
|
|
self.assertIn("created_by", form)
|
2025-01-06 17:03:41 -06:00
|
|
|
|
|
|
|
def test_make_orders_grid(self):
|
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
view = self.make_view()
|
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(user)
|
|
|
|
order = model.Order(order_id=42, customer_id=42, created_by=user)
|
2025-08-31 12:59:28 -05:00
|
|
|
product = model.PendingProduct(
|
|
|
|
status=enum.PendingProductStatus.PENDING, created_by=user
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(product)
|
2025-08-31 12:59:28 -05:00
|
|
|
item = model.OrderItem(
|
|
|
|
pending_product=product,
|
|
|
|
order_qty=1,
|
|
|
|
order_uom=enum.ORDER_UOM_UNIT,
|
|
|
|
status_code=enum.ORDER_ITEM_STATUS_INITIATED,
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
order.items.append(item)
|
|
|
|
self.session.add(order)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# no view perm
|
|
|
|
grid = view.make_orders_grid(product)
|
|
|
|
self.assertEqual(len(grid.actions), 0)
|
|
|
|
|
|
|
|
# with view perm
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(self.request, "is_root", new=True):
|
2025-01-06 17:03:41 -06:00
|
|
|
grid = view.make_orders_grid(product)
|
|
|
|
self.assertEqual(len(grid.actions), 1)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertEqual(grid.actions[0].key, "view")
|
2025-01-06 17:03:41 -06:00
|
|
|
|
|
|
|
def test_make_new_order_batches_grid(self):
|
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
handler = NewOrderBatchHandler(self.config)
|
|
|
|
view = self.make_view()
|
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(user)
|
|
|
|
batch = handler.make_batch(self.session, created_by=user)
|
|
|
|
self.session.add(batch)
|
2025-08-31 12:59:28 -05:00
|
|
|
product = model.PendingProduct(
|
|
|
|
status=enum.PendingProductStatus.PENDING, created_by=user
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(product)
|
2025-08-31 12:59:28 -05:00
|
|
|
row = handler.make_row(
|
|
|
|
pending_product=product, order_qty=1, order_uom=enum.ORDER_UOM_UNIT
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
handler.add_row(batch, row)
|
|
|
|
self.session.commit()
|
|
|
|
|
|
|
|
# no view perm
|
|
|
|
grid = view.make_new_order_batches_grid(product)
|
|
|
|
self.assertEqual(len(grid.actions), 0)
|
|
|
|
|
|
|
|
# with view perm
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(self.request, "is_root", new=True):
|
2025-01-06 17:03:41 -06:00
|
|
|
grid = view.make_new_order_batches_grid(product)
|
|
|
|
self.assertEqual(len(grid.actions), 1)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertEqual(grid.actions[0].key, "view")
|
2025-01-06 17:03:41 -06:00
|
|
|
|
2025-02-21 18:13:57 -06:00
|
|
|
def test_get_template_context(self):
|
|
|
|
enum = self.app.enum
|
|
|
|
model = self.app.model
|
|
|
|
view = self.make_view()
|
|
|
|
product = model.PendingProduct(status=enum.PendingProductStatus.PENDING)
|
2025-08-31 12:59:28 -05:00
|
|
|
orig_context = {"instance": product}
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# local setting omitted by default
|
|
|
|
context = view.get_template_context(orig_context)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertNotIn("use_local_products", context)
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# still omitted even though 'viewing'
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(view, "viewing", new=True):
|
2025-02-21 18:13:57 -06:00
|
|
|
context = view.get_template_context(orig_context)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertNotIn("use_local_products", context)
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# still omitted even though correct status
|
|
|
|
product.status = enum.PendingProductStatus.READY
|
|
|
|
context = view.get_template_context(orig_context)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertNotIn("use_local_products", context)
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# no longer omitted if user has perm
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(self.request, "is_root", new=True):
|
2025-02-21 18:13:57 -06:00
|
|
|
context = view.get_template_context(orig_context)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertIn("use_local_products", context)
|
2025-02-21 18:13:57 -06:00
|
|
|
# nb. true by default
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertTrue(context["use_local_products"])
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# accurately reflects config
|
2025-08-31 12:59:28 -05:00
|
|
|
self.config.setdefault("sideshow.orders.use_local_products", "false")
|
2025-02-21 18:13:57 -06:00
|
|
|
context = view.get_template_context(orig_context)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertFalse(context["use_local_products"])
|
2025-02-21 18:13:57 -06:00
|
|
|
|
2025-01-06 17:03:41 -06:00
|
|
|
def test_delete_instance(self):
|
2025-08-31 12:59:28 -05:00
|
|
|
self.pyramid_config.add_route(
|
|
|
|
"pending_products.view", "/pending/products/{uuid}"
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
handler = NewOrderBatchHandler(self.config)
|
|
|
|
view = self.make_view()
|
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(user)
|
|
|
|
|
|
|
|
# 1st product is standalone, will be deleted
|
2025-08-31 12:59:28 -05:00
|
|
|
product = model.PendingProduct(
|
|
|
|
status=enum.PendingProductStatus.PENDING, created_by=user
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(product)
|
|
|
|
self.session.flush()
|
|
|
|
self.assertEqual(self.session.query(model.PendingProduct).count(), 1)
|
|
|
|
view.delete_instance(product)
|
|
|
|
self.session.flush()
|
|
|
|
self.assertEqual(self.session.query(model.PendingProduct).count(), 0)
|
|
|
|
|
|
|
|
# 2nd product is attached to new order batch, will not be deleted
|
|
|
|
batch = handler.make_batch(self.session, created_by=user)
|
|
|
|
self.session.add(batch)
|
2025-08-31 12:59:28 -05:00
|
|
|
product = model.PendingProduct(
|
|
|
|
status=enum.PendingProductStatus.PENDING, created_by=user
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
self.session.add(product)
|
2025-08-31 12:59:28 -05:00
|
|
|
row = handler.make_row(
|
|
|
|
pending_product=product, order_qty=1, order_uom=enum.ORDER_UOM_UNIT
|
|
|
|
)
|
2025-01-06 17:03:41 -06:00
|
|
|
handler.add_row(batch, row)
|
|
|
|
self.session.flush()
|
|
|
|
self.assertEqual(self.session.query(model.PendingProduct).count(), 1)
|
|
|
|
self.assertRaises(HTTPFound, view.delete_instance, product)
|
|
|
|
self.session.flush()
|
|
|
|
self.assertEqual(self.session.query(model.PendingProduct).count(), 1)
|
|
|
|
|
|
|
|
# but after batch is executed, 2nd product can be deleted
|
|
|
|
batch.executed = datetime.datetime.now()
|
|
|
|
batch.executed_by = user
|
|
|
|
self.session.flush()
|
|
|
|
self.assertEqual(self.session.query(model.PendingProduct).count(), 1)
|
|
|
|
view.delete_instance(product)
|
|
|
|
self.session.flush()
|
|
|
|
self.assertEqual(self.session.query(model.PendingProduct).count(), 0)
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
def test_resolve(self):
|
2025-08-31 12:59:28 -05:00
|
|
|
self.pyramid_config.add_route(
|
|
|
|
"pending_products.view", "/pending/products/{uuid}"
|
|
|
|
)
|
2025-02-21 18:13:57 -06:00
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# sample data
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-02-21 18:13:57 -06:00
|
|
|
self.session.add(user)
|
2025-08-31 12:59:28 -05:00
|
|
|
product = model.PendingProduct(
|
|
|
|
status=enum.PendingProductStatus.PENDING, created_by=user
|
|
|
|
)
|
2025-02-21 18:13:57 -06:00
|
|
|
self.session.add(product)
|
|
|
|
self.session.flush()
|
|
|
|
|
|
|
|
info = {
|
2025-08-31 12:59:28 -05:00
|
|
|
"product_id": "07430500132",
|
|
|
|
"scancode": "07430500132",
|
|
|
|
"brand_name": "Bragg's",
|
|
|
|
"description": "Apple Cider Vinegar",
|
|
|
|
"size": "32oz",
|
|
|
|
"weighed": False,
|
|
|
|
"department_id": None,
|
|
|
|
"department_name": None,
|
|
|
|
"special_order": False,
|
|
|
|
"vendor_name": None,
|
|
|
|
"vendor_item_code": None,
|
|
|
|
"case_size": 12,
|
|
|
|
"unit_cost": 2.99,
|
|
|
|
"unit_price_reg": 5.99,
|
2025-02-21 18:13:57 -06:00
|
|
|
}
|
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(view, "Session", return_value=self.session):
|
|
|
|
with patch.object(self.request, "user", new=user):
|
|
|
|
with patch.object(
|
|
|
|
self.request, "matchdict", new={"uuid": product.uuid}
|
|
|
|
):
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# flash error if wrong status
|
|
|
|
result = view.resolve()
|
|
|
|
self.assertIsInstance(result, HTTPFound)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertTrue(self.request.session.peek_flash("error"))
|
|
|
|
self.assertEqual(
|
|
|
|
self.request.session.pop_flash("error"),
|
|
|
|
["pending product does not have 'ready' status!"],
|
|
|
|
)
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# flash error if product_id not specified
|
|
|
|
product.status = enum.PendingProductStatus.READY
|
|
|
|
result = view.resolve()
|
|
|
|
self.assertIsInstance(result, HTTPFound)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertTrue(self.request.session.peek_flash("error"))
|
|
|
|
self.assertEqual(
|
|
|
|
self.request.session.pop_flash("error"),
|
|
|
|
["must specify valid product_id"],
|
|
|
|
)
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# more sample data
|
2025-08-31 12:59:28 -05:00
|
|
|
order = model.Order(
|
|
|
|
order_id=100, created_by=user, customer_name="Fred Flintstone"
|
|
|
|
)
|
|
|
|
item = model.OrderItem(
|
|
|
|
pending_product=product,
|
|
|
|
order_qty=1,
|
|
|
|
order_uom=enum.ORDER_UOM_UNIT,
|
|
|
|
status_code=enum.ORDER_ITEM_STATUS_READY,
|
|
|
|
)
|
2025-02-21 18:13:57 -06:00
|
|
|
order.items.append(item)
|
|
|
|
self.session.add(order)
|
|
|
|
|
|
|
|
# product + order items updated
|
|
|
|
self.assertIsNone(product.product_id)
|
|
|
|
self.assertEqual(product.status, enum.PendingProductStatus.READY)
|
|
|
|
self.assertIsNone(item.product_id)
|
|
|
|
batch_handler = NewOrderBatchHandler(self.config)
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(
|
|
|
|
batch_handler, "get_product_info_external", return_value=info
|
|
|
|
):
|
|
|
|
with patch.object(
|
|
|
|
self.app, "get_batch_handler", return_value=batch_handler
|
|
|
|
):
|
|
|
|
with patch.object(
|
|
|
|
self.request, "POST", new={"product_id": "07430500132"}
|
|
|
|
):
|
|
|
|
with patch.object(
|
|
|
|
batch_handler,
|
|
|
|
"get_product_info_external",
|
|
|
|
return_value=info,
|
|
|
|
):
|
2025-02-21 18:13:57 -06:00
|
|
|
result = view.resolve()
|
|
|
|
self.assertIsInstance(result, HTTPFound)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertFalse(self.request.session.peek_flash("error"))
|
|
|
|
self.assertEqual(product.product_id, "07430500132")
|
2025-02-21 18:13:57 -06:00
|
|
|
self.assertEqual(product.status, enum.PendingProductStatus.RESOLVED)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertEqual(item.product_id, "07430500132")
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
def test_ignore(self):
|
2025-08-31 12:59:28 -05:00
|
|
|
self.pyramid_config.add_route(
|
|
|
|
"pending_products.view", "/pending/products/{uuid}"
|
|
|
|
)
|
2025-02-21 18:13:57 -06:00
|
|
|
model = self.app.model
|
|
|
|
enum = self.app.enum
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# sample data
|
2025-08-31 12:59:28 -05:00
|
|
|
user = model.User(username="barney")
|
2025-02-21 18:13:57 -06:00
|
|
|
self.session.add(user)
|
2025-08-31 12:59:28 -05:00
|
|
|
product = model.PendingProduct(
|
|
|
|
status=enum.PendingProductStatus.PENDING, created_by=user
|
|
|
|
)
|
2025-02-21 18:13:57 -06:00
|
|
|
self.session.add(product)
|
|
|
|
self.session.flush()
|
|
|
|
|
2025-08-31 12:59:28 -05:00
|
|
|
with patch.object(view, "Session", return_value=self.session):
|
|
|
|
with patch.object(self.request, "user", new=user):
|
|
|
|
with patch.object(
|
|
|
|
self.request, "matchdict", new={"uuid": product.uuid}
|
|
|
|
):
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# flash error if wrong status
|
|
|
|
result = view.ignore()
|
|
|
|
self.assertIsInstance(result, HTTPFound)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertTrue(self.request.session.peek_flash("error"))
|
|
|
|
self.assertEqual(
|
|
|
|
self.request.session.pop_flash("error"),
|
|
|
|
["pending product does not have 'ready' status!"],
|
|
|
|
)
|
2025-02-21 18:13:57 -06:00
|
|
|
|
|
|
|
# product updated
|
|
|
|
product.status = enum.PendingProductStatus.READY
|
|
|
|
self.assertIsNone(product.product_id)
|
|
|
|
self.assertEqual(product.status, enum.PendingProductStatus.READY)
|
|
|
|
result = view.ignore()
|
|
|
|
self.assertIsInstance(result, HTTPFound)
|
2025-08-31 12:59:28 -05:00
|
|
|
self.assertFalse(self.request.session.peek_flash("error"))
|
2025-02-21 18:13:57 -06:00
|
|
|
self.assertIsNone(product.product_id)
|
|
|
|
self.assertEqual(product.status, enum.PendingProductStatus.IGNORED)
|