Files
@ 93889b9694f4
Branch filter:
Location: rattail-project/rattail/tests/autocomplete/test_products.py
93889b9694f4
5.3 KiB
text/x-python
bump: version 0.18.12 → 0.19.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | # -*- coding: utf-8; -*-
from unittest import TestCase
from rattail.config import RattailConfig
try:
import sqlalchemy as sa
from rattail.autocomplete import products as mod
from rattail.db import Session
except ImportError:
pass
else:
class AutocompleterTestCase(TestCase):
def setUp(self):
self.config = RattailConfig()
self.app = self.config.get_app()
self.autocompleter = self.make_autocompleter()
self.engine = sa.create_engine('sqlite://')
self.model = self.app.model
self.model.Base.metadata.create_all(bind=self.engine)
self.session = Session(bind=self.engine)
def tearDown(self):
self.session.rollback()
self.session.close()
class TestProductAutocompleter(AutocompleterTestCase):
def make_autocompleter(self):
return mod.ProductAutocompleter(self.config)
def test_autocomplete(self):
session = self.session
model = self.model
# first create a few products
vinegar = model.Product(description='Apple Cider Vinegar')
session.add(vinegar)
dressing = model.Product(description='Apple Cider Dressing')
session.add(dressing)
oats = model.Product(description='Bulk Oats')
session.add(oats)
deleted = model.Product(description='More Oats', deleted=True)
session.add(deleted)
# searching for nothing yields no results
result = self.autocompleter.autocomplete(session, '')
self.assertEqual(len(result), 0)
# search for 'apple' yields Vinegar, Dressing
result = self.autocompleter.autocomplete(session, 'apple')
self.assertEqual(len(result), 2)
uuids = [info['value'] for info in result]
self.assertIn(vinegar.uuid, uuids)
self.assertIn(dressing.uuid, uuids)
# search for 'oats' yields just the undeleted product
result = self.autocompleter.autocomplete(session, 'oats')
self.assertEqual(len(result), 1)
self.assertEqual(result[0]['value'], oats.uuid)
class TestProductAllAutocompleter(AutocompleterTestCase):
def make_autocompleter(self):
return mod.ProductAllAutocompleter(self.config)
def test_autocomplete(self):
session = self.session
model = self.model
# first create a few products
vinegar = model.Product(description='Apple Cider Vinegar')
session.add(vinegar)
dressing = model.Product(description='Apple Cider Dressing')
session.add(dressing)
oats = model.Product(description='Bulk Oats')
session.add(oats)
deleted = model.Product(description='More Oats', deleted=True)
session.add(deleted)
# searching for nothing yields no results
result = self.autocompleter.autocomplete(session, '')
self.assertEqual(len(result), 0)
# search for 'apple' yields Vinegar, Dressing
result = self.autocompleter.autocomplete(session, 'apple')
self.assertEqual(len(result), 2)
uuids = [info['value'] for info in result]
self.assertIn(vinegar.uuid, uuids)
self.assertIn(dressing.uuid, uuids)
# search for 'oats' yields Bulk, More
result = self.autocompleter.autocomplete(session, 'oats')
self.assertEqual(len(result), 2)
uuids = [info['value'] for info in result]
self.assertIn(oats.uuid, uuids)
self.assertIn(deleted.uuid, uuids)
class TestProductNewOrderAutocompleter(AutocompleterTestCase):
def make_autocompleter(self):
return mod.ProductNewOrderAutocompleter(self.config)
def test_autocomplete(self):
session = self.session
model = self.model
# first create a few products
vinegar = model.Product(description='Apple Cider Vinegar',
upc='074305001321')
session.add(vinegar)
dressing = model.Product(description='Apple Cider Dressing')
session.add(dressing)
oats = model.Product(description='Bulk Oats')
session.add(oats)
deleted = model.Product(description='More Oats', deleted=True)
session.add(deleted)
# searching for nothing yields no results
result = self.autocompleter.autocomplete(session, '')
self.assertEqual(len(result), 0)
# search for 'apple' yields Vinegar, Dressing
result = self.autocompleter.autocomplete(session, 'apple')
self.assertEqual(len(result), 2)
uuids = [info['value'] for info in result]
self.assertIn(vinegar.uuid, uuids)
self.assertIn(dressing.uuid, uuids)
# search for unknown upc yields no results
result = self.autocompleter.autocomplete(session, '7430500116')
self.assertEqual(len(result), 0)
# search for known upc yields just that product
result = self.autocompleter.autocomplete(session, '7430500132')
self.assertEqual(len(result), 1)
self.assertEqual(result[0]['value'], vinegar.uuid)
|