-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_test.go
More file actions
107 lines (83 loc) · 2.81 KB
/
document_test.go
File metadata and controls
107 lines (83 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package openapi
import (
"encoding/json"
"testing"
)
func TestNewDocument(t *testing.T) {
doc := NewDocument("Test API", "1.0.0")
if doc.OpenAPI != "3.1.0" {
t.Errorf("Expected OpenAPI version '3.1.0', got '%s'", doc.OpenAPI)
}
if doc.Info.Title != "Test API" {
t.Errorf("Expected title 'Test API', got '%s'", doc.Info.Title)
}
if doc.Info.Version != "1.0.0" {
t.Errorf("Expected version '1.0.0', got '%s'", doc.Info.Version)
}
if doc.Paths == nil {
t.Error("Expected Paths to be initialized")
}
if doc.Tags == nil {
t.Error("Expected Tags to be initialized")
}
}
func TestDocumentMarshalJSON(t *testing.T) {
doc := NewDocument("Test API", "1.0.0")
doc.WithInfo("A test API", "")
data, err := json.Marshal(doc)
if err != nil {
t.Fatalf("Error marshaling document: %v", err)
}
// Test that we can unmarshal it back
var unmarshaled Document
err = json.Unmarshal(data, &unmarshaled)
if err != nil {
t.Fatalf("Error unmarshaling document: %v", err)
}
if unmarshaled.Info.Title != doc.Info.Title {
t.Errorf("Title mismatch after roundtrip: expected '%s', got '%s'", doc.Info.Title, unmarshaled.Info.Title)
}
}
func TestDocumentWithContact(t *testing.T) {
doc := NewDocument("Test API", "1.0.0")
doc.WithContact("Test Team", "https://example.com", "test@example.com")
if doc.Info.Contact == nil {
t.Fatal("Expected Contact to be set")
}
if doc.Info.Contact.Name != "Test Team" {
t.Errorf("Expected contact name 'Test Team', got '%s'", doc.Info.Contact.Name)
}
if doc.Info.Contact.URL != "https://example.com" {
t.Errorf("Expected contact URL 'https://example.com', got '%s'", doc.Info.Contact.URL)
}
if doc.Info.Contact.Email != "test@example.com" {
t.Errorf("Expected contact email 'test@example.com', got '%s'", doc.Info.Contact.Email)
}
}
func TestDocumentWithLicense(t *testing.T) {
doc := NewDocument("Test API", "1.0.0")
doc.WithLicense("MIT", "https://opensource.org/licenses/MIT")
if doc.Info.License == nil {
t.Fatal("Expected License to be set")
}
if doc.Info.License.Name != "MIT" {
t.Errorf("Expected license name 'MIT', got '%s'", doc.Info.License.Name)
}
if doc.Info.License.URL != "https://opensource.org/licenses/MIT" {
t.Errorf("Expected license URL 'https://opensource.org/licenses/MIT', got '%s'", doc.Info.License.URL)
}
}
func TestDocumentWithServer(t *testing.T) {
doc := NewDocument("Test API", "1.0.0")
doc.AddServer("https://api.example.com", "Production server")
if len(doc.Servers) != 1 {
t.Fatalf("Expected 1 server, got %d", len(doc.Servers))
}
server := doc.Servers[0]
if server.URL != "https://api.example.com" {
t.Errorf("Expected server URL 'https://api.example.com', got '%s'", server.URL)
}
if server.Description != "Production server" {
t.Errorf("Expected server description 'Production server', got '%s'", server.Description)
}
}