Esempio n. 1
0
    def test_writing_excel(self):
        """ This is a pretty crap test. It at least exercises the code.
            If anyone wants to go through the brain damage of actually building
            an 'expected' xlsx output and comparing it
            (see https://github.com/jmcnamara/XlsxWriter/blob/master/xlsxwriter/test/helperfunctions.py for reference)
            , by all means submit a pull request!
        """
        res = QueryResult(
            SimpleQueryFactory(
                sql='select 1 as "a", 2 as ""',
                title='\\/*[]:?this title is longer than 32 characters').sql,
            connections[CONN])

        res.execute_query()
        res.process()

        d = datetime.now()
        d = timezone.make_aware(d, timezone.get_current_timezone())

        res._data = [[1, None], [u"Jenét", d]]

        res = ExcelExporter(
            query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('PK')

        self.assertEqual(res[:2], expected)
    def test_writing_unicode(self):
        res = QueryResult(
            SimpleQueryFactory(sql='select 1 as "a", 2 as ""').sql,
            connections[CONN])
        res.execute_query()
        res.process()
        res._data = [[1, None], ["Jenét", '1']]

        res = CSVExporter(query=None)._get_output(res).getvalue()
        self.assertEqual(
            res.encode('utf-8').decode('utf-8-sig'), 'a,\r\n1,\r\nJenét,1\r\n')
    def test_writing_datetimes(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as "b"').sql, connections[CONN])
        res.execute_query()
        res.process()
        res._data = [[1, date.today()]]

        res = JSONExporter(query=None)._get_output(res).getvalue()
        expected = [{'a': 1, 'b': date.today()}]
        self.assertEqual(res, json.dumps(expected, cls=DjangoJSONEncoder))
    def test_writing_json(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as ""').sql, connections[CONN])
        res.execute_query()
        res.process()
        res._data = [[1, None], [u"Jenét", '1']]

        res = JSONExporter(query=None)._get_output(res).getvalue()
        expected = [{'a': 1, '': None}, {'a': 'Jenét', '': '1'}]
        self.assertEqual(res, json.dumps(expected))
    def test_writing_unicode(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as ""').sql, connections[CONN])
        res.execute_query()
        res.process()
        res._data = [[1, None], [u"Jenét", '1']]

        res = CSVExporter(query=None)._get_output(res).getvalue()
        self.assertEqual(res, 'a,\r\n1,\r\nJenét,1\r\n')
Esempio n. 6
0
class _AbstractQueryResults:
    connection_name = CONN
    query = "select 1 as foo, 'qux' as mux;"

    def setUp(self):
        conn = connections[self.connection_name]
        self.qr = QueryResult(self.query, conn, 1, 1000, 10000)

    def test_column_access(self):
        self.qr._data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        self.assertEqual(self.qr.column(1), [2, 5, 8])

    def test_headers(self):
        self.assertEqual(str(self.qr.headers[0]), "foo")
        self.assertEqual(str(self.qr.headers[1]), "mux")

    def test_data(self):
        self.assertEqual(self.qr.data, [[1, "qux"]])

    def test_unicode_with_nulls(self):
        self.qr._headers = [ColumnHeader('num'), ColumnHeader('char')]
        self.qr._description = [("num", ), ("char", )]
        self.qr._data = [[2, six.u("a")], [3, None]]
        self.qr.process()
        self.assertEqual(self.qr.data, [[2, "a"], [3, None]])

    def test_summary_gets_built(self):
        self.qr.process()
        self.assertEqual(len([h for h in self.qr.headers if h.summary]), 1)
        self.assertEqual(str(self.qr.headers[0].summary), "foo")
        self.assertEqual(self.qr.headers[0].summary.stats["Sum"], 1.0)

    def test_summary_gets_built_for_multiple_cols(self):
        self.qr._headers = [ColumnHeader('a'), ColumnHeader('b')]
        self.qr._description = [("a", ), ("b", )]
        self.qr._data = [[1, 10], [2, 20]]
        self.qr.process()
        self.assertEqual(len([h for h in self.qr.headers if h.summary]), 2)
        self.assertEqual(self.qr.headers[0].summary.stats["Sum"], 3.0)
        self.assertEqual(self.qr.headers[1].summary.stats["Sum"], 30.0)

    def test_numeric_detection(self):
        self.assertEqual(self.qr._get_numerics(), [0])

    def test_get_headers_no_results(self):
        self.qr._description = None
        self.assertEqual([ColumnHeader('--')][0].title,
                         self.qr._get_headers()[0].title)
Esempio n. 7
0
    def test_writing_json(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as ""').sql)
        res.execute_query()
        res.process()
        res._data = [[1, None], [u"Jenét", "1"]]

        res = JSONExporter(query=None)._get_output(res).getvalue()
        expected = [{"a": 1, "": None}, {"a": "Jenét", "": "1"}]
        self.assertEqual(res, json.dumps(expected))
    def test_writing_datetimes(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as "b"').sql, connections[CONN])
        res.execute_query()
        res.process()
        res._data = [[1, date.today()]]

        res = JSONExporter(query=None)._get_output(res).getvalue()
        expected = [{'a': 1, 'b': date.today()}]
        self.assertEqual(res, json.dumps(expected, cls=DjangoJSONEncoder))
    def test_writing_json(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as ""').sql, connections[CONN])
        res.execute_query()
        res.process()
        res._data = [[1, None], [u"Jenét", '1']]

        res = JSONExporter(query=None)._get_output(res).getvalue()
        expected = [{'a': 1, '': None}, {'a': 'Jenét', '': '1'}]
        self.assertEqual(res, json.dumps(expected))
    def test_writing_dict_fields(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as ""',
                                             title='\\/*[]:?this title is longer than 32 characters').sql, connections[CONN])

        res.execute_query()
        res.process()

        res._data = [[1, ['foo', 'bar']], [2, {'foo': 'bar'}]]

        res = ExcelExporter(query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('PK')

        self.assertEqual(res[:2], expected)
    def test_writing_dict_fields(self):
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as ""',
                                             title='\\/*[]:?this title is longer than 32 characters').sql, connections[CONN])

        res.execute_query()
        res.process()

        res._data = [[1, ['foo', 'bar']], [2, {'foo': 'bar'}]]

        res = ExcelExporter(query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('PK')

        self.assertEqual(res[:2], expected)
    def test_writing_excel_with_invalid_characters_in_worksheet_name(self):
        res = QueryResult(
            SimpleQueryFactory(
                sql='select 1 as "a", 2 as ""',
                title='[]:*?/\\this title is longer than 32 characters').sql,
            connections[CONN])
        res.execute_query()
        res.process()

        d = datetime.now()
        d = timezone.make_aware(d, timezone.get_current_timezone())

        res._data = [[1, None], [u"Jenét", d]]

        res = ExcelExporter(
            query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('PK')

        self.assertEqual(res[:2], expected)
Esempio n. 13
0
    def test_writing_pdf(self):
        """ Use same logic as with excel
        """
        res = QueryResult(
            SimpleQueryFactory(
                sql='select 1 as "a", 2 as ""',
                title='this title is longer than 32 characters').sql)
        res.execute_query()
        res.process()

        d = datetime.now()
        d = timezone.make_aware(d, timezone.get_current_timezone())

        res._data = [[1, None], ["Jenét", d]]

        res = PdfExporter(
            query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('%PDF')

        self.assertEqual(res[:4], expected)
Esempio n. 14
0
    def test_writing_excel(self):
        """ This is a pretty crap test. It at least exercises the code.
            If anyone wants to go through the brain damage of actually building
            an 'expected' xlsx output and comparing it
            (see https://github.com/jmcnamara/XlsxWriter/blob/master/xlsxwriter/test/helperfunctions.py for reference)
            , by all means submit a pull request!
        """
        res = QueryResult(SimpleQueryFactory(sql='select 1 as "a", 2 as ""',
                                             title='this title is longer than 32 characters').sql)
        res.execute_query()
        res.process()

        d = datetime.now()
        d = timezone.make_aware(d, timezone.get_current_timezone())

        res._data = [[1, None], [u"Jenét", d]]

        res = ExcelExporter(query=SimpleQueryFactory())._get_output(res).getvalue()

        expected = b('PK')

        self.assertEqual(res[:2], expected)
Esempio n. 15
0
 def setUp(self):
     self.qr = QueryResult('select 1 as "foo", "qux" as "mux";')
Esempio n. 16
0
class TestQueryResults(TestCase):
    def setUp(self):
        self.qr = QueryResult('select 1 as "foo", "qux" as "mux";')

    def test_column_access(self):
        self.qr._data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        self.assertEqual(self.qr.column(1), [2, 5, 8])

    def test_headers(self):
        self.assertEqual(str(self.qr.headers[0]), "foo")
        self.assertEqual(str(self.qr.headers[1]), "mux")

    def test_data(self):
        self.assertEqual(self.qr.data, [[1, "qux"]])

    def test_unicode_detection(self):
        self.assertEqual(self.qr._get_unicodes(), [1])

    def test_uncode_with_nulls(self):
        self.qr._headers = [ColumnHeader('num'), ColumnHeader('char')]
        self.qr._description = [("num", ), ("char", )]
        self.qr._data = [[2, six.u("a")], [3, None]]
        self.qr.process()
        self.assertEqual(self.qr.data, [[2, b"a"], [3, None]])

    def test_summary_gets_built(self):
        self.qr.process()
        self.assertEqual(len([h for h in self.qr.headers if h.summary]), 1)
        self.assertEqual(str(self.qr.headers[0].summary), "foo")
        self.assertEqual(self.qr.headers[0].summary.stats["Sum"], 1.0)

    def test_summary_gets_built_for_multiple_cols(self):
        self.qr._headers = [ColumnHeader('a'), ColumnHeader('b')]
        self.qr._description = [("a", ), ("b", )]
        self.qr._data = [[1, 10], [2, 20]]
        self.qr.process()
        self.assertEqual(len([h for h in self.qr.headers if h.summary]), 2)
        self.assertEqual(self.qr.headers[0].summary.stats["Sum"], 3.0)
        self.assertEqual(self.qr.headers[1].summary.stats["Sum"], 30.0)

    def test_numeric_detection(self):
        self.assertEqual(self.qr._get_numerics(), [0])

    def test_transforms_are_identified(self):
        self.qr._headers = [ColumnHeader('foo')]
        got = self.qr._get_transforms()
        self.assertEqual([(0, '<a href="{0}">{0}</a>')], got)

    def test_transform_alters_row(self):
        self.qr._headers = [ColumnHeader('foo'), ColumnHeader('qux')]
        self.qr._data = [[1, 2]]
        self.qr.process()
        self.assertEqual(['<a href="1">1</a>', 2], self.qr._data[0])

    def test_multiple_transforms(self):
        self.qr._headers = [ColumnHeader('foo'), ColumnHeader('bar')]
        self.qr._data = [[1, 2]]
        self.qr.process()
        self.assertEqual(['<a href="1">1</a>', 'x: 2'], self.qr._data[0])

    def test_get_headers_no_results(self):
        self.qr._description = None
        self.assertEqual([ColumnHeader('--')][0].title,
                         self.qr._get_headers()[0].title)
Esempio n. 17
0
 def setUp(self):
     conn = connections[CONN]
     self.qr = QueryResult('select 1 as "foo", "qux" as "mux";', conn)
 def setUp(self):
     self.qr = QueryResult('select 1 as "foo", "qux" as "mux";')
class TestQueryResults(TestCase):

    def setUp(self):
        self.qr = QueryResult('select 1 as "foo", "qux" as "mux";')

    def test_column_access(self):
        self.qr._data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        self.assertEqual(self.qr.column(1), [2, 5, 8])

    def test_headers(self):
        self.assertEqual(str(self.qr.headers[0]), "foo")
        self.assertEqual(str(self.qr.headers[1]), "mux")

    def test_data(self):
        self.assertEqual(self.qr.data, [[1, "qux"]])

    def test_unicode_detection(self):
        self.assertEqual(self.qr._get_unicodes(), [1])

    def test_unicode_with_nulls(self):
        self.qr._headers = [ColumnHeader('num'), ColumnHeader('char')]
        self.qr._description = [("num",), ("char",)]
        self.qr._data = [[2, six.u("a")], [3, None]]
        self.qr.process()
        self.assertEqual(self.qr.data, [[2, b"a"], [3, None]])

    def test_summary_gets_built(self):
        self.qr.process()
        self.assertEqual(len([h for h in self.qr.headers if h.summary]), 1)
        self.assertEqual(str(self.qr.headers[0].summary), "foo")
        self.assertEqual(self.qr.headers[0].summary.stats["Sum"], 1.0)

    def test_summary_gets_built_for_multiple_cols(self):
        self.qr._headers = [ColumnHeader('a'), ColumnHeader('b')]
        self.qr._description = [("a",), ("b",)]
        self.qr._data = [[1, 10], [2, 20]]
        self.qr.process()
        self.assertEqual(len([h for h in self.qr.headers if h.summary]), 2)
        self.assertEqual(self.qr.headers[0].summary.stats["Sum"], 3.0)
        self.assertEqual(self.qr.headers[1].summary.stats["Sum"], 30.0)

    def test_numeric_detection(self):
        self.assertEqual(self.qr._get_numerics(), [0])

    def test_transforms_are_identified(self):
        self.qr._headers = [ColumnHeader('foo')]
        got = self.qr._get_transforms()
        self.assertEqual([(0, '<a href="{0}">{0}</a>')], got)

    def test_transform_alters_row(self):
        self.qr._headers = [ColumnHeader('foo'), ColumnHeader('qux')]
        self.qr._data = [[1, 2]]
        self.qr.process()
        self.assertEqual(['<a href="1">1</a>', 2], self.qr._data[0])

    def test_multiple_transforms(self):
        self.qr._headers = [ColumnHeader('foo'), ColumnHeader('bar')]
        self.qr._data = [[1, 2]]
        self.qr.process()
        self.assertEqual(['<a href="1">1</a>', 'x: 2'], self.qr._data[0])

    def test_get_headers_no_results(self):
        self.qr._description = None
        self.assertEqual([ColumnHeader('--')][0].title, self.qr._get_headers()[0].title)
Esempio n. 20
0
 def setUp(self):
     conn = connections[CONN]
     self.qr = QueryResult('select 1 as "foo", "qux" as "mux";', conn)
Esempio n. 21
0
 def setUp(self):
     conn = connections[self.connection_name]
     self.qr = QueryResult(self.query, conn, 1, 1000, 10000)