1import Foundation
2import Testing
3
4// Test suite for generated SalesReport API
5// Demonstrates validation of MTL-generated code
6
7@Suite("Generated Sales Report API Tests")
8struct GeneratedAPITests {
9
10 // MARK: - Test Data
11
12 let sampleCategoryMetric = CategoryMetricAPI(
13 categoryName: "Electronics",
14 productCount: 3,
15 totalSales: 8897.00,
16 avgProductPrice: 2965.67
17 )
18
19 let sampleCustomerMetric = CustomerMetricAPI(
20 customerId: "C001",
21 customerName: "Alice Johnson",
22 orderCount: 2,
23 totalSpent: 2798.00,
24 avgOrderValue: 1399.00
25 )
26
27 let sampleProductMetric = ProductMetricAPI(
28 productId: "P001",
29 productName: "Laptop Pro",
30 unitsSold: 5,
31 revenue: 4995.00,
32 averageRating: 4.8
33 )
34
35 // MARK: - SalesReportAPI Tests
36
37 @Test("SalesReportAPI calculates average order value correctly")
38 func testAverageOrderValue() {
39 let report = SalesReportAPI(
40 reportId: "REPORT-001",
41 generatedDate: "2024-01-15",
42 totalRevenue: 15000.00,
43 totalOrders: 10,
44 categoryMetrics: [],
45 customerMetrics: [],
46 productMetrics: []
47 )
48
49 #expect(report.averageOrderValue == 1500.00)
50 }
51
52 @Test("SalesReportAPI handles zero orders gracefully")
53 func testZeroOrders() {
54 let report = SalesReportAPI(
55 reportId: "REPORT-002",
56 generatedDate: "2024-01-15",
57 totalRevenue: 0.00,
58 totalOrders: 0,
59 categoryMetrics: [],
60 customerMetrics: [],
61 productMetrics: []
62 )
63
64 #expect(report.averageOrderValue == 0.00)
65 }
66
67 @Test("SalesReportAPI identifies top category")
68 func testTopCategory() {
69 let categories = [
70 CategoryMetricAPI(categoryName: "Electronics", productCount: 3, totalSales: 8897.00, avgProductPrice: 2965.67),
71 CategoryMetricAPI(categoryName: "Computers", productCount: 4, totalSales: 5450.00, avgProductPrice: 1362.50),
72 CategoryMetricAPI(categoryName: "Accessories", productCount: 3, totalSales: 1500.00, avgProductPrice: 500.00)
73 ]
74
75 let report = SalesReportAPI(
76 reportId: "REPORT-003",
77 generatedDate: "2024-01-15",
78 totalRevenue: 15847.00,
79 totalOrders: 10,
80 categoryMetrics: categories,
81 customerMetrics: [],
82 productMetrics: []
83 )
84
85 #expect(report.topCategory?.categoryName == "Electronics")
86 #expect(report.topCategory?.totalSales == 8897.00)
87 }
88
89 @Test("SalesReportAPI generates summary text")
90 func testGenerateSummary() {
91 let report = SalesReportAPI(
92 reportId: "REPORT-004",
93 generatedDate: "2024-01-15",
94 totalRevenue: 15847.00,
95 totalOrders: 10,
96 categoryMetrics: [sampleCategoryMetric],
97 customerMetrics: [sampleCustomerMetric],
98 productMetrics: [sampleProductMetric]
99 )
100
101 let summary = report.generateSummary()
102
103 #expect(summary.contains("REPORT-004"))
104 #expect(summary.contains("2024-01-15"))
105 #expect(summary.contains("15847.00"))
106 #expect(summary.contains("10"))
107 }
108
109 // MARK: - CategoryMetricAPI Tests
110
111 @Test("CategoryMetricAPI provides market share")
112 func testCategoryMarketShare() {
113 #expect(sampleCategoryMetric.marketShare == 8897.00)
114 }
115
116 // MARK: - CustomerMetricAPI Tests
117
118 @Test("CustomerMetricAPI segments bronze customers correctly")
119 func testBronzeSegment() {
120 let bronzeCustomer = CustomerMetricAPI(
121 customerId: "C100",
122 customerName: "Test User",
123 orderCount: 1,
124 totalSpent: 250.00,
125 avgOrderValue: 250.00
126 )
127
128 #expect(bronzeCustomer.customerSegment == "Bronze")
129 }
130
131 @Test("CustomerMetricAPI segments silver customers correctly")
132 func testSilverSegment() {
133 let silverCustomer = CustomerMetricAPI(
134 customerId: "C101",
135 customerName: "Test User",
136 orderCount: 2,
137 totalSpent: 1200.00,
138 avgOrderValue: 600.00
139 )
140
141 #expect(silverCustomer.customerSegment == "Silver")
142 }
143
144 @Test("CustomerMetricAPI segments gold customers correctly")
145 func testGoldSegment() {
146 let goldCustomer = CustomerMetricAPI(
147 customerId: "C102",
148 customerName: "Test User",
149 orderCount: 5,
150 totalSpent: 3500.00,
151 avgOrderValue: 700.00
152 )
153
154 #expect(goldCustomer.customerSegment == "Gold")
155 }
156
157 @Test("CustomerMetricAPI segments platinum customers correctly")
158 func testPlatinumSegment() {
159 let platinumCustomer = CustomerMetricAPI(
160 customerId: "C103",
161 customerName: "Test User",
162 orderCount: 10,
163 totalSpent: 8000.00,
164 avgOrderValue: 800.00
165 )
166
167 #expect(platinumCustomer.customerSegment == "Platinum")
168 }
169
170 // MARK: - ProductMetricAPI Tests
171
172 @Test("ProductMetricAPI rates excellent performance")
173 func testExcellentPerformance() {
174 let excellentProduct = ProductMetricAPI(
175 productId: "P999",
176 productName: "Bestseller",
177 unitsSold: 15,
178 revenue: 7500.00,
179 averageRating: 4.8
180 )
181
182 #expect(excellentProduct.performanceRating == "Excellent")
183 }
184
185 @Test("ProductMetricAPI rates good performance")
186 func testGoodPerformance() {
187 let goodProduct = ProductMetricAPI(
188 productId: "P998",
189 productName: "Popular Item",
190 unitsSold: 7,
191 revenue: 2100.00,
192 averageRating: 4.2
193 )
194
195 #expect(goodProduct.performanceRating == "Good")
196 }
197
198 @Test("ProductMetricAPI rates fair performance")
199 func testFairPerformance() {
200 let fairProduct = ProductMetricAPI(
201 productId: "P997",
202 productName: "Moderate Seller",
203 unitsSold: 3,
204 revenue: 600.00,
205 averageRating: 3.5
206 )
207
208 #expect(fairProduct.performanceRating == "Fair")
209 }
210
211 @Test("ProductMetricAPI identifies products needing improvement")
212 func testNeedsImprovement() {
213 let poorProduct = ProductMetricAPI(
214 productId: "P996",
215 productName: "Slow Seller",
216 unitsSold: 1,
217 revenue: 100.00,
218 averageRating: 3.0
219 )
220
221 #expect(poorProduct.performanceRating == "Needs Improvement")
222 }
223
224 // MARK: - Integration Tests
225
226 @Test("Full report integration test")
227 func testFullReportIntegration() {
228 let categories = [
229 CategoryMetricAPI(categoryName: "Electronics", productCount: 3, totalSales: 8897.00, avgProductPrice: 2965.67),
230 CategoryMetricAPI(categoryName: "Computers", productCount: 4, totalSales: 5450.00, avgProductPrice: 1362.50)
231 ]
232
233 let customers = [
234 CustomerMetricAPI(customerId: "C001", customerName: "Alice", orderCount: 2, totalSpent: 2798.00, avgOrderValue: 1399.00),
235 CustomerMetricAPI(customerId: "C002", customerName: "Bob", orderCount: 1, totalSpent: 1299.00, avgOrderValue: 1299.00)
236 ]
237
238 let products = [
239 ProductMetricAPI(productId: "P001", productName: "Laptop", unitsSold: 5, revenue: 4995.00, averageRating: 4.8),
240 ProductMetricAPI(productId: "P002", productName: "Mouse", unitsSold: 12, revenue: 348.00, averageRating: 4.5)
241 ]
242
243 let report = SalesReportAPI(
244 reportId: "REPORT-INTEGRATION",
245 generatedDate: "2024-01-15",
246 totalRevenue: 15847.00,
247 totalOrders: 10,
248 categoryMetrics: categories,
249 customerMetrics: customers,
250 productMetrics: products
251 )
252
253 // Verify computed properties
254 #expect(report.averageOrderValue == 1584.70)
255 #expect(report.topCategory?.categoryName == "Electronics")
256 #expect(report.topCustomer?.customerName == "Alice")
257 #expect(report.topProduct?.productName == "Laptop")
258
259 // Verify summary generation
260 let summary = report.generateSummary()
261 #expect(summary.contains("REPORT-INTEGRATION"))
262 #expect(summary.contains("Electronics"))
263 #expect(summary.contains("Alice"))
264 #expect(summary.contains("Laptop"))
265 }
266}