"""
Tests for riak contacts backend and collection.
"""

from twisted.internet.defer import inlineCallbacks, returnValue
from zope.interface.verify import verifyObject

from vumi.tests.helpers import VumiTestCase
from vumi.persist.txriak_manager import TxRiakManager

from go.vumitools.contact import ContactStore

from go_api.collections import ICollection

from go_contacts.backends.riak import (
    RiakContactsBackend, RiakContactsCollection)


@inlineCallbacks
def create_riak_manager(config, add_cleanup):
    manager = TxRiakManager.from_config(config)
    add_cleanup(manager.purge_all)
    yield manager.purge_all()
    returnValue(manager)


class TestRiakContactsBackend(VumiTestCase):
    @inlineCallbacks
    def mk_backend(self):
        manager = yield create_riak_manager(
            {'bucket_prefix': 'test.'}, self.add_cleanup)
        backend = RiakContactsBackend(manager)
        returnValue(backend)

    @inlineCallbacks
    def test_get_contacts_collection(self):
        backend = yield self.mk_backend()
        collection = backend.get_contact_collection("owner-1")
        self.assertEqual(collection.contact_store.user_account_key, "owner-1")
        self.assertTrue(isinstance(collection, RiakContactsCollection))


class TestRiakContactsCollection(VumiTestCase):
    @inlineCallbacks
    def mk_collection(self, owner_id):
        manager = yield create_riak_manager(
            {'bucket_prefix': 'test.'}, self.add_cleanup)
        contact_store = ContactStore(manager, owner_id)
        collection = RiakContactsCollection(contact_store)
        returnValue(collection)

    @inlineCallbacks
    def test_collection_provides_ICollection(self):
        """
        The return value of .get_row_collection() is an object that provides
        ICollection.
        """
        collection = yield self.mk_collection("owner-1")
        verifyObject(ICollection, collection)

    @inlineCallbacks
    def test_init(self):
        collection = yield self.mk_collection("owner-1")
        self.assertEqual(collection.contact_store.user_account_key, "owner-1")

    @inlineCallbacks
    def test_get_non_existent_contact(self):
        collection = yield self.mk_collection("owner-1")
        contact = yield collection.get("bad-contact-id")
        self.assertEqual(contact, None)

@@ -50,16 +50,47 @@
 n.%0A%22%22%22%0A%0A
+from datetime import datetime%0A%0A
 from twi
@@ -1639,483 +1639,1774 @@
     
-@inlineCallbacks%0A    def test_collection_provides_ICollection(self):%0A        %22%22%22%0A        The return value of .get_row_collection() is an object that provides%0A        ICollection.%0A        %22%22%22%0A        collection = yield self.mk_collection(%22owner-1%22)%0A        verifyObject(ICollection, collection)%0A%0A    @inlineCallbacks%0A    def test_init(self):%0A        collection = yield self.mk_collection(%22owner-1%22)%0A        self.assertEqual(collection.contact_store.user_account_key, %22owner-1%22
+EXPECTED_DATE_FORMAT = %22%25Y-%25m-%25d %25H:%25M:%25S.%25f%22%0A%0A    CONTACT_FIELD_DEFAULTS = %7B%0A        u'$VERSION': 2,%0A        u'bbm_pin': None,%0A        u'dob': None,%0A        u'email_address': None,%0A        u'facebook_id': None,%0A        u'groups': %5B%5D,%0A        u'gtalk_id': None,%0A        u'mxit_id': None,%0A        u'name': None,%0A        u'surname': None,%0A        u'twitter_handle': None,%0A        u'wechat_id': None,%0A    %7D%0A%0A    def assert_contact(self, contact, expected_partial):%0A        expected = self.CONTACT_FIELD_DEFAULTS.copy()%0A        expected.update(expected_partial)%0A        if isinstance(expected.get(%22created_at%22), datetime):%0A            expected%5B%22created_at%22%5D = expected%5B%22created_at%22%5D.strftime(%0A                self.EXPECTED_DATE_FORMAT)%0A        self.assertEqual(contact, expected)%0A%0A    @inlineCallbacks%0A    def test_collection_provides_ICollection(self):%0A        %22%22%22%0A        The return value of .get_row_collection() is an object that provides%0A        ICollection.%0A        %22%22%22%0A        collection = yield self.mk_collection(%22owner-1%22)%0A        verifyObject(ICollection, collection)%0A%0A    @inlineCallbacks%0A    def test_init(self):%0A        collection = yield self.mk_collection(%22owner-1%22)%0A        self.assertEqual(collection.contact_store.user_account_key, %22owner-1%22)%0A%0A    @inlineCallbacks%0A    def test_get(self):%0A        collection = yield self.mk_collection(%22owner-1%22)%0A        new_contact = yield collection.contact_store.new_contact(%0A            name=u%22Bob%22, msisdn=u%22+12345%22)%0A        contact = yield collection.get(new_contact.key)%0A        self.assert_contact(contact, %7B%0A            u'key': new_contact.key,%0A            u'created_at': new_contact.created_at,%0A            u'msisdn': u'+12345',%0A            u'name': u'Bob',%0A            u'user_account': u'owner-1',%0A        %7D
 )%0A%0A 

Add test for retrieving a contact.