Skip to content

Commit

Permalink
Output column datatype info for views and matviews. Addresses #184.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmafc committed Mar 23, 2018
1 parent 6ba541a commit 49549b7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pyrseas/dbobject/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def query(dbversion=None):
LEFT JOIN pg_attrdef ON (attrelid = pg_attrdef.adrelid
AND attnum = pg_attrdef.adnum)
LEFT JOIN pg_collation l ON (attcollation = l.oid)
WHERE relkind in ('c', 'r', 'f', 'p', 'v')
WHERE relkind in ('c', 'r', 'f', 'p', 'v', 'm')
AND (nspname != 'pg_catalog' AND nspname != 'information_schema')
AND attnum > 0
ORDER BY nspname, relname, attnum"""
Expand Down
30 changes: 12 additions & 18 deletions tests/dbobject/test_matview.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,28 @@ class MatViewToMapTestCase(DatabaseToMapTestCase):

def test_map_view_simple(self):
"Map a created materialized view"
if self.db.version < 90300:
self.skipTest('Only available on PG 9.3')
stmts = [CREATE_TABLE, CREATE_STMT]
dbmap = self.to_map(stmts)
expmap = {'definition': VIEW_DEFN, 'with_data': True,
expmap = {'columns': [{'c1': {'type': 'integer'}},
{'mc3': {'type': 'integer'}}],
'definition': VIEW_DEFN, 'with_data': True,
'depends_on': ['table t1']}
assert dbmap['schema sd']['materialized view mv1'] == expmap

def test_map_view_comment(self):
"Map a materialized view with a comment"
if self.db.version < 90300:
self.skipTest('Only available on PG 9.3')
dbmap = self.to_map([CREATE_TABLE, CREATE_STMT, COMMENT_STMT])
assert dbmap['schema sd']['materialized view mv1'][
'description'] == 'Test matview mv1'

def test_map_view_index(self):
"Map a materialized view with an index"
if self.db.version < 90300:
self.skipTest('Only available on PG 9.3')
stmts = [CREATE_TABLE, CREATE_STMT,
"CREATE INDEX idx1 ON mv1 (mc3)"]
dbmap = self.to_map(stmts)
expmap = {'definition': VIEW_DEFN, 'with_data': True,
expmap = {'columns': [{'c1': {'type': 'integer'}},
{'mc3': {'type': 'integer'}}],
'definition': VIEW_DEFN, 'with_data': True,
'indexes': {'idx1': {'keys': ['mc3']}},
'depends_on': ['table t1']}
assert dbmap['schema sd']['materialized view mv1'] == expmap
Expand All @@ -52,13 +50,13 @@ class MatViewToSqlTestCase(InputMapToSqlTestCase):

def test_create_view(self):
"Create a materialized view"
if self.db.version < 90300:
self.skipTest('Only available on PG 9.3')
inmap = self.std_map()
inmap['schema sd'].update({'table t1': {
'columns': [{'c1': {'type': 'integer'}}, {'c2': {'type': 'text'}},
{'c3': {'type': 'integer'}}]}})
inmap['schema sd'].update({'materialized view mv1': {
'columns': [{'c1': {'type': 'integer'}},
{'mc3': {'type': 'integer'}}],
'definition': "SELECT c1, c3 * 2 AS mc3 FROM sd.t1",
'depends_on': ['table t1']}})
sql = self.to_sql(inmap)
Expand All @@ -69,17 +67,13 @@ def test_create_view(self):

def test_bad_view_map(self):
"Error creating a materialized view with a bad map"
if self.db.version < 90300:
self.skipTest('Only available on PG 9.3')
inmap = self.std_map()
inmap['schema sd'].update({'mv1': {'definition': VIEW_DEFN}})
with pytest.raises(KeyError):
self.to_sql(inmap)

def test_drop_view(self):
"Drop an existing materialized view with table dependencies"
if self.db.version < 90300:
self.skipTest('Only available on PG 9.3')
stmts = ["CREATE TABLE t1 (c1 INTEGER, c2 TEXT)",
"CREATE TABLE t2 (c1 INTEGER, c3 TEXT)",
"CREATE MATERIALIZED VIEW mv1 AS SELECT t1.c1, c2, c3 "
Expand All @@ -97,25 +91,25 @@ def test_drop_view(self):

def test_view_with_comment(self):
"Create a materialized view with a comment"
if self.db.version < 90300:
self.skipTest('Only available on PG 9.3')
inmap = self.std_map()
inmap['schema sd'].update({'materialized view mv1': {
'columns': [{'c1': {'type': 'integer'}},
{'mc3': {'type': 'integer'}}],
'definition': VIEW_STMT, 'description': "Test matview mv1"}})
sql = self.to_sql(inmap)
assert fix_indent(sql[0]) == CREATE_STMT
assert sql[1] == COMMENT_STMT

def test_view_index(self):
"Create an index on a materialized view"
if self.db.version < 90300:
self.skipTest('Only available on PG 9.3')
stmts = [CREATE_TABLE, CREATE_STMT]
inmap = self.std_map()
inmap['schema sd'].update({'table t1': {
'columns': [{'c1': {'type': 'integer'}}, {'c2': {'type': 'text'}},
{'c3': {'type': 'integer'}}]}})
inmap['schema sd'].update({'materialized view mv1': {
'columns': [{'c1': {'type': 'integer'}},
{'mc3': {'type': 'integer'}}],
'definition': VIEW_DEFN, 'indexes': {'idx1': {'keys': ['mc3']}}}})
sql = self.to_sql(inmap, stmts)
assert sql == ["CREATE INDEX idx1 ON sd.mv1 (mc3)"]
12 changes: 6 additions & 6 deletions tests/dbobject/test_privs.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ def test_map_view(self):
"GRANT SELECT ON v1 TO PUBLIC",
"GRANT REFERENCES ON v1 TO user1"]
dbmap = self.to_map(stmts, no_privs=False)
expmap = self.sort_privileges({'definition':
" SELECT now()::date AS today;",
'privileges': [
{self.db.user: ['all']},
{'PUBLIC': ['select']},
{'user1': ['references']}]})
expmap = self.sort_privileges(
{'columns': [{'today': {'type': 'date'}}],
'definition': " SELECT now()::date AS today;",
'privileges': [{self.db.user: ['all']},
{'PUBLIC': ['select']},
{'user1': ['references']}]})
assert dbmap['schema sd']['view v1'] == expmap

def test_map_function(self):
Expand Down
11 changes: 7 additions & 4 deletions tests/dbobject/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ class ViewToMapTestCase(DatabaseToMapTestCase):
def test_map_view_no_table(self):
"Map a created view without a table dependency"
dbmap = self.to_map([CREATE_STMT])
expmap = {'definition': VIEW_DEFN}
expmap = {'columns': [{'today': {'type': 'date'}}],
'definition': VIEW_DEFN}
assert dbmap['schema sd']['view v1'] == expmap

def test_map_view_table(self):
"Map a created view with a table dependency"
stmts = ["CREATE TABLE t1 (c1 INTEGER, c2 TEXT, c3 INTEGER)",
"CREATE VIEW v1 AS SELECT c1, c3 * 2 FROM t1"]
"CREATE VIEW v1 AS SELECT c1, c3 * 2 AS c2 FROM t1"]
dbmap = self.to_map(stmts)
fmt = "%s%s" if (self.db.version < 90300) else "%s\n %s"
expmap = {'depends_on': ['table t1'],
expmap = {'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'integer'}}],
'depends_on': ['table t1'],
'definition': " SELECT t1.c1,"
"\n t1.c3 * 2\n FROM sd.t1;"}
"\n t1.c3 * 2 AS c2\n FROM sd.t1;"}
assert dbmap['schema sd']['view v1'] == expmap

def test_map_view_columns(self):
Expand Down

0 comments on commit 49549b7

Please sign in to comment.