Exemplo n.º 1
0
    def test_segment_refresh(self):
        s = AllUserSegmentFactory()
        UserFactory()
        s.refresh()
        self.assertEqual(len(s), 2)

        # Change up the segment to only match one user
        s.definition = 'select * from %s where id = %s limit 1' % (
            user_table(), self.u.id)
        s.save()
        self.assertEqual(len(s), 1)
        s.refresh()
        self.assertEqual(len(s), 1)

        # Add a 3rd user, should still only store one user
        u3 = UserFactory()
        s.refresh()
        self.assertEqual(len(s), 1)

        # Expand the definition to include 3 users again
        s.definition = 'select * from %s' % (user_table())
        s.save()
        s.refresh()
        self.assertEqual(len(s), 3)

        # Remove one user
        u3.delete()
        s.refresh()
        self.assertEqual(len(s), 2)
Exemplo n.º 2
0
 def setUp(self):
     Segment.helper = SegmentHelper(redis_obj=fakeredis.FakeStrictRedis(
         charset='utf-8', decode_responses=True))
     self.u = UserFactory()
     self.s = AllUserSegmentFactory()
     app_settings.SEGMENTS_REFRESH_ASYNC = False
     app_settings.SEGMENTS_REFRESH_ON_SAVE = True
Exemplo n.º 3
0
    def test_segment_refresh(self):
        s = AllUserSegmentFactory()
        UserFactory()
        s.refresh()
        self.assertEqual(len(s), 2)

        # Change up the segment to only match one user
        s.definition = 'select * from %s where id = %s limit 1' % (user_table(), self.u.id)
        s.save()
        self.assertEqual(len(s), 1)
        s.refresh()
        self.assertEqual(len(s), 1)

        # Add a 3rd user, should still only store one user
        u3 = UserFactory()
        s.refresh()
        self.assertEqual(len(s), 1)

        # Expand the definition to include 3 users again
        s.definition = 'select * from %s' % (user_table())
        s.save()
        s.refresh()
        self.assertEqual(len(s), 3)

        # Remove one user
        u3.delete()
        s.refresh()
        self.assertEqual(len(s), 2)
Exemplo n.º 4
0
 def test_refresh_existing_segment(self):
     segments.app_settings.SEGMENTS_REFRESH_ON_SAVE = True
     segments.app_settings.SEGMENTS_REFRES_ASYNC = False
     u1 = UserFactory()
     s = AllUserSegmentFactory()
     u2 = UserFactory()
     self.assertEqual(len(s), 1)
     s.refresh()
     self.assertEqual(len(s), 2)
Exemplo n.º 5
0
    def test_diff_segment(self):
        s1 = SegmentFactory()
        u1 = UserFactory()
        s1.add_member(u1)
        s2 = SegmentFactory()
        u2 = UserFactory()
        s2.add_member(u1)
        s2.add_member(u2)

        s1_key = self.helper.segment_key % s1.id
        s2_key = self.helper.segment_key % s2.id
        self.helper.diff_segment(s2_key, s1_key, 'diff_test')
        self.assertEquals(self.helper.redis.smembers('diff_test'),
                          {str(u2.id)})
Exemplo n.º 6
0
    def test_delete_segment(self):
        user = UserFactory()
        segment = AllUserSegmentFactory()
        self.assertTrue(segment.has_member(user))

        segment.delete()
        self.assertFalse(segment.has_member(user))
Exemplo n.º 7
0
 def setUp(self):
     segments.helpers.SegmentHelper.redis = fakeredis.FakeStrictRedis(
             charset='utf-8',
             decode_responses=True)
     self.u = UserFactory()
     self.s = AllUserSegmentFactory()
     app_settings.SEGMENTS_REFRESH_ASYNC = False
     app_settings.SEGMENTS_REFRESH_ON_SAVE = True
Exemplo n.º 8
0
class TestMixin(TestCase):
    def setUp(self):
        Segment.helper = SegmentHelper(redis_obj=fakeredis.FakeStrictRedis(
            charset='utf-8', decode_responses=True))
        self.u = UserFactory()
        self.s = AllUserSegmentFactory()
        app_settings.SEGMENTS_REFRESH_ASYNC = False
        app_settings.SEGMENTS_REFRESH_ON_SAVE = True

    def test_mixin_gives_fields(self):
        self.assertEqual(self.u.segments.count(), 1)
        self.assertEqual(self.u.segments.first(), self.s)

    def test_is_member(self):
        self.assertTrue(self.u.is_member(self.s))

    def test_is_not_member(self):
        definition = 'select * from %s where id != %s' % (user_table(),
                                                          self.u.id)
        s2 = SegmentFactory(definition=definition)
        self.assertFalse(self.u.is_member(s2))
Exemplo n.º 9
0
    def test_refresh_handles_bad_queries(self):
        user = UserFactory()

        with factory.django.mute_signals(signals.post_save):
            s1 = SegmentFactory(definition='fail')
            s2 = SegmentFactory(definition='select %s from %s' %
                                (user.pk, user_table()))

        refresh_segments()
        self.assertListEqual(list(Segment.helper.get_segment_members(s1.id)),
                             [])
        self.assertListEqual(list(Segment.helper.get_segment_members(s2.id)),
                             [str(user.pk)])
Exemplo n.º 10
0
class TestMixin(TestCase):

    def setUp(self):
        segments.helpers.SegmentHelper.redis = fakeredis.FakeStrictRedis(
                charset='utf-8',
                decode_responses=True)
        self.u = UserFactory()
        self.s = AllUserSegmentFactory()
        app_settings.SEGMENTS_REFRESH_ASYNC = False
        app_settings.SEGMENTS_REFRESH_ON_SAVE = True

    def test_mixin_gives_fields(self):
        self.assertEqual(self.u.segments.count(), 1)
        self.assertEqual(self.u.segments.first(), self.s)

    def test_is_member(self):
        self.assertTrue(self.u.is_member(self.s))

    def test_is_not_member(self):
        definition = 'select * from %s where id != %s' % (user_table(), self.u.id)
        s2 = SegmentFactory(definition=definition)
        self.assertFalse(self.u.is_member(s2))
Exemplo n.º 11
0
 def setUp(self):
     self.helper = SegmentHelper()
     self.user = UserFactory()
     SegmentHelper.redis = fakeredis.FakeStrictRedis(charset='utf-8',
                                                     decode_responses=True)