From 151a9bf17df5807038d555e938d53b909006c18c Mon Sep 17 00:00:00 2001 From: Florian Strzelecki Date: Mon, 17 Jan 2022 17:58:13 +0100 Subject: [PATCH] test: improve get_nick_id tests --- test/test_db.py | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/test/test_db.py b/test/test_db.py index 181ca9102c..1fe7e9910a 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -11,7 +11,14 @@ import pytest -from sopel.db import ChannelValues, Nicknames, NickValues, PluginValues, SopelDB +from sopel.db import ( + ChannelValues, + NickIDs, + Nicknames, + NickValues, + PluginValues, + SopelDB, +) from sopel.tools import Identifier @@ -38,6 +45,28 @@ def teardown_function(function): os.remove(db_filename) +def test_get_nick_id(db): + """Test get_nick_id does not create NickID by default.""" + nick = Identifier('Exirel') + session = db.session() + + # Attempt to get nick ID: it is not created by default + with pytest.raises(ValueError): + db.get_nick_id(nick) + + # Create the nick ID + nick_id = db.get_nick_id(nick, create=True) + + # Check that one and only one nickname exists with that ID + nickname = session.query(Nicknames).filter( + Nicknames.nick_id == nick_id, + ).one() # will raise if not one and exactly one + assert nickname.canonical == 'Exirel' + assert nickname.slug == nick.lower() + + session.close() + + @pytest.mark.parametrize('name, slug, variant', ( # Check case insensitive with ASCII only ('Embolalia', 'embolalia', 'eMBOLALIA'), @@ -46,10 +75,12 @@ def teardown_function(function): # Unicode, just in case ('EmbölaliÅ', 'embölaliÅ', 'EMBöLALIÅ'), )) -def test_get_nick_id(db, name, slug, variant): - session = db.ssession() +def test_get_nick_id_casemapping(db, name, slug, variant): + """Test get_nick_id is case-insensitive through an Identifier.""" + session = db.session() nick = Identifier(name) - # Create the NickID + + # Create the nick ID nick_id = db.get_nick_id(nick, create=True) registered = session.query(Nicknames) \ @@ -65,6 +96,16 @@ def test_get_nick_id(db, name, slug, variant): # Even if the case is different assert nick_id == db.get_nick_id(variant) + # And no other nick IDs are created (even with create=True) + assert nick_id == db.get_nick_id(name, create=True) + assert nick_id == db.get_nick_id(variant, create=True) + assert 1 == session.query(NickIDs).count() + + # But a truly different name means a new nick ID + new_nick_id = db.get_nick_id(name + '_test', create=True) + assert new_nick_id != nick_id + assert 2 == session.query(NickIDs).count() + session.close()