Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)