-
Notifications
You must be signed in to change notification settings - Fork 103
Created faking and mocking functions #346
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
base: main
Are you sure you want to change the base?
Conversation
mock and fake functions
…in/pgtap into fake_and_mock_functions just pulling from github
…cements to other routines
Fake and mock functions
|
@theory Hi! Could you please let me know what you think about my PR. I'd appreciate it. |
|
Sorry, @v-maliutin, hyper busy rn, but will get to it. |
|
Not a full review, but a couple comments: AFAIK, there's no reason to modify the Instead of building a function body via |
@jnasbyupgrade thank you for your review. Rolled back changes in Unpackaged |
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.
Looks super interesting, though I'm not sure they need to be procedures. I requested some change, but the big thing that's missing here is documentation. It would be super helpful for comments in the code to explain what each bit is doing and why (primary example, when it creates two mock functions without explanation). But also the new features should be documented in doc/pgtap.mmd, perhaps with a tutorial.
|
Hi, I am going to implement the following features. Please let me know if you think they are a good idea too.
|
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.
Honestly this is a huge feature that I think should probably be its own extension, either distributed separately and depending on pgtap (perhaps a release that includes changes to tap_funky that it requires) or else a separate extension as part of the pgTAP distribution. I'm leaning toward the former, at least as you work through the process, because I'm not sure I have the capacity to take on maintaining such a huge feature I don't use. I'm happy to review code and make recommendations if you decide to distribute it as its own extension.
| Mocking, faking and making your test independent | ||
| ================================================ | ||
|
|
||
| Examples of implementing mocking and faking functionality can be found in my fork [`on GitHub`](https://github.com/v-maliutin/pgtap/tree/examples_forking_and_mocking/examples). |
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.
For permanent documentation I wouldn't refer to "my fork", just link to the source.
|
|
||
| Let's say we have a main table and a subordinate table. | ||
|
|
||
| CREATE TABLE master( |
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.
Let's avoid the term "master"; how about "parent"?
|
|
||
| ### `fake_table()` ### | ||
|
|
||
| PERFORM fake_table( :_table_ident, :_make_table_empty, :_leave_primary_key, :_drop_not_null, :_drop_collation, :_drop_partitions ); |
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.
None of the existing doc params have underscores; please remove them.
| **Parameters** | ||
|
|
||
| `:_table_ident` | ||
| : Text array of tables to be faked in the format 'my_schema.my_table'. Required. |
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 does this param support multiple tables but the rest refer to a single table?
| _drop_collation => false | ||
| ); | ||
|
|
||
| One note. The parameters _leave_primary_key and _drop_not_null are currently contradictory. |
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.
Use backticks for parameter names.
| _func_schema text | ||
| , _func_name text | ||
| , _func_args text | ||
| , _return_set_value text default null | ||
| , _return_scalar_value anyelement default null::text |
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.
Remove leading underscores.
| return query execute _query(_name); | ||
| end; | ||
| %4$sfunction%4$s;', | ||
| _func_schema/*1*/, _func_name/*2*/, _func_result_type/*3*/, '$'/*4*/); |
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.
So it replaces the function in the same schema? I presume you're depending on a transaction ROLLBACK to rollback the mock function, yes?
| _func_schema/*1*/, _func_name/*2*/, _func_args/*3*/, _func_result_type/*4*/, | ||
| _func_language/*5*/, _return_scalar_value/*6*/, pg_typeof(_return_scalar_value)/*7*/, '$'/*8*/); | ||
| execute _mock_ddl; | ||
| end if; |
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.
Seems like there are some conditions that are ignored here, yes? Should it raise an error for unsupported conditions, e.g., a set-returning C function.
| _json text; | ||
| _columns text; | ||
| --returns a query which you can execute and see your table as normal dataset | ||
| --you can find the returned query in the output window in DBeaver, where we see raise notice command output |
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 don't understand what Beaver has to do with this. I have never used it, and it shouldn't be a requirement for users to have it.
|
|
||
| CREATE FUNCTION test_faking_functionality() RETURNS SETOF TEXT AS $$ | ||
| BEGIN | ||
| perform fake_table( |
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 use of procedures will either require that this functionality not be part of pgTAP or that there be patches to remove it for Postgres versions that don't support it.
Seems like it, yes, but again I think this is full-featured enough that it might be better developed and distributed as its own extension. |
I developed some functions to make writing tests easier. There are examples in the source code. Also, I wrote an article on habr. Here it is https://habr.com/ru/companies/gnivc/articles/877314/. I know it is in Russian, but hope modern translation will help.
Anyway such functionality is in any modern language and wide known. I just bringing it in PG world.
One more thing. I am going to introduce my work and pgTap at pgConf in Moscow on March 31 2025.