NubmcNub
NubmcNub

Reputation: 105

Python sqlite3 assertion error

I am pretty sure the code I am doing is logical and makes sense but when I run it against my test the result comes back quite unusual, I am trying find a session from a cookie, and then use it to retrieve the user from the sessions table. I am using the bottle framework to test my program

    active_session = bottle.request.get_cookie(COOKIE_NAME)
cursor = db.cursor()

if active_session:
    cursor.execute("SELECT usernick FROM sessions WHERE sessionid=?", (active_session, ))
    active_user = cursor.fetchone()
    return active_user
else:
    return None

The result is as follows

    self.assertEqual(nick_from_cookie, nick)
AssertionError: ('Bobalooba',) != 'Bobalooba'\

I know I am so close could someone point me in the right direction

Upvotes: 0

Views: 631

Answers (2)

Joran Beasley
Joran Beasley

Reputation: 113988

if active_session:
    cursor.execute("SELECT usernick FROM sessions WHERE sessionid=?", (active_session, ))
    active_user = cursor.fetchone()
    return active_user[0] if active_user else active_user

Upvotes: 1

errata
errata

Reputation: 26842

It's because you are fetching the entire row and comparing it to a single string. You need to grab the single field contained in the retrieved row. Try this as the input to your assertion.

return active_user[0]

Upvotes: 0

Related Questions