| 1 |
# Copyright (c) 2014, Menno Smits |
| 2 |
# Released subject to the New BSD License |
| 3 |
# Please see http://en.wikipedia.org/wiki/BSD_licenses |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
from imapclient.exceptions import InvalidCriteriaError, ProtocolError |
| 8 |
from imapclient.imapclient import ( |
| 9 |
_normalise_search_criteria, |
| 10 |
_quoted, |
| 11 |
join_message_ids, |
| 12 |
normalise_text_list, |
| 13 |
seq_to_parenstr, |
| 14 |
seq_to_parenstr_upper, |
| 15 |
) |
| 16 |
from imapclient.util import assert_imap_protocol |
| 17 |
|
| 18 |
|
| 19 |
class Test_normalise_text_list(unittest.TestCase): |
| 20 |
def check(self, items, expected): |
| 21 |
self.assertEqual(normalise_text_list(items), expected) |
| 22 |
|
| 23 |
def test_unicode(self): |
| 24 |
self.check("Foo", ["Foo"]) |
| 25 |
|
| 26 |
def test_binary(self): |
| 27 |
self.check(b"FOO", ["FOO"]) |
| 28 |
|
| 29 |
def test_tuple(self): |
| 30 |
self.check(("FOO", "BAR"), ["FOO", "BAR"]) |
| 31 |
|
| 32 |
def test_list(self): |
| 33 |
self.check(["FOO", "BAR"], ["FOO", "BAR"]) |
| 34 |
|
| 35 |
def test_iter(self): |
| 36 |
self.check(iter(["FOO", "BAR"]), ["FOO", "BAR"]) |
| 37 |
|
| 38 |
def test_mixed_list(self): |
| 39 |
self.check(["FOO", b"Bar"], ["FOO", "Bar"]) |
| 40 |
|
| 41 |
|
| 42 |
class Test_seq_to_parenstr(unittest.TestCase): |
| 43 |
def check(self, items, expected): |
| 44 |
self.assertEqual(seq_to_parenstr(items), expected) |
| 45 |
|
| 46 |
def test_unicode(self): |
| 47 |
self.check("foO", "(foO)") |
| 48 |
|
| 49 |
def test_binary(self): |
| 50 |
self.check(b"Foo", "(Foo)") |
| 51 |
|
| 52 |
def test_tuple(self): |
| 53 |
self.check(("FOO", "BAR"), "(FOO BAR)") |
| 54 |
|
| 55 |
def test_list(self): |
| 56 |
self.check(["FOO", "BAR"], "(FOO BAR)") |
| 57 |
|
| 58 |
def test_iter(self): |
| 59 |
self.check(iter(["FOO", "BAR"]), "(FOO BAR)") |
| 60 |
|
| 61 |
def test_mixed_list(self): |
| 62 |
self.check(["foo", b"BAR"], "(foo BAR)") |
| 63 |
|
| 64 |
|
| 65 |
class Test_seq_to_parenstr_upper(unittest.TestCase): |
| 66 |
def check(self, items, expected): |
| 67 |
self.assertEqual(seq_to_parenstr_upper(items), expected) |
| 68 |
|
| 69 |
def test_unicode(self): |
| 70 |
self.check("foO", "(FOO)") |
| 71 |
|
| 72 |
def test_binary(self): |
| 73 |
self.check(b"Foo", "(FOO)") |
| 74 |
|
| 75 |
def test_tuple(self): |
| 76 |
self.check(("foo", "BAR"), "(FOO BAR)") |
| 77 |
|
| 78 |
def test_list(self): |
| 79 |
self.check(["FOO", "bar"], "(FOO BAR)") |
| 80 |
|
| 81 |
def test_iter(self): |
| 82 |
self.check(iter(["FOO", "BaR"]), "(FOO BAR)") |
| 83 |
|
| 84 |
def test_mixed_list(self): |
| 85 |
self.check(["foo", b"BAR"], "(FOO BAR)") |
| 86 |
|
| 87 |
|
| 88 |
class Test_join_message_ids(unittest.TestCase): |
| 89 |
def check(self, items, expected): |
| 90 |
self.assertEqual(join_message_ids(items), expected) |
| 91 |
|
| 92 |
def test_int(self): |
| 93 |
self.check(123, b"123") |
| 94 |
|
| 95 |
def test_unicode(self): |
| 96 |
self.check("123", b"123") |
| 97 |
|
| 98 |
def test_unicode_non_numeric(self): |
| 99 |
self.check("2:*", b"2:*") |
| 100 |
|
| 101 |
def test_binary(self): |
| 102 |
self.check(b"123", b"123") |
| 103 |
|
| 104 |
def test_binary_non_numeric(self): |
| 105 |
self.check(b"2:*", b"2:*") |
| 106 |
|
| 107 |
def test_tuple(self): |
| 108 |
self.check((123, 99), b"123,99") |
| 109 |
|
| 110 |
def test_mixed_list(self): |
| 111 |
self.check(["2:3", 123, b"44"], b"2:3,123,44") |
| 112 |
|
| 113 |
def test_iter(self): |
| 114 |
self.check(iter([123, 99]), b"123,99") |
| 115 |
|
| 116 |
|
| 117 |
class Test_normalise_search_criteria(unittest.TestCase): |
| 118 |
def check(self, criteria, charset, expected): |
| 119 |
actual = _normalise_search_criteria(criteria, charset) |
| 120 |
self.assertEqual(actual, expected) |
| 121 |
# Go further and check exact types |
| 122 |
for a, e in zip(actual, expected): |
| 123 |
self.assertEqual( |
| 124 |
type(a), |
| 125 |
type(e), |
| 126 |
"type mismatch: %s (%r) != %s (%r) in %r" |
| 127 |
% (type(a), a, type(e), e, actual), |
| 128 |
) |
| 129 |
|
| 130 |
def test_list(self): |
| 131 |
self.check(["FOO", "\u263a"], "utf-8", [b"FOO", b"\xe2\x98\xba"]) |
| 132 |
|
| 133 |
def test_tuple(self): |
| 134 |
self.check(("FOO", "BAR"), None, [b"FOO", b"BAR"]) |
| 135 |
|
| 136 |
def test_mixed_list(self): |
| 137 |
self.check(["FOO", b"BAR"], None, [b"FOO", b"BAR"]) |
| 138 |
|
| 139 |
def test_quoting(self): |
| 140 |
self.check(["foo bar"], None, [_quoted(b'"foo bar"')]) |
| 141 |
|
| 142 |
def test_ints(self): |
| 143 |
self.check(["modseq", 500], None, [b"modseq", b"500"]) |
| 144 |
|
| 145 |
def test_unicode(self): |
| 146 |
self.check("Foo", None, [b"Foo"]) |
| 147 |
|
| 148 |
def test_binary(self): |
| 149 |
self.check(b"FOO", None, [b"FOO"]) |
| 150 |
|
| 151 |
def test_unicode_with_charset(self): |
| 152 |
self.check("\u263a", "UTF-8", [b"\xe2\x98\xba"]) |
| 153 |
|
| 154 |
def test_binary_with_charset(self): |
| 155 |
# charset is unused when criteria is binary. |
| 156 |
self.check(b"FOO", "UTF-9", [b"FOO"]) |
| 157 |
|
| 158 |
def test_no_quoting_when_criteria_given_as_string(self): |
| 159 |
self.check("foo bar", None, [b"foo bar"]) |
| 160 |
|
| 161 |
def test_None(self): |
| 162 |
self.assertRaises(InvalidCriteriaError, _normalise_search_criteria, None, None) |
| 163 |
|
| 164 |
def test_empty(self): |
| 165 |
self.assertRaises(InvalidCriteriaError, _normalise_search_criteria, "", None) |
| 166 |
|
| 167 |
|
| 168 |
class TestAssertIMAPProtocol(unittest.TestCase): |
| 169 |
def test_assert_imap_protocol(self): |
| 170 |
assert_imap_protocol(True) |
| 171 |
with self.assertRaises(ProtocolError): |
| 172 |
assert_imap_protocol(False) |
| 173 |
|
| 174 |
def test_assert_imap_protocol_with_message(self): |
| 175 |
assert_imap_protocol(True, b"foo") |
| 176 |
with self.assertRaises(ProtocolError): |
| 177 |
assert_imap_protocol(False, b"foo") |