Added tests for cromosoma and little fixes

This commit is contained in:
Antonio de la Rosa 2015-12-08 17:37:14 +01:00
parent 50be717add
commit 271c6c787a
2 changed files with 88 additions and 4 deletions

View file

@ -97,6 +97,8 @@ class WebModel:
self.valid_fields=[] self.valid_fields=[]
self.last_id=0
# A method where create the new fields of this model # A method where create the new fields of this model
def create_fields(self): def create_fields(self):
@ -155,6 +157,7 @@ class WebModel:
except: except:
self.query_error='Cannot insert the new row' self.query_error='Cannot insert the new row'
return False return False
sql="insert into `"+self.name+"` (`"+"`, `".join(fields)+"`) VALUES ("+", ".join(values)+")" sql="insert into `"+self.name+"` (`"+"`, `".join(fields)+"`) VALUES ("+", ".join(values)+")"
@ -163,6 +166,8 @@ class WebModel:
if cursor.rowcount>0: if cursor.rowcount>0:
self.last_id=cursor.lastrowid
return True return True
else: else:
self.query_error='Cannot insert the new row' self.query_error='Cannot insert the new row'
@ -324,9 +329,9 @@ class WebModel:
return cursor.fetchone() return cursor.fetchone()
def insert_id(self, cursor): def insert_id(self):
return cursor.lastrowid return self.last_id
def element_exists(self, id): def element_exists(self, id):
@ -562,8 +567,8 @@ class WebModel:
# Method for drop sql tables and related # Method for drop sql tables and related
def drop_table(name): def drop(self):
pass return WebModel.query(WebModel, 'DROP TABLE '+self.name, [], self.connection_id)
#Return an array with all fields #Return an array with all fields
@ -692,6 +697,14 @@ class WebModel:
for k,r in self.fields.items(): for k,r in self.fields.items():
self.fields[k].required=r 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. #Create a form based in table.

71
tests/webmodeltest.py Normal file
View file

@ -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()