Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Riakclient tests #8

Merged
merged 3 commits into from about 2 years ago

2 participants

John Watson Jeff Pollard
John Watson
Owner

No description provided.

tests/nydus/db/backends/riak/tests.py
((15 lines not shown))
  15
+        self.expected = {
  16
+            'host': '127.0.0.1',
  17
+            'port': 8098,
  18
+            'prefix': 'riak',
  19
+            'mapred_prefix': 'mapred',
  20
+            'client_id': None,
  21
+        }
  22
+        
  23
+        self.conn = Riak(num=0)
  24
+
  25
+    def test_init(self):
  26
+        args, _, _, defaults = getargspec(Riak.__init__)
  27
+        args = [arg for arg in args if arg != 'self']
  28
+
  29
+        self.assertItemsEqual(args, self.expected.keys())
  30
+        self.assertItemsEqual(defaults, self.expected.values())
1
Jeff Pollard Owner
Fluxx added a note April 11, 2012

This a little bit subtle, but unless a method needs to adhere to a certain interface, I don't think it's necessary/useful to test that the __init__ method kwargs are certain values. It's more important to test behavior of the object.

So in this case, wat's important to the __init__ behavior is:

  1. Omission of certain properties default to expected values. I.e. leaving out the port defaults to 8098.
  2. When providing properties, it uses the user provided properties when constructing the connection. I.e .I provide port 8000 and the connection is on 8000.

I would test these by constructing clients with various combinations of default and provided properties and asserting state on the returned connection from connect()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/nydus/db/backends/riak/tests.py
((20 lines not shown))
  20
+            'client_id': None,
  21
+        }
  22
+        
  23
+        self.conn = Riak(num=0)
  24
+
  25
+    def test_init(self):
  26
+        args, _, _, defaults = getargspec(Riak.__init__)
  27
+        args = [arg for arg in args if arg != 'self']
  28
+
  29
+        self.assertItemsEqual(args, self.expected.keys())
  30
+        self.assertItemsEqual(defaults, self.expected.values())
  31
+
  32
+        self.assertDictContainsSubset(self.expected, self.conn.__dict__)
  33
+
  34
+    def test_identifier(self):
  35
+        self.assertEquals('http://127.0.0.1:8098/riak', self.conn.identifier)
1
Jeff Pollard Owner
Fluxx added a note April 11, 2012

Also test that when provided different properties in the constructor the identifier changes. Right now I can change the implementation of identifier to this and the test would still pass:

@property
def identifier(self):
    return 'http://127.0.0.1:8098/riak'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/nydus/db/backends/riak/tests.py
((34 lines not shown))
  34
+    def test_identifier(self):
  35
+        self.assertEquals('http://127.0.0.1:8098/riak', self.conn.identifier)
  36
+
  37
+    @mock.patch('nydus.db.backends.riak.RiakClient')
  38
+    def test_connect_riakclient_options(self, _RiakClient):
  39
+        self.conn.connect()
  40
+
  41
+        _RiakClient.assert_called_with(**self.expected)
  42
+
  43
+    def test_connect_returns_riakclient(self):
  44
+        client = self.conn.connect()
  45
+
  46
+        self.assertIsInstance(client, RiakClient)
  47
+        
  48
+    def test_provides_retryable_exceptions(self):
  49
+        self.assertIn(RiakError, self.conn.retryable_exceptions)
1
Jeff Pollard Owner
Fluxx added a note April 11, 2012

Are the other exceptions, socket.error, httplib.HTTPException important to test for too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tests/nydus/db/backends/riak/tests.py
((26 lines not shown))
  26
+        args, _, _, defaults = getargspec(Riak.__init__)
  27
+        args = [arg for arg in args if arg != 'self']
  28
+
  29
+        self.assertItemsEqual(args, self.expected.keys())
  30
+        self.assertItemsEqual(defaults, self.expected.values())
  31
+
  32
+        self.assertDictContainsSubset(self.expected, self.conn.__dict__)
  33
+
  34
+    def test_identifier(self):
  35
+        self.assertEquals('http://127.0.0.1:8098/riak', self.conn.identifier)
  36
+
  37
+    @mock.patch('nydus.db.backends.riak.RiakClient')
  38
+    def test_connect_riakclient_options(self, _RiakClient):
  39
+        self.conn.connect()
  40
+
  41
+        _RiakClient.assert_called_with(**self.expected)
1
Jeff Pollard Owner
Fluxx added a note April 11, 2012

To be a little more refined on this test, what's important for connect() is that it's called with the values of self.port, self.host, self.prefix, etc. So you might want to write the test more this way:

_RiakClient.assert_called_with(host=self.conn.host, port=self.conn.port, prefix=self.conn.prefix, mapred_prefix=self.conn.mapred_prefix, client_id=self.conn.client_id)

This helps your test be more expressive on what the actual behavior is, and helps insulate your test against unnecessarily breaking when things change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
John Watson dctrwatson merged commit e06d177 into from April 11, 2012
John Watson dctrwatson closed this April 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
0  backends/riak/__init__.py b/tests/nydus/db/backends/riak/__init__.py
No changes.
9  setup.py
@@ -3,13 +3,14 @@
3 3
 from setuptools import setup, find_packages
4 4
 
5 5
 tests_require = [
6  
-    'nose',
7  
-    'unittest2',
8  
-    'mock',
9  
-    'redis',
10 6
     'Django>=1.2,<1.4',
  7
+    'mock',
  8
+    'nose',
11 9
     'psycopg2',
  10
+    'redis',
  11
+    'riak',
12 12
     'thoonk==1.0.1.0',
  13
+    'unittest2',
13 14
 ]
14 15
 
15 16
 dependency_links = [
71  tests/nydus/db/backends/riak/tests.py
... ...
@@ -0,0 +1,71 @@
  1
+from __future__ import absolute_import
  2
+
  3
+import mock
  4
+from httplib import HTTPException
  5
+from inspect import getargspec
  6
+from riak import RiakClient, RiakError
  7
+from socket import error as SocketError
  8
+
  9
+from tests import BaseTest
  10
+
  11
+from nydus.db.backends.riak import Riak
  12
+from nydus.db.base import Cluster, create_cluster
  13
+
  14
+
  15
+class RiakTest(BaseTest):
  16
+    def setUp(self):
  17
+        self.expected_defaults = {
  18
+            'host': '127.0.0.1',
  19
+            'port': 8098,
  20
+            'prefix': 'riak',
  21
+            'mapred_prefix': 'mapred',
  22
+            'client_id': None,
  23
+        }
  24
+        
  25
+        self.modified_props = {
  26
+            'host': '127.0.0.254',
  27
+            'port': 8908,
  28
+            'prefix': 'kair',
  29
+            'mapred_prefix': 'derpam',
  30
+            'client_id': 'MjgxMDg2MzQx',
  31
+        }
  32
+
  33
+        self.conn = Riak(num=0)
  34
+        self.modified_conn = Riak(num=1, **self.modified_props)
  35
+
  36
+    def test_init_defaults(self):
  37
+        self.assertDictContainsSubset(self.expected_defaults, self.conn.__dict__)
  38
+
  39
+    def test_init_properties(self):
  40
+        self.assertDictContainsSubset(self.modified_props, self.modified_conn.__dict__)
  41
+
  42
+    def test_identifier(self):
  43
+        expected_identifier = 'http://%(host)s:%(port)s/%(prefix)s' % self.conn.__dict__
  44
+        self.assertEquals(expected_identifier, self.conn.identifier)
  45
+
  46
+    def test_identifier_properties(self):
  47
+        expected_identifier = 'http://%(host)s:%(port)s/%(prefix)s' % self.modified_props
  48
+        self.assertEquals(expected_identifier, self.modified_conn.identifier)
  49
+
  50
+    @mock.patch('nydus.db.backends.riak.RiakClient')
  51
+    def test_connect_riakclient_options(self, _RiakClient):
  52
+        self.conn.connect()
  53
+
  54
+        _RiakClient.assert_called_with(host=self.conn.host, port=self.conn.port, prefix=self.conn.prefix, \
  55
+                                        mapred_prefix=self.conn.mapred_prefix, client_id=self.conn.client_id)
  56
+
  57
+    @mock.patch('nydus.db.backends.riak.RiakClient')
  58
+    def test_connect_riakclient_modified_options(self, _RiakClient):
  59
+        self.modified_conn.connect()
  60
+
  61
+        _RiakClient.assert_called_with(host=self.modified_conn.host, port=self.modified_conn.port, prefix=self.modified_conn.prefix, \
  62
+                                        mapred_prefix=self.modified_conn.mapred_prefix, client_id=self.modified_conn.client_id)
  63
+
  64
+    def test_connect_returns_riakclient(self):
  65
+        client = self.conn.connect()
  66
+
  67
+        self.assertIsInstance(client, RiakClient)
  68
+        
  69
+    def test_provides_retryable_exceptions(self):
  70
+        self.assertItemsEqual([RiakError, HTTPException, SocketError], self.conn.retryable_exceptions)
  71
+
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.