def _create(self): if self.values.get(self.primary_field): return self._update() count_fields = len(self.fields) sql = "INSERT INTO {} VALUES ({})".format( self.table_name, ", ".join(["?" for x in range(count_fields)])) execute((sql, list(self.values.values()))) return True
def delete(self): if self.values[self.primary_field]: sql = "DELETE FROM {} WHERE {} = {}".format( self.table_name, self.primary_field, self.values[self.primary_field]) execute(sql) return True return False
def test_execute_insert(self): count_fields = len(Comment.fields) sql = "INSERT INTO {} VALUES ({})".format( Comment.table_name, ", ".join(["?" for x in range(count_fields)])) execute((sql, [ None, 'Ярослав', 'Демиденко', 'Текст', 'Владимирович', '+7(999)9999999', '*****@*****.**', 1 ]), self.db_name) qs = self._getSqlQuery(Comment).select("id").fetchall() self.assertEqual(len(qs), len(Comment.get_default_data()) + 1)
def _update(self): if not self.values.get(self.primary_field): return self._create() values = [] for name, value in self.values.items(): if name != self.primary_field: value = "'{}'".format(value) if isinstance(value, str) else value values.append("{}={}".format(name, value)) sql = "UPDATE {} SET {} WHERE {} = {}".format( self.table_name, ", ".join(values), self.primary_field, self.values[self.primary_field]) execute(sql) return True
def test_execute_update(self): qs = self._getSqlQuery(Comment).select("*").fetchall() item = qs[0] values = [] item.values["first_name"] = "test" for name, value in item.values.items(): if name != item.primary_field: value = "'{}'".format(value) if isinstance(value, str) else value values.append("{}={}".format(name, value)) sql = "UPDATE {} SET {} WHERE {} = {}".format( item.table_name, ", ".join(values), item.primary_field, item.values[item.primary_field]) execute(sql, self.db_name) qs = self._getSqlQuery(Comment).select("*").fetchall() updated_item = qs[0] self.assertEqual(updated_item.values["first_name"], item.values["first_name"])