def deep_runner(container): depth = random.randint(1, 10) i = 0 connections = [None] * depth for i in range3(depth): connections[i] = container.acquire_connection() for i in range3(depth - 1, -1, -1): container.release_connection(connections[i])
def test_ucd_classes(self): class_cc = unicode5.CharClass.ucd_category('Cc') class_c = unicode5.CharClass.ucd_category('C') for code in range3(0x20): self.assertTrue(class_cc.test(character(code))) self.assertTrue(class_c.test(character(code))) for code in range3(0x7F, 0xA0): self.assertTrue(class_cc.test(character(code))) self.assertTrue(class_c.test(character(code))) self.assertFalse(class_cc.test(character(0xAD))) self.assertTrue(class_c.test(character(0xAD))) self.assertTrue( unicode5.CharClass.ucd_category('Cf').test(character(0xAD)))
def test_multiget(self): threads = [] for i in range3(10): threads.append(threading.Thread(target=self.domain3_thread_oneshot)) for i in range3(10): threads.append(threading.Thread(target=self.domain4_thread_oneshot)) for t in threads: t.start() while threads: t = threads.pop() t.join() # success criteria? that we survived self.client.idle_cleanup(3) self.client.idle_cleanup(0)
def test_letnum(self): """Basic syntax definitions:: <let-num> ::= <upper> | <lower> | <number> <let-num-hyp> ::= <upper> | <lower> | <number> | "-" """ for i in range3(0x00, 0x2D): self.assertFalse(urn.is_upper(character(i))) self.assertFalse(urn.is_lower(character(i))) self.assertFalse(urn.is_number(character(i))) self.assertFalse(urn.is_letnum(character(i))) self.assertFalse(urn.is_letnumhyp(character(i))) self.assertFalse(urn.is_upper(character(0x2D))) self.assertFalse(urn.is_lower(character(0x2D))) self.assertFalse(urn.is_number(character(0x2D))) self.assertFalse(urn.is_letnum(character(0x2D))) self.assertTrue(urn.is_letnumhyp(character(0x2D))) for i in range3(0x2E, 0x30): self.assertFalse(urn.is_upper(character(i))) self.assertFalse(urn.is_lower(character(i))) self.assertFalse(urn.is_number(character(i))) self.assertFalse(urn.is_letnum(character(i))) self.assertFalse(urn.is_letnumhyp(character(i))) for i in range3(0x30, 0x3A): self.assertFalse(urn.is_upper(character(i))) self.assertFalse(urn.is_lower(character(i))) self.assertTrue(urn.is_number(character(i))) self.assertTrue(urn.is_letnum(character(i))) self.assertTrue(urn.is_letnumhyp(character(i))) for i in range3(0x3A, 0x41): self.assertFalse(urn.is_upper(character(i))) self.assertFalse(urn.is_lower(character(i))) self.assertFalse(urn.is_number(character(i))) self.assertFalse(urn.is_letnum(character(i))) self.assertFalse(urn.is_letnumhyp(character(i))) for i in range3(0x41, 0x5B): self.assertTrue(urn.is_upper(character(i))) self.assertFalse(urn.is_lower(character(i))) self.assertFalse(urn.is_number(character(i))) self.assertTrue(urn.is_letnum(character(i))) self.assertTrue(urn.is_letnumhyp(character(i))) for i in range3(0x5B, 0x61): self.assertFalse(urn.is_upper(character(i))) self.assertFalse(urn.is_lower(character(i))) self.assertFalse(urn.is_number(character(i))) self.assertFalse(urn.is_letnum(character(i))) self.assertFalse(urn.is_letnumhyp(character(i))) for i in range3(0x61, 0x7B): self.assertFalse(urn.is_upper(character(i))) self.assertTrue(urn.is_lower(character(i))) self.assertFalse(urn.is_number(character(i))) self.assertTrue(urn.is_letnum(character(i))) self.assertTrue(urn.is_letnumhyp(character(i))) for i in range3(0x7B, 0xFF): self.assertFalse(urn.is_upper(character(i))) self.assertFalse(urn.is_lower(character(i))) self.assertFalse(urn.is_number(character(i))) self.assertFalse(urn.is_letnum(character(i))) self.assertFalse(urn.is_letnumhyp(character(i)))
def test_make_delimiter(self): # function to make a delimiter been_there = set() for i in range3(100): # try it 100 times, should be different each time boundary = multipart.make_boundary_delimiter() self.assertFalse(boundary in been_there) # check the syntax self.assertTrue(multipart.is_valid_boundary(boundary)) # check length, need at least 20 characters in length # representing 120 bits of information self.assertTrue(len(boundary) >= 20) been_there.add(boundary) # now do one with a prefix boundary = multipart.make_boundary_delimiter(b"-- boundary ") self.assertTrue(multipart.is_valid_boundary(boundary)) self.assertTrue(len(boundary) >= 32) been_there.add(boundary) # now do an illegal one try: boundary = multipart.make_boundary_delimiter(b" boundary ") self.fail("boundary starts with space") except ValueError: pass try: boundary = multipart.make_boundary_delimiter(b"-- {boundary} ") self.fail("boundary contains illegal character") except ValueError: pass
def test_create(self): ss = blockstore.StreamStore(bs=self.bs, ls=self.ls, entity_set=self.cdef['Streams']) s1 = ss.new_stream("text/plain") self.assertTrue(isinstance(s1, edm.Entity)) self.assertTrue(s1.exists) try: self.assertTrue(s1.key() is not None) except KeyError: self.fail("stream entity has not key") self.assertTrue(s1['mimetype'].value == "text/plain") s2 = ss.new_stream( params.MediaType('text', 'plain', {'charset': ('charset', 'utf-8')})) self.assertTrue(isinstance(s2, edm.Entity)) self.assertTrue(s2['mimetype'].value == "text/plain; charset=utf-8") skey1 = s1.key() skey2 = s2.key() s1 = ss.get_stream(skey1) self.assertTrue(isinstance(s1, edm.Entity)) for i in range3(10): try: ss.get_stream(i) self.assertTrue(i == skey1 or i == skey2) except KeyError: self.assertFalse(i == skey1 or i == skey2)
def test_goodsize(self): data = b"How long is a piece of string?" src = io.BytesIO(data) # default buffer is larger than src b = BufferedStreamWrapper(src) self.assertTrue(isinstance(b, io.RawIOBase)) self.assertTrue(b.readable()) self.assertFalse(b.writable()) self.assertTrue(b.seekable()) self.assertTrue(b.length == len(data)) pos = b.tell() self.assertTrue(pos == 0, "buffer starts at beginning of stream") for i in range3(100): # check seek and read newpos = random.randint(0, len(data) + 1) rlen = random.randint(0, len(data) + 1) whence = random.choice((io.SEEK_SET, io.SEEK_CUR, io.SEEK_END)) if whence == io.SEEK_CUR: adj = newpos - pos elif whence == io.SEEK_END: adj = newpos - len(data) elif whence == io.SEEK_SET: adj = newpos b.seek(adj, whence) self.assertTrue(b.tell() == newpos, "Expected %i found %i" % (newpos, b.tell())) pos = newpos xlen = max(0, min(len(data) - pos, rlen)) rdata = b.read(rlen) self.assertTrue(len(rdata) == xlen) self.assertTrue(rdata == data[pos:pos + rlen]) pos += xlen
def test_retries(self): request = http.ClientRequest("http://www.domain1.com/", max_retries=10, min_retry_time=4) self.assertTrue(request.max_retries == 10) self.assertTrue(request.nretries == 0) self.assertTrue(request.retry_time == 0) MockTime.now = 10.0 ranges = [ (10.0, 10.0), # 10+0 +-0 (13.0, 15.0), # 10+4 +-1 (13.0, 15.0), # 10+4 +-1 (16.0, 20.0), # 10+8 +-2 (19.0, 25.0), # 10+12 +-3 (25.0, 35.0), # 10+20 +-5 (34.0, 50.0), # 10+32 +-8 (49.0, 75.0), # 10+52 +-13 (73.0, 115.0), # 10+84 +-21 (112.0, 180.0) ] # 10+136 +-34 for i in range3(10): # simulate a failed send request.connect(None, 0) request.disconnect(1) self.assertTrue(request.can_retry(), "retry %ith time" % i) self.assertTrue( request.retry_time >= ranges[i][0], "%f too small in pair %i" % (request.retry_time, i)) self.assertTrue( request.retry_time <= ranges[i][1], "%f too large in pair %i" % (request.retry_time, i))
def multidata_app(self, environ, start_response): # the point of this app is to generate data of unknown # length forcing chunked encoding where possible start_response("200 OK", []) for i in range3(random.randint(2, 10)): yield b"Hello" yield b"xyz"
def test_filter(self): es = self.schema['SampleEntities.Employees'] with es.open() as collection: collection.create_table() for i in range3(20): new_hire = collection.new_entity() new_hire.set_key('%05X' % i) new_hire["EmployeeName"].set_from_value('Talent #%i' % i) new_hire["Address"]["City"].set_from_value('Chunton') new_hire["Address"]["Street"].set_from_value( random.choice( ('Mill Road', 'Main Street', 'Privet Drive'))) collection.insert_entity(new_hire) self.assertTrue(len(collection) == 20) collection.set_filter( core.CommonExpression.from_str( "substringof('Road',Address/Street)")) roads = len(collection) collection.set_filter( core.CommonExpression.from_str( "endswith(Address/Street,'Street')")) streets = len(collection) collection.set_filter( core.CommonExpression.from_str( "startswith(Address/Street,'Privet')")) drives = len(collection) self.assertTrue( roads + streets + drives == 20, "Failed to match all records: %i found" % (roads + streets + drives)) collection.set_filter( core.CommonExpression.from_str("EmployeeName eq 'Talent #13'")) self.assertTrue(len(collection) == 1, "Just one matching employee") talent = collection.values()[0] self.assertTrue(talent['EmployeeID'].value == '0000D')
def test_multiget(self): threads = [] for i in range3(10): threads.append( threading.Thread(target=self.domain3_thread_oneshot)) for i in range3(10): threads.append( threading.Thread(target=self.domain4_thread_oneshot)) for t in threads: t.start() while threads: t = threads.pop() t.join() # success criteria? that we survived self.client.idle_cleanup(3) self.client.idle_cleanup(0)
def test_name_start(self): """Productions:: [4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] [5] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]""" n_namestartchars = 0 n_namechars = 0 for code in range3(0x10000): c = character(code) if structures.is_name_char(c): n_namechars += 1 if structures.is_name_start_char(c): n_namestartchars += 1 else: self.assertFalse(structures.is_name_start_char(c), "NameStart not a name char: %s" % c) self.assertTrue(n_namechars == 54129, "name char total %i" % n_namechars) self.assertTrue(n_namestartchars == 54002, "name start char total %i" % n_namestartchars)
def test_retries(self): request = http.ClientRequest("http://www.domain1.com/", max_retries=10, min_retry_time=4) self.assertTrue(request.max_retries == 10) self.assertTrue(request.nretries == 0) self.assertTrue(request.retry_time == 0) MockTime.now = 10.0 ranges = [ (10.0, 10.0), # 10+0 +-0 (13.0, 15.0), # 10+4 +-1 (13.0, 15.0), # 10+4 +-1 (16.0, 20.0), # 10+8 +-2 (19.0, 25.0), # 10+12 +-3 (25.0, 35.0), # 10+20 +-5 (34.0, 50.0), # 10+32 +-8 (49.0, 75.0), # 10+52 +-13 (73.0, 115.0), # 10+84 +-21 (112.0, 180.0), ] # 10+136 +-34 for i in range3(10): # simulate a failed send request.connect(None, 0) request.disconnect(1) self.assertTrue(request.can_retry(), "retry %ith time" % i) self.assertTrue(request.retry_time >= ranges[i][0], "%f too small in pair %i" % (request.retry_time, i)) self.assertTrue(request.retry_time <= ranges[i][1], "%f too large in pair %i" % (request.retry_time, i))
def test_data(mem_cache): with mem_cache.open() as collection: for i in range3(26): e = collection.new_entity() e.set_key(str(i)) e['Value'].set_from_value(character(0x41 + i)) e['Expires'].set_from_value( iso.TimePoint.from_unix_time(time.time() + 10 * i)) collection.insert_entity(e)
def test_kill(self): threads = [] for i in range3(10): threads.append(threading.Thread(target=self.domain3_thread_oneshot)) for i in range3(10): threads.append(threading.Thread(target=self.domain4_thread_oneshot)) for t in threads: t.start() # we can't guarantee we'll find active connections unfortunately time.sleep(1) logging.info("%i active connections", self.client.active_count()) self.client.active_cleanup(3) logging.info("%i active connections after active_cleanup(3)", self.client.active_count()) self.client.active_cleanup(0) logging.info("%i active connections after active_cleanup(0)", self.client.active_count()) while threads: t = threads.pop() t.join()
def mock_challenge(): data = [byte(0)] * 56 data[0:7] = list(b"NTLMSSP") data[8] = byte(2) data[16] = byte(56) data[20] = byte(1) data[21] = byte(0x82) for i in range3(8): data[24 + i] = byte(random.randint(0, 255)) return join_bytes(data)
def test_require_end(self): p = unicode5.BasicParser("hello") for i in range3(5): try: p.require_end() self.fail("require_end failed to raise exception") except unicode5.ParserError as e: self.assertTrue(e.production == ul("end")) p.next_char() p.require_end()
def compare_strings(self, expected, found, label="Test"): for i in range3(len(expected)): if i >= len(found): self.fail("%s truncation failure:\n%s... expected %s" % (label, found[0:i], expected[i])) if expected[i] == found[i]: continue self.fail("%s mismatch:\n%s... expected %s ; found %s" % (label, repr(found[0:i + 1]), repr(expected[i]), repr(found[i])))
def find_edges(self, test_func, max): edges = [] flag = False for code in range3(max + 1): c = character(code) if flag != test_func(c): flag = not flag edges.append(code) if flag: edges.append(max + 1) return edges
def test_ucd_blocks(self): class_basic_latin = unicode5.CharClass.ucd_block('Basic Latin') self.assertTrue(class_basic_latin is unicode5.CharClass.ucd_block( 'basiclatin'), "block name normalization") for code in range3(0x80): self.assertTrue(class_basic_latin.test(character(code))) self.assertFalse(class_basic_latin.test(character(0x80))) # randomly pick one of the other blocks class_basic_latin = unicode5.CharClass.ucd_block('Arrows') self.assertFalse(class_basic_latin.test(character(0x2150))) self.assertTrue(class_basic_latin.test(character(0x2190)))
def test_parse_integer(self): p = unicode5.BasicParser(ul("23p")) # all defaults, unbounded self.assertTrue(p.parse_integer() == 23) self.assertTrue(p.pos == 2) p.setpos(1) # provide a minimum value self.assertTrue(p.parse_integer(4) is None) self.assertTrue(p.parse_integer(2) == 3) p.setpos(1) # provide a minimum and maximum value self.assertTrue(p.parse_integer(0, 2) is None) self.assertTrue(p.parse_integer(1, 4) == 3) p.setpos(0) # min value < 0, should throw an error try: p.parse_integer(-1) self.fail("min = -1 didn't raise exception") except ValueError: # and it shouldn't move the parser self.assertTrue(p.pos == 0) # min value > max, should throw an error try: p.parse_integer(3, 1) self.fail("min > max didn't raise exception") except ValueError: # and it shouldn't move the parser self.assertTrue(p.pos == 0) # check we can exceed ordinary integer sizes istr = ul("123456789" + "0" * 256) p = unicode5.BasicParser(istr) # test max digits self.assertTrue(p.parse_integer(0, None, 10) == 1234567890) # check wide zeros self.assertTrue(p.parse_integer(0, None, 10) == 0) self.assertTrue(p.pos == 20) p.setpos(0) # check large numbers self.assertTrue(p.parse_integer(0, None, 15) == 123456789000000) # test Arabic digits, should not parse! p = unicode5.BasicParser( u8(b'\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5' b'\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9')) for i in range3(10): self.assertTrue(p.parse_integer() is None) p.next_char() # test binary forms p = unicode5.BasicParser(b"234p") self.assertTrue(p.parse_integer(max_digits=1) == 2) self.assertTrue(p.parse_integer(0, 2) is None) self.assertTrue(p.parse_integer() == 34) p.next_char() self.assertTrue(p.parse_integer() is None)
def test_ucd_blocks(self): class_basic_latin = unicode5.CharClass.ucd_block('Basic Latin') self.assertTrue( class_basic_latin is unicode5.CharClass.ucd_block('basiclatin'), "block name normalization") for code in range3(0x80): self.assertTrue(class_basic_latin.test(character(code))) self.assertFalse(class_basic_latin.test(character(0x80))) # randomly pick one of the other blocks class_basic_latin = unicode5.CharClass.ucd_block('Arrows') self.assertFalse(class_basic_latin.test(character(0x2150))) self.assertTrue(class_basic_latin.test(character(0x2190)))
def test_boundary_syntax(self): """Tests for basic boundary classes.""" # bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" / "+" / "_" / # "," / "-" / "." / "/" / ":" / "=" / "?" extras = b"'()+_,-./:=?" for i in range3(0, 256): b = byte(i) self.assertTrue(multipart.is_bcharnospace(b) == (b in extras or grammar.is_digit(b) or grammar.is_alpha(b))) self.assertTrue(multipart.is_bchars(b) == (b in extras or grammar.is_digit(b) or grammar.is_alpha(b) or b == grammar.SP))
def test_kill(self): threads = [] for i in range3(10): threads.append( threading.Thread(target=self.domain3_thread_oneshot)) for i in range3(10): threads.append( threading.Thread(target=self.domain4_thread_oneshot)) for t in threads: t.start() # we can't guarantee we'll find active connections unfortunately time.sleep(1) logging.info("%i active connections", self.client.active_count()) self.client.active_cleanup(3) logging.info("%i active connections after active_cleanup(3)", self.client.active_count()) self.client.active_cleanup(0) logging.info("%i active connections after active_cleanup(0)", self.client.active_count()) while threads: t = threads.pop() t.join()
def test_specials(self): """Tests for basic byte classes.""" # specials = "(" / ")" / "<" / ">" / "@" / "," / ";" / ":" / # "\" / <"> / "." / "[" / "]" specials = b"()<>@,;:\\\".[]" for i in range3(0, 256): b = byte(i) self.assertTrue( multipart.is_special(b) == (b in specials), "is_special(byte(%i))" % i) p = multipart.RFC822Parser("atomic/?={}.blondie") self.assertTrue(p.is_atom(), "It is an atom") result = p.parse_atom() self.assertTrue(result == b"atomic/?={}", "rfc822 atom: %s" % result)
def print_pretty_weird(self, e1, e2): c1 = e1.get_canonical_children() c2 = e2.get_canonical_children() if len(c1) != len(c2): logging.debug( "Number of children mismatch in similar elements..." "\n>>>\n%s\n>>>\n%s\n>>>\n%s", repr(c1), repr(c2), str(e1)) return for i in range3(len(c1)): if c1[i] != c2[i]: if isinstance(c1[i], xml.XMLElement) and \ isinstance(c2[i], xml.XMLElement): self.print_pretty_weird(c1[i], c2[i]) else: logging.debug("Mismatch in similar elements..." "\n>>>\n%s\n>>>\n%s", repr(e1), repr(e2))
def test_lock_multithread(self): ls = blockstore.LockStore(entity_set=self.cdef['BlockLocks'], lock_timeout=3) threads = [] for i in range3(50): threads.append( threading.Thread(target=self.lock_runner, args=(ls, ))) for t in threads: t.start() time.sleep(1 if random.random() < 0.1 else 0) while threads: t = threads.pop() t.join() self.assertTrue(self.mt_count > 1) logging.info("%i out of %i threads obtained the lock", self.mt_count, 50)
def test_new_consumer(self): tp = lti.BLTIToolProvider() keys = {} secrets = {} for i in range3(100): key, secret = tp.new_consumer() self.assertFalse(key in keys, "Repeated key from TP") keys[key] = secret self.assertFalse(secret in secrets, "Repeated secret from IP") secrets[secret] = key key, secret = tp.new_consumer("www.example.com") try: key, secret = tp.new_consumer("www.example.com") self.fail("Failure to spot duplicate key") except lti.BLTIDuplicateKeyError: pass
def test_retry(self): dbapi = MockAPI(1) for i in range3(5): container = MockContainer(container=self.container, dbapi=dbapi, max_connections=5) container.bad_count = i c = container.acquire_connection() t = sqlds.SQLTransaction(container, c) try: t.begin() if i >= 3: self.fail("Expected error after %i OperationalErrors" % i) except sqlds.SQLError: if i < 3: self.fail("Missing retries with %i OperationalErrors" % i) t.close() container.release_connection(c)
def test_constructor(self): c = unicode5.CharClass() if MAX_CHAR < 0x10FFFF: logging.warn("unicode5 tests truncated to character(0x%X) by " "narrow python build" % MAX_CHAR) for code in range3(MAX_CHAR + 1): self.assertFalse(c.test(character(code))) c = unicode5.CharClass('a') self.assertTrue(self.class_test(c) == 'a') c = unicode5.CharClass(('a', 'z')) self.assertTrue(self.class_test(c) == 'abcdefghijklmnopqrstuvwxyz') c = unicode5.CharClass('abcxyz') self.assertTrue( len(c.ranges) == 2, "No range optimization: %s" % repr(c.ranges)) self.assertTrue(self.class_test(c) == 'abcxyz') cc = unicode5.CharClass(c) self.assertTrue(self.class_test(cc) == 'abcxyz')
def test_lock_multithread(self): ls = blockstore.LockStore(entity_set=self.cdef['BlockLocks'], lock_timeout=3) threads = [] for i in range3(50): threads.append(threading.Thread(target=self.lock_runner, args=(ls,))) for t in threads: t.start() time.sleep(1 if random.random() < 0.1 else 0) while threads: t = threads.pop() t.join() self.assertTrue(self.mt_count > 1) logging.info( "%i out of %i threads obtained the lock", self.mt_count, 50)
def test_multithread(self): # we ask for 5 connections and should get them container = MockContainer(container=self.container, dbapi=MockAPI(1), max_connections=5) self.assertTrue(container.cpool_max == 5, "Expected 5 connections") threads = [] for i in range3(100): threads.append( threading.Thread(target=deep_runner, args=(container, ))) for t in threads: t.start() while threads: t = threads.pop() t.join() # success criteria? that we survived pass
def test_parse_digits(self): p = unicode5.BasicParser(ul("23p")) # min value of 0 self.assertTrue(p.parse_digits(0) == ul("23")) self.assertTrue(p.pos == 2) # min value of 2, should fail p.setpos(1) self.assertTrue(p.parse_digits(2) is None) # shouldn't move the parser self.assertTrue(p.pos == 1) # min value of 0, should throw an error try: p.parse_digits(-1) self.fail("min=-1 didn't raise exception") except ValueError: # and it shouldn't move the parser self.assertTrue(p.pos == 1) # min value > max, should throw an error try: p.parse_digits(3, 1) self.fail("min > max didn't raise exception") except ValueError: # and it shouldn't move the parser self.assertTrue(p.pos == 1) # check we can exceed ordinary integer sizes istr = ul("123456789" + "0" * 256) p = unicode5.BasicParser(istr) self.assertTrue(len(p.parse_digits(0, 256)) == 256) # and check that runs of 0 don't mean a thing self.assertTrue(p.parse_digits(0, 256) == ul("000000000")) # test Arabic digits, should not parse! p = unicode5.BasicParser( u8(b'\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5' b'\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9')) for i in range3(10): self.assertTrue(p.parse_digits(1) is None) p.next_char() # test binary forms p = unicode5.BasicParser(b"234p") # unlike parse_digit we return a string, even if only one digit self.assertTrue(p.parse_digits(1, 1) == b"2") self.assertTrue(p.parse_digits(1) == b"34") p.next_char() self.assertTrue(p.parse_digits(1) is None) self.assertTrue(p.parse_digits(0) == b"")
def test_filter(self): es = self.schema['SampleEntities.Employees'] with es.open() as collection: collection.create_table() for i in range3(20): new_hire = collection.new_entity() new_hire.set_key('%05X' % i) new_hire["EmployeeName"].set_from_value('Talent #%i' % i) new_hire["Address"]["City"].set_from_value('Chunton') new_hire["Address"]["Street"].set_from_value( random.choice( ('Mill Road', 'Main Street', 'Privet Drive'))) collection.insert_entity(new_hire) self.assertTrue(len(collection) == 20) collection.set_filter( core.CommonExpression.from_str( "substringof('Road',Address/Street)")) roads = len(collection) collection.set_filter( core.CommonExpression.from_str( "endswith(Address/Street,'Street')")) streets = len(collection) collection.set_filter( core.CommonExpression.from_str( "startswith(Address/Street,'Privet')")) drives = len(collection) self.assertTrue( roads + streets + drives == 20, "Failed to match all records: %i found" % (roads + streets + drives)) collection.set_filter( core.CommonExpression.from_str( "EmployeeName eq 'Talent #13'")) self.assertTrue(len(collection) == 1, "Just one matching employee") talent = collection.values()[0] self.assertTrue(talent['EmployeeID'].value == '0000D') collection.set_filter( core.CommonExpression.from_str( "substring(EmployeeName, 8, 3) eq '#13'")) self.assertTrue(len(collection) == 1, "Just one matching employee")
def test_parse_digit_value(self): p = unicode5.BasicParser(ul("2p")) self.assertTrue(p.parse_digit_value() == 2) self.assertTrue(p.pos == 1) self.assertTrue(p.parse_digit_value() is None) p.next_char() self.assertTrue(p.parse_digit_value() is None) # test Arabic digits, should not parse! p = unicode5.BasicParser( u8(b'\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5' b'\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9')) for i in range3(10): self.assertTrue(p.parse_digit_value() is None) p.next_char() # test binary forms p = unicode5.BasicParser(b"2p") self.assertTrue(p.parse_digit_value() == 2) self.assertTrue(p.parse_digit_value() is None) p.next_char() self.assertTrue(p.parse_digit_value() is None)
def test_match_digit(self): p = unicode5.BasicParser(ul("2p")) self.assertTrue(p.match_digit()) p.next_char() self.assertFalse(p.match_digit()) p.next_char() self.assertFalse(p.match_digit()) # test Arabic digits, should not match! p = unicode5.BasicParser( u8(b'\xd9\xa0\xd9\xa1\xd9\xa2\xd9\xa3\xd9\xa4\xd9\xa5' b'\xd9\xa6\xd9\xa7\xd9\xa8\xd9\xa9')) for i in range3(10): self.assertFalse(p.match_digit()) p.next_char() p = unicode5.BasicParser(b"2p") self.assertTrue(p.match_digit()) p.next_char() self.assertFalse(p.match_digit()) p.next_char() self.assertFalse(p.match_digit())
def test_iter(self): es = self.schema['SampleEntities.Employees'] with es.open() as collection: collection.create_table() for i in range3(10): new_hire = collection.new_entity() new_hire.set_key('%05X' % i) new_hire["EmployeeName"].set_from_value('Talent #%i' % i) new_hire["Address"]["City"].set_from_value('Chunton') new_hire["Address"]["Street"].set_from_value( random.choice( ('Mill Road', 'Main Street', 'Privet Drive'))) collection.insert_entity(new_hire) self.assertTrue(len(collection) == 10) keys = set() for talent in collection.values(): self.assertTrue( talent['EmployeeName'].value.startswith('Talent ')) keys.add(talent['EmployeeID'].value) self.assertTrue(len(keys) == 10)