Example #1
0
 def test_equality(self):
     cid = CaseInsensitiveDict({'SPAM': 'blueval', 'Eggs': 'redval'})
     othercid = CaseInsensitiveDict({'spam': 'blueval', 'eggs': 'redval'})
     self.assertEqual(cid, othercid)
     del othercid['spam']
     self.assertNotEqual(cid, othercid)
     self.assertEqual(cid, {'spam': 'blueval', 'eggs': 'redval'})
Example #2
0
 def test_get(self):
     cid = CaseInsensitiveDict()
     cid['spam'] = 'oneval'
     cid['SPAM'] = 'blueval'
     self.assertEqual(cid.get('spam'), 'blueval')
     self.assertEqual(cid.get('SPAM'), 'blueval')
     self.assertEqual(cid.get('sPam'), 'blueval')
     self.assertEqual(cid.get('notspam', 'default'), 'default')
Example #3
0
 def test_get(self):
     cid = CaseInsensitiveDict()
     cid['spam'] = 'oneval'
     cid['SPAM'] = 'blueval'
     self.assertEqual(cid.get('spam'), 'blueval')
     self.assertEqual(cid.get('SPAM'), 'blueval')
     self.assertEqual(cid.get('sPam'), 'blueval')
     self.assertEqual(cid.get('notspam', 'default'), 'default')
Example #4
0
 def test_make_key_lowercase(self):
     cid = CaseInsensitiveDict({
         'Accept': 'application/json',
         'user-Agent': 'requests',
     })
     keyset = frozenset(['accept', 'user-agent'])
     self.assertEqual(frozenset(i[0] for i in cid.items()), keyset)
     self.assertEqual(frozenset(cid.keys()), keyset)
     self.assertEqual(frozenset(cid), keyset)
Example #5
0
 def test_make_key_lowercase(self):
     cid = CaseInsensitiveDict({
         'Accept': 'application/json',
         'user-Agent': 'requests',
     })
     keyset = frozenset(['accept', 'user-agent'])
     self.assertEqual(frozenset(i[0] for i in cid.items()), keyset)
     self.assertEqual(frozenset(cid.keys()), keyset)
     self.assertEqual(frozenset(cid), keyset)
Example #6
0
 def test_setitem(self):
     """__setitem__ should behave case-insensitively."""
     cid = CaseInsensitiveDict()
     cid['spam'] = 'oneval'
     cid['Spam'] = 'twoval'
     cid['sPAM'] = 'redval'
     cid['SPAM'] = 'blueval'
     self.assertEqual(cid['spam'], 'blueval')
     self.assertEqual(cid['SPAM'], 'blueval')
     self.assertEqual(list(cid.keys()), ['spam'])
Example #7
0
 def test_setdefault(self):
     cid = CaseInsensitiveDict({'Spam': 'blueval'})
     self.assertEqual(
         cid.setdefault('spam', 'notblueval'),
         'blueval'
     )
     self.assertEqual(
         cid.setdefault('notspam', 'notblueval'),
         'notblueval'
     )
Example #8
0
 def test_setitem(self):
     """__setitem__ should behave case-insensitively."""
     cid = CaseInsensitiveDict()
     cid['spam'] = 'oneval'
     cid['Spam'] = 'twoval'
     cid['sPAM'] = 'redval'
     cid['SPAM'] = 'blueval'
     self.assertEqual(cid['spam'], 'blueval')
     self.assertEqual(cid['SPAM'], 'blueval')
     self.assertEqual(list(cid.keys()), ['spam'])
Example #9
0
 def test_preserve_last_key_case(self):
     cid = CaseInsensitiveDict({
         'Accept': 'application/json',
         'user-Agent': 'requests',
     })
     cid.update({'ACCEPT': 'application/json'})
     cid['USER-AGENT'] = 'requests'
     keyset = frozenset(['accept', 'user-agent'])
     self.assertEqual(frozenset(i[0] for i in cid.items()), keyset)
     self.assertEqual(frozenset(cid.keys()), keyset)
     self.assertEqual(frozenset(cid), keyset)
Example #10
0
 def test_copy(self):
     cid = CaseInsensitiveDict({'SPAM': 'blueval', 'Eggs': 'redval'})
     othercid = cid.copy()
     d = {'spam': 'blueval', 'eggs': 'redval'}
     self.assertEqual(cid, othercid)
     self.assertEqual(cid, d)
     self.assertEqual(othercid, d)
     del othercid['spam']
     self.assertNotEqual(cid, othercid)
     self.assertEqual(cid, d)
     self.assertNotEqual(othercid, d)
Example #11
0
 def test_copy(self):
     cid = CaseInsensitiveDict({'SPAM': 'blueval', 'Eggs': 'redval'})
     othercid = cid.copy()
     d = {'spam': 'blueval', 'eggs': 'redval'}
     self.assertEqual(cid, othercid)
     self.assertEqual(cid, d)
     self.assertEqual(othercid, d)
     del othercid['spam']
     self.assertNotEqual(cid, othercid)
     self.assertEqual(cid, d)
     self.assertNotEqual(othercid, d)
Example #12
0
 def test_contains(self):
     cid = CaseInsensitiveDict()
     cid['Spam'] = 'someval'
     self.assertTrue('Spam' in cid)
     self.assertTrue('spam' in cid)
     self.assertTrue('SPAM' in cid)
     self.assertTrue('sPam' in cid)
     self.assertFalse('notspam' in cid)
Example #13
0
 def test_update(self):
     cid = CaseInsensitiveDict()
     cid['spam'] = 'blueval'
     cid.update({'sPam': 'notblueval'})
     self.assertEqual(cid['spam'], 'notblueval')
     cid = CaseInsensitiveDict({'Foo': 'foo', 'BAr': 'bar'})
     cid.update({'fOO': 'anotherfoo', 'bAR': 'anotherbar'})
     self.assertEqual(len(cid), 2)
     self.assertEqual(cid['foo'], 'anotherfoo')
     self.assertEqual(cid['bar'], 'anotherbar')
Example #14
0
    def generate_comments(self, comments):
        comments = CaseInsensitiveDict(comments)

        # Packet type + vorbis magic
        packet = array('B', self.signature)

        packet.extend(utils.int_struct.pack(len(self.vendor_name)))
        packet.extend(self.vendor_name)

        packet.extend(utils.int_struct.pack(len(comments)))

        for k, v in comments.items():
            comment = '{}={}'.format(k, v).encode()
            packet.extend(utils.int_struct.pack(len(comment)))
            packet.extend(comment)

        if self.framing_bit:
            packet.append(1)

        return io.BytesIO(packet)
Example #15
0
    def generate_comments(self, comments):
        comments = CaseInsensitiveDict(comments)

        # Packet type + vorbis magic
        packet = array('B', self.signature)

        packet.extend(utils.int_struct.pack(len(self.vendor_name)))
        packet.extend(self.vendor_name)

        packet.extend(utils.int_struct.pack(len(comments)))

        for k, v in comments.items():
            comment = '{}={}'.format(k, v).encode()
            packet.extend(utils.int_struct.pack(len(comment)))
            packet.extend(comment)

        if self.framing_bit:
            packet.append(1)

        return io.BytesIO(packet)
Example #16
0
    def process_comments(self, packet):
        """Reads the comments.

        :param packet: Object to read from, has a read method.
        :type packet: ``pytag.containers.PacketReader``
        :returns: A ``dict``-like object with all the comments.
        :rtype: ``pytag.structures.CaseInsensitiveDict``
        """

        # Signature is not used
        packet.read(self.signature_struct.size)

        (vendor_lenght, ) = utils.int_struct.unpack(packet.read(4))
        packet.read(vendor_lenght)

        (user_comment_list_length, ) = utils.int_struct.unpack(packet.read(4))

        comments = CaseInsensitiveDict()
        for i in range(user_comment_list_length):
            (length, ) = utils.int_struct.unpack(packet.read(4))
            comment = packet.read(length).decode('utf-8')
            comments.update((comment.split('='), ))

        return comments
Example #17
0
    def process_comments(self, packet):
        """Reads the comments.

        :param packet: Object to read from, has a read method.
        :type packet: ``pytag.containers.PacketReader``
        :returns: A ``dict``-like object with all the comments.
        :rtype: ``pytag.structures.CaseInsensitiveDict``
        """

        # Signature is not used
        packet.read(self.signature_struct.size)

        (vendor_lenght,) = utils.int_struct.unpack(packet.read(4))
        packet.read(vendor_lenght)

        (user_comment_list_length,) = utils.int_struct.unpack(packet.read(4))

        comments = CaseInsensitiveDict()
        for i in range(user_comment_list_length):
            (length,) = utils.int_struct.unpack(packet.read(4))
            comment = packet.read(length).decode('utf-8')
            comments.update((comment.split('='),))

        return comments
Example #18
0
 def test_update(self):
     cid = CaseInsensitiveDict()
     cid['spam'] = 'blueval'
     cid.update({'sPam': 'notblueval'})
     self.assertEqual(cid['spam'], 'notblueval')
     cid = CaseInsensitiveDict({'Foo': 'foo', 'BAr': 'bar'})
     cid.update({'fOO': 'anotherfoo', 'bAR': 'anotherbar'})
     self.assertEqual(len(cid), 2)
     self.assertEqual(cid['foo'], 'anotherfoo')
     self.assertEqual(cid['bar'], 'anotherbar')
Example #19
0
 def test_preserve_last_key_case(self):
     cid = CaseInsensitiveDict({
         'Accept': 'application/json',
         'user-Agent': 'requests',
     })
     cid.update({'ACCEPT': 'application/json'})
     cid['USER-AGENT'] = 'requests'
     keyset = frozenset(['accept', 'user-agent'])
     self.assertEqual(frozenset(i[0] for i in cid.items()), keyset)
     self.assertEqual(frozenset(cid.keys()), keyset)
     self.assertEqual(frozenset(cid), keyset)
Example #20
0
 def test_update_retains_unchanged(self):
     cid = CaseInsensitiveDict({'foo': 'foo', 'bar': 'bar'})
     cid.update({'foo': 'newfoo'})
     self.assertEquals(cid['bar'], 'bar')
Example #21
0
 def test_iterable_init(self):
     cid = CaseInsensitiveDict([('Foo', 'foo'), ('BAr', 'bar')])
     self.assertEqual(len(cid), 2)
     self.assertTrue('foo' in cid)
     self.assertTrue('bar' in cid)
Example #22
0
 def test_kwargs_init(self):
     cid = CaseInsensitiveDict(FOO='foo', BAr='bar')
     self.assertEqual(len(cid), 2)
     self.assertTrue('foo' in cid)
     self.assertTrue('bar' in cid)
Example #23
0
 def test_docstring_example(self):
     cid = CaseInsensitiveDict()
     cid['Key'] = 'value'
     self.assertEqual(cid['KEY'], 'value')
     self.assertEqual(cid['KEy'], 'value')
     self.assertEqual(list(cid), ['key'])
Example #24
0
 def test_len(self):
     cid = CaseInsensitiveDict({'a': 'a', 'b': 'b'})
     cid['A'] = 'a'
     self.assertEqual(len(cid), 2)
Example #25
0
 def test_getitem(self):
     cid = CaseInsensitiveDict({'Spam': 'blueval'})
     self.assertEqual(cid['spam'], 'blueval')
     self.assertEqual(cid['SPAM'], 'blueval')
Example #26
0
 def test_delitem(self):
     cid = CaseInsensitiveDict()
     cid['Spam'] = 'someval'
     del cid['sPam']
     self.assertFalse('spam' in cid)
     self.assertEqual(len(cid), 0)
Example #27
0
 def test_iter(self):
     cid = CaseInsensitiveDict({'Spam': 'spam', 'Eggs': 'eggs'})
     keys = frozenset(['spam', 'eggs'])
     self.assertEqual(frozenset(iter(cid)), keys)
Example #28
0
 def test_mapping_init(self):
     cid = CaseInsensitiveDict({'Foo': 'foo', 'BAr': 'bar'})
     self.assertEqual(len(cid), 2)
     self.assertTrue('foo' in cid)
     self.assertTrue('bar' in cid)
Example #29
0
 def test_update_retains_unchanged(self):
     cid = CaseInsensitiveDict({'foo': 'foo', 'bar': 'bar'})
     cid.update({'foo': 'newfoo'})
     self.assertEquals(cid['bar'], 'bar')
Example #30
0
 def test_setdefault(self):
     cid = CaseInsensitiveDict({'Spam': 'blueval'})
     self.assertEqual(cid.setdefault('spam', 'notblueval'), 'blueval')
     self.assertEqual(cid.setdefault('notspam', 'notblueval'), 'notblueval')