From c969accb4783896e3225d126e23e0c6d5851de7f Mon Sep 17 00:00:00 2001 From: cssho Date: Tue, 4 Oct 2016 13:10:25 +0900 Subject: [PATCH] Support display maximum length of character varying. --- README.md | 1 + pg_table_markdown/__init__.py | 10 +++++--- tests.py | 47 +++++++++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 252f3b5..f83a1a4 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Options: --database_url TEXT Database connection URL --table_schema TEXT Postgres table_schema, default is: public --output_file TEXT Path for generated markdown file + --max_length To display maximum length of character varying, default is: False --help Show this message and exit. ``` diff --git a/pg_table_markdown/__init__.py b/pg_table_markdown/__init__.py index b886c80..1af59f6 100644 --- a/pg_table_markdown/__init__.py +++ b/pg_table_markdown/__init__.py @@ -9,13 +9,14 @@ TABLE_HEADER = 'Column | Type | Default | Nullable \n' TABLE_DIVIDER = '--- | --- | --- | --- \n' TABLE_ROW = '{column_name} | {data_type} | {column_default} | {is_nullable} \n' - +TABLE_ROW_WITH_MAXLENGTH = '{column_name} | {data_type}({character_maximum_length}) | {column_default} | {is_nullable} \n' @click.command() @click.option('--database_url', prompt=True, help='Database connection URL') @click.option('--table_schema', default='public', help='Postgres table_schema, default is: public') @click.option('--output_file', prompt=True, help='Path for generated markdown file') -def cli(database_url, table_schema, output_file): +@click.option('--max_length', is_flag=True, help='To display maximum length of character varying, default is: False') +def cli(database_url, table_schema, output_file, max_length): """ Export Postgres table documentation to a markdown file """ @@ -37,5 +38,8 @@ def cli(database_url, table_schema, output_file): f.write(TABLE_HEADER) f.write(TABLE_DIVIDER) for column in parsed[table_name]: - f.write(TABLE_ROW.format(**column)) + if max_length and column['character_maximum_length'] is not None: + f.write(TABLE_ROW_WITH_MAXLENGTH.format(**column)) + else: + f.write(TABLE_ROW.format(**column)) f.write('\n') diff --git a/tests.py b/tests.py index 05d15f6..87e7dc3 100644 --- a/tests.py +++ b/tests.py @@ -16,7 +16,7 @@ DATABASE_PORT, DATABASE_NAME) OUTPUT_FILE = '/test/tables.md' - +OUTPUT_FILE2 = '/test/tables_with_length.md' def database_connection(): connection = psycopg2.connect( @@ -50,7 +50,8 @@ def build_table(self): ( id SERIAL PRIMARY KEY, email TEXT NOT NULL, - password TEXT NOT NULL + password TEXT NOT NULL, + display_name CHARACTER VARYING(256) ); """ cursor = db.cursor() @@ -73,12 +74,38 @@ def test_generate_table_markdown(self): self.assertEqual("id | integer | nextval('my_users_id_seq'::regclass) | NO \n", result[4]) self.assertEqual('email | text | None | NO \n', result[5]) self.assertEqual('password | text | None | NO \n', result[6]) - self.assertEqual('\n', result[7]) + self.assertEqual('display_name | character varying | None | YES \n',result[7]) + self.assertEqual('\n', result[8]) + + self.assertEqual('### my_whatever \n', result[9]) + self.assertEqual('\n', result[10]) + self.assertEqual('Column | Type | Default | Nullable \n', result[11]) + self.assertEqual('--- | --- | --- | --- \n', result[12]) + self.assertEqual("id | integer | nextval('my_whatever_id_seq'::regclass) | NO \n", result[13]) + self.assertEqual('stuff | text | None | NO \n', result[14]) + self.assertEqual('\n', result[15]) + + output_file = '--output_file={0}'.format(OUTPUT_FILE2) + max_length = '--max_length' + proc = subprocess.Popen(['pgtablemd', database_url, table_schema, output_file, max_length]) + proc.communicate() + with open(OUTPUT_FILE2, 'r') as f: + result = f.readlines() + + self.assertEqual('### my_users \n', result[0]) + self.assertEqual('\n', result[1]) + self.assertEqual('Column | Type | Default | Nullable \n', result[2]) + self.assertEqual('--- | --- | --- | --- \n', result[3]) + self.assertEqual("id | integer | nextval('my_users_id_seq'::regclass) | NO \n", result[4]) + self.assertEqual('email | text | None | NO \n', result[5]) + self.assertEqual('password | text | None | NO \n', result[6]) + self.assertEqual('display_name | character varying(256) | None | YES \n',result[7]) + self.assertEqual('\n', result[8]) - self.assertEqual('### my_whatever \n', result[8]) - self.assertEqual('\n', result[9]) - self.assertEqual('Column | Type | Default | Nullable \n', result[10]) - self.assertEqual('--- | --- | --- | --- \n', result[11]) - self.assertEqual("id | integer | nextval('my_whatever_id_seq'::regclass) | NO \n", result[12]) - self.assertEqual('stuff | text | None | NO \n', result[13]) - self.assertEqual('\n', result[14]) + self.assertEqual('### my_whatever \n', result[9]) + self.assertEqual('\n', result[10]) + self.assertEqual('Column | Type | Default | Nullable \n', result[11]) + self.assertEqual('--- | --- | --- | --- \n', result[12]) + self.assertEqual("id | integer | nextval('my_whatever_id_seq'::regclass) | NO \n", result[13]) + self.assertEqual('stuff | text | None | NO \n', result[14]) + self.assertEqual('\n', result[15])