def test_result_submission_stats__no_submissions(self):
        from nuorisovaalit.models import School
        from nuorisovaalitadmin.models import CSVSubmission
        from nuorisovaalitadmin.views.allianssi import result_submission_stats

        session = DBSession()
        school_count = session.query(School).count()

        # Check that there is no submission to begin with.
        self.assertEquals(0, session.query(CSVSubmission)\
                          .filter_by(kind=CSVSubmission.RESULT).count())

        options = result_submission_stats(DummyRequest())

        self.assertEquals(u'Tuloslista-info', options['title'])
        self.assertEquals(school_count, options['school_count'])
        self.assertEquals(0, options['school_count_submitted'])
        self.assertEquals(school_count, options['school_count_not_submitted'])
        self.assertEquals('0.00', options['submitted'])
        self.assertEquals('100.00', options['not_submitted'])
    def test_result_submission_stats__with_submission(self):
        from nuorisovaalit.models import School
        from nuorisovaalitadmin.models import CSVSubmission
        from nuorisovaalitadmin.views.allianssi import result_submission_stats

        session = DBSession()
        populate_testing_db()
        school_count = session.query(School).count()

        school = session.query(School).first()
        self.assertTrue(school is not None)
        session.add(CSVSubmission({}, school, CSVSubmission.RESULT))
        session.flush()

        options = result_submission_stats(DummyRequest())
        self.assertEquals(u'Tuloslista-info', options['title'])
        self.assertEquals(school_count, options['school_count'])
        self.assertEquals(1, options['school_count_submitted'])
        self.assertEquals(school_count - 1, options['school_count_not_submitted'])
        self.assertEquals('{0:.2f}'.format(100 * 1 / float(school_count)),
                          options['submitted'])
        self.assertEquals('{0:.2f}'.format(100 * (school_count - 1) / float(school_count)),
                          options['not_submitted'])
    def test_result_submission_stats__with_different_kinds_of_schools(self):
        from nuorisovaalit.models import District
        from nuorisovaalit.models import School
        from nuorisovaalitadmin.models import CSVSubmission
        from nuorisovaalitadmin.views.allianssi import result_submission_stats

        session = DBSession()

        # Initial conditions.
        self.assertEquals(0, session.query(District).count())
        self.assertEquals(0, session.query(School).count())
        self.assertEquals(0, session.query(CSVSubmission).count())

        # Populate db.
        district1 = District(u'district 1', 1)
        district2 = District(u'district 2', 2)
        district3 = District(u'district 3', 3)
        session.add(district1)
        session.add(district2)
        session.add(district3)
        session.flush()

        school1 = School(u'schööl 1', district1)
        school2 = School(u'schööl 2', district1)
        school3 = School(u'schööl 3', district2)
        school4 = School(u'schööl 4', district2)
        school5 = School(u'schööl 5', district3)
        school6 = School(u'schööl 6', district3)
        school7 = School(u'schööl 7', district3)
        session.add(school1)
        session.add(school2)
        session.add(school3)
        session.add(school4)
        session.add(school5)
        session.add(school6)
        session.add(school7)
        session.flush()

        session.add(CSVSubmission({}, school3, CSVSubmission.VOTER))
        session.add(CSVSubmission({}, school3, CSVSubmission.RESULT))
        session.add(CSVSubmission({}, school4, CSVSubmission.RESULT))
        session.add(CSVSubmission({}, school5, CSVSubmission.VOTER))
        session.add(CSVSubmission({}, school6, CSVSubmission.RESULT))
        session.add(CSVSubmission({}, school7, CSVSubmission.VOTER))
        session.add(CSVSubmission({}, school7, CSVSubmission.RESULT))
        session.flush()

        options = result_submission_stats(DummyRequest())
        schools_not_submitted = options.pop('schools_not_submitted')

        self.assertEquals([
            u'schööl 1',
            u'schööl 2',
            u'schööl 5',
        ], sorted(s.name for s in schools_not_submitted))

        self.assertEquals({
            'title': u'Tuloslista-info',
            'school_count': 7,
            'school_count_submitted': 4,
            'school_count_not_submitted': 3,
            'submitted': '57.14',
            'not_submitted': '42.86',
        }, options)