Skip to content

Commit

Permalink
Merge PRs #142 and #145 from r0.7 to master branch. Fixes issue #141.
Browse files Browse the repository at this point in the history
  • Loading branch information
darthunix authored and jmafc committed Aug 29, 2016
1 parent 8d5d06e commit 465e76e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
9 changes: 8 additions & 1 deletion pyrseas/dbobject/__init__.py
Expand Up @@ -305,7 +305,11 @@ def map_privs(self):
if prv:
privlist.append(privileges_to_map(prv, self.allprivs,
self.owner))
return privlist
sorted_privlist = []
for sortedItem in sorted([list(i.keys())[0] for i in privlist]):
sorted_privlist.append([item for item in privlist
if list(item.keys())[0] == sortedItem][0])
return sorted_privlist

def _comment_text(self):
"""Return the text for the SQL COMMENT statement
Expand Down Expand Up @@ -502,6 +506,9 @@ def _from_catalog(self):
This is may be overriden by derived classes as needed.
"""
for obj in self.fetch():
if hasattr(obj, 'options'):
if type(obj.options) is list:
obj.options = sorted(obj.options)
self[obj.key()] = obj

def to_map(self, opts):
Expand Down
10 changes: 10 additions & 0 deletions pyrseas/testutils.py
Expand Up @@ -360,6 +360,16 @@ def yaml_load(self, filename, subdir=None):
def remove_tempfiles(self):
remove_temp_files(TEST_DIR)

@staticmethod
def sort_privileges(data):
try:
sorted_privlist = []
for sortedItem in sorted([list(i.keys())[0] for i in data['privileges']]):
sorted_privlist.append([item for item in data['privileges'] if list(item.keys())[0] == sortedItem][0])
data['privileges'] = sorted_privlist
finally:
return data


class InputMapToSqlTestCase(PyrseasTestCase):
"""Base class for "input map to SQL" test cases"""
Expand Down
2 changes: 1 addition & 1 deletion tests/dbobject/test_foreign.py
Expand Up @@ -249,7 +249,7 @@ def test_map_user_mapping_options(self):
dbmap = self.to_map(stmts)
assert dbmap['foreign data wrapper fdw1']['server fs1'] == {
'user mappings': {'PUBLIC': {'options': [
'user=john', 'password=doe']}}}
'password=doe', 'user=john']}}}


class UserMappingToSqlTestCase(InputMapToSqlTestCase):
Expand Down
26 changes: 13 additions & 13 deletions tests/dbobject/test_privs.py
Expand Up @@ -39,23 +39,23 @@ def test_map_schema(self):
stmts = ["CREATE SCHEMA s1", "GRANT USAGE ON SCHEMA s1 TO PUBLIC",
"GRANT CREATE, USAGE ON SCHEMA s1 TO user1"]
dbmap = self.to_map(stmts, no_privs=False)
expmap = {'privileges': [{self.db.user: ['all']},
{'PUBLIC': ['usage']}, {'user1': ['all']}]}
expmap = self.sort_privileges({'privileges': [{self.db.user: ['all']},
{'PUBLIC': ['usage']}, {'user1': ['all']}]})
assert dbmap['schema s1'] == expmap

def test_map_table(self):
"Map a table with various GRANTs"
stmts = [CREATE_TABLE, GRANT_SELECT % 'PUBLIC', GRANT_INSUPD % 'user1',
"GRANT REFERENCES, TRIGGER ON t1 TO user2 WITH GRANT OPTION"]
dbmap = self.to_map(stmts, no_privs=False)
expmap = {'columns': [{'c1': {'type': 'integer'}},
expmap = self.sort_privileges({'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'privileges': [{self.db.user: ['all']},
{'PUBLIC': ['select']},
{'user1': ['insert', 'update']},
{'user2': [{'trigger': {'grantable': True}},
{'references': {
'grantable': True}}]}]}
'grantable': True}}]}]})
assert dbmap['schema public']['table t1'] == expmap

def test_map_column(self):
Expand All @@ -65,11 +65,11 @@ def test_map_column(self):
"GRANT INSERT (c1, c2) ON t1 TO user1",
"GRANT INSERT (c2), UPDATE (c2) ON t1 TO user2"]
dbmap = self.to_map(stmts, no_privs=False)
expmap = {'columns': [
expmap = self.sort_privileges({'columns': [
{'c1': {'type': 'integer', 'privileges': [{'user1': ['insert']}]}},
{'c2': {'type': 'text', 'privileges': [
{'user1': ['insert']}, {'user2': ['insert', 'update']}]}}],
'privileges': [{self.db.user: ['all']}, {'PUBLIC': ['select']}]}
'privileges': [{self.db.user: ['all']}, {'PUBLIC': ['select']}]})
assert dbmap['schema public']['table t1'] == expmap

def test_map_sequence(self):
Expand All @@ -78,11 +78,11 @@ def test_map_sequence(self):
"GRANT SELECT ON SEQUENCE seq1 TO PUBLIC",
"GRANT USAGE, UPDATE ON SEQUENCE seq1 TO user1"]
dbmap = self.to_map(stmts, no_privs=False)
expmap = {'start_value': 1, 'increment_by': 1, 'max_value': None,
expmap = self.sort_privileges({'start_value': 1, 'increment_by': 1, 'max_value': None,
'min_value': None, 'cache_value': 1,
'privileges': [{self.db.user: ['all']},
{'PUBLIC': ['select']},
{'user1': ['usage', 'update']}]}
{'user1': ['usage', 'update']}]})
assert dbmap['schema public']['sequence seq1'] == expmap

def test_map_view(self):
Expand All @@ -91,10 +91,10 @@ 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 = {'definition': " SELECT now()::date AS today;",
expmap = self.sort_privileges({'definition': " SELECT now()::date AS today;",
'privileges': [{self.db.user: ['all']},
{'PUBLIC': ['select']},
{'user1': ['references']}]}
{'user1': ['references']}]})
assert dbmap['schema public']['view v1'] == expmap

def test_map_function(self):
Expand Down Expand Up @@ -125,8 +125,8 @@ def test_map_fd_wrapper(self):
stmts = [CREATE_FDW,
"GRANT USAGE ON FOREIGN DATA WRAPPER fdw1 TO PUBLIC"]
dbmap = self.to_map(stmts, no_privs=False, superuser=True)
expmap = {'privileges': [{self.db.user: ['usage']},
{'PUBLIC': ['usage']}]}
expmap = self.sort_privileges({'privileges': [{self.db.user: ['usage']},
{'PUBLIC': ['usage']}]})
assert dbmap['foreign data wrapper fdw1'] == expmap

def test_map_server(self):
Expand All @@ -152,7 +152,7 @@ def test_map_foreign_table(self):
'privileges': [{self.db.user: ['all']},
{'PUBLIC': ['select']},
{'user1': ['insert', 'update']}]}
assert dbmap['schema public']['foreign table ft1'] == expmap
assert dbmap['schema public']['foreign table ft1'] == self.sort_privileges(expmap)


class PrivilegeToSqlTestCase(InputMapToSqlTestCase):
Expand Down

0 comments on commit 465e76e

Please sign in to comment.