From 271c6c787af25042e6c89ad0262feb50335f1280 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Tue, 8 Dec 2015 17:37:14 +0100 Subject: [PATCH] Added tests for cromosoma and little fixes --- cromosoma/webmodel.py | 21 ++++++++++--- tests/webmodeltest.py | 71 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 tests/webmodeltest.py diff --git a/cromosoma/webmodel.py b/cromosoma/webmodel.py index 8ac6c30..36cbce9 100644 --- a/cromosoma/webmodel.py +++ b/cromosoma/webmodel.py @@ -96,6 +96,8 @@ class WebModel: self.updated=False self.valid_fields=[] + + self.last_id=0 # A method where create the new fields of this model @@ -155,6 +157,7 @@ class WebModel: except: self.query_error='Cannot insert the new row' + return False sql="insert into `"+self.name+"` (`"+"`, `".join(fields)+"`) VALUES ("+", ".join(values)+")" @@ -163,6 +166,8 @@ class WebModel: if cursor.rowcount>0: + self.last_id=cursor.lastrowid + return True else: self.query_error='Cannot insert the new row' @@ -324,9 +329,9 @@ class WebModel: return cursor.fetchone() - def insert_id(self, cursor): + def insert_id(self): - return cursor.lastrowid + return self.last_id def element_exists(self, id): @@ -562,8 +567,8 @@ class WebModel: # Method for drop sql tables and related - def drop_table(name): - pass + def drop(self): + return WebModel.query(WebModel, 'DROP TABLE '+self.name, [], self.connection_id) #Return an array with all fields @@ -692,6 +697,14 @@ class WebModel: for k,r in self.fields.items(): self.fields[k].required=r + #Choose all fields to updated + + def set_valid_fields(self, fields={}): + + if len(fields)==0: + fields=self.fields.keys() + + self.valid_fields=fields #Create a form based in table. diff --git a/tests/webmodeltest.py b/tests/webmodeltest.py new file mode 100644 index 0000000..f2c8099 --- /dev/null +++ b/tests/webmodeltest.py @@ -0,0 +1,71 @@ +from settings import config +from paramecio.cromosoma.webmodel import WebModel +from paramecio.cromosoma import corefields +import unittest + + +# Create TestWebModelMethods + +class ExampleModel(WebModel): + + def create_fields(self): + + # I can change other fields here, how the name. + + self.register(corefields.CharField('title')) + self.register(corefields.CharField('content')) + +model=ExampleModel() + +class TestWebModelMethods(unittest.TestCase): + + def test_test_table(self): + + sql=model.create_table() + + self.assertTrue(WebModel.query(WebModel, sql)) + + post={'title': 'Example title', 'content': 'New content'} + + model.set_valid_fields() + + self.assertTrue(model.insert(post)) + + self.assertEqual(model.insert_id(), 1) + + post={'title': 'Example title Updated', 'content': 'New content Updated'} + + model.conditions=['WHERE id=%s', [1]] + + self.assertTrue(model.update(post)) + + model.yes_reset_conditions=False + + model.conditions=['WHERE id=%s', [1]] + + self.assertEqual(model.select_count(), 1) + + self.assertEqual(model.select_a_row(1, ['title']), {'title': 'Example title Updated'}) + + self.assertEqual(model.select_a_row_where(['title']), {'title': 'Example title Updated'}) + + model.yes_reset_conditions=True + + model.reset_conditions() + + self.assertEqual(model.conditions, ['WHERE 1=1', []]) + + cur=model.select() + + row=model.fetch(cur) + + self.assertEqual(row, {'id': 1, 'title': 'Example title Updated', 'content': 'New content Updated'}) + + + self.assertTrue(model.element_exists(1)) + + self.assertTrue(model.drop()) + +if __name__ == '__main__': + unittest.main() + \ No newline at end of file