-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Part 4|*] Add unit tests for the previous introduced SQL Backend methods #10292
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
[Part 4|*] Add unit tests for the previous introduced SQL Backend methods #10292
Conversation
415144d to
439b619
Compare
439b619 to
c922bc3
Compare
3648e20 to
ae6c366
Compare
|
/gemini review |
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.
Code Review
This pull request adds comprehensive unit tests for the newly introduced SQL backend for the payments database. The changes include refactoring existing tests to be database-agnostic, which is a great improvement for maintainability. The new SQL queries and the Go store implementation are well-structured and cover a wide range of functionalities. My main feedback is on adhering to the testing style guide in the new TestQueryPayments function, where t.Fatalf and t.Errorf should be replaced with assertions from the require library as mandated by the repository's style guide.
payments/db/payment_test.go
Outdated
| if err != nil { | ||
| t.Fatalf("unable to create test "+ | ||
| "payment: %v", err) | ||
| } |
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 project's style guide specifies that unit tests must use the require library for assertions.1 Please replace if err != nil { t.Fatalf(...) } with require.NoError(t, err, ...) for consistency and conciseness. This also applies to other t.Fatalf and t.Errorf calls in this test function, which should be replaced with appropriate require assertions (e.g., require.Equal, require.Len).
require.NoError(t, err, "unable to create test payment")Style Guide References
Footnotes
-
The style guide mandates the use of the
requirelibrary for assertions in unit tests to ensure consistency and readability. ↩
ae6c366 to
b011ea6
Compare
b515aaa to
bdbcc54
Compare
c486d3d to
363b01b
Compare
ziggie1984
left a 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.
I think Part 4 is also ready to land 🫡
| func genInfo(t *testing.T) (*PaymentCreationInfo, lntypes.Preimage, error) { | ||
|
|
||
| preimage, _, err := genPreimageAndHash(t) | ||
| if err != nil { |
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 will be done in Part 7 to avoid rebase collisions because of the whole stacked up series of payment PRs
yyforyongyu
left a 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.
Q: I just realized these flags are not added to the make file, and these are not running in the CI?
I ran this locally to get an understanding of the test coverage,
go test -v -tags=test_db_sqlite -coverprofile=cover.out ./payments/db && go tool cover -func=cover.out | grep "sql_store.go"ok github.com/lightningnetwork/lnd/payments/db 8.059s coverage: 33.7% of statements
github.com/lightningnetwork/lnd/payments/db/sql_store.go:119: NewSQLStore 83.3%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:146: fetchPaymentWithCompleteData 80.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:172: batchLoadPayments 0.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:204: batchLoadPaymentsCoreData 0.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:269: batchLoadPaymentCustomRecords 100.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:303: batchLoadHtlcAttempts 100.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:336: batchLoadHopsForAttempts 100.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:370: batchLoadHopCustomRecords 100.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:404: batchLoadRouteCustomRecords 42.9%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:443: batchLoadPaymentResolutions 81.8%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:491: loadPaymentResolutions 75.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:508: computePaymentStatusFromResolutions 100.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:550: batchLoadPaymentsRelatedData 72.7%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:630: buildPaymentFromBatchData 92.3%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:729: QueryPayments 92.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:901: fetchPaymentByHash 83.3%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:925: FetchPayment 92.3%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:975: FetchInFlightPayments 0.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1111: DeleteFailedAttempts 93.8%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1157: computePaymentStatusFromDB 75.0%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1209: DeletePayment 87.5%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1272: InitPayment 79.3%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1381: insertRouteHops 46.9%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1505: RegisterAttempt 76.5%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1632: SettleAttempt 81.8%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1707: FailAttempt 66.7%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1796: Fail 81.8%
github.com/lightningnetwork/lnd/payments/db/sql_store.go:1881: DeletePayments 91.2%
looks like FetchInFlightPayments is not tested.
Now that every method of the interface was implemented we can remove the embedded reference we put into place for the sql store implementation so that the interface would succeed. This is now removed.
Since now the sql backend is more strict in using the same session key we refactor the helper so that we can easily change the session key for every new attempt.
We make the QueryPayments test db agnostic and also keep a small test for querying the duplicate payments case in the kv world.
We are now not supporting the LegacyPayload for the onion packet anymore. All payments and their onion payload need to be tlv encoded. The sql backend assumes tlv so we have to always set the in memory presentation of a hop where the legacy parameter is still available but deprecated to false, otherwise the hops will not be equal and unit tests for the sql backend will fail when switched on in the next commits.
In commit adds the harness which will be used to run db agnostic tests against the kv and sql backend. We have adopted all the unit tests so far so that with this commit all the payment tests not specifically put into the kv_store_test.go should all pass for all backends.
The design of the sql and kv db are a bit different. A harness interface is introduced which allows us to unit most of the test and keep the backend specific tests at a minimum.
363b01b to
d7d635c
Compare
d7d635c to
cfc0dc0
Compare
|
Will add some additional unit test in PR 7, because I already refactored the helper methods as proposed by yy in this PR. |
|
Final coverage when all tests are being merged see PR 7: |
yyforyongyu
left a 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.
LGTM ![]()
ellemouton
left a 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.
lgtm!
4a6c7ab
into
lightningnetwork:elle-payment-sql-series-new

This PR adds the DB related unit tests for backend functions which were introduced in #10287,#10291 and #10368
and finalizes the implementation for the SQL backend.
This PR now lets the unit tests and the itest pass for the SQL payments backend.