Ejemplo n.º 1
0
    def testStop(self):

        '''
        A GET request should stop the slicer for a haiku.
        '''

        # Create an admin and haiku.
        admin = User.createAdministrator('username', 'password')
        haiku = Haiku.createHaiku(admin.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Start the slicer
        sched.createSlicer(haiku, slicer.slice)

        with self.app as c:

            # Re-get the haiku.
            haiku = Haiku.getHaikuBySlug('test')

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            # Hit the route, check for redirect back to browse.
            rv = c.get('/admin/stop/' + str(haiku.id), follow_redirects=True)
            self.assertTemplateUsed('admin/browse.html')

            # Check that the slicer is not present.
            job = sched.getJobByHaiku(haiku)
            self.assertFalse(job)
Ejemplo n.º 2
0
    def testGetHaikuBySlug(self):

        '''
        getHaikuBySlug() should get a record by the url slug.
        '''

        # Create records.
        user = User.createAdministrator('username', 'password')
        haiku = Haiku.createHaiku(user.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Fetch and check.
        retrievedHaiku = Haiku.getHaikuBySlug('test')
        self.assertEquals(haiku.id, retrievedHaiku.id)
Ejemplo n.º 3
0
    def testCreateHaiku(self):

        '''
        createHaiku() should create and save a new record.
        '''

        # At start, no records.
        self.assertEquals(Haiku.query.count(), 0)

        # Create records.
        user = User.createAdministrator('username', 'password')
        haiku = Haiku.createHaiku(user.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Get time delta.
        timeDelta = (dt.datetime.now() - haiku.created_on).total_seconds()

        # Check for the new record, fetch.
        self.assertEquals(Haiku.query.count(), 1)
        retrievedHaiku = Haiku.query.get(haiku.id)

        # Check attribute assignments.
        self.assertEqual(retrievedHaiku.created_by, user.id)
        self.assertEqual(retrievedHaiku.url_slug, 'test')
        self.assertEqual(retrievedHaiku.word_round_length, 1000)
        self.assertEqual(retrievedHaiku.slicing_interval, 1)
        self.assertEqual(retrievedHaiku.min_blind_submissions, 5)
        self.assertEqual(retrievedHaiku.blind_submission_value, 100)
        self.assertEqual(retrievedHaiku.decay_mean_lifetime, 30 / math.log(2))
        self.assertEqual(retrievedHaiku.seed_capital, 1000)
        self.assertLess(timeDelta, 0.1)
Ejemplo n.º 4
0
    def setUp(self):

        '''
        Create existing haiku record to test against.
        '''

        super(ValidateHaikuUnitTest, self).setUp()

        admin = User.createAdministrator('username', 'password')
        haiku = Haiku.createHaiku(admin.id, 'slug', 1000, 1, 5, 100, 20, 1000)
Ejemplo n.º 5
0
    def testDeleteHaiku(self):

        '''
        deleteHaiku() should delete a record.
        '''

        # Create records.
        user = User.createAdministrator('username', 'password')
        haiku1 = Haiku.createHaiku(user.id, 'test1', 1000, 1, 5, 100, 30, 1000)
        haiku2 = Haiku.createHaiku(user.id, 'test2', 1000, 1, 5, 100, 30, 1000)

        # At start, two records.
        self.assertEquals(Haiku.query.count(), 2)

        # Delete and check.
        Haiku.deleteHaiku(haiku1.id)
        self.assertEquals(Haiku.query.count(), 1)
        retrievedHaiku = Haiku.query.first()
        self.assertEquals(retrievedHaiku.id, haiku2.id)
Ejemplo n.º 6
0
    def testDelete(self):

        '''
        A GET request should delete the haiku.
        '''

        # Create an admin and haiku.
        admin = User.createAdministrator('username', 'password')

        # Create 2 haiku.
        haiku1 = Haiku.createHaiku(admin.id, 'test1', 1000, 1, 5, 100, 30, 1000)
        haiku2 = Haiku.createHaiku(admin.id, 'test2', 1000, 1, 5, 100, 30, 1000)

        with self.app as c:

            # Re-get the haiku.
            haiku1 = Haiku.getHaikuBySlug('test1')
            haiku2 = Haiku.getHaikuBySlug('test2')

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            # At the start, 2 records.
            self.assertEquals(Haiku.query.count(), 2)

            # Hit the route.
            rv = c.get('/admin/delete/' + str(haiku1.id), follow_redirects=True)
            self.assertTemplateUsed('admin/browse.html')

            # 1 record.
            self.assertEquals(Haiku.query.count(), 1)

            # Check that the right record was deleted.
            haiku = Haiku.query.first()
            self.assertEquals(haiku.id, haiku2.id)
Ejemplo n.º 7
0
    def setUp(self):

        '''
        Instantiate the scheduler.
        '''

        super(HaikuSchedulerUnitTest, self).setUp()

        # Create admin and haiku.
        self.user = User.createAdministrator('username', 'password')
        self.haiku = Haiku.createHaiku(self.user.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Instantiate the scheduler.
        self.sched = HaikuScheduler()
        self.sched.start()
Ejemplo n.º 8
0
    def testDeleteSlicer(self):

        '''
        deleteSlicer() should delete the correct slicer.
        '''

        # Create a second haiku.
        haiku2 = Haiku.createHaiku(self.user.id, 'test2', 1000, 1, 5, 100, 30, 1000)

        # Dummy slicing routine.
        def testSlicer():
            pass

        # Add two slicers.
        self.sched.add_interval_job(
            func = testSlicer,
            name = self.sched.getJobName(self.haiku),
            seconds = 1,
            jobstore = HaikuScheduler.JOBSTORE)

        self.sched.add_interval_job(
            func = testSlicer,
            name = self.sched.getJobName(haiku2),
            seconds = 1,
            jobstore = HaikuScheduler.JOBSTORE)

        # Should be 2 jobs.
        self.assertEquals(len(self.sched.get_jobs()), 2)

        # Delete the second slicer.
        self.sched.deleteSlicer(haiku2)

        # Should be 1 job.
        self.assertEquals(len(self.sched.get_jobs()), 1)

        # Check that the right slicer was deleted.
        self.assertTrue(self.sched.checkForSlicer(self.haiku))
        self.assertFalse(self.sched.checkForSlicer(haiku2))