Skip to content

Commit 8d4c96d

Browse files
committed
fix default metadata on postings accounts
1 parent e253445 commit 8d4c96d

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

internal/controller/ledger/controller_default.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,23 @@ func (ctrl *DefaultController) createTransaction(ctx context.Context, store Stor
428428
for k, v := range values {
429429
accountMetadata[account][k] = v
430430
}
431-
432431
if schema != nil {
433432
schema.Chart.InsertDefaultAccountMetadata(account, accountMetadata[account])
434433
}
435434
}
436435
}
436+
if schema != nil {
437+
for _, posting := range result.Postings {
438+
if accountMetadata[posting.Source] == nil {
439+
accountMetadata[posting.Source] = metadata.Metadata{}
440+
}
441+
schema.Chart.InsertDefaultAccountMetadata(posting.Source, accountMetadata[posting.Source])
442+
if accountMetadata[posting.Destination] == nil {
443+
accountMetadata[posting.Destination] = metadata.Metadata{}
444+
}
445+
schema.Chart.InsertDefaultAccountMetadata(posting.Destination, accountMetadata[posting.Destination])
446+
}
447+
}
437448

438449
transaction := ledger.NewTransaction().
439450
WithPostings(result.Postings...).

internal/controller/ledger/controller_default_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,18 @@ func testCreateTransaction(t *testing.T, withSchema bool) {
9090
Return(nil, nil)
9191
}
9292

93-
store.EXPECT().
94-
CommitTransaction(gomock.Any(), gomock.Any(), map[string]metadata.Metadata{}).
95-
Return(nil)
93+
if withSchema {
94+
store.EXPECT().
95+
CommitTransaction(gomock.Any(), gomock.Any(), map[string]metadata.Metadata{
96+
"world": {},
97+
"bank": {},
98+
}).
99+
Return(nil)
100+
} else {
101+
store.EXPECT().
102+
CommitTransaction(gomock.Any(), gomock.Any(), map[string]metadata.Metadata{}).
103+
Return(nil)
104+
}
96105

97106
store.EXPECT().
98107
InsertLog(gomock.Any(), gomock.Cond(func(x any) bool {

test/e2e/api_schema_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ var _ = Context("Ledger schema API tests", func() {
313313
V2PostTransaction: components.V2PostTransaction{
314314
Force: pointer.For(true),
315315
AccountMetadata: map[string]map[string]string{
316-
"users:001": {
316+
"users:002": {
317317
"bar": "test2",
318318
},
319319
},
@@ -335,11 +335,22 @@ var _ = Context("Ledger schema API tests", func() {
335335
})
336336
Expect(err).To(BeNil())
337337

338+
// involved in a posting
338339
res, err := Wait(specContext, DeferClient(testServer)).Ledger.V2.GetAccount(ctx, operations.V2GetAccountRequest{
339340
Ledger: "default",
340341
Address: "users:001",
341342
})
342343
Expect(err).To(BeNil())
344+
Expect(res.V2AccountResponse.Data.Metadata).To(Equal(map[string]string{
345+
"foo": "test",
346+
}))
347+
348+
// involved in accountMetadata
349+
res, err = Wait(specContext, DeferClient(testServer)).Ledger.V2.GetAccount(ctx, operations.V2GetAccountRequest{
350+
Ledger: "default",
351+
Address: "users:002",
352+
})
353+
Expect(err).To(BeNil())
343354
Expect(res.V2AccountResponse.Data.Metadata).To(Equal(map[string]string{
344355
"foo": "test",
345356
"bar": "test2",

0 commit comments

Comments
 (0)