Skip to content

Commit

Permalink
Merge branch 'feature/issue-26-update-riak-store-and-interface-to-mat…
Browse files Browse the repository at this point in the history
…ch' into develop
  • Loading branch information
hodgestar committed Nov 2, 2015
2 parents ce65e3b + f0fe0f9 commit ec8f265
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 125 deletions.
5 changes: 0 additions & 5 deletions go_optouts/api_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ def opt_out_not_deleted(self, request, failure):

# Methods

@app.route('/')
def addresses(self, request):
request.setHeader('Content-Type', 'application/json')
return json.dumps(self._info)

@app.route('/optouts/<string:addresstype>/<string:address>',
methods=['GET'])
def get_address(self, request, addresstype, address):
Expand Down
15 changes: 0 additions & 15 deletions go_optouts/backends/interface.py

This file was deleted.

9 changes: 0 additions & 9 deletions go_optouts/backends/memory.py

This file was deleted.

61 changes: 0 additions & 61 deletions go_optouts/backends/tests/test_riak.py

This file was deleted.

4 changes: 2 additions & 2 deletions go_optouts/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from api_methods import API
from store.memory import OptOutMemory
from store.memory import MemoryOptOutCollection

app = API(OptOutMemory())
app = API(MemoryOptOutCollection())

app.app.run('localhost', 8080)
15 changes: 10 additions & 5 deletions go_optouts/store/interface.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import zope.interface


class IOptOutStore(zope.interface.Interface):
class IOptOutBackend(zope.interface.Interface):
def get_opt_out_collection(owner_id):
""" Return the opt out collection for the specified owner.
:param str owner_id:
The id of the owner of the opt out store.
"""


class IOptOutCollection(zope.interface.Interface):

def get(address_type, address):
""" Retrieve the opt out for an address. """
pass

def put(address_type, address):
""" Store a record of an opt out for an address. """
pass

def delete(address_type, address):
""" Remove an opt out for an address. """
pass

def count():
""" Return the number of opt outs. """
pass
30 changes: 26 additions & 4 deletions go_optouts/store/memory.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
from zope.interface import implements
from interface import IOptOutStore
import uuid

from zope.interface import implements

from .interface import IOptOutBackend, IOptOutCollection


class MemoryOptOutBackend(object):
""" Memory opt out backend. """

implements(IOptOutBackend)

def __init__(self):
self._collections = {}

def get_opt_out_collection(self, owner_id):
""" Return the opt out collection for the specified owner.
:param str owner_id:
The id of the owner of the opt out store.
"""
collection = self._collections.get(owner_id)
if collection is None:
collection = self._collections[owner_id] = MemoryOptOutCollection()
return collection


class OptOutMemory(object):
class MemoryOptOutCollection(object):
"""
This implements the IOptOutStore interface.
Expand All @@ -17,7 +39,7 @@ class OptOutMemory(object):
}
"""

implements(IOptOutStore)
implements(IOptOutCollection)

def __init__(self):
# _store maps (address_type, address) pairs to opt outs
Expand Down
28 changes: 19 additions & 9 deletions go_optouts/backends/riak.py → go_optouts/store/riak.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
""" Riak opt out backend. """

from twisted.internet.defer import inlineCallbacks, returnValue
from zope.interface import implements

from go.vumitools.opt_out import OptOutStore, OptOut

from .interface import IOptOutBackend, IOptOutCollection


class RiakOptOutBackend(object):
""" Riak opt out backend.
Expand All @@ -14,8 +17,7 @@ class RiakOptOutBackend(object):
A Riak manager for the opt out stores.
"""

# TODO: this should declare that it implements the
# opt out backend interface
implements(IOptOutBackend)

def __init__(self, riak_manager):
self.riak_manager = riak_manager
Expand All @@ -39,8 +41,7 @@ class RiakOptOutCollection(object):
The opt out store to provide access to.
"""

# TODO: this should declare that it implements the
# opt out collection interface
implements(IOptOutCollection)

def __init__(self, opt_out_store):
self.store = opt_out_store
Expand All @@ -63,24 +64,33 @@ def _opt_out_to_dict(cls, opt_out):
opt_out.get_data(), OptOut.field_descriptors.keys())

@inlineCallbacks
def get_opt_out(self, addresstype, address):
def get(self, addresstype, address):
opt_out = yield self.store.get_opt_out(addresstype, address)
if opt_out is None:
returnValue(None)
returnValue(self._opt_out_to_dict(opt_out))

@inlineCallbacks
def save_opt_out(self, addresstype, address):
def put(self, addresstype, address):
opt_out = yield self.store.new_opt_out(
addresstype, address, message={
'message_id': None, # TODO: fix opt out store
# TODO: Fix the Vumi Go opt out store to allow descriptions
# of why an address was opted out. Currently the only
# description allowed is a Vumi message id. :|
'message_id': None,
})
returnValue(self._opt_out_to_dict(opt_out))

@inlineCallbacks
def delete_opt_out(self, addresstype, address):
def delete(self, addresstype, address):
opt_out = yield self.store.get_opt_out(addresstype, address)
if opt_out is None:
returnValue(None)
opt_out_dict = self._opt_out_to_dict(opt_out)
yield opt_out.delete()
returnValue(opt_out)
returnValue(opt_out_dict)

@inlineCallbacks
def count(self):
count = yield self.store.count()
returnValue(count)
46 changes: 33 additions & 13 deletions go_optouts/store/tests/test_memory.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
"""
Tests for OptOutMemory.
Tests for opt_out_http_api.store.memory.
"""
from zope.interface.verify import verifyClass, verifyObject
from go_optouts.store.interface import IOptOutStore
from go_optouts.store.memory import OptOutMemory
from go_optouts.store.interface import (
IOptOutBackend, IOptOutCollection)
from go_optouts.store.memory import (
MemoryOptOutBackend, MemoryOptOutCollection)
from twisted.trial.unittest import TestCase


class TestMemory(TestCase):
class TestMemoryOptOutBackend(TestCase):
def mk_backend(self):
return MemoryOptOutBackend()

def test_class_iface(self):
self.assertTrue(verifyClass(IOptOutBackend, MemoryOptOutBackend))

def test_instance_iface(self):
backend = self.mk_backend()
self.assertTrue(verifyObject(IOptOutBackend, backend))

def test_get_opt_out_collection(self):
backend = self.mk_backend()
collection = backend.get_opt_out_collection("owner-1")
self.assertTrue(isinstance(collection, MemoryOptOutCollection))


class TestMemoryOptOutCollection(TestCase):
def test_setup_class_iface(self):
self.assertTrue(verifyClass(IOptOutStore, OptOutMemory))
self.assertTrue(verifyClass(IOptOutCollection, MemoryOptOutCollection))

def test_setup_instance_iface(self):
self.assertTrue(verifyObject(IOptOutStore, OptOutMemory()))
collection = MemoryOptOutCollection()
self.assertTrue(verifyObject(IOptOutCollection, collection))

def test_put_and_get(self):
store = OptOutMemory()
store = MemoryOptOutCollection()
opt1 = store.put("twitter_handle", "@trevor")
self.assertEqual(len(opt1["id"]), 36) # length of uuid-4 string
self.assertEqual(opt1, {
Expand All @@ -31,17 +51,17 @@ def test_put_and_get(self):
})

def test_get_missing(self):
store = OptOutMemory()
store = MemoryOptOutCollection()
opt3 = store.get("mxit", "praekelt_mxit")
self.assertEqual(None, opt3)

def test_delete_missing(self):
store = OptOutMemory()
store = MemoryOptOutCollection()
opt_out_delete = store.delete("twitter_handle", "@trevor")
self.assertEqual(None, opt_out_delete)

def test_put_and_delete(self):
store = OptOutMemory()
store = MemoryOptOutCollection()
opt_put = store.put("facebook", "trevor_fb")
self.assertEqual(len(opt_put["id"]), 36)
self.assertEqual(opt_put, {
Expand All @@ -61,20 +81,20 @@ def test_put_and_delete(self):
self.assertEqual(opt_out_get, None)

def test_count_zero(self):
store = OptOutMemory()
store = MemoryOptOutCollection()
opt_count_zero = store.count()
self.assertEqual(opt_count_zero, 0)

def test_count_one(self):
store = OptOutMemory()
store = MemoryOptOutCollection()
opt_count_one = store.count()
self.assertEqual(opt_count_one, 0)
store.put("FB", "fb_PRP")
opt_count_one = store.count()
self.assertEqual(opt_count_one, 1)

def test_count_many(self):
store = OptOutMemory()
store = MemoryOptOutCollection()
opt_count = store.count()
self.assertEqual(opt_count, 0)
store.put("facebook", "trevor_fb")
Expand Down

0 comments on commit ec8f265

Please sign in to comment.