Exemple #1
0
 def adjust_channel_value(self, channel, key, value):
     """Sets the value for a given key to be associated with the channel."""
     channel = Identifier(channel).lower()
     value = json.dumps(value, ensure_ascii=False)
     session = self.ssession()
     try:
         result = session.query(ChannelValues) \
             .filter(ChannelValues.channel == channel)\
             .filter(ChannelValues.key == key) \
             .one_or_none()
         # ChannelValue exists, update
         if result:
             result.value = float(result.value) + float(value)
             session.commit()
         # DNE - Insert
         else:
             new_channelvalue = ChannelValues(channel=channel,
                                              key=key,
                                              value=float(value))
             session.add(new_channelvalue)
             session.commit()
     except SQLAlchemyError:
         session.rollback()
         raise
     finally:
         session.close()
Exemple #2
0
def test_get_channel_value(db):
    session = db.ssession()

    cv = ChannelValues(channel='#asdf', key='qwer', value='\"zxcv\"')
    session.add(cv)
    session.commit()

    result = db.get_channel_value('#asdf', 'qwer')
    assert result == 'zxcv'
    session.close()
Exemple #3
0
def test_get_channel_value(db: SopelDB):
    with db.session() as session:
        channel_value = ChannelValues(
            channel='#channel',
            key='testkey',
            value='\"value\"',
        )
        session.add(channel_value)
        session.commit()

    result = db.get_channel_value('#channel', 'testkey')
    assert result == 'value'
Exemple #4
0
 def adjust_channel_list(self, channel, key, entries, adjustmentdirection):
     """Sets the value for a given key to be associated with the channel."""
     channel = Identifier(channel).lower()
     if not isinstance(entries, list):
         entries = [entries]
     entries = json.dumps(entries, ensure_ascii=False)
     session = self.ssession()
     try:
         result = session.query(ChannelValues) \
             .filter(ChannelValues.channel == channel)\
             .filter(ChannelValues.key == key) \
             .one_or_none()
         # ChannelValue exists, update
         if result:
             if adjustmentdirection == 'add':
                 for entry in entries:
                     if entry not in result.value:
                         result.value.append(entry)
             elif adjustmentdirection == 'del':
                 for entry in entries:
                     while entry in result.value:
                         result.value.remove(entry)
             session.commit()
         # DNE - Insert
         else:
             values = []
             if adjustmentdirection == 'add':
                 for entry in entries:
                     if entry not in values:
                         values.append(entry)
             elif adjustmentdirection == 'del':
                 for entry in entries:
                     while entry in values:
                         values.remove(entry)
             new_channelvalue = ChannelValues(channel=channel,
                                              key=key,
                                              value=values)
             session.add(new_channelvalue)
             session.commit()
     except SQLAlchemyError:
         session.rollback()
         raise
     finally:
         session.close()
Exemple #5
0
def test_get_channel_slug_with_migration(db: SopelDB):
    channel = db.make_identifier('#[channel]')
    db.set_channel_value(channel, 'testkey', 'cval')
    assert db.get_channel_slug(channel) == channel.lower()
    assert db.get_channel_value(channel, 'testkey') == 'cval'

    # insert a value with the wrong casemapping
    old_channel = Identifier._lower_swapped('#[channel]')
    assert old_channel == '#[channel]'
    assert channel.lower() == '#{channel}'

    with db.session() as session:
        channel_value = ChannelValues(
            channel=old_channel,
            key='oldkey',
            value='"value"'  # result from json.dumps
        )
        session.add(channel_value)
        session.commit()

    assert db.get_channel_slug(old_channel) == channel.lower(), (
        'Channel with previous casemapping must return the new version.')
    assert db.get_channel_value(old_channel, 'oldkey') == 'value', (
        'Key associated to an old version must be migrated to the new one')