mardi 5 mai 2015

Comparing tuples to strings

I am trying to retrieve a user from my sessions table in the database. In my unit test i get an error:

line 138, in test_session_user
    self.assertEqual(nick_from_cookie, nick)
AssertionError: ('Bobalooba',) != 'Bobalooba'

I understand that this is because my code is returning a tuple when the test is comparing my return value against a string therefore it is not matching.

Is there a way for me to do this comparison using the tuple or is their a better way to query the result i need. Below is the table im using aswell as my function:

def session_user(db):
    """try to
    retrieve the user from the sessions table
    return usernick or None if no valid session is present
    """
    cursor = db.cursor()
    sessionid = bottle.request.get_cookie(COOKIE_NAME)
    usernick = None
    if sessionid:
        cursor.execute("SELECT usernick FROM sessions WHERE sessionid=?", (sessionid,))
        usernick = cursor.fetchone()
    return usernick

Table:

CREATE TABLE sessions (
            sessionid text unique primary key,
            usernick text,
            FOREIGN KEY(usernick) REFERENCES users(nick)
);

Aucun commentaire:

Enregistrer un commentaire