-
Notifications
You must be signed in to change notification settings - Fork 11
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
mock flink sql gateway client #59
Conversation
I hope you don't mind. I have converted this into draft. I see that you are still working on it. Please use Ready for review button once this is ready. |
test_seeds.py passed
|
for i in range(0, len(stats)): | ||
assert sql_equivalent(stats[i], expect_sql_sequential[i]) | ||
|
||
# run a second time, need clean |
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.
Why do we need to test the same thing twice?
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.
just an example in case someone need run operation (like dbt-run) multiple time in one function, i'll remove it
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.
Thanks, with this mock it won't test anything new but with actual Flink instance it will be a valid test.
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.
done
tests/conftest.py
Outdated
} | ||
|
||
# we need setup MockFlinkSqlGatewayClient | ||
MockFlinkSqlGatewayClient.create_session( |
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.
Can we make it to create and use instant instead of using it like singleton?
This will lead to problems in case of running tests in parallel or just not cleaning after in some single test.
Tests by nature should be isolated from each other as much as possible.
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.
you are absolutely right, i'll handle it
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.
done
""" | ||
] | ||
stats = MockFlinkSqlGatewayClient.all_statements() | ||
assert len(stats) == len(expect_sql_sequential) |
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.
This code could be extracted to separate class like AssertionUtils which we could add as extension to test cases and in tests just call
self.assertSqlEquals(mock.all_statements(), expected_sqls)
very similar how you would use regular asserts in unittests https://docs.python.org/3/library/unittest.html
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
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.
done
@@ -46,6 +48,7 @@ | |||
select /** fetch_timeout_ms(10000) */ /** fetch_mode('streaming') */ * from base where id = 10 | |||
""" | |||
|
|||
|
|||
class TestSeeds(): |
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.
Could I ask you to move this class to new directory named component
so we we will have unit tests in regular directories that correspond to modules its testing, component
which will have all the tests with the mock you have created and then e2e
for tests with actual Flink intance. What do you think?
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.
done
tests/sqlgateway/mock/gw_router.py
Outdated
@staticmethod | ||
def start(): | ||
httpretty.enable() | ||
# DO NOT change register order, httpretty regex match = first match |
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.
Please remove copied comment.
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.
done
tests/sqlgateway/mock/mock_client.py
Outdated
return MockFlinkSqlGatewayClient.router.all_statements() | ||
|
||
|
||
def sql_equivalent(s1: str, s2: str) -> bool: |
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.
This would land in the assert utils
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.
done
tests/tmp/test_mock_gw.py
Outdated
self.assertEqual(200, r.status_code) | ||
print(r.text) | ||
|
||
# session 测试 ========= |
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.
Please keep comments in English
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.
-_-
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.
I hope that's not a problem.
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.
done
done |
tests/component/e2e/test_seeds.py
Outdated
@@ -0,0 +1,106 @@ | |||
import os |
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.
We have got small missunderstanding. This would be just a component test as it is using mock instead of real Flink, so should be in test/component
in the future we should create an end-to-end test that will start actual Flink instance during test and it would be kept in test/e2e
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.
ok
tests/sqlgateway/mock/mock_client.py
Outdated
return self.router.all_statements() | ||
|
||
@staticmethod | ||
def setup(host: str = None, port: str | int = None, session_name: str = None): |
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.
the union |
operator is as far as I am aware available from 3.10. It has unfortunately still used by very few people https://w3techs.com/technologies/history_details/pl-python/3
in setup.py we are stating this library is using Python 3.7 or newer. Can we get rid of this for now? Maybe lets standardize port as int
type.
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.
ok
tests/sqlgateway/mock/mock_client.py
Outdated
import json | ||
|
||
|
||
class MockFlinkSqlGatewayClient: |
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.
I got very confused by this name. I initially thought this is a replacment for flink.sqlgateway.client.FlinkSqlGatewayClient
but it is actually a Mock of an SqlGateway and we still use the regular client. So maybe the better name would be just MockFlinkSqlGateway
?
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.
that is a mock sqlgateway
tests/sqlgateway/mock/mock_client.py
Outdated
self.router = GwRouter(test_config) | ||
self.router.start() | ||
# create session | ||
r = requests.post(f"{host_port}/v1/sessions", json.dumps({"sessionName": f"{session_name}"})) |
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.
This seems like not actually used. It will be the real flink.sqlgateway.client.FlinkSqlGatewayClient
that will be opening the session anyway.
tests/sqlgateway/mock/mock_client.py
Outdated
session_handle = r.json()['sessionHandle'] | ||
session = SqlGatewaySession(SqlGatewayConfig(host, port, session_name), session_handle) | ||
self.session = session | ||
return session |
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.
This return is ignored by rest of the code.
tests/sqlgateway/mock/mock_client.py
Outdated
self.session = session | ||
return session | ||
|
||
def execute_statement(self, sql: str) -> SqlGatewayOperation: |
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.
This is not used.
@zqWu Sorry for the delay. Had finally a chance to actually run properly your PR. This looks really great! Thank you for that work. I have left some comments. Looked like you initially wanted to substitute Thanks for changing it to use instance. I am not familiar with that library, would it work if we would create few instances (but using different port each time) in parallel? This is problem for another time anyway. Also in the future I guess we will need to add more complex behaviors like making it return error or some specific response. Please check my comments and then I can merge it. |
thanks for review, i'll handle the problems above |
i have implemented a mock sqlgateway, with some test code. i think, it will be useful for some test ( not stream and some flink specific sql) for real e2e test when not use default_catalog, it requires:
these works seems heavy to repeat |
Should be doable using testcontainers. This is something to be left for the future. |
else: | ||
raise "NOT support" | ||
|
||
def _execute_by_sqlite(self, sql) -> list[Any]: |
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.
Return type should be List[Any]
imported from typing
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.
done
return [x.replace(pattern, chars) for x in r] if pattern else r | ||
|
||
@staticmethod | ||
def _add_data_node(res_list, kind, fields: str | List[Any]): |
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.
Union of types is not supported in Python < 3.10
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.
done
The file paths are unnecessary nested. Component tests should just be in |
my branch for this pr is messy, gonna close this and promot another |
Description
a mock sql flink client
Resolves #34
PR Checklist