|
15 | 15 | package apiversion |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "github.com/getkin/kin-openapi/openapi3" |
18 | 19 | "testing" |
19 | 20 | "time" |
20 | 21 |
|
@@ -402,3 +403,95 @@ func TestNew_WithVersion(t *testing.T) { |
402 | 403 | }) |
403 | 404 | } |
404 | 405 | } |
| 406 | + |
| 407 | +func TestFindLatestContentVersionMatched(t *testing.T) { |
| 408 | + testCases := []struct { |
| 409 | + name string |
| 410 | + targetVersion string |
| 411 | + expectedMatch string |
| 412 | + }{ |
| 413 | + { |
| 414 | + name: "exact match 2023-01-01", |
| 415 | + targetVersion: "2023-01-01", |
| 416 | + expectedMatch: "2023-01-01", |
| 417 | + }, |
| 418 | + { |
| 419 | + name: "exact match 2023-11-15", |
| 420 | + targetVersion: "2023-11-15", |
| 421 | + expectedMatch: "2023-11-15", |
| 422 | + }, |
| 423 | + { |
| 424 | + name: "exact match 2024-05-30", |
| 425 | + targetVersion: "2024-05-30", |
| 426 | + expectedMatch: "2024-05-30", |
| 427 | + }, |
| 428 | + { |
| 429 | + name: "approx match 2023-01-01", |
| 430 | + targetVersion: "2023-01-02", |
| 431 | + expectedMatch: "2023-01-01", |
| 432 | + }, |
| 433 | + { |
| 434 | + name: "approx match 2023-01-01", |
| 435 | + targetVersion: "2023-01-31", |
| 436 | + expectedMatch: "2023-01-01", |
| 437 | + }, |
| 438 | + { |
| 439 | + name: "approx match 2023-02-01", |
| 440 | + targetVersion: "2023-02-20", |
| 441 | + expectedMatch: "2023-02-01", |
| 442 | + }, |
| 443 | + { |
| 444 | + name: "future date", |
| 445 | + targetVersion: "2030-02-20", |
| 446 | + expectedMatch: "2024-05-30", |
| 447 | + }, |
| 448 | + { |
| 449 | + name: "past date", |
| 450 | + targetVersion: "1999-02-20", |
| 451 | + expectedMatch: "1999-02-20", |
| 452 | + }, |
| 453 | + } |
| 454 | + |
| 455 | + for _, tt := range testCases { |
| 456 | + t.Run(tt.name, func(t *testing.T) { |
| 457 | + t.Parallel() |
| 458 | + targetVersion, err := New(WithVersion(tt.targetVersion)) |
| 459 | + require.NoError(t, err) |
| 460 | + r := FindLatestContentVersionMatched(oasOperationAllVersions(), targetVersion) |
| 461 | + // transform time to str with format "2006-01-02" |
| 462 | + assert.Equal(t, tt.expectedMatch, r.String()) |
| 463 | + }) |
| 464 | + } |
| 465 | +} |
| 466 | + |
| 467 | +func oasOperationAllVersions() *openapi3.Operation { |
| 468 | + responses := &openapi3.Responses{} |
| 469 | + responses.Set("200", &openapi3.ResponseRef{ |
| 470 | + Value: &openapi3.Response{ |
| 471 | + Content: map[string]*openapi3.MediaType{ |
| 472 | + "application/vnd.atlas.2023-01-01+json": {}, |
| 473 | + "application/vnd.atlas.2023-01-01+csv": {}, |
| 474 | + "application/vnd.atlas.2023-02-01+json": {}, |
| 475 | + "application/vnd.atlas.2023-10-01+json": {}, |
| 476 | + "application/vnd.atlas.2023-11-15+json": {}, |
| 477 | + "application/vnd.atlas.2024-05-30+json": {}, |
| 478 | + }, |
| 479 | + }, |
| 480 | + }) |
| 481 | + |
| 482 | + return &openapi3.Operation{ |
| 483 | + OperationID: "operationId", |
| 484 | + Responses: responses, |
| 485 | + RequestBody: &openapi3.RequestBodyRef{ |
| 486 | + Value: &openapi3.RequestBody{ |
| 487 | + Content: map[string]*openapi3.MediaType{ |
| 488 | + "application/vnd.atlas.2023-01-01+json": {}, |
| 489 | + "application/vnd.atlas.2023-02-01+json": {}, |
| 490 | + "application/vnd.atlas.2023-10-01+json": {}, |
| 491 | + "application/vnd.atlas.2023-11-15+json": {}, |
| 492 | + "application/vnd.atlas.2024-05-30+json": {}, |
| 493 | + }, |
| 494 | + }, |
| 495 | + }, |
| 496 | + } |
| 497 | +} |
0 commit comments