Beispiel #1
0
    def test_can_save_a_member4(self):
        mbr = Member(
            self.member.account,
            {
                'member_id': 200,
                'email':u"*****@*****.**",
                'first_name':u"Emma",
                'member_status_id': MemberStatus.Active
            })
        MockAdapter.expected = False

        with self.assertRaises(ex.MemberUpdateError):
            mbr.save()
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
            (
                'PUT',
                '/members/200',
                {
                    'member_id': 200,
                    'email':u"*****@*****.**",
                    'fields': {'first_name': u"Emma"},
                    'status_to': mbr['member_status_id']
                }
            ))
Beispiel #2
0
    def test_can_delete_a_member(self):
        mbr = Member(self.member.account, {'email':u"*****@*****.**"})

        with self.assertRaises(ex.NoMemberIdError):
            mbr.delete()
        self.assertEquals(self.member.account.adapter.called, 0)
        self.assertFalse(self.member.is_deleted())
Beispiel #3
0
    def test_can_save_a_member6(self):
        mbr = Member(
            self.member.account,
            {
                'email':u"*****@*****.**",
                'fields': {'first_name':u"Emma"}
            })
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }

        result = mbr.save(group_ids=[1025])

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
            (
                'POST',
                '/members/add',
                {
                    'email':u"*****@*****.**",
                    'fields': {'first_name': u"Emma"},
                    'group_ids': [1025]
                }
            ))
Beispiel #4
0
    def test_can_save_a_member6(self):
        mbr = Member(
            self.member.account,
            {
                'email':"*****@*****.**",
                'fields': {'first_name':"Emma"}
            })
        MockAdapter.expected = {
            'status': "a",
            'added': True,
            'member_id': 1024
        }

        result = mbr.save(group_ids=[1025])

        self.assertIsNone(result)
        self.assertEqual(mbr.account.adapter.called, 1)
        self.assertEqual(mbr.account.adapter.call,
            (
                'POST',
                '/members/add',
                {
                    'email':"*****@*****.**",
                    'fields': {'first_name': "Emma"},
                    'group_ids': [1025]
                }
            ))
Beispiel #5
0
    def test_can_save_a_member4(self):
        mbr = Member(
            self.member.account,
            {
                'member_id': 200,
                'email':"*****@*****.**",
                'first_name':"Emma",
                'member_status_id': MemberStatus.Active
            })
        MockAdapter.expected = False

        with self.assertRaises(ex.MemberUpdateError):
            mbr.save()
        self.assertEqual(mbr.account.adapter.called, 1)
        self.assertEqual(mbr.account.adapter.call,
            (
                'PUT',
                '/members/200',
                {
                    'member_id': 200,
                    'email':"*****@*****.**",
                    'fields': {'first_name': "Emma"},
                    'status_to': mbr['member_status_id']
                }
            ))
Beispiel #6
0
    def test_can_delete_a_member(self):
        mbr = Member(self.member.account, {'email': u"*****@*****.**"})

        with self.assertRaises(ex.NoMemberIdError):
            mbr.delete()
        self.assertEquals(self.member.account.adapter.called, 0)
        self.assertFalse(self.member.is_deleted())
Beispiel #7
0
 def setUp(self):
     Account.default_adapter = MockAdapter
     self.member = Member(
         Account(account_id="100", public_key="xxx", private_key="yyy"), {
             'member_id':
             1000,
             'email':
             u"*****@*****.**",
             'status':
             u"opt-out",
             'member_status_id':
             u"o",
             'change_type':
             u"u",
             'last_modified_at':
             datetime.now().strftime(SERIALIZED_DATETIME_FORMAT),
             'member_since':
             datetime.now().strftime(SERIALIZED_DATETIME_FORMAT),
             'deleted_at':
             None
         })
     self.member.account.fields._dict = {
         2000: {
             'shortcut_name': u"first_name"
         },
         2001: {
             'shortcut_name': u"last_name"
         }
     }
Beispiel #8
0
 def setUp(self):
     Account.default_adapter = MockAdapter
     self.mailings = Member(
         Account(account_id="100", public_key="xxx", private_key="yyy"), {
             'member_id': 1000,
             'email': u"*****@*****.**",
             'status': u"opt-out"
         }).mailings
Beispiel #9
0
 def test_can_ask_if_member_has_opted_out2(self):
     member = Member(
         self.member.account, {
             'member_id': 1000,
             'email': u"*****@*****.**",
             'member_status_id': u"active"
         })
     has_opted_out = member.has_opted_out()
     self.assertIsInstance(has_opted_out, bool)
     self.assertFalse(has_opted_out)
     self.assertEquals(member.account.adapter.called, 0)
Beispiel #10
0
 def test_can_ask_if_member_has_opted_out2(self):
     member = Member(
         self.member.account,
         {
             'member_id':1000,
             'email':u"*****@*****.**",
             'member_status_id':u"active"
         }
     )
     has_opted_out = member.has_opted_out()
     self.assertIsInstance(has_opted_out, bool)
     self.assertFalse(has_opted_out)
     self.assertEquals(member.account.adapter.called, 0)
Beispiel #11
0
    def test_can_delete_a_member2(self):
        MockAdapter.expected = None
        mbr = Member(
            self.member.account,
            {
                'member_id': 200,
                'email':u"*****@*****.**",
                'deleted_at': datetime.now().strftime(SERIALIZED_DATETIME_FORMAT)
            })

        result = mbr.delete()

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 0)
        self.assertTrue(mbr.is_deleted())
Beispiel #12
0
 def test_can_save_a_member(self):
     mbr = Member(self.member.account, {'email':"*****@*****.**"})
     MockAdapter.expected = {
         'status': "a",
         'added': True,
         'member_id': 1024
     }
     result = mbr.save()
     self.assertIsNone(result)
     self.assertEqual(mbr.account.adapter.called, 1)
     self.assertEqual(
         mbr.account.adapter.call,
         ('POST', '/members/add', {'email':"*****@*****.**"}))
     self.assertEqual(1024, mbr['member_id'])
     self.assertEqual(MemberStatus.Active, mbr['member_status_id'])
Beispiel #13
0
    def test_can_delete_a_member3(self):
        MockAdapter.expected = True
        mbr = Member(self.member.account, {
            'member_id': 200,
            'email': u"*****@*****.**",
            'deleted_at': None
        })

        result = mbr.delete()

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
                          ('DELETE', '/members/200', {}))
        self.assertTrue(mbr.is_deleted())
Beispiel #14
0
    def test_can_delete_a_member2(self):
        MockAdapter.expected = None
        mbr = Member(
            self.member.account, {
                'member_id': 200,
                'email': u"*****@*****.**",
                'deleted_at':
                datetime.now().strftime(SERIALIZED_DATETIME_FORMAT)
            })

        result = mbr.delete()

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 0)
        self.assertTrue(mbr.is_deleted())
Beispiel #15
0
 def test_can_save_a_member(self):
     mbr = Member(self.member.account, {'email':u"*****@*****.**"})
     MockAdapter.expected = {
         'status': u"a",
         'added': True,
         'member_id': 1024
     }
     result = mbr.save()
     self.assertIsNone(result)
     self.assertEquals(mbr.account.adapter.called, 1)
     self.assertEquals(
         mbr.account.adapter.call,
         ('POST', '/members/add', {'email':u"*****@*****.**"}))
     self.assertEquals(1024, mbr['member_id'])
     self.assertEquals(MemberStatus.Active, mbr['member_status_id'])
Beispiel #16
0
    def find_one_by_member_id(self, member_id, deleted=False):
        """
        Lazy-loads a single :class:`Member` by ID

        :param member_id: The member identifier
        :type member_id: :class:`int`
        :param deleted: Whether to include deleted members
        :type deleted: :class:`bool`
        :rtype: :class:`Member` or :class:`None`

        Usage::

            >>> from emma.model.account import Account
            >>> acct = Account(1234, "08192a3b4c5d6e7f", "f7e6d5c4b3a29180")
            >>> acct.members.find_one_by_member_id(0) # does not exist
            raises <KeyError>
            >>> acct.members.find_one_by_member_id(123)
            <Member{'member_id': 123, 'email': u"*****@*****.**", ...}>
            >>> acct.members[123]
            <Member{'member_id': 123, 'email': u"*****@*****.**", ...}>
        """
        member_id = int(member_id)
        path = '/members/%s' % member_id
        params = {"deleted": True} if deleted else {}
        if member_id not in self._dict:
            raw = self.account.adapter.get(path, params)
            if raw:
                self._dict[member_id] = Member(self.account, raw)

        return (member_id in self._dict) and self._dict[member_id] or None
Beispiel #17
0
 def test_can_opt_out_a_member2(self):
     member = Member(
         self.member.account, {
             'member_id': 1000,
             'email': u"*****@*****.**",
             'member_status_id': u"a"
         })
     MockAdapter.expected = True
     self.assertFalse(member.has_opted_out())
     result = member.opt_out()
     self.assertIsNone(result)
     self.assertEquals(member.account.adapter.called, 1)
     self.assertEquals(
         member.account.adapter.call,
         ('PUT', '/members/email/optout/[email protected]', {}))
     self.assertTrue(member.has_opted_out())
Beispiel #18
0
    def find_one_by_email(self, email, deleted=False):
        """
        Lazy-loads a single :class:`Member` by email address

        :param email: The email address
        :type email: :class:`str`
        :param deleted: Whether to include deleted members
        :type deleted: :class:`bool`
        :rtype: :class:`Member` or :class:`None`

        Usage::

            >>> from emma.model.account import Account
            >>> acct = Account(1234, "08192a3b4c5d6e7f", "f7e6d5c4b3a29180")
            >>> acct.members.find_one_by_email("*****@*****.**") # does not exist
            raises <KeyError>
            >>> acct.members.find_one_by_email("*****@*****.**")
            <Member{'member_id': 123, 'email': u"*****@*****.**", ...}>
            >>> acct.members["*****@*****.**"]
            <Member{'member_id': 123, 'email': u"*****@*****.**", ...}>
        """
        path = '/members/email/%s' % email
        params = {"deleted": True} if deleted else {}
        members = [x for x in self._dict.values() if x['email'] == email]
        if not members:
            member = self.account.adapter.get(path, params)
            if member is not None:
                self._dict[member['member_id']] = \
                    Member(self.account, member)
                return self._dict[member['member_id']]
        else:
            member = members[0]
        return member
Beispiel #19
0
    def test_can_add_groups_to_a_member(self):
        mbr = Member(self.groups.member.account)

        with self.assertRaises(ex.NoMemberIdError):
            mbr.groups.save([
                mbr.groups.factory({'member_group_id':1024})
            ])
        self.assertEqual(self.groups.member.account.adapter.called, 0)
Beispiel #20
0
 def test_can_opt_out_a_member2(self):
     member = Member(
         self.member.account,
         {
             'member_id':1000,
             'email':u"*****@*****.**",
             'member_status_id':u"a"
         }
     )
     MockAdapter.expected = True
     self.assertFalse(member.has_opted_out())
     result = member.opt_out()
     self.assertIsNone(result)
     self.assertEquals(member.account.adapter.called, 1)
     self.assertEquals(
         member.account.adapter.call,
         ('PUT', '/members/email/optout/[email protected]', {}))
     self.assertTrue(member.has_opted_out())
Beispiel #21
0
    def test_can_delete_a_member3(self):
        MockAdapter.expected = True
        mbr = Member(
            self.member.account,
            {
                'member_id': 200,
                'email':u"*****@*****.**",
                'deleted_at': None
            })

        result = mbr.delete()

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(
            mbr.account.adapter.call,
            ('DELETE', '/members/200', {}))
        self.assertTrue(mbr.is_deleted())
Beispiel #22
0
 def setUp(self):
     Account.default_adapter = MockAdapter
     self.mailings =  Member(
         Account(account_id="100", public_key="xxx", private_key="yyy"),
         {
             'member_id':1000,
             'email':u"*****@*****.**",
             'status':u"opt-out"
         }
     ).mailings
Beispiel #23
0
 def test_can_save_a_member3(self):
     mbr = Member(
         self.member.account,
         {'email':u"*****@*****.**",
          'first_name':u"Emma"})
     MockAdapter.expected = {
         'status': u"a",
         'added': True,
         'member_id': 1024
     }
     result = mbr.save(signup_form_id=u"http://example.com/signup")
     self.assertIsNone(result)
     self.assertEquals(mbr.account.adapter.called, 1)
     self.assertEquals(
         mbr.account.adapter.call,
         ('POST', '/members/add', {
             'email':u"*****@*****.**",
             'fields': {'first_name': u"Emma"},
             'signup_form_id': u"http://example.com/signup"}
         ))
     self.assertEquals(1024, mbr['member_id'])
     self.assertEquals(MemberStatus.Active, mbr['member_status_id'])
Beispiel #24
0
 def test_can_save_a_member3(self):
     mbr = Member(
         self.member.account,
         {'email':"*****@*****.**",
          'first_name':"Emma"})
     MockAdapter.expected = {
         'status': "a",
         'added': True,
         'member_id': 1024
     }
     result = mbr.save(signup_form_id="http://example.com/signup")
     self.assertIsNone(result)
     self.assertEqual(mbr.account.adapter.called, 1)
     self.assertEqual(
         mbr.account.adapter.call,
         ('POST', '/members/add', {
             'email':"*****@*****.**",
             'fields': {'first_name': "Emma"},
             'signup_form_id': "http://example.com/signup"}
         ))
     self.assertEqual(1024, mbr['member_id'])
     self.assertEqual(MemberStatus.Active, mbr['member_status_id'])
Beispiel #25
0
 def test_can_save_a_member5(self):
     mbr = Member(
         self.member.account, {
             'member_id': 200,
             'email': u"*****@*****.**",
             'fields': {
                 'first_name': u"Emma"
             },
             'member_status_id': MemberStatus.Active
         })
     MockAdapter.expected = True
     result = mbr.save()
     self.assertIsNone(result)
     self.assertEquals(mbr.account.adapter.called, 1)
     self.assertEquals(mbr.account.adapter.call,
                       ('PUT', '/members/200', {
                           'member_id': 200,
                           'email': u"*****@*****.**",
                           'fields': {
                               'first_name': u"Emma"
                           },
                           'status_to': mbr['member_status_id']
                       }))
Beispiel #26
0
 def test_can_save_a_member5(self):
     mbr = Member(
         self.member.account,
         {
             'member_id': 200,
             'email':u"*****@*****.**",
             'fields': {'first_name':u"Emma"},
             'member_status_id': MemberStatus.Active
         })
     MockAdapter.expected = True
     result = mbr.save()
     self.assertIsNone(result)
     self.assertEquals(mbr.account.adapter.called, 1)
     self.assertEquals(mbr.account.adapter.call,
         (
             'PUT',
             '/members/200',
             {
                 'member_id': 200,
                 'email':u"*****@*****.**",
                 'fields': {'first_name': u"Emma"},
                 'status_to': mbr['member_status_id']
             }
         ))
Beispiel #27
0
class MemberMailingCollectionTest(unittest.TestCase):
    def setUp(self):
        Account.default_adapter = MockAdapter
        self.mailings =  Member(
            Account(account_id="100", public_key="xxx", private_key="yyy"),
            {
                'member_id':1000,
                'email':u"*****@*****.**",
                'status':u"opt-out"
            }
        ).mailings

    def test_fetch_all_returns_a_dictionary(self):
        member = Member(self.mailings.member.account)
        mailings = MemberMailingCollection(member)
        with self.assertRaises(ex.NoMemberIdError):
            mailings.fetch_all()
        self.assertEquals(mailings.member.account.adapter.called, 0)

    def test_fetch_all_returns_a_dictionary2(self):
        MockAdapter.expected = [{'mailing_id': 201, 'delivery_type':u"d"}]
        self.assertIsInstance(self.mailings.fetch_all(), dict)
        self.assertEquals(self.mailings.member.account.adapter.called, 1)
        self.assertEquals(
            self.mailings.member.account.adapter.call,
            ('GET', '/members/1000/mailings', {}))

    def test_fetch_all_populates_collection(self):
        MockAdapter.expected = [{'mailing_id': 201, 'delivery_type':u"d"}]
        self.assertEquals(0, len(self.mailings))
        self.mailings.fetch_all()
        self.assertEquals(1, len(self.mailings))

    def test_fetch_all_caches_results(self):
        MockAdapter.expected = [{'mailing_id': 201, 'delivery_type':u"d"}]
        self.mailings.fetch_all()
        self.mailings.fetch_all()
        self.assertEquals(self.mailings.member.account.adapter.called, 1)

    def test_collection_can_be_accessed_like_a_dictionary(self):
        MockAdapter.expected = [{'mailing_id': 201, 'delivery_type':u"d"}]
        self.mailings.fetch_all()
        self.assertIsInstance(self.mailings, MemberMailingCollection)
        self.assertEquals(1, len(self.mailings))
        self.assertIsInstance(self.mailings[201], Mailing)
        self.assertEquals(self.mailings[201]['mailing_id'], 201)
        self.assertEquals(
            self.mailings[201]['delivery_type'],
            DeliveryType.Delivered)
Beispiel #28
0
class MemberMailingCollectionTest(unittest.TestCase):
    def setUp(self):
        Account.default_adapter = MockAdapter
        self.mailings =  Member(
            Account(account_id="100", public_key="xxx", private_key="yyy"),
            {
                'member_id':1000,
                'email':"*****@*****.**",
                'status':"opt-out"
            }
        ).mailings

    def test_fetch_all_returns_a_dictionary(self):
        member = Member(self.mailings.member.account)
        mailings = MemberMailingCollection(member)
        with self.assertRaises(ex.NoMemberIdError):
            mailings.fetch_all()
        self.assertEqual(mailings.member.account.adapter.called, 0)

    def test_fetch_all_returns_a_dictionary2(self):
        MockAdapter.expected = [{'mailing_id': 201, 'delivery_type':"d"}]
        self.assertIsInstance(self.mailings.fetch_all(), dict)
        self.assertEqual(self.mailings.member.account.adapter.called, 1)
        self.assertEqual(
            self.mailings.member.account.adapter.call,
            ('GET', '/members/1000/mailings', {}))

    def test_fetch_all_populates_collection(self):
        MockAdapter.expected = [{'mailing_id': 201, 'delivery_type':"d"}]
        self.assertEqual(0, len(self.mailings))
        self.mailings.fetch_all()
        self.assertEqual(1, len(self.mailings))

    def test_fetch_all_caches_results(self):
        MockAdapter.expected = [{'mailing_id': 201, 'delivery_type':"d"}]
        self.mailings.fetch_all()
        self.mailings.fetch_all()
        self.assertEqual(self.mailings.member.account.adapter.called, 1)

    def test_collection_can_be_accessed_like_a_dictionary(self):
        MockAdapter.expected = [{'mailing_id': 201, 'delivery_type':"d"}]
        self.mailings.fetch_all()
        self.assertIsInstance(self.mailings, MemberMailingCollection)
        self.assertEqual(1, len(self.mailings))
        self.assertIsInstance(self.mailings[201], Mailing)
        self.assertEqual(self.mailings[201]['mailing_id'], 201)
        self.assertEqual(
            self.mailings[201]['delivery_type'],
            DeliveryType.Delivered)
Beispiel #29
0
    def factory(self, raw=None):
        """
        New :class:`Member` factory

        :param raw: Raw data with which to populate class
        :type raw: :class:`dict`
        :rtype: :class:`Member`

        Usage::

            >>> from emma.model.account import Account
            >>> acct = Account(1234, "08192a3b4c5d6e7f", "f7e6d5c4b3a29180")
            >>> acct.members.factory()
            <Member{}>
            >>> acct.members.factory({'email': u"*****@*****.**"})
            <Member{'email': u"*****@*****.**"}>
        """
        return Member(self.account, raw)
Beispiel #30
0
 def setUp(self):
     Account.default_adapter = MockAdapter
     self.member = Member(
         Account(account_id="100", public_key="xxx", private_key="yyy"),
         {
             'member_id':1000,
             'email':u"*****@*****.**",
             'status':u"opt-out",
             'member_status_id':u"o",
             'change_type':u"u",
             'last_modified_at': datetime.now().strftime(SERIALIZED_DATETIME_FORMAT),
             'member_since': datetime.now().strftime(SERIALIZED_DATETIME_FORMAT),
             'deleted_at': None
         }
     )
     self.member.account.fields._dict = {
         2000: {'shortcut_name': u"first_name"},
         2001: {'shortcut_name': u"last_name"}
     }
Beispiel #31
0
    def fetch_all_by_import_id(self, import_id):
        """
        Updates the collection with a dictionary of all members from a given
        import. *Does not lazy-load*

        :param import_id: The import identifier
        :type import_id: :class:`int` or :class:`str`
        :rtype: :class:`dict` of :class:`Member` objects

        Usage::

            >>> from emma.model.account import Account
            >>> acct = Account(1234, "08192a3b4c5d6e7f", "f7e6d5c4b3a29180")
            >>> acct.members.fetch_all_by_import_id(123)
            {123: <Member>, 321: <Member>, ...}
        """
        path = '/members/imports/%s/members' % import_id
        members = dict((x['member_id'], Member(self.account, x))
                       for x in self.account.adapter.get(path))
        self._replace_all(members)
        return members
Beispiel #32
0
    def fetch_all(self, deleted=False):
        """
        Lazy-loads the full set of :class:`Member` objects

        :param deleted: Whether to include deleted members
        :type deleted: :class:`bool`
        :rtype: :class:`dict` of :class:`Member` objects

        Usage::

            >>> from emma.model.account import Account
            >>> acct = Account(1234, "08192a3b4c5d6e7f", "f7e6d5c4b3a29180")
            >>> acct.members.fetch_all()
            {123: <Member>, 321: <Member>, ...}
        """
        path = '/members'
        params = {"deleted": True} if deleted else {}
        if not self._dict:
            self._dict = dict(
                (x['member_id'], Member(self.account, x))
                for x in self.account.adapter.paginated_get(path, params))
        return self._dict
Beispiel #33
0
    def fetch_all(self):
        """
        Lazy-loads the full set of :class:`Member` objects

        :rtype: :class:`dict` of :class:`Member` objects

        Usage::

            >>> from emma.model.account import Account
            >>> acct = Account(1234, "08192a3b4c5d6e7f", "f7e6d5c4b3a29180")
            >>> imprt = acct.imports[1024]
            >>> imprt.members.fetch_all()
            {200: <Member>, 201: <Member>, ...}
        """
        if not 'import_id' in self.member_import:
            raise ex.NoImportIdError()

        path = '/members/imports/%s/members' % self.member_import['import_id']
        if not self._dict:
            self._dict = dict(
                (x['member_id'], Member(self.member_import.account, x))
                for x in self.member_import.account.adapter.paginated_get(
                    path))
        return self._dict
Beispiel #34
0
 def test_can_get_opt_out_detail_for_member2(self):
     MockAdapter.expected = []
     member = Member(self.member.account)
     with self.assertRaises(ex.NoMemberIdError):
         member.get_opt_out_detail()
     self.assertEquals(member.account.adapter.called, 0)
Beispiel #35
0
class MemberGroupCollectionTest(unittest.TestCase):
    def setUp(self):
        Account.default_adapter = MockAdapter
        self.groups =  Member(
            Account(account_id="100", public_key="xxx", private_key="yyy"),
            {
                'member_id':1000,
                'email':u"*****@*****.**",
                'status':u"opt-out"
            }
        ).groups

    def test_fetch_all_returns_a_dictionary(self):
        groups = MemberGroupCollection(Member(self.groups.member.account))
        with self.assertRaises(ex.NoMemberIdError):
            groups.fetch_all()
        self.assertEquals(groups.member.account.adapter.called, 0)

    def test_fetch_all_returns_a_dictionary2(self):
        MockAdapter.expected = [{'member_group_id': 200}]
        self.assertIsInstance(self.groups.fetch_all(), dict)
        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            ('GET', '/members/1000/groups', {}))

    def test_fetch_all_populates_collection(self):
        MockAdapter.expected = [{'member_group_id': 200}]
        self.assertEquals(0, len(self.groups))
        self.groups.fetch_all()
        self.assertEquals(1, len(self.groups))

    def test_fetch_all_caches_results(self):
        MockAdapter.expected = [{'member_group_id': 200}]
        self.groups.fetch_all()
        self.groups.fetch_all()
        self.assertEquals(self.groups.member.account.adapter.called, 1)

    def test_collection_can_be_accessed_like_a_dictionary(self):
        MockAdapter.expected = [{'member_group_id': 200}]
        self.groups.fetch_all()
        self.assertIsInstance(self.groups, MemberGroupCollection)
        self.assertEquals(1, len(self.groups))
        self.assertIsInstance(self.groups[200], Group)

    def test_factory_produces_a_group(self):
        grp = self.groups.factory()
        self.assertIsInstance(grp, Group)
        self.assertEquals(0, len(grp))

    def test_factory_produces_a_group2(self):
        grp = self.groups.factory({'member_group_id':1024})
        self.assertIsInstance(grp, Group)
        self.assertEquals(1, len(grp))
        self.assertEquals(1024, grp['member_group_id'])

    def test_can_add_groups_to_a_member(self):
        mbr = Member(self.groups.member.account)

        with self.assertRaises(ex.NoMemberIdError):
            mbr.groups.save([
                mbr.groups.factory({'member_group_id':1024})
            ])
        self.assertEquals(self.groups.member.account.adapter.called, 0)

    def test_can_add_groups_to_a_member2(self):
        MockAdapter.expected = []
        self.groups.save([])
        self.assertEquals(self.groups.member.account.adapter.called, 0)

    def test_can_add_groups_to_a_member3(self):
        MockAdapter.expected = [300, 301]
        self.groups.save([
            self.groups.factory({'member_group_id': 300}),
            self.groups.factory({'member_group_id': 301})
        ])
        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            ('PUT', '/members/1000/groups', {'group_ids': [300, 301]}))

    def test_can_add_groups_to_a_member4(self):
        MockAdapter.expected = [300, 301]
        self.groups.member.add_groups([300, 301])
        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            ('PUT', '/members/1000/groups', {'group_ids': [300, 301]}))

    def test_can_drop_groups_from_a_member(self):
        mbr = Member(self.groups.member.account)

        with self.assertRaises(ex.NoMemberIdError):
            mbr.groups.delete([300, 301])
        self.assertEquals(self.groups.member.account.adapter.called, 0)

    def test_can_drop_groups_from_a_member2(self):
        self.groups.delete()

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            ('DELETE', '/members/1000/groups', {})
        )
        self.assertEquals(0, len(self.groups))

    def test_can_drop_groups_from_a_member3(self):
        MockAdapter.expected = [300, 301]
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301}),
            302: self.groups.factory({'member_group_id': 302})
        }

        self.groups.delete([300, 301])

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            (
                'PUT',
                '/members/1000/groups/remove',
                {'group_ids': [300, 301]}))
        self.assertEquals(1, len(self.groups))
        self.assertIsInstance(self.groups[302], Group)

    def test_can_drop_groups_from_a_member4(self):
        MockAdapter.expected = [300, 301]
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301}),
            302: self.groups.factory({'member_group_id': 302})
        }

        self.groups.member.drop_groups([300, 301])

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            (
                'PUT',
                '/members/1000/groups/remove',
                {'group_ids': [300, 301]}))
        self.assertEquals(1, len(self.groups))
        self.assertIsInstance(self.groups[302], Group)

    def test_can_drop_groups_from_a_member5(self):
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301}),
            302: self.groups.factory({'member_group_id': 302})
        }

        self.groups.delete()

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            ('DELETE', '/members/1000/groups', {})
        )
        self.assertEquals(0, len(self.groups))

    def test_can_drop_groups_from_a_member6(self):
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301}),
            302: self.groups.factory({'member_group_id': 302})
        }

        self.groups.member.drop_groups()

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            ('DELETE', '/members/1000/groups', {})
        )
        self.assertEquals(0, len(self.groups))

    def test_can_drop_a_single_group_with_del(self):
        # Setup
        MockAdapter.expected = True
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301})
        }

        del(self.groups[300])

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(
            self.groups.member.account.adapter.call,
            (
                'PUT',
                '/members/1000/groups/remove',
                {'group_ids': [300]}))
        self.assertEquals(1, len(self.groups))
        self.assertIn(301, self.groups)
Beispiel #36
0
class MemberTest(unittest.TestCase):
    def setUp(self):
        Account.default_adapter = MockAdapter
        self.member = Member(
            Account(account_id="100", public_key="xxx", private_key="yyy"),
            {
                'member_id':1000,
                'email':u"*****@*****.**",
                'status':u"opt-out",
                'member_status_id':u"o",
                'change_type':u"u",
                'last_modified_at': datetime.now().strftime(SERIALIZED_DATETIME_FORMAT),
                'member_since': datetime.now().strftime(SERIALIZED_DATETIME_FORMAT),
                'deleted_at': None
            }
        )
        self.member.account.fields._dict = {
            2000: {'shortcut_name': u"first_name"},
            2001: {'shortcut_name': u"last_name"}
        }

    def test_can_parse_special_fields_correctly(self):
        self.assertIsInstance(self.member['last_modified_at'], datetime)
        self.assertIsInstance(self.member['member_since'], datetime)
        self.assertIsNone(self.member.get('deleted_at'))

    def test_can_represent_a_member(self):
        self.assertEquals(
            u"<Member" + repr(self.member._dict) + u">",
            repr(self.member))

    def test_can_set_valid_field_value_with_dictionary_access(self):
        self.member['first_name'] = u"Emma"
        self.assertEquals(u"Emma", self.member['first_name'])

    def test_group_collection_can_be_accessed(self):
        self.assertIsInstance(self.member.groups, MemberGroupCollection)

    def test_mailing_collection_can_be_accessed(self):
        self.assertIsInstance(self.member.mailings, MemberMailingCollection)

    def test_can_get_opt_out_detail_for_member(self):
        MockAdapter.expected = []
        detail = self.member.get_opt_out_detail()
        self.assertIsInstance(detail, list)
        self.assertEquals(self.member.account.adapter.called, 1)

    def test_can_get_opt_out_detail_for_member2(self):
        MockAdapter.expected = []
        member = Member(self.member.account)
        with self.assertRaises(ex.NoMemberIdError):
            member.get_opt_out_detail()
        self.assertEquals(member.account.adapter.called, 0)

    def test_can_get_opt_out_detail_for_member3(self):
        MockAdapter.expected = []
        self.member['member_status_id'] = MemberStatus.Active
        self.member.get_opt_out_detail()
        self.assertEquals(self.member.account.adapter.called, 0)

    def test_can_ask_if_member_has_opted_out(self):
        has_opted_out = self.member.has_opted_out()
        self.assertIsInstance(has_opted_out, bool)
        self.assertTrue(has_opted_out)
        self.assertEquals(self.member.account.adapter.called, 0)

    def test_can_ask_if_member_has_opted_out2(self):
        member = Member(
            self.member.account,
            {
                'member_id':1000,
                'email':u"*****@*****.**",
                'member_status_id':u"active"
            }
        )
        has_opted_out = member.has_opted_out()
        self.assertIsInstance(has_opted_out, bool)
        self.assertFalse(has_opted_out)
        self.assertEquals(member.account.adapter.called, 0)

    def test_can_ask_if_member_has_opted_out3(self):
        member = Member(self.member.account)
        with self.assertRaises(ex.NoMemberStatusError):
            member.has_opted_out()
        self.assertEquals(member.account.adapter.called, 0)

    def test_can_opt_out_a_member(self):
        member = Member(self.member.account)
        with self.assertRaises(ex.NoMemberEmailError):
            member.opt_out()
        self.assertEquals(member.account.adapter.called, 0)

    def test_can_opt_out_a_member2(self):
        member = Member(
            self.member.account,
            {
                'member_id':1000,
                'email':u"*****@*****.**",
                'member_status_id':u"a"
            }
        )
        MockAdapter.expected = True
        self.assertFalse(member.has_opted_out())
        result = member.opt_out()
        self.assertIsNone(result)
        self.assertEquals(member.account.adapter.called, 1)
        self.assertEquals(
            member.account.adapter.call,
            ('PUT', '/members/email/optout/[email protected]', {}))
        self.assertTrue(member.has_opted_out())

    def test_can_save_a_member(self):
        mbr = Member(self.member.account, {'email':u"*****@*****.**"})
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }
        result = mbr.save()
        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(
            mbr.account.adapter.call,
            ('POST', '/members/add', {'email':u"*****@*****.**"}))
        self.assertEquals(1024, mbr['member_id'])
        self.assertEquals(MemberStatus.Active, mbr['member_status_id'])

    def test_can_save_a_member2(self):
        mbr = Member(
            self.member.account,
            {'email':u"*****@*****.**",
             'first_name':u"Emma"})
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }
        result = mbr.save()
        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(
            mbr.account.adapter.call,
            ('POST', '/members/add', {'email':u"*****@*****.**",
                                      'fields': {'first_name': u"Emma"}}))
        self.assertEquals(1024, mbr['member_id'])
        self.assertEquals(MemberStatus.Active, mbr['member_status_id'])

    def test_can_save_a_member3(self):
        mbr = Member(
            self.member.account,
            {'email':u"*****@*****.**",
             'first_name':u"Emma"})
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }
        result = mbr.save(signup_form_id=u"http://example.com/signup")
        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(
            mbr.account.adapter.call,
            ('POST', '/members/add', {
                'email':u"*****@*****.**",
                'fields': {'first_name': u"Emma"},
                'signup_form_id': u"http://example.com/signup"}
            ))
        self.assertEquals(1024, mbr['member_id'])
        self.assertEquals(MemberStatus.Active, mbr['member_status_id'])

    def test_can_save_a_member4(self):
        mbr = Member(
            self.member.account,
            {
                'member_id': 200,
                'email':u"*****@*****.**",
                'first_name':u"Emma",
                'member_status_id': MemberStatus.Active
            })
        MockAdapter.expected = False

        with self.assertRaises(ex.MemberUpdateError):
            mbr.save()
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
            (
                'PUT',
                '/members/200',
                {
                    'member_id': 200,
                    'email':u"*****@*****.**",
                    'fields': {'first_name': u"Emma"},
                    'status_to': mbr['member_status_id']
                }
            ))

    def test_can_save_a_member5(self):
        mbr = Member(
            self.member.account,
            {
                'member_id': 200,
                'email':u"*****@*****.**",
                'fields': {'first_name':u"Emma"},
                'member_status_id': MemberStatus.Active
            })
        MockAdapter.expected = True
        result = mbr.save()
        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
            (
                'PUT',
                '/members/200',
                {
                    'member_id': 200,
                    'email':u"*****@*****.**",
                    'fields': {'first_name': u"Emma"},
                    'status_to': mbr['member_status_id']
                }
            ))

    def test_can_save_a_member6(self):
        mbr = Member(
            self.member.account,
            {
                'email':u"*****@*****.**",
                'fields': {'first_name':u"Emma"}
            })
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }

        result = mbr.save(group_ids=[1025])

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
            (
                'POST',
                '/members/add',
                {
                    'email':u"*****@*****.**",
                    'fields': {'first_name': u"Emma"},
                    'group_ids': [1025]
                }
            ))

    def test_can_delete_a_member(self):
        mbr = Member(self.member.account, {'email':u"*****@*****.**"})

        with self.assertRaises(ex.NoMemberIdError):
            mbr.delete()
        self.assertEquals(self.member.account.adapter.called, 0)
        self.assertFalse(self.member.is_deleted())

    def test_can_delete_a_member2(self):
        MockAdapter.expected = None
        mbr = Member(
            self.member.account,
            {
                'member_id': 200,
                'email':u"*****@*****.**",
                'deleted_at': datetime.now().strftime(SERIALIZED_DATETIME_FORMAT)
            })

        result = mbr.delete()

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 0)
        self.assertTrue(mbr.is_deleted())

    def test_can_delete_a_member3(self):
        MockAdapter.expected = True
        mbr = Member(
            self.member.account,
            {
                'member_id': 200,
                'email':u"*****@*****.**",
                'deleted_at': None
            })

        result = mbr.delete()

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(
            mbr.account.adapter.call,
            ('DELETE', '/members/200', {}))
        self.assertTrue(mbr.is_deleted())
Beispiel #37
0
 def test_fetch_all_returns_a_dictionary(self):
     member = Member(self.mailings.member.account)
     mailings = MemberMailingCollection(member)
     with self.assertRaises(ex.NoMemberIdError):
         mailings.fetch_all()
     self.assertEquals(mailings.member.account.adapter.called, 0)
Beispiel #38
0
 def test_fetch_all_returns_a_dictionary(self):
     groups = MemberGroupCollection(Member(self.groups.member.account))
     with self.assertRaises(ex.NoMemberIdError):
         groups.fetch_all()
     self.assertEquals(groups.member.account.adapter.called, 0)
Beispiel #39
0
    def test_can_drop_groups_from_a_member(self):
        mbr = Member(self.groups.member.account)

        with self.assertRaises(ex.NoMemberIdError):
            mbr.groups.delete([300, 301])
        self.assertEquals(self.groups.member.account.adapter.called, 0)
Beispiel #40
0
 def test_can_get_opt_out_detail_for_member2(self):
     MockAdapter.expected = []
     member = Member(self.member.account)
     with self.assertRaises(ex.NoMemberIdError):
         member.get_opt_out_detail()
     self.assertEquals(member.account.adapter.called, 0)
Beispiel #41
0
class MemberGroupCollectionTest(unittest.TestCase):
    def setUp(self):
        Account.default_adapter = MockAdapter
        self.groups = Member(
            Account(account_id="100", public_key="xxx", private_key="yyy"), {
                'member_id': 1000,
                'email': u"*****@*****.**",
                'status': u"opt-out"
            }).groups

    def test_fetch_all_returns_a_dictionary(self):
        groups = MemberGroupCollection(Member(self.groups.member.account))
        with self.assertRaises(ex.NoMemberIdError):
            groups.fetch_all()
        self.assertEquals(groups.member.account.adapter.called, 0)

    def test_fetch_all_returns_a_dictionary2(self):
        MockAdapter.expected = [{'member_group_id': 200}]
        self.assertIsInstance(self.groups.fetch_all(), dict)
        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('GET', '/members/1000/groups', {}))

    def test_fetch_all_populates_collection(self):
        MockAdapter.expected = [{'member_group_id': 200}]
        self.assertEquals(0, len(self.groups))
        self.groups.fetch_all()
        self.assertEquals(1, len(self.groups))

    def test_fetch_all_caches_results(self):
        MockAdapter.expected = [{'member_group_id': 200}]
        self.groups.fetch_all()
        self.groups.fetch_all()
        self.assertEquals(self.groups.member.account.adapter.called, 1)

    def test_collection_can_be_accessed_like_a_dictionary(self):
        MockAdapter.expected = [{'member_group_id': 200}]
        self.groups.fetch_all()
        self.assertIsInstance(self.groups, MemberGroupCollection)
        self.assertEquals(1, len(self.groups))
        self.assertIsInstance(self.groups[200], Group)

    def test_factory_produces_a_group(self):
        grp = self.groups.factory()
        self.assertIsInstance(grp, Group)
        self.assertEquals(0, len(grp))

    def test_factory_produces_a_group2(self):
        grp = self.groups.factory({'member_group_id': 1024})
        self.assertIsInstance(grp, Group)
        self.assertEquals(1, len(grp))
        self.assertEquals(1024, grp['member_group_id'])

    def test_can_add_groups_to_a_member(self):
        mbr = Member(self.groups.member.account)

        with self.assertRaises(ex.NoMemberIdError):
            mbr.groups.save([mbr.groups.factory({'member_group_id': 1024})])
        self.assertEquals(self.groups.member.account.adapter.called, 0)

    def test_can_add_groups_to_a_member2(self):
        MockAdapter.expected = []
        self.groups.save([])
        self.assertEquals(self.groups.member.account.adapter.called, 0)

    def test_can_add_groups_to_a_member3(self):
        MockAdapter.expected = [300, 301]
        self.groups.save([
            self.groups.factory({'member_group_id': 300}),
            self.groups.factory({'member_group_id': 301})
        ])
        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('PUT', '/members/1000/groups', {
                              'group_ids': [300, 301]
                          }))

    def test_can_add_groups_to_a_member4(self):
        MockAdapter.expected = [300, 301]
        self.groups.member.add_groups([300, 301])
        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('PUT', '/members/1000/groups', {
                              'group_ids': [300, 301]
                          }))

    def test_can_drop_groups_from_a_member(self):
        mbr = Member(self.groups.member.account)

        with self.assertRaises(ex.NoMemberIdError):
            mbr.groups.delete([300, 301])
        self.assertEquals(self.groups.member.account.adapter.called, 0)

    def test_can_drop_groups_from_a_member2(self):
        self.groups.delete()

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('DELETE', '/members/1000/groups', {}))
        self.assertEquals(0, len(self.groups))

    def test_can_drop_groups_from_a_member3(self):
        MockAdapter.expected = [300, 301]
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301}),
            302: self.groups.factory({'member_group_id': 302})
        }

        self.groups.delete([300, 301])

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('PUT', '/members/1000/groups/remove', {
                              'group_ids': [300, 301]
                          }))
        self.assertEquals(1, len(self.groups))
        self.assertIsInstance(self.groups[302], Group)

    def test_can_drop_groups_from_a_member4(self):
        MockAdapter.expected = [300, 301]
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301}),
            302: self.groups.factory({'member_group_id': 302})
        }

        self.groups.member.drop_groups([300, 301])

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('PUT', '/members/1000/groups/remove', {
                              'group_ids': [300, 301]
                          }))
        self.assertEquals(1, len(self.groups))
        self.assertIsInstance(self.groups[302], Group)

    def test_can_drop_groups_from_a_member5(self):
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301}),
            302: self.groups.factory({'member_group_id': 302})
        }

        self.groups.delete()

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('DELETE', '/members/1000/groups', {}))
        self.assertEquals(0, len(self.groups))

    def test_can_drop_groups_from_a_member6(self):
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301}),
            302: self.groups.factory({'member_group_id': 302})
        }

        self.groups.member.drop_groups()

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('DELETE', '/members/1000/groups', {}))
        self.assertEquals(0, len(self.groups))

    def test_can_drop_a_single_group_with_del(self):
        # Setup
        MockAdapter.expected = True
        self.groups._dict = {
            300: self.groups.factory({'member_group_id': 300}),
            301: self.groups.factory({'member_group_id': 301})
        }

        del (self.groups[300])

        self.assertEquals(self.groups.member.account.adapter.called, 1)
        self.assertEquals(self.groups.member.account.adapter.call,
                          ('PUT', '/members/1000/groups/remove', {
                              'group_ids': [300]
                          }))
        self.assertEquals(1, len(self.groups))
        self.assertIn(301, self.groups)
Beispiel #42
0
 def test_can_ask_if_member_has_opted_out3(self):
     member = Member(self.member.account)
     with self.assertRaises(ex.NoMemberStatusError):
         member.has_opted_out()
     self.assertEquals(member.account.adapter.called, 0)
Beispiel #43
0
 def test_can_opt_out_a_member(self):
     member = Member(self.member.account)
     with self.assertRaises(ex.NoMemberEmailError):
         member.opt_out()
     self.assertEquals(member.account.adapter.called, 0)
Beispiel #44
0
 def test_can_ask_if_member_has_opted_out3(self):
     member = Member(self.member.account)
     with self.assertRaises(ex.NoMemberStatusError):
         member.has_opted_out()
     self.assertEquals(member.account.adapter.called, 0)
Beispiel #45
0
 def test_can_opt_out_a_member(self):
     member = Member(self.member.account)
     with self.assertRaises(ex.NoMemberEmailError):
         member.opt_out()
     self.assertEquals(member.account.adapter.called, 0)
Beispiel #46
0
class MemberTest(unittest.TestCase):
    def setUp(self):
        Account.default_adapter = MockAdapter
        self.member = Member(
            Account(account_id="100", public_key="xxx", private_key="yyy"), {
                'member_id':
                1000,
                'email':
                u"*****@*****.**",
                'status':
                u"opt-out",
                'member_status_id':
                u"o",
                'change_type':
                u"u",
                'last_modified_at':
                datetime.now().strftime(SERIALIZED_DATETIME_FORMAT),
                'member_since':
                datetime.now().strftime(SERIALIZED_DATETIME_FORMAT),
                'deleted_at':
                None
            })
        self.member.account.fields._dict = {
            2000: {
                'shortcut_name': u"first_name"
            },
            2001: {
                'shortcut_name': u"last_name"
            }
        }

    def test_can_parse_special_fields_correctly(self):
        self.assertIsInstance(self.member['last_modified_at'], datetime)
        self.assertIsInstance(self.member['member_since'], datetime)
        self.assertIsNone(self.member.get('deleted_at'))

    def test_can_represent_a_member(self):
        self.assertEquals(u"<Member" + repr(self.member._dict) + u">",
                          repr(self.member))

    def test_can_set_valid_field_value_with_dictionary_access(self):
        self.member['first_name'] = u"Emma"
        self.assertEquals(u"Emma", self.member['first_name'])

    def test_group_collection_can_be_accessed(self):
        self.assertIsInstance(self.member.groups, MemberGroupCollection)

    def test_mailing_collection_can_be_accessed(self):
        self.assertIsInstance(self.member.mailings, MemberMailingCollection)

    def test_can_get_opt_out_detail_for_member(self):
        MockAdapter.expected = []
        detail = self.member.get_opt_out_detail()
        self.assertIsInstance(detail, list)
        self.assertEquals(self.member.account.adapter.called, 1)

    def test_can_get_opt_out_detail_for_member2(self):
        MockAdapter.expected = []
        member = Member(self.member.account)
        with self.assertRaises(ex.NoMemberIdError):
            member.get_opt_out_detail()
        self.assertEquals(member.account.adapter.called, 0)

    def test_can_get_opt_out_detail_for_member3(self):
        MockAdapter.expected = []
        self.member['member_status_id'] = MemberStatus.Active
        self.member.get_opt_out_detail()
        self.assertEquals(self.member.account.adapter.called, 0)

    def test_can_ask_if_member_has_opted_out(self):
        has_opted_out = self.member.has_opted_out()
        self.assertIsInstance(has_opted_out, bool)
        self.assertTrue(has_opted_out)
        self.assertEquals(self.member.account.adapter.called, 0)

    def test_can_ask_if_member_has_opted_out2(self):
        member = Member(
            self.member.account, {
                'member_id': 1000,
                'email': u"*****@*****.**",
                'member_status_id': u"active"
            })
        has_opted_out = member.has_opted_out()
        self.assertIsInstance(has_opted_out, bool)
        self.assertFalse(has_opted_out)
        self.assertEquals(member.account.adapter.called, 0)

    def test_can_ask_if_member_has_opted_out3(self):
        member = Member(self.member.account)
        with self.assertRaises(ex.NoMemberStatusError):
            member.has_opted_out()
        self.assertEquals(member.account.adapter.called, 0)

    def test_can_opt_out_a_member(self):
        member = Member(self.member.account)
        with self.assertRaises(ex.NoMemberEmailError):
            member.opt_out()
        self.assertEquals(member.account.adapter.called, 0)

    def test_can_opt_out_a_member2(self):
        member = Member(
            self.member.account, {
                'member_id': 1000,
                'email': u"*****@*****.**",
                'member_status_id': u"a"
            })
        MockAdapter.expected = True
        self.assertFalse(member.has_opted_out())
        result = member.opt_out()
        self.assertIsNone(result)
        self.assertEquals(member.account.adapter.called, 1)
        self.assertEquals(
            member.account.adapter.call,
            ('PUT', '/members/email/optout/[email protected]', {}))
        self.assertTrue(member.has_opted_out())

    def test_can_save_a_member(self):
        mbr = Member(self.member.account, {'email': u"*****@*****.**"})
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }
        result = mbr.save()
        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
                          ('POST', '/members/add', {
                              'email': u"*****@*****.**"
                          }))
        self.assertEquals(1024, mbr['member_id'])
        self.assertEquals(MemberStatus.Active, mbr['member_status_id'])

    def test_can_save_a_member2(self):
        mbr = Member(self.member.account, {
            'email': u"*****@*****.**",
            'first_name': u"Emma"
        })
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }
        result = mbr.save()
        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call, ('POST', '/members/add', {
            'email': u"*****@*****.**",
            'fields': {
                'first_name': u"Emma"
            }
        }))
        self.assertEquals(1024, mbr['member_id'])
        self.assertEquals(MemberStatus.Active, mbr['member_status_id'])

    def test_can_save_a_member3(self):
        mbr = Member(self.member.account, {
            'email': u"*****@*****.**",
            'first_name': u"Emma"
        })
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }
        result = mbr.save(signup_form_id=u"http://example.com/signup")
        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
                          ('POST', '/members/add', {
                              'email': u"*****@*****.**",
                              'fields': {
                                  'first_name': u"Emma"
                              },
                              'signup_form_id': u"http://example.com/signup"
                          }))
        self.assertEquals(1024, mbr['member_id'])
        self.assertEquals(MemberStatus.Active, mbr['member_status_id'])

    def test_can_save_a_member4(self):
        mbr = Member(
            self.member.account, {
                'member_id': 200,
                'email': u"*****@*****.**",
                'first_name': u"Emma",
                'member_status_id': MemberStatus.Active
            })
        MockAdapter.expected = False

        with self.assertRaises(ex.MemberUpdateError):
            mbr.save()
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
                          ('PUT', '/members/200', {
                              'member_id': 200,
                              'email': u"*****@*****.**",
                              'fields': {
                                  'first_name': u"Emma"
                              },
                              'status_to': mbr['member_status_id']
                          }))

    def test_can_save_a_member5(self):
        mbr = Member(
            self.member.account, {
                'member_id': 200,
                'email': u"*****@*****.**",
                'fields': {
                    'first_name': u"Emma"
                },
                'member_status_id': MemberStatus.Active
            })
        MockAdapter.expected = True
        result = mbr.save()
        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
                          ('PUT', '/members/200', {
                              'member_id': 200,
                              'email': u"*****@*****.**",
                              'fields': {
                                  'first_name': u"Emma"
                              },
                              'status_to': mbr['member_status_id']
                          }))

    def test_can_save_a_member6(self):
        mbr = Member(self.member.account, {
            'email': u"*****@*****.**",
            'fields': {
                'first_name': u"Emma"
            }
        })
        MockAdapter.expected = {
            'status': u"a",
            'added': True,
            'member_id': 1024
        }

        result = mbr.save(group_ids=[1025])

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call, ('POST', '/members/add', {
            'email': u"*****@*****.**",
            'fields': {
                'first_name': u"Emma"
            },
            'group_ids': [1025]
        }))

    def test_can_delete_a_member(self):
        mbr = Member(self.member.account, {'email': u"*****@*****.**"})

        with self.assertRaises(ex.NoMemberIdError):
            mbr.delete()
        self.assertEquals(self.member.account.adapter.called, 0)
        self.assertFalse(self.member.is_deleted())

    def test_can_delete_a_member2(self):
        MockAdapter.expected = None
        mbr = Member(
            self.member.account, {
                'member_id': 200,
                'email': u"*****@*****.**",
                'deleted_at':
                datetime.now().strftime(SERIALIZED_DATETIME_FORMAT)
            })

        result = mbr.delete()

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 0)
        self.assertTrue(mbr.is_deleted())

    def test_can_delete_a_member3(self):
        MockAdapter.expected = True
        mbr = Member(self.member.account, {
            'member_id': 200,
            'email': u"*****@*****.**",
            'deleted_at': None
        })

        result = mbr.delete()

        self.assertIsNone(result)
        self.assertEquals(mbr.account.adapter.called, 1)
        self.assertEquals(mbr.account.adapter.call,
                          ('DELETE', '/members/200', {}))
        self.assertTrue(mbr.is_deleted())