def get_meal(self, date, index): try: meal = (Session.query(Meal).filter( and_(Meal.date == date, Meal.index == index)).one()) except NoResultFound: meal = Meal(date, index) Session.add(meal) return meal
def get_meal(self, date, index): try: meal = (Session.query(Meal). filter(and_(Meal.date == date, Meal.index == index)) .one()) except NoResultFound: meal = Meal(date, index) Session.add(meal) return meal
def _get_one(self): self.recipe = Recipe('carbo') self.m1 = Meal(datetime.date(2010, 01, 01), 0, [self.recipe]) self.m2 = Meal(datetime.date(2010, 01, 05), 1, [self.recipe]) Session.add_all([self.recipe, self.m1, self.m2]) fro = datetime.date(2010, 01, 01) to = datetime.date(2010, 01, 10) pw = PlanningWidget(fro, to) return pw
def keyPressEvent(self, event): if event.key() == Qt.Key_Delete: # Pas de confirmation, pas de sauvegarde : on est des oufs ! ids = [item.data(Qt.UserRole) for item in self.selectedItems()] Session.query(Recipe).filter(Recipe.id.in_(ids)).delete('fetch') self.reload() elif event.key() == Qt.Key_N: self.editRecipe(None) else: super(RecipesWidget, self).keyPressEvent(event)
def test_remove_recipe(self): rw = RecipesWidget() self.assertEqual(len(rw.selectedItems()), 0) before = Session.query(Recipe).count() rw.item(0).setSelected(True) rw.item(3).setSelected(True) QTest.keyClick(rw, Qt.Key_Delete) self.assertEqual(Session.query(Recipe).count(), before - 2) self.assertEqual(rw.count(), before - 2)
def test_cant_add_twice_the_same_even_after_reload(self): tw = TagsWidget() QTest.keyClicks(tw.lineEdit, 'lourd') tw.addTag() Session.add(Tag(u'Another tag')) tw.reload() QTest.keyClicks(tw.lineEdit, 'lourd') tw.addTag() self.assertEqual(tw.listWidget.count(), 1)
def test_we_got_the_right_meals(self): recipe = Recipe('carbo') m1 = Meal(datetime.date(2010, 01, 01), 0, [recipe]) m2 = Meal(datetime.date(2010, 01, 05), 0, [recipe]) m3 = Meal(datetime.date(2010, 01, 12), 1, [recipe]) Session.add_all((recipe, m1, m2, m3)) obj = mock.Mock(PlanningWidget) obj.fro = datetime.date(2010, 01, 01) obj.to = datetime.date(2010, 01, 10) self.assertEqual(PlanningWidget._meals_in_range(obj), [m1, m2])
def test_create_recipe(self): fut = RecipeEditionDialog() self.assertEqual(fut.name.text(), '') self.assertEqual(fut.description.toPlainText(), '') self.assertEqual(fut.tags.text(), '') fut.name.setText(u'Endive') fut.description.setPlainText(u'desc') fut.tags.setText(u'légume') tag = Tag(u'légume') Session.add(tag) fut.accept() r = fut.recipe self.assertEqual(r.name, u'Endive') self.assertEqual(r.description, u'desc') self.assertEqual(r.tags, [tag])
def reload(self): tags = Session.query(Tag.name, Tag.id).all() self.tagsdict = dict(tags) self.availableTags = self.tagsdict.keys() for i in xrange(self.listWidget.count()): tag = self.listWidget.item(i).text() if tag in self.availableTags: self.availableTags.remove(tag) self.lineEdit.completer().model().setStringList(self.availableTags)
def test_edit_recipe(self): r = self.recipes[1] fut = RecipeEditionDialog(r) self.assertEqual(fut.name.text(), r.name) self.assertEqual(fut.description.toPlainText(), r.description) self.assertIn(fut.tags.text(), [u'pâtes;tomate', u'tomate;pâtes']) fut.name.setText(u'Endive') fut.description.setPlainText(u'desc') fut.tags.setText(u'légume') tag = Tag(u'légume') Session.add(tag) fut.accept() r = fut.recipe self.assertEqual(r.name, u'Endive') self.assertEqual(r.description, u'desc') self.assertEqual(r.tags, [tag])
def test_getting_the_entered_tags(self): tw = TagsWidget() QTest.keyClicks(tw.lineEdit, 'lourd') tw.addTag() expected = [t.id for t in Session.query(Tag.id) .filter(Tag.name == u'lourd') .all()] self.assertEqual(tw.tags(), expected)
def test_getting_the_entered_tags(self): tw = TagsWidget() QTest.keyClicks(tw.lineEdit, 'lourd') tw.addTag() expected = [ t.id for t in Session.query(Tag.id).filter(Tag.name == u'lourd').all() ] self.assertEqual(tw.tags(), expected)
def load_some_tags(): t1 = Tag(u'pâtes') t2 = Tag(u'tomate') r1 = Recipe(u'carbonara') r1.tags = [t1] r2 = Recipe(u'bolognaise') r2.tags = [t1, t2] r3 = Recipe(u'tomates farcies') r3.tags = [t2] r4 = Recipe(u'saucisses-purée') tags = [t1, t2] recipes = [r1, r2, r3, r4] Session.add_all(tags) Session.add_all(recipes) Session.flush() return tags, recipes
def add_recipe(self, id, date, index): meal = self.get_meal(date, index) recipe = Session.query(Recipe).get(id) meal.recipes.append(recipe) self.widgets[(date, index)].addRecipe(recipe)
def setUp(self): super(TagsWidgetTestCase, self).setUp() Session.add_all(Tag(t) for t in [u'lourd', u'pâtes', u'hiver']) self.app = get_app()
def remove_recipe(self, id, date, index): meal = self.get_meal(date, index) recipe = Session.query(Recipe).get(id) meal.recipes.remove(recipe) self.widgets[(date, index)].removeRecipe(recipe)
def doubleClick(self, item): recipe = Session.query(Recipe).get(item.data(Qt.UserRole)) self.editRecipe(recipe)
def get_recipes(tags=()): recipes = Session.query(Recipe) for t in tags: recipes = recipes.filter(Recipe.tags.any(Tag.id == t)) return recipes.all()
def test_get_existing_tag(self): t = Tag('yay') Session.add(t) self.assertEqual(t, Tag.get('yay'))
def _meals_in_range(self): return (Session.query(Meal).options(joinedload('recipes')) .filter(and_(self.fro <= Meal.date, Meal.date < self.to)) .all())
def save_recipe(self): self.recipe.name = self.name.text() self.recipe.description = self.description.toPlainText() self.recipe.tags = [Tag.get(t) for t in self.tags.text().split(';') ] if self.tags.text() else [] Session.add(self.recipe)
def save_recipe(self): self.recipe.name = self.name.text() self.recipe.description = self.description.toPlainText() self.recipe.tags = [Tag.get(t) for t in self.tags.text().split(';')] if self.tags.text() else [] Session.add(self.recipe)
def _meals_in_range(self): return (Session.query(Meal).options(joinedload('recipes')).filter( and_(self.fro <= Meal.date, Meal.date < self.to)).all())
def tearDown(self): super(TestCase, self).tearDown() from re_eat.models import Session Session.remove()