]> git.jsancho.org Git - datasette-connectors.git/blobdiff - tests/dummy.py
Overwriting Connector class is enough to operate with
[datasette-connectors.git] / tests / dummy.py
index feadf0a2e1ed2e81c1e703769b5225149a391650..04c6686ad459a81eba587adbed51c984c526eccb 100644 (file)
-from datasette_connectors.row import Row
-from datasette_connectors.connectors import Connector
+import datasette_connectors as dc
 
 
-class DummyConnector(Connector):
-    _connector_type = 'dummy'
+class DummyConnector(dc.Connector):
+    connector_type = 'dummy'
 
-    @staticmethod
-    def table_names(path):
+    def table_names(self):
         return ['table1', 'table2']
 
-    @staticmethod
-    def table_columns(path, table):
-        return ['c1', 'c2', 'c3']
-
-    @staticmethod
-    def get_all_foreign_keys(path):
-        return {
-            'table1': {'incoming': [], 'outgoing': []},
-            'table2': {'incoming': [], 'outgoing': []},
-        }
-
-    @staticmethod
-    def table_counts(path, *args, **kwargs):
-        return {
-            'table1': 2,
-            'table2': 2,
-        }
-
-
-def inspect(path):
-    tables = {}
-    views = []
-
-    for table in ['table1', 'table2']:
-        tables[table] = {
-            'name': table,
-            'columns': ['c1', 'c2', 'c3'],
-            'primary_keys': [],
-            'count': 2,
-            'label_column': None,
-            'hidden': False,
-            'fts_table': None,
-            'foreign_keys': {'incoming': [], 'outgoing': []},
-        }
-
-    return tables, views, _connector_type
-
-
-class Connection:
-    def __init__(self, path):
-        self.path = path
-
-    def execute(self, sql, params=None, truncate=False, page_size=None, max_returned_rows=None):
-        sql = sql.strip()
-
-        rows = []
+    def hidden_table_names(self):
+        return []
+
+    def detect_spatialite(self):
+        return False
+
+    def view_names(self):
+        return []
+
+    def table_count(self, table_name):
+        return 2
+
+    def table_info(self, table_name):
+        return [
+            {
+                'idx': 0,
+                'name': 'c1',
+                'primary_key': False,
+            },
+            {
+                'idx': 0,
+                'name': 'c2',
+                'primary_key': False,
+            },
+            {
+                'idx': 0,
+                'name': 'c3',
+                'primary_key': False,
+            },
+        ]
+
+    def detect_fts(self, table_name):
+        return False
+
+    def foreign_keys(self, table_name):
+        return []
+
+    def table_exists(self, table_name):
+        return table_name in ['table1', 'table2']
+
+    def table_definition(self, table_type, table_name):
+        return 'CREATE TABLE ' + table_name + ' (c1, c2, c3)'
+
+    def indices_definition(self, table_name):
+        return []
+
+    def execute(
+        self,
+        sql,
+        params=None,
+        truncate=False,
+        custom_time_limit=None,
+        page_size=None,
+        log_sql_errors=True,
+    ):
+        results = []
         truncated = False
-        description = []
+        description = ()
 
         if sql == 'select c1 from table1':
-            rows = [
-                Row({'c1': 10}),
-                Row({'c1': 20})
+            results = [
+                {'c1': 10},
+                {'c1': 20},
             ]
             description = (('c1',),)
-        elif sql == 'select rowid, * from table2 order by rowid limit 51':
-            rows = [
-                Row({'rowid': 1, 'c1': 100, 'c2': 120, 'c3': 130}),
-                Row({'rowid': 2, 'c1': 200, 'c2': 220, 'c3': 230})
-            ]
-            description = (('rowid',), ('c1',), ('c2',), ('c3',))
-        elif sql == 'select count(*) from table2':
-            rows = [Row({'count(*)': 2})]
-            description = (('count(*)',),)
-        elif sql == """select distinct rowid from table2 
-                        where rowid is not null
-                        limit 31""":
-            rows = [
-                Row({'rowid': 1}),
-                Row({'rowid': 2})
+        elif sql == 'select c1, c2, c3 from table2 limit 51':
+            results = [
+                {'c1': 100, 'c2': 120, 'c3': 130},
+                {'c1': 200, 'c2': 220, 'c3': 230},
             ]
-            description = (('rowid',),)
-        elif sql == """select distinct c1 from table2 
-                        where c1 is not null
-                        limit 31""":
-            rows = [
-                Row({'c1': 100}),
-                Row({'c1': 200})
+            description = (('c1',), ('c2',), ('c3',))
+        elif sql == "select * from (select c1, c2, c3 from table2 ) limit 0":
+            pass
+        elif sql == "select c1, count(*) as n from ( select c1, c2, c3 from table2 ) where c1 is not null group by c1 limit 31":
+            results = [
+                {'c1': 100, 'n': 1},
+                {'c1': 200, 'n': 1},
             ]
-            description = (('c1',),)
-        elif sql == """select distinct c2 from table2 
-                        where c2 is not null
-                        limit 31""":
-            rows = [
-                Row({'c2': 120}),
-                Row({'c2': 220})
+            description = (('c1',), ('n',))
+        elif sql == "select c2, count(*) as n from ( select c1, c2, c3 from table2 ) where c2 is not null group by c2 limit 31":
+            results = [
+                {'c2': 120, 'n': 1},
+                {'c2': 220, 'n': 1},
             ]
-            description = (('c2',),)
-        elif sql == """select distinct c3 from table2 
-                        where c3 is not null
-                        limit 31""":
-            rows = [
-                Row({'c3': 130}),
-                Row({'c3': 230})
+            description = (('c2',), ('n',))
+        elif sql == "select c3, count(*) as n from ( select c1, c2, c3 from table2 ) where c3 is not null group by c3 limit 31":
+            results = [
+                {'c3': 130, 'n': 1},
+                {'c3': 230, 'n': 1},
             ]
-            description = (('c3',),)
-        elif sql == 'select sql from sqlite_master where name = :n and type=:t':
-            if params['t'] != 'view':
-                rows = [Row({'sql': 'CREATE TABLE ' + params['n'] + ' (c1, c2, c3)'})]
-                description = (('sql',),)
+            description = (('c3',), ('n',))
+        elif sql == 'select date(c1) from ( select c1, c2, c3 from table2 ) where c1 glob "????-??-*" limit 100;':
+            pass
+        elif sql == "select c1, c2, c3 from blah limit 51":
+            raise dc.OperationalError("no such table: blah")
         else:
-            raise Exception("Unexpected query: %s" % sql)
+            raise Exception("Unexpected query:", sql)
 
-        return rows, truncated, description
+        return results, truncated, description