csvquerytool / test / test_query.py

import csvquerytool
import os
import StringIO
import unittest

class QueryTests(unittest.TestCase):

    def setUp(self):
        data_path = os.path.join(os.path.dirname(__file__), "data")
        self.departments_csv = os.path.join(data_path, "departments.csv")
        self.employees_csv = os.path.join(data_path, "employees.csv")

    def test_select(self):
        stdout = StringIO.StringIO()
        csvquerytool.run_query("SELECT * FROM csv ORDER BY department_id, surname", [self.employees_csv], stdout)
        self.assertEqual(stdout.getvalue(), "\r\n".join(["surname,department_id", "John,None", "Rafferty,31", "Jones,33", "Steinberg,33", "Robinson,34", "Smith,34", ""]))

    def test_join(self):
        stdout = StringIO.StringIO()
        csvquerytool.run_query("SELECT csv.department_name, csv2.surname FROM csv JOIN csv2 ON csv.department_id = csv2.department_id ORDER BY csv.department_id, csv2.surname", [self.departments_csv, self.employees_csv], stdout)
        self.assertEqual(stdout.getvalue(), "\r\n".join(["department_name,surname", "Sales,Rafferty", "Engineering,Jones", "Engineering,Steinberg", "Clerical,Robinson", "Clerical,Smith", ""]))

if __name__ == "__main__":
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.