def test_postgres_execute_unique_violation(self, execute): execute.side_effect = errors.UniqueViolation() response = self.fetch('/pdexecute?value=1') self.assertEqual(response.code, 409) problem = json.loads(response.body) self.assertEqual(problem['title'], 'Unique Violation')
def test_postgres_execute_unique_violation(self, execute): execute.side_effect = errors.UniqueViolation() response = self.fetch('/execute?value=1') self.assertEqual(response.code, 409) self.assertIn(b'Unique Violation', response.body)
def createEvent(self, ecreator, roomid, etitle, edescription, estart, eend, tags, photourl, websites): """ Create an Event from the information provided. Uses: * :func:`~app.DAOs.PhotoDAO.PhotoDAO.insertPhoto` * :func:`~app.DAOs.TagDAO.TagDAO.tagEvent` * :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.addWebsite` * :func:`~app.DAOs.WebsiteDAO.WebsiteDAO.addWebsitesToEvent` :param ecreator: the event creator's UID. :type ecreator: int :param roomid: the rid of the room in which the event will take place. :type roomid: int :param etitle: the event's title. :type etitle: str :param edescription: the event's description. :type edescription: str :param estart: the event's start timestamp. :type estart: str :param eend: the event's end timestamp, which must be greater than the start timestamp. :type eend: str :param tags: a list of integers corresponding to tag IDs :type tags: list of int :param photourl: url of a photo to be related to the event. Can be empty. :type photourl: str :param websites: a list of dictionaries containing website urls and wdescriptions. can be empty. :type websites: list of dicts :return Tuple: SQL result of Query as a tuple. """ cursor = self.conn.cursor() # Insert photo into table if it does not exist, then get the photoid. photoid = PhotoDAO().insertPhoto(photourl=photourl, uid=ecreator, cursor=cursor)[0] # Build the query to create an event entry. query = sql.SQL( "insert into {table1} ({insert_fields})" "values (%s, %s, %s, %s, %s, %s, CURRENT_TIMESTAMP, %s, %s, %s) " "returning {pkey1}").format(table1=sql.Identifier('events'), insert_fields=sql.SQL(',').join([ sql.Identifier('ecreator'), sql.Identifier('roomid'), sql.Identifier('etitle'), sql.Identifier('edescription'), sql.Identifier('estart'), sql.Identifier('eend'), sql.Identifier('ecreation'), sql.Identifier('estatus'), sql.Identifier('estatusdate'), sql.Identifier('photoid') ]), pkey1=sql.Identifier('eid')) # Try to insert the event into the database, catch if any event is duplicated. try: cursor.execute( query, (int(ecreator), int(roomid), str(etitle), str(edescription), str(estart), str(eend), 'active', None, photoid)) result = cursor.fetchone() eid = result[0] except errors.UniqueViolation as unique_error: self.conn.close() return errors.UniqueViolation( "An event with the same name, in the same room, " "that starts at the same time, already exists in the system.") # Once the event is created, tag it with the list of tags provided. Catch any bad tags. try: for tag in tags: TagDAO().tagEvent(eid=eid, tid=tag, cursor=cursor) except errors.ForeignKeyViolation as fk_error: self.conn.close() return fk_error # Once tagged, insert the websites, if any, that do not already exist, and relate them to the event. if websites is not None: try: for website in websites: wid = WebsiteDAO().addWebsite(url=website['url'], cursor=cursor, uid=ecreator) WebsiteDAO().addWebsitesToEvent( eid=eid, wid=wid[0], wdescription=website['wdescription'], cursor=cursor, uid=ecreator) # Do not know if this is the right error to expect. except TypeError as e: self.conn.close() return e except ValueError as e: self.conn.close() return e # Commit changes if no errors occur. self.conn.commit() self.conn.close() return result