def filter_by(self, category): """Filter data by a category.""" try: if len(category) == 1: data = self.db_api.select_one(query.filter_(category[0])) elif len(category) == 2: data = self.db_api.select_one(query.filter_(category[0]), tuple([category[1]])) else: raise ValueError("Too many categories to filter by.") if not data: return "No movie match this restriction." return self.printer.display(data, columns=[category[0]]) except ValueError as err: return ", ".join(err.args)
def test_filter_by_movies_with_oscar_nomination(self): self.db_api.insert_many(query.update(), self.update_data) data = self.db_api.select_one(query.filter_("oscar_nom")) self.assertEqual( list(map(itemgetter(0), data)), ["The Shawshank Redemption", "Memento", "In Bruges"], )
def test_filter_by_actor(self): self.db_api.insert_many(query.update(), self.update_data) data = self.db_api.select_one(query.filter_("actor"), data=("Szymon Piotr Warszawski", )) self.assertEqual( data, [( "Gods", "Tomasz Kot, Piotr Glowacki, Szymon Piotr Warszawski, Magdalena Czerwinska", )], ),
def test_filter_by_language(self): self.db_api.insert_many(query.update(), self.update_data) data = self.db_api.select_one(query.filter_("language"), data=("polish", )) self.assertEqual(list(map(itemgetter(0), data)), ["Gods"])
def test_filter_by_movies_over_80_ratio(self): self.db_api.insert_many(query.update(), self.update_data) data = self.db_api.select_one(query.filter_("eighty")) self.assertEqual(list(map(itemgetter(0), data)), ["Memento", "The Godfather"])
def test_filter_by_director(self): self.db_api.insert_many(query.update(), self.update_data) data = self.db_api.select_one(query.filter_("director"), data=("Lukasz Palkowski", )) self.assertEqual(data[0][1], "Lukasz Palkowski")