def cleanup(self): from models_test import Person, EmploymentStatus, Phone,\ Tag, TagsForPerson DBSession.query(TagsForPerson).delete() DBSession.query(Tag).delete() DBSession.query(Phone).delete() DBSession.query(Person).delete() DBSession.query(EmploymentStatus).delete()
def test_list(self): from c2cgeoform.views import list from models_test import Person DBSession.add(Person(name="Peter", first_name="Smith")) DBSession.add(Person(name="John", first_name="Wayne")) request = testing.DummyRequest() request.matchdict['schema'] = 'tests_persons' response = list(request) self.assertTrue('entities' in response) entities = response['entities'] self.assertEquals(2, len(entities))
def test_list(self): from c2cgeoform.views import list from models_test import Person DBSession.add(Person(name="Peter", first_name="Smith")) DBSession.add(Person(name="John", first_name="Wayne")) request = self._get_request() request.matchdict['schema'] = 'tests_persons' response = list(request) self.assertTrue('entities' in response) entities = response['entities'] self.assertEquals(2, len(entities)) schema = response['schema'] self.assertEquals(['id', 'name'], schema.list_fields)
def test_view_user(self): from c2cgeoform.views import view_user from models_test import Person person = Person(name="Peter", first_name="Smith", hash="123-456") DBSession.add(person) DBSession.flush() request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.matchdict['hash'] = "123-456" response = view_user(request) self.assertTrue('schema' in response) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('Peter' in form_html) self.assertTrue('Smith' in form_html)
def test_edit_submit_invalid(self): from c2cgeoform.views import edit from models_test import Person person = Person(name="Peter", first_name="Smith") DBSession.add(person) DBSession.flush() request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.matchdict['id'] = str(person.id) request.POST['id'] = str(person.id) request.POST['submit'] = 'submit' response = edit(request) self.assertTrue('schema' in response) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('class="errorMsgLbl"' in form_html)
def test_edit_submit_invalid(self): from c2cgeoform.views import edit from models_test import Person person = Person(name="Peter", first_name="Smith") DBSession.add(person) DBSession.flush() request = testing.DummyRequest() request.matchdict['schema'] = 'tests_persons' request.matchdict['id'] = str(person.id) request.POST['id'] = str(person.id) request.POST['submit'] = 'submit' response = edit(request) self.assertTrue('schema' in response) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('class="errorMsgLbl"' in form_html)
def test_view_admin(self): from c2cgeoform.views import view_admin from models_test import Person person = Person(name="Peter", first_name="Smith") DBSession.add(person) DBSession.flush() request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.matchdict['id'] = str(person.id) response = view_admin(request) self.assertTrue('schema' in response) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('name="id"' in form_html) self.assertTrue('value="' + str(person.id) + '"' in form_html) self.assertTrue('Peter' in form_html) self.assertTrue('Smith' in form_html)
def test_view(self): from c2cgeoform.views import view from models_test import Person person = Person(name="Peter", first_name="Smith") DBSession.add(person) DBSession.flush() request = testing.DummyRequest() request.matchdict['schema'] = 'tests_persons' request.matchdict['id'] = str(person.id) response = view(request) self.assertTrue('schema' in response) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('name="id"' in form_html) self.assertTrue('value="' + str(person.id) + '"' in form_html) self.assertTrue('Peter' in form_html) self.assertTrue('Smith' in form_html)
def test_form_submit_successful(self): from c2cgeoform.views import form from models_test import Person request = testing.DummyRequest(post=MultiDict()) request.matchdict['schema'] = 'tests_persons' request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('__start__', 'phones:sequence') request.POST.add('__start__', 'phones:mapping') request.POST.add('id', '') request.POST.add('number', '123456789') request.POST.add('personId', '') request.POST.add('__end__', 'phones:mapping') request.POST.add('__end__', 'phones:sequence') request.POST.add('__start__', 'tags:sequence') request.POST.add('tags', u'0') request.POST.add('tags', u'1') request.POST.add('__end__', 'tags:sequence') response = form(request) person = DBSession.query(Person).one() self.assertEquals('Peter', person.name) self.assertEquals('Smith', person.first_name) self.assertEquals(1, len(person.phones)) phone = person.phones[0] self.assertEquals('123456789', phone.number) self.assertIsNotNone(phone.id) self.assertEquals(2, len(person.tags)) tag_for_person1 = person.tags[0] self.assertEquals(0, tag_for_person1.tag_id) self.assertEquals(person.id, tag_for_person1.person_id) self.assertIsNotNone(tag_for_person1.id) tag_for_person2 = person.tags[1] self.assertEquals(1, tag_for_person2.tag_id) self.assertEquals(person.id, tag_for_person2.person_id) self.assertIsNotNone(tag_for_person2.id) id = person.id phone_id = phone.id self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('name="id" value="' + str(id) + '"' in form_html) self.assertTrue('name="id" value="' + str(phone_id) + '"' in form_html) self.assertTrue( 'name="person_id" value="' + str(id) + '"' in form_html) self.assertTrue('Tag A' in form_html) self.assertTrue('Tag B' in form_html) self.assertTrue('name="submit"' not in form_html)
def test_edit_show(self): from c2cgeoform.views import edit from models_test import Person person = Person(name="Peter", first_name="Smith") DBSession.add(person) DBSession.flush() request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.matchdict['id'] = str(person.id) response = edit(request) self.assertTrue('schema' in response) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('name="id"' in form_html) self.assertTrue('value="' + str(person.id) + '"' in form_html) self.assertTrue('value="Peter"' in form_html) self.assertTrue('value="Smith"' in form_html) self.assertTrue('name="submit"' in form_html)
def bus_stops(request): stops = DBSession.query(BusStop).all() return { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'id': stop.id, 'properties': { 'name': stop.name}, 'geometry': mapping(to_shape(stop.geom)) } for stop in stops] }
def bus_stops(request): stops = DBSession.query(BusStop).all() return { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'id': stop.id, 'properties': { 'name': stop.name }, 'geometry': mapping(to_shape(stop.geom)) } for stop in stops] }
def test_form_submit_only_validate(self): from c2cgeoform.views import form from models_test import Person request = testing.DummyRequest(post=MultiDict()) request.matchdict['schema'] = 'tests_persons' request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('__only_validate__', '1') form(request) count = DBSession.query(Person).count() # form was valid, but no row was created self.assertEquals(0, count)
def test_form_submit_only_validate(self): from c2cgeoform.views import form from models_test import Person request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('__only_validate__', '1') form(request) count = DBSession.query(Person).count() # form was valid, but no row was created self.assertEquals(0, count)
def test_form_submit_confirmation_back(self): from c2cgeoform.views import form from models_test import Person request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') response = form(request) # valid submission, confirmation page should be shown self.assertTrue(isinstance(response, HTTPFound)) url = TestView.BASE_URL + '/form/confirm?__submission_id__=' self.assertTrue(response.location.startswith(url)) # get submission_id matcher = re.search('__submission_id__=(.*)', response.location) submission_id = matcher.group(1) # now simulate going back to the form request2 = self._get_request() request2.session = request.session request2.matchdict['schema'] = 'tests_persons' request2.params['__submission_id__'] = submission_id response = form(request2) form_html = response['form'] # form is shown again with values restored from the session self.assertTrue( '<input type="text" name="name" value="Peter"' in form_html) # and no row was created count = DBSession.query(Person).count() self.assertEquals(0, count)
def tearDown(self): # noqa self.cleanup() DBSession.remove() testing.tearDown()
def setUp(self): # noqa curdir = os.path.dirname(os.path.abspath(__file__)) configfile = os.path.realpath(os.path.join(curdir, '../../tests.ini')) settings = get_appsettings(configfile) apply_local_settings(settings) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) from .models_test import Person, EmploymentStatus, Tag # noqa Base.metadata.create_all(engine) self.cleanup() # fill some test data into the `EmploymentStatus` and `Tags` table DBSession.add(EmploymentStatus(id=0, name='Worker')) DBSession.add(EmploymentStatus(id=1, name='Employee')) DBSession.add( EmploymentStatus(id=2, name='Self-employed and contractor')) DBSession.add(EmploymentStatus(id=3, name='Director')) DBSession.add(EmploymentStatus(id=4, name='Office holder')) DBSession.add(Tag(id=0, name='Tag A')) DBSession.add(Tag(id=1, name='Tag B')) DBSession.add(Tag(id=2, name='Tag C')) DBSession.add(Tag(id=3, name='Tag D')) DBSession.add(Tag(id=4, name='Tag E')) self.request = testing.DummyRequest(post=MultiDict(), dbsession=DBSession) testing.setUp(request=self.request) config = testing.setUp() config.add_route('form', '/{schema}/form/') config.add_route('view_user', '/{schema}/form/{hash}') config.add_route('confirm', '/{schema}/form/confirm') init_deform('c2cgeoform')
def _add_test_persons(self): self.person1 = Person(name="Smith", first_name="Peter") DBSession.add(self.person1) DBSession.add(Person(name="Wayne", first_name="John")) DBSession.add(Person(name="Elda", first_name="Hasbrouck")) DBSession.add(Person(name="Lashaun", first_name="Brasel")) DBSession.add(Person(name="Lashawna", first_name="Ashford")) DBSession.add(Person(name="Lesha", first_name="Snellgrove")) DBSession.add(Person(name="Sulema", first_name="Page")) DBSession.add(Person(name="Derek", first_name="Boroughs")) DBSession.add(Person(name="Odis", first_name="Bateman")) DBSession.add(Person(name="Venetta", first_name="Briganti")) DBSession.add(Person(name="Monte", first_name="Quill")) DBSession.add(Person(name="Daniel", first_name="Ruth")) DBSession.add(Person(name="Eloise", first_name="Hellickson")) DBSession.add(Person(name="Hee", first_name="Deloney")) DBSession.add(Person(name="Sharee", first_name="Warf")) DBSession.add(Person(name="Delpha", first_name="Philip")) DBSession.add(Person(name="Claudio", first_name="Campfield")) DBSession.add(Person(name="Janessa", first_name="Beatty")) DBSession.add(Person(name="Hollis", first_name="Richmond")) DBSession.add(Person(name="Karoline", first_name="Carew")) DBSession.add(Person(name="Bess", first_name="Papp")) DBSession.add(Person(name="Vada", first_name="Infantino")) DBSession.flush()
def setup_test_data(settings): from c2cgeoform.models import DBSession import transaction if DBSession.query(District).get(0) is None: DBSession.add(District(id=0, name="Pully")) DBSession.add(District(id=1, name="Paudex")) DBSession.add(District(id=2, name="Belmont-sur-Lausanne")) DBSession.add(District(id=3, name="Trois-Chasseurs")) DBSession.add(District(id=4, name="La Claie-aux-Moines")) DBSession.add(District(id=5, name="Savigny")) DBSession.add(District(id=6, name="Mollie-Margot")) if DBSession.query(Situation).get(0) is None: DBSession.add(Situation(id=0, name="Road", name_fr="Route")) DBSession.add(Situation(id=1, name="Sidewalk", name_fr="Trottoir")) DBSession.add(Situation(id=2, name="Berm", name_fr="Berme")) DBSession.add( Situation(id=3, name="Vegetated berm", name_fr="Berme végétalisée")) DBSession.add(Situation(id=4, name="Green zone", name_fr="Zone verte")) DBSession.add(Situation(id=5, name="Cobblestone", name_fr="Pavés")) if DBSession.query(BusStop).count() == 0: _add_bus_stops(DBSession, settings['bus_stops_file']) if DBSession.query(Address).get(0) is None: DBSession.add(Address(id=0, label="Bern")) DBSession.add(Address(id=1, label="Lausanne")) DBSession.add(Address(id=2, label="Genève")) DBSession.add(Address(id=3, label="Zurich")) DBSession.add(Address(id=4, label="Lugano")) transaction.commit()
def _add_test_persons(): from models_test import Person DBSession.add(Person(name="Smith", first_name="Peter")) DBSession.add(Person(name="Wayne", first_name="John")) DBSession.add(Person(name="Elda", first_name="Hasbrouck")) DBSession.add(Person(name="Lashaun", first_name="Brasel")) DBSession.add(Person(name="Lashawna", first_name="Ashford")) DBSession.add(Person(name="Lesha", first_name="Snellgrove")) DBSession.add(Person(name="Sulema", first_name="Page")) DBSession.add(Person(name="Derek", first_name="Boroughs")) DBSession.add(Person(name="Odis", first_name="Bateman")) DBSession.add(Person(name="Venetta", first_name="Briganti")) DBSession.add(Person(name="Monte", first_name="Quill")) DBSession.add(Person(name="Daniel", first_name="Ruth")) DBSession.add(Person(name="Eloise", first_name="Hellickson")) DBSession.add(Person(name="Hee", first_name="Deloney")) DBSession.add(Person(name="Sharee", first_name="Warf")) DBSession.add(Person(name="Delpha", first_name="Philip")) DBSession.add(Person(name="Claudio", first_name="Campfield")) DBSession.add(Person(name="Janessa", first_name="Beatty")) DBSession.add(Person(name="Hollis", first_name="Richmond")) DBSession.add(Person(name="Karoline", first_name="Carew")) DBSession.add(Person(name="Bess", first_name="Papp")) DBSession.add(Person(name="Vada", first_name="Infantino"))
def test_edit_submit_successful(self): from c2cgeoform.views import edit from models_test import Person, Phone, TagsForPerson person = Person(name="Peter", first_name="Smith") phone = Phone(number="123456789") person.phones.append(phone) tag_for_person = TagsForPerson(tag_id=0) person.tags.append(tag_for_person) DBSession.add(person) DBSession.flush() old_tag_for_person_id = tag_for_person.id request = testing.DummyRequest(post=MultiDict()) request.matchdict['schema'] = 'tests_persons' request.matchdict['id'] = str(person.id) request.POST.add('id', str(person.id)) request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('age', '43') request.POST.add('__start__', 'phones:sequence') request.POST.add('__start__', 'phones:mapping') request.POST.add('id', str(phone.id)) request.POST.add('number', '23456789') request.POST.add('person_id', str(person.id)) request.POST.add('__end__', 'phones:mapping') request.POST.add('__start__', 'phones:mapping') request.POST.add('number', '123456') request.POST.add('__end__', 'phones:mapping') request.POST.add('__end__', 'phones:sequence') request.POST.add('__start__', 'tags:sequence') request.POST.add('tags', u'0') request.POST.add('tags', u'1') request.POST.add('__end__', 'tags:sequence') response = edit(request) person = DBSession.query(Person).get(person.id) self.assertEquals('Peter', person.name) self.assertEquals('Smith', person.first_name) self.assertEquals(43, person.age) self.assertEquals(2, len(person.phones)) updated_phone = person.phones[0] self.assertEquals('23456789', updated_phone.number) self.assertEquals(phone.id, updated_phone.id) new_phone = person.phones[1] self.assertEquals('123456', new_phone.number) self.assertIsNotNone(new_phone.id) self.assertEquals(2, len(person.tags)) tag_for_person1 = person.tags[0] self.assertEquals(0, tag_for_person1.tag_id) self.assertEquals(person.id, tag_for_person1.person_id) self.assertIsNotNone(tag_for_person1.id) # a new row is created, also for the old entry self.assertNotEquals(old_tag_for_person_id, tag_for_person1.id) tag_for_person2 = person.tags[1] self.assertEquals(1, tag_for_person2.tag_id) self.assertEquals(person.id, tag_for_person2.person_id) self.assertIsNotNone(tag_for_person2.id) tags_for_persons = DBSession.query(TagsForPerson).all() # check that the old entry was deleted, so that there are only 2 self.assertEquals(2, len(tags_for_persons)) self.assertTrue('schema' in response) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('name="id"' in form_html) self.assertTrue('value="' + str(person.id) + '"' in form_html) self.assertTrue( 'name="id" value="' + str(person.id) + '"' in form_html) self.assertTrue('name="id" value="' + str(phone.id) + '"' in form_html) self.assertTrue( 'name="id" value="' + str(new_phone.id) + '"' in form_html) self.assertTrue( 'name="person_id" value="' + str(person.id) + '"' in form_html) self.assertTrue('name="submit"' in form_html) self.assertTrue('Tag A' in form_html) self.assertTrue('Tag B' in form_html)
def test_form_submit_successful(self): from c2cgeoform.views import form, confirmation from models_test import Person request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('__start__', 'phones:sequence') request.POST.add('__start__', 'phones:mapping') request.POST.add('id', '') request.POST.add('number', '123456789') request.POST.add('personId', '') request.POST.add('__end__', 'phones:mapping') request.POST.add('__end__', 'phones:sequence') request.POST.add('__start__', 'tags:sequence') request.POST.add('tags', u'0') request.POST.add('tags', u'1') request.POST.add('__end__', 'tags:sequence') response = form(request) # valid submission, confirmation page should be shown self.assertTrue(isinstance(response, HTTPFound)) url = TestView.BASE_URL + '/form/confirm?__submission_id__=' self.assertTrue(response.location.startswith(url)) # get submission_id matcher = re.search('__submission_id__=(.*)', response.location) submission_id = matcher.group(1) # simulate that the confirmation page is shown request2 = self._get_request() request2.session = request.session request2.matchdict['schema'] = 'tests_persons' request2.params['__submission_id__'] = submission_id response = confirmation(request2) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue( '<input type="hidden" name="__submission_id__"' in form_html) self.assertTrue( '<input type="hidden" name="__store_form__"' in form_html) # now simulate that the confirmation page is submitted request3 = self._get_request() request3.session = request.session request3.matchdict['schema'] = 'tests_persons' request3.params['__submission_id__'] = submission_id request3.params['__store_form__'] = '1' response = confirmation(request3) person = DBSession.query(Person).one() self.assertIsNotNone(person.hash) self.assertEquals('Peter', person.name) self.assertEquals('Smith', person.first_name) self.assertEquals(1, len(person.phones)) phone = person.phones[0] self.assertEquals('123456789', phone.number) self.assertIsNotNone(phone.id) self.assertEquals(2, len(person.tags)) tag_for_person1 = person.tags[0] self.assertEquals(0, tag_for_person1.tag_id) self.assertEquals(person.id, tag_for_person1.person_id) self.assertIsNotNone(tag_for_person1.id) tag_for_person2 = person.tags[1] self.assertEquals(1, tag_for_person2.tag_id) self.assertEquals(person.id, tag_for_person2.person_id) self.assertIsNotNone(tag_for_person2.id) self.assertTrue(isinstance(response, HTTPFound)) self.assertEquals(TestView.BASE_URL + '/form/' + person.hash, response.location)
def test_form_submit_successful(self): from c2cgeoform.views import form, confirmation from models_test import Person request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('__start__', 'phones:sequence') request.POST.add('__start__', 'phones:mapping') request.POST.add('id', '') request.POST.add('number', '123456789') request.POST.add('personId', '') request.POST.add('__end__', 'phones:mapping') request.POST.add('__end__', 'phones:sequence') request.POST.add('__start__', 'tags:sequence') request.POST.add('tags', u'0') request.POST.add('tags', u'1') request.POST.add('__end__', 'tags:sequence') response = form(request) # valid submission, confirmation page should be shown self.assertTrue(isinstance(response, HTTPFound)) url = TestView.BASE_URL + '/form/confirm?__submission_id__=' self.assertTrue(response.location.startswith(url)) # get submission_id matcher = re.search('__submission_id__=(.*)', response.location) submission_id = matcher.group(1) # simulate that the confirmation page is shown request2 = self._get_request() request2.session = request.session request2.matchdict['schema'] = 'tests_persons' request2.params['__submission_id__'] = submission_id response = confirmation(request2) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue( '<input type="hidden" name="__submission_id__"' in form_html) self.assertTrue( '<input type="hidden" name="__store_form__"' in form_html) # now simulate that the confirmation page is submitted request3 = self._get_request() request3.session = request.session request3.matchdict['schema'] = 'tests_persons' request3.params['__submission_id__'] = submission_id request3.params['__store_form__'] = '1' response = confirmation(request3) person = DBSession.query(Person).one() self.assertIsNotNone(person.hash) self.assertEquals('Peter', person.name) self.assertEquals('Smith', person.first_name) self.assertEquals(1, len(person.phones)) phone = person.phones[0] self.assertEquals('123456789', phone.number) self.assertIsNotNone(phone.id) self.assertEquals(2, len(person.tags)) tag_for_person1 = person.tags[0] self.assertEquals(0, tag_for_person1.tag_id) self.assertEquals(person.id, tag_for_person1.person_id) self.assertIsNotNone(tag_for_person1.id) tag_for_person2 = person.tags[1] self.assertEquals(1, tag_for_person2.tag_id) self.assertEquals(person.id, tag_for_person2.person_id) self.assertIsNotNone(tag_for_person2.id) self.assertTrue(isinstance(response, HTTPFound)) self.assertEquals( TestView.BASE_URL + '/form/' + person.hash, response.location)
def setup_test_data(settings): from c2cgeoform.models import DBSession import transaction if DBSession.query(District).get(0) is None: DBSession.add(District(id=0, name="Pully")) DBSession.add(District(id=1, name="Paudex")) DBSession.add(District(id=2, name="Belmont-sur-Lausanne")) DBSession.add(District(id=3, name="Trois-Chasseurs")) DBSession.add(District(id=4, name="La Claie-aux-Moines")) DBSession.add(District(id=5, name="Savigny")) DBSession.add(District(id=6, name="Mollie-Margot")) if DBSession.query(Situation).get(0) is None: DBSession.add(Situation(id=0, name="Road", name_fr="Route")) DBSession.add(Situation(id=1, name="Sidewalk", name_fr="Trottoir")) DBSession.add(Situation(id=2, name="Berm", name_fr="Berme")) DBSession.add(Situation( id=3, name="Vegetated berm", name_fr="Berme végétalisée")) DBSession.add(Situation(id=4, name="Green zone", name_fr="Zone verte")) DBSession.add(Situation(id=5, name="Cobblestone", name_fr="Pavés")) if DBSession.query(BusStop).count() == 0: _add_bus_stops(DBSession, settings['bus_stops_file']) if DBSession.query(Address).get(0) is None: DBSession.add(Address(id=0, label="Bern")) DBSession.add(Address(id=1, label="Lausanne")) DBSession.add(Address(id=2, label="Genève")) DBSession.add(Address(id=3, label="Zurich")) DBSession.add(Address(id=4, label="Lugano")) transaction.commit()
def setup_test_data(): from c2cgeoform.models import DBSession import transaction if DBSession.query(District).get(0) is None: DBSession.add(District(id=0, name="Pully")) DBSession.add(District(id=1, name="Paudex")) DBSession.add(District(id=2, name="Belmont-sur-Lausanne")) DBSession.add(District(id=3, name="Trois-Chasseurs")) DBSession.add(District(id=4, name="La Claie-aux-Moines")) DBSession.add(District(id=5, name="Savigny")) DBSession.add(District(id=6, name="Mollie-Margot")) if DBSession.query(Situation).get(0) is None: DBSession.add(Situation(id=0, name="Road", name_fr="Route")) DBSession.add(Situation(id=1, name="Sidewalk", name_fr="Trottoir")) DBSession.add(Situation(id=2, name="Berm", name_fr="Berme")) DBSession.add(Situation( id=3, name="Vegetated berm", name_fr="Berme végétalisée")) DBSession.add(Situation(id=4, name="Green zone", name_fr="Zone verte")) DBSession.add(Situation(id=5, name="Cobblestone", name_fr="Pavés")) transaction.commit()
def test_form_submit_successful_without_confirmation(self): from c2cgeoform.views import form, confirmation from c2cgeoform.schema import register_schema from models_test import Person register_schema('tests_persons_no_confirmation', Person, show_confirmation=False) request = self._get_request() request.matchdict['schema'] = 'tests_persons_no_confirmation' request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('__start__', 'phones:sequence') request.POST.add('__start__', 'phones:mapping') request.POST.add('id', '') request.POST.add('number', '123456789') request.POST.add('personId', '') request.POST.add('__end__', 'phones:mapping') request.POST.add('__end__', 'phones:sequence') request.POST.add('__start__', 'tags:sequence') request.POST.add('tags', u'0') request.POST.add('tags', u'1') request.POST.add('__end__', 'tags:sequence') response = form(request) # valid submission, redirect to confirmation view self.assertTrue(isinstance(response, HTTPFound)) url = TestView.BASE_URL + '_no_confirmation' + \ '/form/confirm?__submission_id__=' self.assertTrue(response.location.startswith(url)) # get submission_id matcher = re.search('__submission_id__=(.*)', response.location) submission_id = matcher.group(1) # simulate that the confirmation view is called, # which directly persists the object request2 = self._get_request() request2.session = request.session request2.matchdict['schema'] = 'tests_persons_no_confirmation' request2.params['__submission_id__'] = submission_id response = confirmation(request2) person = DBSession.query(Person).one() self.assertIsNotNone(person.hash) self.assertEquals('Peter', person.name) self.assertEquals('Smith', person.first_name) self.assertEquals(1, len(person.phones)) phone = person.phones[0] self.assertEquals('123456789', phone.number) self.assertIsNotNone(phone.id) self.assertEquals(2, len(person.tags)) tag_for_person1 = person.tags[0] self.assertEquals(0, tag_for_person1.tag_id) self.assertEquals(person.id, tag_for_person1.person_id) self.assertIsNotNone(tag_for_person1.id) tag_for_person2 = person.tags[1] self.assertEquals(1, tag_for_person2.tag_id) self.assertEquals(person.id, tag_for_person2.person_id) self.assertIsNotNone(tag_for_person2.id) self.assertTrue(isinstance(response, HTTPFound)) self.assertEquals( TestView.BASE_URL + '_no_confirmation' + '/form/' + person.hash, response.location)
def test_form_submit_successful_without_confirmation(self): from c2cgeoform.views import form, confirmation from c2cgeoform.schema import register_schema from models_test import Person register_schema( 'tests_persons_no_confirmation', Person, show_confirmation=False) request = self._get_request() request.matchdict['schema'] = 'tests_persons_no_confirmation' request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('__start__', 'phones:sequence') request.POST.add('__start__', 'phones:mapping') request.POST.add('id', '') request.POST.add('number', '123456789') request.POST.add('personId', '') request.POST.add('__end__', 'phones:mapping') request.POST.add('__end__', 'phones:sequence') request.POST.add('__start__', 'tags:sequence') request.POST.add('tags', u'0') request.POST.add('tags', u'1') request.POST.add('__end__', 'tags:sequence') response = form(request) # valid submission, redirect to confirmation view self.assertTrue(isinstance(response, HTTPFound)) url = TestView.BASE_URL + '_no_confirmation' + \ '/form/confirm?__submission_id__=' self.assertTrue(response.location.startswith(url)) # get submission_id matcher = re.search('__submission_id__=(.*)', response.location) submission_id = matcher.group(1) # simulate that the confirmation view is called, # which directly persists the object request2 = self._get_request() request2.session = request.session request2.matchdict['schema'] = 'tests_persons_no_confirmation' request2.params['__submission_id__'] = submission_id response = confirmation(request2) person = DBSession.query(Person).one() self.assertIsNotNone(person.hash) self.assertEquals('Peter', person.name) self.assertEquals('Smith', person.first_name) self.assertEquals(1, len(person.phones)) phone = person.phones[0] self.assertEquals('123456789', phone.number) self.assertIsNotNone(phone.id) self.assertEquals(2, len(person.tags)) tag_for_person1 = person.tags[0] self.assertEquals(0, tag_for_person1.tag_id) self.assertEquals(person.id, tag_for_person1.person_id) self.assertIsNotNone(tag_for_person1.id) tag_for_person2 = person.tags[1] self.assertEquals(1, tag_for_person2.tag_id) self.assertEquals(person.id, tag_for_person2.person_id) self.assertIsNotNone(tag_for_person2.id) self.assertTrue(isinstance(response, HTTPFound)) self.assertEquals( TestView.BASE_URL + '_no_confirmation' + '/form/' + person.hash, response.location)
def setUp(self): # noqa curdir = os.path.dirname(os.path.abspath(__file__)) configfile = os.path.realpath( os.path.join(curdir, '../../tests.ini')) settings = get_appsettings(configfile) apply_local_settings(settings) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) from .models_test import Person, EmploymentStatus, Tag # noqa Base.metadata.create_all(engine) self.cleanup() # fill some test data into the `EmploymentStatus` and `Tags` table DBSession.add(EmploymentStatus(id=0, name='Worker')) DBSession.add(EmploymentStatus(id=1, name='Employee')) DBSession.add(EmploymentStatus( id=2, name='Self-employed and contractor')) DBSession.add(EmploymentStatus(id=3, name='Director')) DBSession.add(EmploymentStatus(id=4, name='Office holder')) DBSession.add(Tag(id=0, name='Tag A')) DBSession.add(Tag(id=1, name='Tag B')) DBSession.add(Tag(id=2, name='Tag C')) DBSession.add(Tag(id=3, name='Tag D')) DBSession.add(Tag(id=4, name='Tag E')) self.request = testing.DummyRequest(post=MultiDict(), dbsession=DBSession) testing.setUp(request=self.request) config = testing.setUp() config.add_route('form', '/{schema}/form/') config.add_route('view_user', '/{schema}/form/{hash}') config.add_route('confirm', '/{schema}/form/confirm') init_deform('c2cgeoform')
def setUp(self): # noqa curdir = os.path.dirname(os.path.abspath(__file__)) configfile = os.path.realpath( os.path.join(curdir, '../../development.ini')) settings = get_appsettings(configfile) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) from models_test import Person, EmploymentStatus, Tag # noqa Base.metadata.create_all(engine) self.cleanup() # fill some test data into the `EmploymentStatus` and `Tags` table DBSession.add(EmploymentStatus(id=0, name='Worker')) DBSession.add(EmploymentStatus(id=1, name='Employee')) DBSession.add(EmploymentStatus( id=2, name='Self-employed and contractor')) DBSession.add(EmploymentStatus(id=3, name='Director')) DBSession.add(EmploymentStatus(id=4, name='Office holder')) DBSession.add(Tag(id=0, name='Tag A')) DBSession.add(Tag(id=1, name='Tag B')) DBSession.add(Tag(id=2, name='Tag C')) DBSession.add(Tag(id=3, name='Tag D')) DBSession.add(Tag(id=4, name='Tag E'))
def test_edit_submit_successful(self): from c2cgeoform.views import edit from models_test import Person, Phone, TagsForPerson person = Person(name="Peter", first_name="Smith") phone = Phone(number="123456789") person.phones.append(phone) tag_for_person = TagsForPerson(tag_id=0) person.tags.append(tag_for_person) DBSession.add(person) DBSession.flush() old_tag_for_person_id = tag_for_person.id request = self._get_request() request.matchdict['schema'] = 'tests_persons' request.matchdict['id'] = str(person.id) request.POST.add('id', str(person.id)) request.POST.add('submit', 'submit') request.POST.add('name', 'Peter') request.POST.add('first_name', 'Smith') request.POST.add('age', '43') request.POST.add('__start__', 'phones:sequence') request.POST.add('__start__', 'phones:mapping') request.POST.add('id', str(phone.id)) request.POST.add('number', '23456789') request.POST.add('person_id', str(person.id)) request.POST.add('__end__', 'phones:mapping') request.POST.add('__start__', 'phones:mapping') request.POST.add('number', '123456') request.POST.add('__end__', 'phones:mapping') request.POST.add('__end__', 'phones:sequence') request.POST.add('__start__', 'tags:sequence') request.POST.add('tags', u'0') request.POST.add('tags', u'1') request.POST.add('__end__', 'tags:sequence') response = edit(request) person = DBSession.query(Person).get(person.id) self.assertEquals('Peter', person.name) self.assertEquals('Smith', person.first_name) self.assertEquals(43, person.age) self.assertEquals(2, len(person.phones)) updated_phone = person.phones[0] self.assertEquals('23456789', updated_phone.number) self.assertEquals(phone.id, updated_phone.id) new_phone = person.phones[1] self.assertEquals('123456', new_phone.number) self.assertIsNotNone(new_phone.id) self.assertEquals(2, len(person.tags)) tag_for_person1 = person.tags[0] self.assertEquals(0, tag_for_person1.tag_id) self.assertEquals(person.id, tag_for_person1.person_id) self.assertIsNotNone(tag_for_person1.id) # a new row is created, also for the old entry self.assertNotEquals(old_tag_for_person_id, tag_for_person1.id) tag_for_person2 = person.tags[1] self.assertEquals(1, tag_for_person2.tag_id) self.assertEquals(person.id, tag_for_person2.person_id) self.assertIsNotNone(tag_for_person2.id) tags_for_persons = DBSession.query(TagsForPerson).all() # check that the old entry was deleted, so that there are only 2 self.assertEquals(2, len(tags_for_persons)) self.assertTrue('schema' in response) self.assertTrue('form' in response) form_html = response['form'] self.assertTrue('name="id"' in form_html) self.assertTrue('value="' + str(person.id) + '"' in form_html) self.assertTrue('name="id" value="' + str(person.id) + '"' in form_html) self.assertTrue('name="id" value="' + str(phone.id) + '"' in form_html) self.assertTrue('name="id" value="' + str(new_phone.id) + '"' in form_html) self.assertTrue('name="person_id" value="' + str(person.id) + '"' in form_html) self.assertTrue('name="submit"' in form_html) self.assertTrue('Tag A' in form_html) self.assertTrue('Tag B' in form_html)
def addresses(request): if 'term' not in request.params: return HTTPBadRequest() term = '%%%s%%' % request.params['term'] query = DBSession.query(Address).filter(Address.label.ilike(term)) return [{'id': addr.id, 'label': addr.label} for addr in query]