def test_person(self): ''' Faz as operações básicas de CRUD com o model Person. ''' #não deve ser possível criar um objeto em branco try: p = Person() DBSession.add(p) DBSession.flush() except sql_exc.IntegrityError: DBSession.rollback() self.assertEqual(p.facebookId, None) #cria um objeto completo e testa seus atributos e métodos p = Person(facebookId='131234873', username='******', name=u'Jurema Carijó', gender='female') DBSession.add(p) DBSession.flush() self.assertNotEqual(p, None) self.assertEqual(p.facebookId, '131234873') self.assertEqual(p.username, 'jurema.carijo') self.assertEqual(p.name, u'Jurema Carijó') self.assertEqual(p.gender, 'female') pes = p.to_dict() self.assertEqual(p.facebookId, pes['facebookId']) self.assertEqual(p.username, pes['username']) self.assertEqual(p.name, pes['name']) self.assertEqual(p.gender, pes['gender']) rep = u"<Person('%s', '%s')>" % (p.facebookId, p.name) self.assertEqual(unicode(p), rep)
def test_addPersonToShortToLongPassword(self): passwordToShortLen = Person.const()['passwordMinLen'] - 1 passwordToLongLen = Person.const()['passwordMaxLen'] + 1 shortpasswordPost = { 'name': 'John', 'password': '******' * passwordToShortLen } longpasswordPost = { 'name': 'John', 'password': '******' * passwordToLongLen } resp = self.app.post('/api/people', data=json.dumps(shortpasswordPost), content_type='application/json') self.assertEqual(resp.status_code, 400) self.assertTrue("Password length" in resp.data) resp = self.app.post('/api/people', data=json.dumps(longpasswordPost), content_type='application/json') self.assertEqual(resp.status_code, 400) self.assertTrue("Password length" in resp.data)
def test_addPersonToShortToLongSurname(self): surnameToShortLen = Person.const()['surnameMinLen'] - 1 surnameToLongLen = Person.const()['surnameMaxLen'] + 1 shortsurNamePost = { 'name': 'John', 'surname': 'a' * surnameToShortLen, 'password': '******' } longsurNamePost = { 'name': 'John', 'surname': 'a' * surnameToLongLen, 'password': '******' } resp = self.app.post('/api/people', data=json.dumps(shortsurNamePost), content_type='application/json') self.assertEqual(resp.status_code, 400) self.assertTrue("Surname length" in resp.data) resp = self.app.post('/api/people', data=json.dumps(longsurNamePost), content_type='application/json') self.assertEqual(resp.status_code, 400) self.assertTrue("Surname length" in resp.data)
def test_addPersonToShortToLongAcademicDegree(self): academicDegreeMinLen = Person.const()['academicDegreeMinLen'] academicDegreeMaxLen = Person.const()['academicDegreeMaxLen'] academicDegreeToShortLen = academicDegreeMinLen - 1 academicDegreeToLongLen = academicDegreeMaxLen + 1 longacademicDegreePost = { 'name': 'John', 'academicDegree': 'a' * academicDegreeToLongLen, 'password': '******' } minPost = { 'name': 'John', 'academicDegree': 'a' * academicDegreeMinLen, 'password': '******' } maxPost = { 'name': 'John', 'academicDegree': 'a' * academicDegreeMaxLen, 'password': '******' } resp = self.app.post('/api/people', data=json.dumps(longacademicDegreePost), content_type='application/json') self.assertEqual(resp.status_code, 400) self.assertTrue("AcademicDegree length" in resp.data) resp = self.app.post('/api/people', data=json.dumps(minPost), content_type='application/json') self.assertEqual(resp.status_code, 201) resp = self.app.post('/api/people', data=json.dumps(maxPost), content_type='application/json') self.assertEqual(resp.status_code, 201)
def test_addPersonToShortToLongSex(self): sexMinLen = Person.const()['sexMinLen'] sexMaxLen = Person.const()['sexMaxLen'] sexToLongLen = sexMaxLen + 1 longsexPost = { 'name': 'John', 'sex': 'a' * sexToLongLen, 'password': '******' } minPost = { 'name': 'John', 'sex': 'a' * sexMinLen, 'password': '******' } maxPost = { 'name': 'John', 'sex': 'a' * sexMaxLen, 'password': '******' } resp = self.app.post('/api/people', data=json.dumps(longsexPost), content_type='application/json') self.assertEqual(resp.status_code, 400) self.assertTrue("Sex length" in resp.data) resp = self.app.post('/api/people', data=json.dumps(minPost), content_type='application/json') self.assertEqual(resp.status_code, 201) resp = self.app.post('/api/people', data=json.dumps(maxPost), content_type='application/json') self.assertEqual(resp.status_code, 201)
def test_addPersonNotValidToSmallToBigAge(self): ageMin = Person.const()['ageMin'] ageMax = Person.const()['ageMax'] toYoung = ageMin - 1 toOld = ageMax + 1 youngPost = {'name': 'John', 'age': toYoung, 'password': '******'} oldPost = {'name': 'John', 'age': toOld, 'password': '******'} minPost = {'name': 'John', 'age': ageMin, 'password': '******'} maxPost = {'name': 'John', 'age': ageMax, 'password': '******'} resp = self.app.post('/api/people', data=json.dumps(youngPost), content_type='application/json') self.assertEqual(resp.status_code, 400) self.assertTrue("Age must be between" in resp.data) resp = self.app.post('/api/people', data=json.dumps(oldPost), content_type='application/json') self.assertEqual(resp.status_code, 400) self.assertTrue("Age must be between" in resp.data) resp = self.app.post('/api/people', data=json.dumps(minPost), content_type='application/json') self.assertEqual(resp.status_code, 201) resp = self.app.post('/api/people', data=json.dumps(maxPost), content_type='application/json') self.assertEqual(resp.status_code, 201)