|
8 | 8 | "github.com/stretchr/testify/require"
|
9 | 9 |
|
10 | 10 | billingentity "github.com/openmeterio/openmeter/openmeter/billing/entity"
|
| 11 | + "github.com/openmeterio/openmeter/pkg/clock" |
11 | 12 | )
|
12 | 13 |
|
13 | 14 | type idDiff = diff[string]
|
@@ -263,6 +264,105 @@ func TestInvoiceLineDiffing(t *testing.T) {
|
263 | 264 | },
|
264 | 265 | }, lineDiff)
|
265 | 266 | })
|
| 267 | + |
| 268 | + // DeletedAt handling |
| 269 | + t.Run("support for detailed lines being deleted using deletedAt", func(t *testing.T) { |
| 270 | + base := cloneLines(template) |
| 271 | + snapshotAsDBState(base) |
| 272 | + |
| 273 | + base[1].Children.GetByID("2.1").DeletedAt = lo.ToPtr(clock.Now()) |
| 274 | + |
| 275 | + lineDiff, err := diffInvoiceLines(base) |
| 276 | + require.NoError(t, err) |
| 277 | + |
| 278 | + requireDiff(t, lineDiffExpectation{ |
| 279 | + AffectedLineIDs: []string{"2"}, |
| 280 | + ChildrenDiff: &lineDiffExpectation{ |
| 281 | + LineBase: idDiff{ |
| 282 | + ToDelete: []string{"2.1"}, |
| 283 | + }, |
| 284 | + Discounts: idDiff{ |
| 285 | + ToDelete: []string{"D2.1.1"}, |
| 286 | + }, |
| 287 | + }, |
| 288 | + }, lineDiff) |
| 289 | + }) |
| 290 | + |
| 291 | + t.Run("support for parent lines with children being deleted using deletedAt", func(t *testing.T) { |
| 292 | + base := cloneLines(template) |
| 293 | + snapshotAsDBState(base) |
| 294 | + |
| 295 | + base[1].DeletedAt = lo.ToPtr(clock.Now()) |
| 296 | + |
| 297 | + lineDiff, err := diffInvoiceLines(base) |
| 298 | + require.NoError(t, err) |
| 299 | + |
| 300 | + requireDiff(t, lineDiffExpectation{ |
| 301 | + LineBase: idDiff{ |
| 302 | + ToDelete: []string{"2"}, |
| 303 | + }, |
| 304 | + ChildrenDiff: &lineDiffExpectation{ |
| 305 | + LineBase: idDiff{ |
| 306 | + ToDelete: []string{"2.1", "2.2"}, |
| 307 | + }, |
| 308 | + Discounts: idDiff{ |
| 309 | + ToDelete: []string{"D2.1.1"}, |
| 310 | + }, |
| 311 | + }, |
| 312 | + }, lineDiff) |
| 313 | + }) |
| 314 | + |
| 315 | + t.Run("support for parent lines without children being deleted using deletedAt", func(t *testing.T) { |
| 316 | + base := cloneLines(template) |
| 317 | + snapshotAsDBState(base) |
| 318 | + |
| 319 | + base[0].DeletedAt = lo.ToPtr(clock.Now()) |
| 320 | + |
| 321 | + lineDiff, err := diffInvoiceLines(base) |
| 322 | + require.NoError(t, err) |
| 323 | + |
| 324 | + requireDiff(t, lineDiffExpectation{ |
| 325 | + LineBase: idDiff{ |
| 326 | + ToDelete: []string{"1"}, |
| 327 | + }, |
| 328 | + }, lineDiff) |
| 329 | + }) |
| 330 | + |
| 331 | + t.Run("deleted, changed lines are not triggering updates", func(t *testing.T) { |
| 332 | + base := cloneLines(template) |
| 333 | + base[0].DeletedAt = lo.ToPtr(clock.Now()) |
| 334 | + snapshotAsDBState(base) |
| 335 | + base[0].Description = lo.ToPtr("test") |
| 336 | + |
| 337 | + lineDiff, err := diffInvoiceLines(base) |
| 338 | + require.NoError(t, err) |
| 339 | + |
| 340 | + requireDiff(t, lineDiffExpectation{}, lineDiff) |
| 341 | + }) |
| 342 | + |
| 343 | + t.Run("deleted, changed lines are not triggering updates", func(t *testing.T) { |
| 344 | + base := cloneLines(template) |
| 345 | + base[1].DeletedAt = lo.ToPtr(clock.Now()) |
| 346 | + base[1].Children.GetByID("2.1").DeletedAt = lo.ToPtr(clock.Now()) |
| 347 | + |
| 348 | + snapshotAsDBState(base) |
| 349 | + base[1].DeletedAt = nil |
| 350 | + base[1].Children.GetByID("2.1").DeletedAt = nil |
| 351 | + |
| 352 | + lineDiff, err := diffInvoiceLines(base) |
| 353 | + require.NoError(t, err) |
| 354 | + |
| 355 | + requireDiff(t, lineDiffExpectation{ |
| 356 | + LineBase: idDiff{ |
| 357 | + ToUpdate: []string{"2"}, |
| 358 | + }, |
| 359 | + ChildrenDiff: &lineDiffExpectation{ |
| 360 | + LineBase: idDiff{ |
| 361 | + ToUpdate: []string{"2.1"}, |
| 362 | + }, |
| 363 | + }, |
| 364 | + }, lineDiff) |
| 365 | + }) |
266 | 366 | }
|
267 | 367 |
|
268 | 368 | func mapLinesToIDs(lines []*billingentity.Line) []string {
|
|
0 commit comments