Commit afddedef authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added first version of generator script.

parent 2296a89a
File added
This diff is collapsed.
from unittest import TestCase
import MySQLdb as mysql
from generate import Model, php_value, php_assoc, flatten, read_tables, read_fields
class GenerateTest(TestCase):
conn = mysql.connect('localhost', 'root', 'mysql12#$', 'test')
def setUp(self):
self.model = Model('payments')
def test_php_value_dict(self):
self.assertEqual(php_value({}), 'array()')
self.assertEqual(php_value({'foo': 1}), "array(\n\t'foo' => 1\n)")
self.assertEqual(php_value({'foo': 1, 'bar': 'baz'}), """array(
\t'foo' => 1,
\t'bar' => 'baz'
)""")
self.assertEqual(php_value({'foo': tuple(range(3))}), """array(
\t'foo' => array(0, 1, 2)
)""")
self.assertMultiLineEqual(php_value({'foo': set(range(2))}), """array(
\t'foo' => array(
\t\t0,
\t\t1
\t)
)""")
def test_php_value_tuple(self):
self.assertEqual(php_value(()), 'array()')
self.assertEqual(php_value((1,)), 'array(1)')
self.assertEqual(php_value((1, 'foo')), "array(1, 'foo')")
def test_php_value_set(self):
self.assertEqual(php_value(set()), 'array()')
self.assertEqual(php_value(set([1])), 'array(\n\t1\n)')
self.assertEqual(php_value(set([1, 2])), 'array(\n\t1,\n\t2\n)')
def test_php_value_non_iterable(self):
self.assertEqual(php_value('foo'), "'foo'")
self.assertEqual(php_value("foo's"), "'foo\\'s'")
self.assertEqual(php_value(1), '1')
def test_php_assoc(self):
self.assertEqual(php_assoc(('users', {'through': 'payments'})),
"array('users', 'through' => 'payments')")
def test_flatten(self):
self.assertEqual(flatten([['a'], ['b']]), ['a', 'b'])
self.assertEqual(flatten([['a'], ['b'], ('c', 'd')]), list('abcd'))
def test_read_tables(self):
self.assertEqual(read_tables(self.conn), ['payments', 'users'])
def test_read_fields(self):
fields = read_fields(self.conn, 'payments')
self.assertEqual([field['Field'] for field in fields],
['id', 'amount', 'user_id'])
def test_model_read_attributes(self):
self.model.read_attributes(self.conn)
self.assertEqual(self.model.accessible_attr, ['amount'])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment