<commit_before># modu
# Copyright (C) 2007 <NAME>
#
# $Id$
#
# See LICENSE for details

import time

from twisted.trial import unittest

from modu import persist
from modu.web import editable, app
from modu.persist import storable, adbapi
from modu.util import form, test, tags

class DatatypesTestCase(unittest.TestCase):
	def setUp(self):
		self.store = persist.Store.get_store()
		if not(self.store):
			pool = adbapi.connect('MySQLdb://modu:modu@localhost/modu')
			self.store = persist.Store(pool)
			#self.store.debug_file = sys.stderr
		
		for sql in test.TEST_TABLES.split(";"):
			if(sql.strip()):
				self.store.pool.runOperation(sql)
	
	
	def tearDown(self):
		pass
	
	
	def get_request(self, form_data={}):
		environ = test.generate_test_wsgi_environment(form_data)
		environ['REQUEST_URI'] = '/app-test/test-resource'
		environ['SCRIPT_FILENAME'] = ''
		environ['HTTP_HOST'] = '____store-test-domain____:1234567'
		environ['SERVER_NAME'] = '____store-test-domain____'
		environ['HTTP_PORT'] = '1234567'
		
		application = app.get_application(environ)
		self.failIf(application is  None, "Didn't get an application object.")
		
		req = app.configure_request(environ, application)
		req['modu.pool'] = app.acquire_db(application.db_url)
		persist.activate_store(req)
		
		return req
	
	
	def test_checkboxfield(self):
		test_itemdef = editable.itemdef(
			selected		= editable.definition(
								type		= 'CheckboxField',
								label		= 'Selected'
							)
		)
		
		test_storable = storable.Storable('test')
		test_storable.selected = 1
		
		itemdef_form = test_itemdef.get_form('detail', test_storable)
		
		reference_form = form.FormNode('test-form')
		reference_form['selected'](type='checkbox', label='Selected', checked=True)
		reference_form['save'](type='submit', value='save', weight=1000)
		reference_form['cancel'](type='submit', value='cancel', weight=1000)
		
		req = self.get_request()
		itemdef_form_html = itemdef_form.render(req)
		reference_form_html = reference_form.render(req)
		
		self.failUnlessEqual(itemdef_form_html, reference_form_html, "Didn't get expected form output, got:\n%s\n  instead of:\n%s" % (itemdef_form_html, reference_form_html) )	
	
	
	def test_stringfield(self):
		test_itemdef = editable.itemdef(
			name			= editable.definition(
								type		= 'StringField',
								label		= 'Name'
							)
		)
		
		test_storable = storable.Storable('test')
		test_storable.name = 'Test Name'
		
		itemdef_form = test_itemdef.get_form('detail', test_storable)
		
		reference_form = form.FormNode('test-form')
		reference_form['name'](type='textfield', label='Name', value='<NAME>')
		reference_form['save'](type='submit', value='save', weight=1000)
		reference_form['cancel'](type='submit', value='cancel', weight=1000)
		
		req = self.get_request()
		itemdef_form_html = itemdef_form.render(req)
		reference_form_html = reference_form.render(req)
		
		self.failUnlessEqual(itemdef_form_html, reference_form_html, "Didn't get expected form output, got:\n%s\n  instead of:\n%s" % (itemdef_form_html, reference_form_html) )	
	
	
	def test_autocomplete_field(self):
		test_itemdef = editable.itemdef(
			name			= editable.definition(
								type		= 'ForeignAutocompleteField',
								label		= 'Name',
								url			= '/autocomplete/url',
								fvalue		= 'id',
								flabel		= 'title',
								ftable		= 'category'
							)
		)
		
		req = self.get_request()
		req.store.ensure_factory('test')
		
		test_storable = storable.Storable('test')
		test_storable.set_factory(req.store.get_factory('test'))
		test_storable.name = 'Test Name'
		
		itemdef_form = test_itemdef.get_form('detail', test_storable)
		
		reference_form = form.FormNode('test-form')
		reference_form['name-ac-fieldset'](type='fieldset', style='brief', label='Name')
		reference_form['name-ac-fieldset']['name-autocomplete'](type='textfield', weight=0,
								attributes={'id':'test-form-name-autocomplete'})
		reference_form['name-ac-fieldset']['ac-support'](weight=1, value=tags.script(type='text/javascript')
									['$("#test-form-name-autocomplete").autocomplete("/autocomplete/url", {onItemSelect:get_ac_callback("test-form-name-ac-callback"), autoFill:1, selectFirst:1, selectOnly:1, minChars:1});'])
		reference_form['name-ac-fieldset']['name'](type='hidden', weight=2, value=0,
								attributes={'id':'test-form-name-ac-callback'})
		reference_form['save'](type='submit', value='save', weight=1000)
		reference_form['cancel'](type='submit', value='cancel', weight=1000)
		
		itemdef_form_html = itemdef_form.render(req)
		reference_form_html = reference_form.render(req)
		
		self.failUnlessEqual(itemdef_form_html, reference_form_html, "Didn't get expected form output, got:\n%s\n  instead of:\n%s" % (itemdef_form_html, reference_form_html) )	
	
	
	def test_linked_labelfield(self):
		test_itemdef = editable.itemdef(
			__special		= editable.definition(
								item_url	= 'http://www.example.com'
							),
			linked_name		= editable.definition(<commit_msg>fixed test bug<commit_after># modu
# Copyright (C) 2007 <NAME>
#
# $Id$
#
# See LICENSE for details

import time

from twisted.trial import unittest

from modu import persist
from modu.web import editable, app
from modu.persist import storable, adbapi
from modu.util import form, test, tags

class DatatypesTestCase(unittest.TestCase):
	def setUp(self):
		self.store = persist.Store.get_store()
		if not(self.store):
			pool = adbapi.connect('MySQLdb://modu:modu@localhost/modu')
			self.store = persist.Store(pool)
			#self.store.debug_file = sys.stderr
		
		for sql in test.TEST_TABLES.split(";"):
			if(sql.strip()):
				self.store.pool.runOperation(sql)
	
	
	def tearDown(self):
		pass
	
	
	def get_request(self, form_data={}):
		environ = test.generate_test_wsgi_environment(form_data)
		environ['REQUEST_URI'] = '/app-test/test-resource'
		environ['SCRIPT_FILENAME'] = ''
		environ['HTTP_HOST'] = '____store-test-domain____:1234567'
		environ['SERVER_NAME'] = '____store-test-domain____'
		environ['HTTP_PORT'] = '1234567'
		
		application = app.get_application(environ)
		self.failIf(application is  None, "Didn't get an application object.")
		
		req = app.configure_request(environ, application)
		req['modu.pool'] = app.acquire_db(application.db_url)
		persist.activate_store(req)
		
		return req
	
	
	def test_checkboxfield(self):
		test_itemdef = editable.itemdef(
			selected		= editable.definition(
								type		= 'CheckboxField',
								label		= 'Selected'
							)
		)
		
		test_storable = storable.Storable('test')
		test_storable.selected = 1
		
		itemdef_form = test_itemdef.get_form('detail', test_storable)
		
		reference_form = form.FormNode('test-form')
		reference_form['selected'](type='checkbox', label='Selected', checked=True)
		reference_form['save'](type='submit', value='save', weight=1000)
		reference_form['cancel'](type='submit', value='cancel', weight=1000)
		
		req = self.get_request()
		itemdef_form_html = itemdef_form.render(req)
		reference_form_html = reference_form.render(req)
		
		self.failUnlessEqual(itemdef_form_html, reference_form_html, "Didn't get expected form output, got:\n%s\n  instead of:\n%s" % (itemdef_form_html, reference_form_html) )	
	
	
	def test_stringfield(self):
		test_itemdef = editable.itemdef(
			name			= editable.definition(
								type		= 'StringField',
								label		= 'Name'
							)
		)
		
		test_storable = storable.Storable('test')
		test_storable.name = '<NAME>'
		
		itemdef_form = test_itemdef.get_form('detail', test_storable)
		
		reference_form = form.FormNode('test-form')
		reference_form['name'](type='textfield', label='Name', value='<NAME>')
		reference_form['save'](type='submit', value='save', weight=1000)
		reference_form['cancel'](type='submit', value='cancel', weight=1000)
		
		req = self.get_request()
		itemdef_form_html = itemdef_form.render(req)
		reference_form_html = reference_form.render(req)
		
		self.failUnlessEqual(itemdef_form_html, reference_form_html, "Didn't get expected form output, got:\n%s\n  instead of:\n%s" % (itemdef_form_html, reference_form_html) )	
	
	
	def test_autocomplete_field(self):
		test_itemdef = editable.itemdef(
			name			= editable.definition(
								type		= 'ForeignAutocompleteField',
								label		= 'Name',
								url			= '/autocomplete/url',
								fvalue		= 'id',
								flabel		= 'title',
								ftable		= 'category'
							)
		)
		
		req = self.get_request()
		req.store.ensure_factory('test')
		
		test_storable = storable.Storable('test')
		test_storable.set_factory(req.store.get_factory('test'))
		test_storable.name = 'Test Name'
		
		itemdef_form = test_itemdef.get_form('detail', test_storable)
		
		reference_form = form.FormNode('test-form')
		reference_form['name-ac-fieldset'](type='fieldset', style='brief', label='Name')
		reference_form['name-ac-fieldset']['name-autocomplete'](type='textfield', weight=0,
								attributes={'id':'test-form-name-autocomplete'})
		reference_form['name-ac-fieldset']['ac-support'](weight=1, value=tags.script(type='text/javascript')
									['$("#test-form-name-autocomplete").autocomplete("/autocomplete/url", {onItemSelect:select_foreign_item("test-form-name-ac-callback"), autoFill:1, selectFirst:1, selectOnly:1, minChars:1});'])
		reference_form['name-ac-fieldset']['name'](type='hidden', weight=2, value=0,
								attributes={'id':'test-form-name-ac-callback'})
		reference_form['save'](type='submit', value='save', weight=1000)
		reference_form['cancel'](type='submit', value='cancel', weight=1000)
		
		itemdef_form_html = itemdef_form.render(req)
		reference_form_html = reference_form.render(req)
		
		self.failUnlessEqual(itemdef_form_html, reference_form_html, "Didn't get expected form output, got:\n%s\n  instead of:\n%s" % (itemdef_form_html, reference_form_html) )	
	
	
	def test_linked_labelfield(self):
		test_itemdef = editable.itemdef(
			__special		= editable.definition(
								item_url	= 'http://www.example.com'
							),
			linked_name		= editable.definition(
{'fraction_non_alphanumeric': 0.14317224784439542, 'fraction_numerical': 0.010226589131742531, 'mean_word_length': 4.667613636363637, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2606273', 'n_tokens_mistral': 4027, 'n_tokens_neox': 3352, 'n_words': 534}