Ejemplo n.º 1
0
    def test_before_after_insert(self):
        self.get_output([
            '--db', 'sqlite:///' + self.db_file, '--insert',
            'examples/dummy.csv', '--before-insert',
            'SELECT 1; CREATE TABLE foobar (date DATE)', '--after-insert',
            'INSERT INTO dummy VALUES (0, 5, 6)'
        ])

        output_file = six.StringIO()
        utility = SQL2CSV([
            '--db', 'sqlite:///' + self.db_file, '--query',
            'SELECT * FROM foobar'
        ], output_file)
        utility.run()
        output = output_file.getvalue()
        output_file.close()
        self.assertEqual(output, 'date\n')

        output_file = six.StringIO()
        utility = SQL2CSV([
            '--db', 'sqlite:///' + self.db_file, '--query',
            'SELECT * FROM dummy'
        ], output_file)
        utility.run()
        output = output_file.getvalue()
        output_file.close()
        self.assertEqual(output, 'a,b,c\n1,2.0,3.0\n0,5.0,6.0\n')
Ejemplo n.º 2
0
    def test_unicode(self):
        target_output = self.csvsql('examples/test_utf8.csv')
        args = ['--db', "sqlite:///" + self.db_file, '--query', 'select * from foo']
        output_file = six.StringIO()

        utility = SQL2CSV(args, output_file)
        utility.main()

        self.assertEqual(output_file.getvalue().strip(), target_output)
Ejemplo n.º 3
0
    def test_query(self):
        args = ['--query', 'select 6*9 as question']
        output_file = StringIO()

        utility = SQL2CSV(args, output_file)
        utility.main()
        csv = output_file.getvalue()

        self.assertTrue('question' in csv)
        self.assertTrue('54' in csv)
Ejemplo n.º 4
0
    def test_no_header_row(self):
        self.csvsql('examples/dummy.csv')
        args = ['--db', "sqlite:///" + self.db_file, '--no-header-row', '--query', 'select * from foo']
        output_file = six.StringIO()
        utility = SQL2CSV(args, output_file)
        utility.main()
        csv = output_file.getvalue()

        self.assertTrue('a,b,c' not in csv)
        self.assertTrue('1,2,3' in csv)
Ejemplo n.º 5
0
    def test_wilcard(self):
        self.csvsql('examples/dummy.csv')
        args = ['--db', "sqlite:///" + self.db_file, '--query', "select * from foo where a LIKE '%'"]
        output_file = six.StringIO()
        utility = SQL2CSV(args, output_file)
        utility.main()
        csv = output_file.getvalue()

        self.assertTrue('a,b,c' in csv)
        self.assertTrue('1,2,3' in csv)
Ejemplo n.º 6
0
    def test_linenumbers(self):
        self.csvsql('examples/dummy.csv')
        args = ['--db', "sqlite:///" + self.db_file, '--linenumbers', '--query', 'select * from foo']
        output_file = six.StringIO()
        utility = SQL2CSV(args, output_file)
        utility.main()
        csv = output_file.getvalue()

        self.assertTrue('line_number,a,b,c' in csv)
        self.assertTrue('1,1,2,3' in csv)
Ejemplo n.º 7
0
    def test_stdin(self):
        output_file = six.StringIO()
        input_file = six.StringIO("select cast(3.1415 * 13.37 as integer) as answer")

        with stdin_as_string(input_file):
            utility = SQL2CSV([], output_file)
            utility.main()
            csv = output_file.getvalue()

            self.assertTrue('answer' in csv)
            self.assertTrue('42' in csv)
Ejemplo n.º 8
0
    def test_stdin_with_query(self):
        args = ['--query', 'select 6*9 as question']
        output_file = six.StringIO()
        input_file = six.StringIO("select cast(3.1415 * 13.37 as integer) as answer")

        with stdin_as_string(input_file):
            utility = SQL2CSV(args, output_file)
            utility.main()
            csv = output_file.getvalue()

            self.assertTrue('question' in csv)
            self.assertTrue('42' not in csv)