def update(self, id: int, **kwargs) -> bool: """ >>> Category().update(id: int, **kwargs) -> bool >>> Category().update(id: int, name: str = "some str") -> bool Update category, where id is the category's ID. Returns False on failure else True for success. """ sql = " ".join(["UPDATE category SET", placeholder_update(list(kwargs.keys())), "WHERE", placeholder_update(["id"])]) values = list(kwargs.values()) + [id] return self.write(sql, values)
def test_two_keys(self): keys = [ 'full_name', 'age', ] val = "full_name=?, age=?" self.assertEqual(placeholder_update(keys), val)
def update(self, id: int, **kwargs) -> bool: """ >>> Quiz().update(id: int, **kwargs) -> bool >>> Quiz().update(id: int [, cat_id:int = CAT_ID | question: str = Q_STR | options: str = OP_STR | answer: str = A_STR]) -> bool Update category, where id is the category's ID. Returns False on failure else True for success. """ sql = " ".join([ "UPDATE quiz SET", placeholder_update(list(kwargs.keys())), "WHERE", placeholder_update(["id"]) ]) values = list(kwargs.values()) + [ id, ] return self.write(sql, values)
def delete(self, id: int) -> bool: """ >>> Category().delete(id: int) -> bool Delete category by ID. Returns False on failure else True for success. """ sql = " ".join(["DELETE FROM category WHERE", placeholder_update(["id"])]) values = [id, ] return self.write(sql, values)
def one(self, id: int): """ >>> row = read().one(id: int) -> sqlite3.Row >>> print(row['id'], row['name']) Returns a sqlite3.Row passing an integer ID """ sql = " ".join(["SELECT id, name FROM category WHERE", placeholder_update(["id"])]) values = [id, ] cur = self.get_cursor() row = cur.execute(sql, values).fetchone() or [] return row
def one(self, id): """ >>> row = read().one(id: int) -> sqlite3.Row >>> print(row['id'], row['cat_id'], row['question'], row['options'], row['answer']) Returns a sqlite3.Row passing an integer ID """ sql = " ".join([ "SELECT id, cat_id, question, options, answer", "FROM quiz WHERE", placeholder_update(["id"]) ]) cur = self.get_cursor() row = cur.execute(sql, [id]).fetchone() or [] return row
def test_empty_keys(self): keys = [] val = "" self.assertEqual(placeholder_update(keys), val)
def test_non_string_keys(self): with self.assertRaises(Exception): keys = [1, 2, 3] placeholder_update(keys)
def test_five_keys(self): keys = ['full_name', 'age', 'dob', 'job', 'height'] val = "full_name=?, age=?, dob=?, job=?, height=?" self.assertEqual(placeholder_update(keys), val)
def test_one_key(self): keys = [ 'full_name', ] val = "full_name=?" self.assertEqual(placeholder_update(keys), val)