Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenharrison committed Nov 25, 2024
1 parent 9643eed commit f22314d
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 10 deletions.
2 changes: 1 addition & 1 deletion checker/api_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestApiChange_SourceUrl(t *testing.T) {
require.Equal(t, "", apiChangeSourceFile.GetSourceFile())
}

func TestApiChange_WithOrigin(t *testing.T) {
func TestApiChange_WithLocation(t *testing.T) {
apiChangeSourceFile := apiChange.WithLocation(&openapi3.Origin{
Fields: map[string]openapi3.Location{"field": {
Line: 1,
Expand Down
41 changes: 32 additions & 9 deletions checker/check_request_body_required_value_updated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,46 @@ func TestRequestBodyBecameRequired(t *testing.T) {

// CL: changing request's body to optional
func TestRequestBodyBecameOptional(t *testing.T) {
s1, err := open("../data/checker/request_body_became_optional_base.yaml")
s1, err := openWithLocation("../data/checker/request_body_became_optional_base.yaml")
require.NoError(t, err)
s2, err := open("../data/checker/request_body_became_optional_base.yaml")
s2, err := openWithLocation("../data/checker/request_body_became_optional_revision.yaml")
require.NoError(t, err)

s2.Spec.Paths.Value("/api/v1.0/groups").Post.RequestBody.Value.Required = false
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestBodyRequiredUpdatedCheck), d, osm, checker.INFO)
require.Len(t, errs, 1)
require.Equal(t, checker.ApiChange{
Id: checker.RequestBodyBecameOptionalId,
Level: checker.INFO,
Operation: "POST",
Path: "/api/v1.0/groups",
Source: load.NewSource("../data/checker/request_body_became_optional_revision.yaml"),
OperationId: "createOneGroup",
SourceLine: 19,
SourceColumn: 9,
}, errs[0])
}

// CL: changing request's body to optional by deletion
func TestRequestBodyBecameOptionalDeleted(t *testing.T) {
s1, err := openWithLocation("../data/checker/request_body_became_optional_base.yaml")
require.NoError(t, err)
s2, err := openWithLocation("../data/checker/request_body_became_optional_deleted.yaml")
require.NoError(t, err)

d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestBodyRequiredUpdatedCheck), d, osm, checker.INFO)
require.Len(t, errs, 1)
require.Equal(t, checker.ApiChange{
Id: checker.RequestBodyBecameOptionalId,
Level: checker.INFO,
Operation: "POST",
Path: "/api/v1.0/groups",
Source: load.NewSource("../data/checker/request_body_became_optional_base.yaml"),
OperationId: "createOneGroup",
Id: checker.RequestBodyBecameOptionalId,
Level: checker.INFO,
Operation: "POST",
Path: "/api/v1.0/groups",
Source: load.NewSource("../data/checker/request_body_became_optional_deleted.yaml"),
OperationId: "createOneGroup",
SourceLine: 9,
SourceColumn: 5,
}, errs[0])
}
59 changes: 59 additions & 0 deletions data/checker/request_body_became_optional_deleted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
info:
title: Tufin
version: "2.0"
servers:
- url: https://localhost:9080
paths:
/api/v1.0/groups:
post:
tags:
- Group
operationId: createOneGroup
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/GroupView'
description: Creates one project.
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/GroupView'
description: OK
"409":
content:
application/json:
schema:
$ref: '#/components/schemas/GroupView'
description: Conflict
summary: Create One Project
components:
parameters:
groupId:
in: path
name: groupId
required: true
schema:
type: string
schemas:
GroupView:
type: object
properties:
data:
type: object
properties:
created:
type: string
format: date-time
readOnly: true
pattern: "^[a-z]+$"
id:
type: string
readOnly: true
name:
type: string
required:
- name
60 changes: 60 additions & 0 deletions data/checker/request_body_became_optional_revision.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
openapi: 3.0.1
info:
title: Tufin
version: "2.0"
servers:
- url: https://localhost:9080
paths:
/api/v1.0/groups:
post:
tags:
- Group
operationId: createOneGroup
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/GroupView'
description: Creates one project.
required: false
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/GroupView'
description: OK
"409":
content:
application/json:
schema:
$ref: '#/components/schemas/GroupView'
description: Conflict
summary: Create One Project
components:
parameters:
groupId:
in: path
name: groupId
required: true
schema:
type: string
schemas:
GroupView:
type: object
properties:
data:
type: object
properties:
created:
type: string
format: date-time
readOnly: true
pattern: "^[a-z]+$"
id:
type: string
readOnly: true
name:
type: string
required:
- name
1 change: 1 addition & 0 deletions docs/BREAKING-CHANGES-EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ These examples are automatically generated from unit tests.
[changing request property type](../checker/check_request_property_type_changed_test.go?plain=1#L64)
[changing request query parameter format](../checker/check_request_parameters_type_changed_test.go?plain=1#L110)
[changing request query parameter type](../checker/check_request_parameters_type_changed_test.go?plain=1#L38)
[changing request's body to optional by deletion](../checker/check_request_body_required_value_updated_test.go?plain=1#L58)
[changing request's body to optional](../checker/check_request_body_required_value_updated_test.go?plain=1#L35)
[changing request's body to required is breaking](../checker/check_request_body_required_value_updated_test.go?plain=1#L12)
[changing required request property to not read-only](../checker/check_request_property_write_only_read_only_test.go?plain=1#L187)
Expand Down

0 comments on commit f22314d

Please sign in to comment.