Ejemplo n.º 1
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)])
Ejemplo n.º 2
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)})
Ejemplo n.º 3
0
    def test_refresh_handles_bad_queries(self, mocked_segment):
        segments.app_settings.SEGMENTS_REFRESH_ON_SAVE = False

        s1 = SegmentFactory()
        s1.definition = 'fail'
        s1.save()
        s1.refresh = Mock(return_value=True)

        s2 = SegmentFactory()
        s2.refresh = Mock(return_value=False)

        mocked_segment.return_value = [s1, s2]

        refresh_segments()

        self.assertEqual(s1.refresh.call_count, 1)

        segments.app_settings.SEGMENTS_REFRESH_ON_SAVE = True
Ejemplo n.º 4
0
 def test_refresh_non_existing_segment(self):
     SegmentFactory(definition='SELECT 1;')
     bad_id = Segment.objects.order_by('pk').last().pk + 1
     with self.assertLogs(logger='segments.tasks', level='ERROR') as cm:
         refresh_segment(bad_id)  #bad ID
     self.assertIn(
         'SEGMENTS: Unable to refresh segment id %s. DoesNotExist.' %
         bad_id,
         cm.output[0],
     )
Ejemplo n.º 5
0
 def test_remove_segment_membership_segment_exists(self):
     s = SegmentFactory()
     s.add_member(self.user)
     self.helper.remove_segment_membership(s.id, self.user.id)
     self.assertFalse(self.helper.segment_has_member(s.id, self.user.id))
Ejemplo n.º 6
0
 def test_refresh_segment_valid_sql(self):
     s = SegmentFactory()
     valid_sql = 'select * from %s' % user_table()
     self.assertEquals(self.helper.refresh_segment(s.id, valid_sql), 1)
Ejemplo n.º 7
0
 def test_delete_segment(self, p_delete_segment):
     s = SegmentFactory()
     s.add_member(self.user)
     s.delete()
     self.assertFalse(self.helper.segment_has_member(s.id, self.user.id))
     self.assertTrue(p_delete_segment.called)
Ejemplo n.º 8
0
 def test_remove_refreshed_user(self):
     s = SegmentFactory()
     self.helper.refresh_segment(
         s.id, 'select %s from %s' % (self.user.pk, user_table()))
     self.helper.remove_refreshed_user(self.user.id)
     self.assertEquals(len(list(self.helper.get_refreshed_users())), 0)
Ejemplo n.º 9
0
 def test_refresh_segment_invalid_sql(self):
     s = SegmentFactory()
     invalid_sql = 'abc select '
     self.assertEquals(self.helper.refresh_segment(s.id, invalid_sql), 0)
Ejemplo n.º 10
0
 def test_get_segment_membership_count(self):
     s = SegmentFactory()
     s.add_member(self.user)
     self.assertEquals(self.helper.get_segment_membership_count(s.id), 1)
Ejemplo n.º 11
0
 def test_get_segment_members_invalid_segment(self):
     s = SegmentFactory()
     s.add_member(self.user)
     members = self.helper.get_segment_members(99999)
     self.assertEquals(len(list(members)), 0)
Ejemplo n.º 12
0
 def test_refresh_non_existing_segment(self):
     s = SegmentFactory()
     refresh_segment(s.id + 1)  #bad ID
Ejemplo n.º 13
0
 def test_get_user_segments_when_invalid_user(self):
     s = SegmentFactory()
     s.add_member(self.user)
     segments = self.helper.get_user_segments(9999)
     self.assertEquals(len(segments), 0)
Ejemplo n.º 14
0
 def test_segment_has_member_nonexistant_segment(self):
     s = SegmentFactory()
     s.add_member(self.user)
     self.helper.remove_segment_membership(99999, self.user.id)
     self.assertTrue(self.helper.segment_has_member(s.id, self.user.id))
Ejemplo n.º 15
0
 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))
Ejemplo n.º 16
0
 def test_segment_has_member_when_segment_exists(self):
     s = SegmentFactory()
     s.add_member(self.user)
     self.assertTrue(self.helper.segment_has_member(s.id, self.user.id))
Ejemplo n.º 17
0
 def test_add_segment_membership(self):
     s = SegmentFactory()
     s.add_member(self.user)
     self.assertTrue(self.helper.segment_has_member(s.id, self.user.id))
Ejemplo n.º 18
0
 def test_user_doesnt_belong_to_segment(self):
     definition = 'select * from %s where id != %s' % (user_table(),
                                                       self.u.id)
     s = SegmentFactory(definition=definition)
     self.assertFalse(s.has_member(self.u))
Ejemplo n.º 19
0
 def test_user_belongs_to_segment(self):
     definition = 'select * from %s where id = %s' % (user_table(),
                                                      self.u.id)
     s = SegmentFactory(definition=definition)
     s.refresh()
     self.assertTrue(s.has_member(self.u))
Ejemplo n.º 20
0
 def test_segment_invalid(self):
     try:
         s = SegmentFactory(definition='fail')
     except SegmentExecutionError:
         pass
Ejemplo n.º 21
0
 def test_get_user_segments_when_segment_exists(self):
     s = SegmentFactory()
     s.add_member(self.user)
     segments = self.helper.get_user_segments(self.user.id)
     self.assertTrue(len(segments) > 0)
Ejemplo n.º 22
0
 def test_user_adds_to_segment(self):
     s = SegmentFactory()
     s.add_member(self.u)
     self.assertTrue(s.has_member(self.u))