Пример #1
0
    def test_good_ajax_request(self):
        """
        Make sure that ajax request works correctly.
        """

        module_system = get_test_system()
        block = WordCloudBlock(module_system, DictFieldData(self.raw_field_data), Mock())

        post_data = MultiDict(('student_words[]', word) for word in ['cat', 'cat', 'dog', 'sun'])
        response = json.loads(block.handle_ajax('submit', post_data))
        assert response['status'] == 'success'
        assert response['submitted'] is True
        assert response['total_count'] == 22
        self.assertDictEqual(
            response['student_words'],
            {'sun': 1, 'dog': 6, 'cat': 12}
        )

        self.assertListEqual(
            response['top_words'],
            [{'text': 'cat', 'size': 12, 'percent': 55.0},
             {'text': 'dad', 'size': 2, 'percent': 9.0},
             {'text': 'dog', 'size': 6, 'percent': 27.0},
             {'text': 'mom', 'size': 1, 'percent': 5.0},
             {'text': 'sun', 'size': 1, 'percent': 4.0}]
        )

        assert 100.0 == sum((i['percent'] for i in response['top_words']))
Пример #2
0
    def test_bad_ajax_request(self):
        """
        Make sure that answer for incorrect request is error json.
        """

        module_system = get_test_system()
        block = WordCloudBlock(module_system, DictFieldData(self.raw_field_data), Mock())

        response = json.loads(block.handle_ajax('bad_dispatch', {}))
        self.assertDictEqual(response, {
            'status': 'fail',
            'error': 'Unknown Command!'
        })