Example #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)
Example #2
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)
Example #3
0
    def test_raw_user_query(self):
        invalid = ['', None, 12345, "select 'pretendemail'"]
        for i in invalid:
            items, count = execute_raw_user_query(i)
            self.assertEquals(count, 0)
            self.assertEquals(len(items), 0)

        valid_sql = 'select * from %s' % user_table()
        items, count = execute_raw_user_query(valid_sql)
        self.assertEquals(count, 1)
        self.assertEquals(len(items), 1)
Example #4
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)])
Example #5
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)
Example #6
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)
Example #7
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))
Example #8
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))
Example #9
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))
Example #10
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))
Example #11
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))
Example #12
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))