Skip to content

Commit e1233ff

Browse files
author
andy
committed
pytest framework and server commands
1 parent 7d8674f commit e1233ff

File tree

6 files changed

+1286
-1911
lines changed

6 files changed

+1286
-1911
lines changed

redis/client.py

+40-27
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class StrictRedis(object):
243243
'PING': lambda r: nativestr(r) == 'PONG',
244244
'RANDOMKEY': lambda r: r and r or None,
245245
'SCRIPT': parse_script,
246-
'SET': lambda r: r and nativestr(r) == 'OK' or None,
246+
'SET': lambda r: r and nativestr(r) == 'OK',
247247
'TIME': lambda x: (int(x[0]), int(x[1]))
248248
}
249249
)
@@ -430,22 +430,10 @@ def dbsize(self):
430430
"Returns the number of keys in the current database"
431431
return self.execute_command('DBSIZE')
432432

433-
def time(self):
434-
"""
435-
Returns the server time as a 2-item tuple of ints:
436-
(seconds since epoch, microseconds into this second).
437-
"""
438-
return self.execute_command('TIME')
439-
440433
def debug_object(self, key):
441434
"Returns version specific metainformation about a give key"
442435
return self.execute_command('DEBUG', 'OBJECT', key)
443436

444-
def delete(self, *names):
445-
"Delete one or more keys specified by ``names``"
446-
return self.execute_command('DEL', *names)
447-
__delitem__ = delete
448-
449437
def echo(self, value):
450438
"Echo the string back from the server"
451439
return self.execute_command('ECHO', value)
@@ -514,6 +502,13 @@ def slaveof(self, host=None, port=None):
514502
return self.execute_command("SLAVEOF", "NO", "ONE")
515503
return self.execute_command("SLAVEOF", host, port)
516504

505+
def time(self):
506+
"""
507+
Returns the server time as a 2-item tuple of ints:
508+
(seconds since epoch, microseconds into this second).
509+
"""
510+
return self.execute_command('TIME')
511+
517512
#### BASIC KEY COMMANDS ####
518513
def append(self, key, value):
519514
"""
@@ -523,13 +518,6 @@ def append(self, key, value):
523518
"""
524519
return self.execute_command('APPEND', key, value)
525520

526-
def getrange(self, key, start, end):
527-
"""
528-
Returns the substring of the string value stored at ``key``,
529-
determined by the offsets ``start`` and ``end`` (both are inclusive)
530-
"""
531-
return self.execute_command('GETRANGE', key, start, end)
532-
533521
def bitcount(self, key, start=None, end=None):
534522
"""
535523
Returns the count of set bits in the value of ``key``. Optional
@@ -558,6 +546,11 @@ def decr(self, name, amount=1):
558546
"""
559547
return self.execute_command('DECRBY', name, amount)
560548

549+
def delete(self, *names):
550+
"Delete one or more keys specified by ``names``"
551+
return self.execute_command('DEL', *names)
552+
__delitem__ = delete
553+
561554
def exists(self, name):
562555
"Returns a boolean indicating whether key ``name`` exists"
563556
return self.execute_command('EXISTS', name)
@@ -601,6 +594,13 @@ def getbit(self, name, offset):
601594
"Returns a boolean indicating the value of ``offset`` in ``name``"
602595
return self.execute_command('GETBIT', name, offset)
603596

597+
def getrange(self, key, start, end):
598+
"""
599+
Returns the substring of the string value stored at ``key``,
600+
determined by the offsets ``start`` and ``end`` (both are inclusive)
601+
"""
602+
return self.execute_command('GETRANGE', key, start, end)
603+
604604
def getset(self, name, value):
605605
"""
606606
Set the value at key ``name`` to ``value`` if key doesn't exist
@@ -643,20 +643,33 @@ def mget(self, keys, *args):
643643
args = list_or_args(keys, args)
644644
return self.execute_command('MGET', *args)
645645

646-
def mset(self, mapping):
647-
"Sets each key in the ``mapping`` dict to its corresponding value"
646+
def mset(self, *args, **kwargs):
647+
"""
648+
Sets key/values based on a mapping. Mapping can be supplied as a single
649+
dictionary argument or as kwargs.
650+
"""
651+
if args:
652+
if len(args) != 1 or not isinstance(args[0], dict):
653+
raise RedisError('MSET requires **kwargs or a single dict arg')
654+
kwargs.update(args[0])
648655
items = []
649-
for pair in iteritems(mapping):
656+
for pair in iteritems(kwargs):
650657
items.extend(pair)
651658
return self.execute_command('MSET', *items)
652659

653-
def msetnx(self, mapping):
660+
def msetnx(self, *args, **kwargs):
654661
"""
655-
Sets each key in the ``mapping`` dict to its corresponding value if
656-
none of the keys are already set
662+
Sets key/values based on a mapping if none of the keys are already set.
663+
Mapping can be supplied as a single dictionary argument or as kwargs.
664+
Returns a boolean indicating if the operation was successful.
657665
"""
666+
if args:
667+
if len(args) != 1 or not isinstance(args[0], dict):
668+
raise RedisError('MSETNX requires **kwargs or a single '
669+
'dict arg')
670+
kwargs.update(args[0])
658671
items = []
659-
for pair in iteritems(mapping):
672+
for pair in iteritems(kwargs):
660673
items.extend(pair)
661674
return self.execute_command('MSETNX', *items)
662675

run_tests

-9
This file was deleted.

setup.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
#!/usr/bin/env python
22
import os
3+
import sys
34

45
from redis import __version__
56

67
try:
78
from setuptools import setup
9+
from setuptools.command.test import test as TestCommand
10+
11+
class PyTest(TestCommand):
12+
def finalize_options(self):
13+
TestCommand.finalize_options(self)
14+
self.test_args = []
15+
self.test_suite = True
16+
17+
def run_tests(self):
18+
#import here, cause outside the eggs aren't loaded
19+
import pytest
20+
errno = pytest.main(self.test_args)
21+
sys.exit(errno)
22+
823
except ImportError:
24+
925
from distutils.core import setup
26+
PyTest = lambda x: x
1027

1128
f = open(os.path.join(os.path.dirname(__file__), 'README.rst'))
1229
long_description = f.read()
@@ -25,7 +42,8 @@
2542
keywords=['Redis', 'key-value store'],
2643
license='MIT',
2744
packages=['redis'],
28-
test_suite='tests.all_tests',
45+
tests_require=['pytest'],
46+
cmdclass={'test': PyTest},
2947
classifiers=[
3048
'Development Status :: 5 - Production/Stable',
3149
'Environment :: Console',

tests/__init__.py

-31
Original file line numberDiff line numberDiff line change
@@ -1,31 +0,0 @@
1-
import unittest
2-
3-
from tests.server_commands import ServerCommandsTestCase
4-
from tests.connection_pool import ConnectionPoolTestCase
5-
from tests.connection_pool import BlockingConnectionPoolTestCase
6-
from tests.pipeline import PipelineTestCase
7-
from tests.lock import LockTestCase
8-
from tests.pubsub import PubSubTestCase, PubSubRedisDownTestCase
9-
from tests.encoding import (PythonParserEncodingTestCase,
10-
HiredisEncodingTestCase)
11-
12-
try:
13-
import hiredis
14-
use_hiredis = True
15-
except ImportError:
16-
use_hiredis = False
17-
18-
19-
def all_tests():
20-
suite = unittest.TestSuite()
21-
suite.addTest(unittest.makeSuite(ServerCommandsTestCase))
22-
suite.addTest(unittest.makeSuite(ConnectionPoolTestCase))
23-
suite.addTest(unittest.makeSuite(BlockingConnectionPoolTestCase))
24-
suite.addTest(unittest.makeSuite(PipelineTestCase))
25-
suite.addTest(unittest.makeSuite(LockTestCase))
26-
suite.addTest(unittest.makeSuite(PubSubTestCase))
27-
suite.addTest(unittest.makeSuite(PubSubRedisDownTestCase))
28-
suite.addTest(unittest.makeSuite(PythonParserEncodingTestCase))
29-
if use_hiredis:
30-
suite.addTest(unittest.makeSuite(HiredisEncodingTestCase))
31-
return suite

0 commit comments

Comments
 (0)