-
Notifications
You must be signed in to change notification settings - Fork 319
Add a decorator for creating table via class-style. #823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YogiLiu
wants to merge
6
commits into
kayak:master
Choose a base branch
from
YogiLiu:feat/class-style
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03ed2d0
feat(queries): add a decorator for creating table via class-style
YogiLiu a97ff6f
docs(README): fix code block
YogiLiu e594506
docs(README): fix typo
YogiLiu 8463987
docs(README): fix typo
YogiLiu 2846493
fix(queries): set table to field in table_class
YogiLiu 2187289
fix(queries): remove str field
YogiLiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,245 @@ | ||||||
| # Most of test cases are copied and modified from test_tables.py | ||||||
|
|
||||||
| import unittest | ||||||
|
|
||||||
| from pypika import Database, Dialects, Schema, SQLLiteQuery, Table, SYSTEM_TIME, table_class, Field | ||||||
|
|
||||||
|
|
||||||
| class TableStructureTests(unittest.TestCase): | ||||||
| def test_table_sql(self): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertEqual('"test_table"', str(T)) | ||||||
|
|
||||||
| def test_table_with_no_superclass(self): | ||||||
| with self.assertRaises(TypeError): | ||||||
| @table_class("test_table") | ||||||
| class T: | ||||||
| pass | ||||||
|
|
||||||
| def test_table_with_bad_superclass(self): | ||||||
| with self.assertRaises(TypeError): | ||||||
| @table_class("test_table") | ||||||
| class T(object): | ||||||
| pass | ||||||
|
|
||||||
| def test_table_with_alias(self): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| table = T.as_("my_table") | ||||||
|
|
||||||
| self.assertEqual('"test_table" "my_table"', table.get_sql(with_alias=True, quote_char='"')) | ||||||
|
|
||||||
| def test_table_with_schema_arg(self): | ||||||
| @table_class("test_table", schema=Schema("x_schema")) | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertEqual('"x_schema"."test_table"', str(T)) | ||||||
|
|
||||||
| def test_table_with_field(self): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| f = Field('f') | ||||||
|
|
||||||
| self.assertEqual('"f"', T.f.get_sql(with_alias=True, quote_char='"')) | ||||||
| self.assertEqual(id(T), id(T.f.table)) | ||||||
|
|
||||||
| def test_table_with_field_and_ailas(self): | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| f = Field('f', alias='my_f') | ||||||
|
|
||||||
| self.assertEqual('"f" "my_f"', T.f.get_sql(with_alias=True, quote_char='"')) | ||||||
| self.assertEqual(id(T), id(T.f.table)) | ||||||
|
|
||||||
| def test_table_with_unset_field(self): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertEqual('"f"', T.f.get_sql(with_alias=True, quote_char='"')) | ||||||
| self.assertEqual(id(T), id(T.f.table)) | ||||||
|
|
||||||
| def test_table_with_schema_and_schema_parent_arg(self): | ||||||
| @table_class("test_table", schema=Schema("x_schema", parent=Database("x_db"))) | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertEqual('"x_db"."x_schema"."test_table"', str(T)) | ||||||
|
|
||||||
| def test_table_for_system_time_sql(self): | ||||||
| with self.subTest("with between criterion"): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| table = T.for_(SYSTEM_TIME.between('2020-01-01', '2020-02-01')) | ||||||
|
|
||||||
| self.assertEqual('"test_table" FOR SYSTEM_TIME BETWEEN \'2020-01-01\' AND \'2020-02-01\'', str(table)) | ||||||
|
|
||||||
| with self.subTest("with as of criterion"): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| table = T.for_(SYSTEM_TIME.as_of('2020-01-01')) | ||||||
|
|
||||||
| self.assertEqual('"test_table" FOR SYSTEM_TIME AS OF \'2020-01-01\'', str(table)) | ||||||
|
|
||||||
| with self.subTest("with from to criterion"): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| table = T.for_(SYSTEM_TIME.from_to('2020-01-01', '2020-02-01')) | ||||||
|
|
||||||
| self.assertEqual('"test_table" FOR SYSTEM_TIME FROM \'2020-01-01\' TO \'2020-02-01\'', str(table)) | ||||||
|
|
||||||
| def test_table_for_period_sql(self): | ||||||
| with self.subTest("with between criterion"): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| table = T.for_(T.valid_period.between('2020-01-01', '2020-02-01')) | ||||||
|
|
||||||
| self.assertEqual('"test_table" FOR "valid_period" BETWEEN \'2020-01-01\' AND \'2020-02-01\'', str(table)) | ||||||
|
|
||||||
| with self.subTest("with as of criterion"): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| table = T.for_(T.valid_period.as_of('2020-01-01')) | ||||||
|
|
||||||
| self.assertEqual('"test_table" FOR "valid_period" AS OF \'2020-01-01\'', str(table)) | ||||||
|
|
||||||
| with self.subTest("with from to criterion"): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| table = T.for_(T.valid_period.from_to('2020-01-01', '2020-02-01')) | ||||||
|
|
||||||
| self.assertEqual('"test_table" FOR "valid_period" FROM \'2020-01-01\' TO \'2020-02-01\'', str(table)) | ||||||
|
|
||||||
|
|
||||||
| class TableEqualityTests(unittest.TestCase): | ||||||
| def test_tables_equal_by_name(self): | ||||||
| @table_class("test_table") | ||||||
| class T1(Table): | ||||||
| pass | ||||||
|
|
||||||
| @table_class("test_table") | ||||||
| class T2(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertEqual(T1, T2) | ||||||
|
|
||||||
| def test_tables_equal_by_schema_and_name(self): | ||||||
| @table_class("test_table", schema="a") | ||||||
| class T1(Table): | ||||||
| pass | ||||||
|
|
||||||
| @table_class("test_table", schema="a") | ||||||
| class T2(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertEqual(T1, T2) | ||||||
|
|
||||||
| def test_tables_equal_by_schema_and_name_using_schema(self): | ||||||
| a = Schema("a") | ||||||
|
|
||||||
| @table_class("test_table", schema=a) | ||||||
| class T1(Table): | ||||||
| pass | ||||||
|
|
||||||
| @table_class("test_table", schema=a) | ||||||
| class T2(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertEqual(T1, T2) | ||||||
|
|
||||||
| def test_tables_equal_by_schema_and_name_using_schema_with_parent(self): | ||||||
| parent = Schema("parent") | ||||||
| a = Schema("a", parent=parent) | ||||||
|
|
||||||
| @table_class("test_table", schema=a) | ||||||
| class T1(Table): | ||||||
| pass | ||||||
|
|
||||||
| @table_class("test_table", schema=a) | ||||||
| class T2(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertEqual(T1, T2) | ||||||
|
|
||||||
| def test_tables_not_equal_by_schema_and_name_using_schema_with_different_parents( | ||||||
| self, | ||||||
| ): | ||||||
| parent = Schema("parent") | ||||||
| a = Schema("a", parent=parent) | ||||||
|
|
||||||
| @table_class("test_table", schema=a) | ||||||
| class T1(Table): | ||||||
| pass | ||||||
|
|
||||||
| @table_class("test_table", schema=Schema("a")) | ||||||
| class T2(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertNotEqual(T1, T2) | ||||||
|
|
||||||
| def test_tables_not_equal_with_different_schemas(self): | ||||||
|
|
||||||
| @table_class("test_table", schema="a") | ||||||
| class T1(Table): | ||||||
| pass | ||||||
|
|
||||||
| @table_class("test_table", schema="b") | ||||||
| class T2(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertNotEqual(T1, T2) | ||||||
|
|
||||||
| def test_tables_not_equal_with_different_names(self): | ||||||
|
|
||||||
| @table_class("t", schema="a") | ||||||
| class T1(Table): | ||||||
| pass | ||||||
|
|
||||||
| @table_class("q", schema="a") | ||||||
| class T2(Table): | ||||||
| pass | ||||||
|
|
||||||
| self.assertNotEqual(T1, T2) | ||||||
|
|
||||||
|
|
||||||
| class TableDialectTests(unittest.TestCase): | ||||||
| def test_table_with_default_query_cls(self): | ||||||
| @table_class("test_table") | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| q = T.select("1") | ||||||
| self.assertIs(q.dialect, None) | ||||||
|
|
||||||
| def test_table_with_dialect_query_cls(self): | ||||||
|
|
||||||
| @table_class("test_table", query_cls=SQLLiteQuery) | ||||||
| class T(Table): | ||||||
| pass | ||||||
|
|
||||||
| q = T.select("1") | ||||||
| self.assertIs(q.dialect, Dialects.SQLLITE) | ||||||
|
|
||||||
| def test_table_with_bad_query_cls(self): | ||||||
| with self.assertRaises(TypeError): | ||||||
| @table_class("test_table", query_cls=object) | ||||||
| class T(Table): | ||||||
| pass | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.