Skip to content

Commit d26c2be

Browse files
committed
Simplify tests
1 parent 7696d51 commit d26c2be

File tree

6 files changed

+223
-289
lines changed

6 files changed

+223
-289
lines changed

Modules/Tests/NetworkingTests/Mapper/ProductMapperTests.swift

Lines changed: 15 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -598,85 +598,10 @@ final class ProductMapperTests: XCTestCase {
598598
///
599599
func test_metadata_flexible_decoding_array_format() throws {
600600
// Given - JSON with metadata as array (current format)
601-
let jsonString = """
602-
{
603-
"id": 282,
604-
"name": "Test Product",
605-
"slug": "test-product",
606-
"permalink": "https://example.com/product/test-product/",
607-
"date_created_gmt": "2019-02-19T17:33:31",
608-
"date_modified_gmt": "2019-02-19T17:48:01",
609-
"type": "simple",
610-
"status": "publish",
611-
"featured": false,
612-
"catalog_visibility": "visible",
613-
"description": "",
614-
"short_description": "",
615-
"sku": "",
616-
"price": "10.00",
617-
"regular_price": "10.00",
618-
"sale_price": "",
619-
"on_sale": false,
620-
"purchasable": true,
621-
"total_sales": 0,
622-
"virtual": false,
623-
"downloadable": false,
624-
"downloads": [],
625-
"download_limit": -1,
626-
"download_expiry": -1,
627-
"button_text": "",
628-
"external_url": "",
629-
"tax_status": "taxable",
630-
"tax_class": "",
631-
"manage_stock": false,
632-
"stock_quantity": null,
633-
"stock_status": "instock",
634-
"backorders": "no",
635-
"backorders_allowed": false,
636-
"backordered": false,
637-
"sold_individually": false,
638-
"weight": "",
639-
"dimensions": {
640-
"length": "",
641-
"width": "",
642-
"height": ""
643-
},
644-
"shipping_required": false,
645-
"shipping_taxable": false,
646-
"shipping_class": "",
647-
"shipping_class_id": 0,
648-
"reviews_allowed": true,
649-
"average_rating": "0.00",
650-
"rating_count": 0,
651-
"related_ids": [],
652-
"upsell_ids": [],
653-
"cross_sell_ids": [],
654-
"parent_id": 0,
655-
"purchase_note": "",
656-
"categories": [],
657-
"tags": [],
658-
"images": [],
659-
"attributes": [],
660-
"default_attributes": [],
661-
"variations": [],
662-
"grouped_products": [],
663-
"menu_order": 0,
664-
"meta_data": [
665-
{
666-
"id": 1001,
667-
"key": "custom_field_1",
668-
"value": "value1"
669-
},
670-
{
671-
"id": 1002,
672-
"key": "custom_field_2",
673-
"value": "value2"
674-
}
675-
]
601+
guard let data = Loader.contentsOf("minimal-product-array-metadata") else {
602+
XCTFail("Unable to load test data")
603+
return
676604
}
677-
"""
678-
679-
let data = jsonString.data(using: .utf8)!
680605
let decoder = JSONDecoder()
681606
decoder.dateDecodingStrategy = .formatted(DateFormatter.Defaults.dateTimeFormatter)
682607
decoder.userInfo[.siteID] = dummySiteID
@@ -698,85 +623,10 @@ final class ProductMapperTests: XCTestCase {
698623
///
699624
func test_metadata_flexible_decoding_dictionary_format() throws {
700625
// Given - JSON with metadata as object keyed by index strings (new format)
701-
let jsonString = """
702-
{
703-
"id": 282,
704-
"name": "Test Product",
705-
"slug": "test-product",
706-
"permalink": "https://example.com/product/test-product/",
707-
"date_created_gmt": "2019-02-19T17:33:31",
708-
"date_modified_gmt": "2019-02-19T17:48:01",
709-
"type": "simple",
710-
"status": "publish",
711-
"featured": false,
712-
"catalog_visibility": "visible",
713-
"description": "",
714-
"short_description": "",
715-
"sku": "",
716-
"price": "10.00",
717-
"regular_price": "10.00",
718-
"sale_price": "",
719-
"on_sale": false,
720-
"purchasable": true,
721-
"total_sales": 0,
722-
"virtual": false,
723-
"downloadable": false,
724-
"downloads": [],
725-
"download_limit": -1,
726-
"download_expiry": -1,
727-
"button_text": "",
728-
"external_url": "",
729-
"tax_status": "taxable",
730-
"tax_class": "",
731-
"manage_stock": false,
732-
"stock_quantity": null,
733-
"stock_status": "instock",
734-
"backorders": "no",
735-
"backorders_allowed": false,
736-
"backordered": false,
737-
"sold_individually": false,
738-
"weight": "",
739-
"dimensions": {
740-
"length": "",
741-
"width": "",
742-
"height": ""
743-
},
744-
"shipping_required": false,
745-
"shipping_taxable": false,
746-
"shipping_class": "",
747-
"shipping_class_id": 0,
748-
"reviews_allowed": true,
749-
"average_rating": "0.00",
750-
"rating_count": 0,
751-
"related_ids": [],
752-
"upsell_ids": [],
753-
"cross_sell_ids": [],
754-
"parent_id": 0,
755-
"purchase_note": "",
756-
"categories": [],
757-
"tags": [],
758-
"images": [],
759-
"attributes": [],
760-
"default_attributes": [],
761-
"variations": [],
762-
"grouped_products": [],
763-
"menu_order": 0,
764-
"meta_data": {
765-
"0": {
766-
"id": 2001,
767-
"key": "custom_field_a",
768-
"value": "valueA"
769-
},
770-
"1": {
771-
"id": 2002,
772-
"key": "custom_field_b",
773-
"value": "valueB"
774-
}
775-
}
626+
guard let data = Loader.contentsOf("minimal-product-dictionary-metadata") else {
627+
XCTFail("Unable to load test data")
628+
return
776629
}
777-
"""
778-
779-
let data = jsonString.data(using: .utf8)!
780630
let decoder = JSONDecoder()
781631
decoder.dateDecodingStrategy = .formatted(DateFormatter.Defaults.dateTimeFormatter)
782632
decoder.userInfo[.siteID] = dummySiteID
@@ -787,15 +637,15 @@ final class ProductMapperTests: XCTestCase {
787637
// Then
788638
XCTAssertEqual(product.customFields.count, 2)
789639
// Since dictionary values don't have a guaranteed order, we need to check by key
790-
let fieldA = product.customFields.first { $0.key == "custom_field_a" }
791-
let fieldB = product.customFields.first { $0.key == "custom_field_b" }
792-
793-
XCTAssertNotNil(fieldA)
794-
XCTAssertNotNil(fieldB)
795-
XCTAssertEqual(fieldA?.metadataID, 2001)
796-
XCTAssertEqual(fieldA?.value.stringValue, "valueA")
797-
XCTAssertEqual(fieldB?.metadataID, 2002)
798-
XCTAssertEqual(fieldB?.value.stringValue, "valueB")
640+
let field1 = product.customFields.first { $0.key == "custom_field_1" }
641+
let field2 = product.customFields.first { $0.key == "custom_field_2" }
642+
643+
XCTAssertNotNil(field1)
644+
XCTAssertNotNil(field2)
645+
XCTAssertEqual(field1?.metadataID, 1001)
646+
XCTAssertEqual(field1?.value.stringValue, "value1")
647+
XCTAssertEqual(field2?.metadataID, 1002)
648+
XCTAssertEqual(field2?.value.stringValue, "value2")
799649
}
800650
}
801651

Modules/Tests/NetworkingTests/Mapper/ProductVariationMapperTests.swift

Lines changed: 8 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -113,68 +113,10 @@ final class ProductVariationMapperTests: XCTestCase {
113113
///
114114
func test_product_variation_metadata_flexible_decoding_array_format() throws {
115115
// Given - JSON with metadata as array (current format) containing subscription data
116-
let jsonString = """
117-
{
118-
"id": 2783,
119-
"date_created_gmt": "2019-11-14T12:35:23",
120-
"date_modified_gmt": "2019-11-14T12:35:23",
121-
"description": "Test variation",
122-
"permalink": "https://example.com/product/test-product/?attribute_darkness=87%25&attribute_flavor=strawberry&attribute_shape=marble",
123-
"sku": "test-sku",
124-
"price": "25.00",
125-
"regular_price": "25.00",
126-
"sale_price": "",
127-
"date_on_sale_from_gmt": null,
128-
"date_on_sale_to_gmt": null,
129-
"on_sale": false,
130-
"status": "publish",
131-
"purchasable": true,
132-
"virtual": false,
133-
"downloadable": false,
134-
"downloads": [],
135-
"download_limit": -1,
136-
"download_expiry": -1,
137-
"tax_status": "taxable",
138-
"tax_class": "",
139-
"manage_stock": false,
140-
"stock_quantity": null,
141-
"stock_status": "instock",
142-
"backorders": "no",
143-
"backorders_allowed": false,
144-
"backordered": false,
145-
"weight": "",
146-
"dimensions": {
147-
"length": "",
148-
"width": "",
149-
"height": ""
150-
},
151-
"shipping_class": "",
152-
"shipping_class_id": 0,
153-
"image": null,
154-
"attributes": [
155-
{
156-
"id": 0,
157-
"name": "Darkness",
158-
"option": "87%"
159-
}
160-
],
161-
"menu_order": 1,
162-
"meta_data": [
163-
{
164-
"id": 3001,
165-
"key": "_subscription_price",
166-
"value": "15.00"
167-
},
168-
{
169-
"id": 3002,
170-
"key": "_subscription_period",
171-
"value": "month"
172-
}
173-
]
116+
guard let data = Loader.contentsOf("minimal-product-variation-array-metadata") else {
117+
XCTFail("Unable to load test data")
118+
return
174119
}
175-
"""
176-
177-
let data = jsonString.data(using: .utf8)!
178120
let decoder = JSONDecoder()
179121
decoder.dateDecodingStrategy = .formatted(DateFormatter.Defaults.dateTimeFormatter)
180122
decoder.userInfo[.siteID] = dummySiteID
@@ -193,68 +135,10 @@ final class ProductVariationMapperTests: XCTestCase {
193135
///
194136
func test_product_variation_metadata_flexible_decoding_dictionary_format() throws {
195137
// Given - JSON with metadata as object keyed by index strings (new format)
196-
let jsonString = """
197-
{
198-
"id": 2783,
199-
"date_created_gmt": "2019-11-14T12:35:23",
200-
"date_modified_gmt": "2019-11-14T12:35:23",
201-
"description": "Test variation",
202-
"permalink": "https://example.com/product/test-product/?attribute_darkness=87%25&attribute_flavor=strawberry&attribute_shape=marble",
203-
"sku": "test-sku",
204-
"price": "25.00",
205-
"regular_price": "25.00",
206-
"sale_price": "",
207-
"date_on_sale_from_gmt": null,
208-
"date_on_sale_to_gmt": null,
209-
"on_sale": false,
210-
"status": "publish",
211-
"purchasable": true,
212-
"virtual": false,
213-
"downloadable": false,
214-
"downloads": [],
215-
"download_limit": -1,
216-
"download_expiry": -1,
217-
"tax_status": "taxable",
218-
"tax_class": "",
219-
"manage_stock": false,
220-
"stock_quantity": null,
221-
"stock_status": "instock",
222-
"backorders": "no",
223-
"backorders_allowed": false,
224-
"backordered": false,
225-
"weight": "",
226-
"dimensions": {
227-
"length": "",
228-
"width": "",
229-
"height": ""
230-
},
231-
"shipping_class": "",
232-
"shipping_class_id": 0,
233-
"image": null,
234-
"attributes": [
235-
{
236-
"id": 0,
237-
"name": "Darkness",
238-
"option": "87%"
239-
}
240-
],
241-
"menu_order": 1,
242-
"meta_data": {
243-
"0": {
244-
"id": 4001,
245-
"key": "_subscription_price",
246-
"value": "20.00"
247-
},
248-
"1": {
249-
"id": 4002,
250-
"key": "_subscription_period",
251-
"value": "week"
252-
}
253-
}
138+
guard let data = Loader.contentsOf("minimal-product-variation-dictionary-metadata") else {
139+
XCTFail("Unable to load test data")
140+
return
254141
}
255-
"""
256-
257-
let data = jsonString.data(using: .utf8)!
258142
let decoder = JSONDecoder()
259143
decoder.dateDecodingStrategy = .formatted(DateFormatter.Defaults.dateTimeFormatter)
260144
decoder.userInfo[.siteID] = dummySiteID
@@ -265,8 +149,8 @@ final class ProductVariationMapperTests: XCTestCase {
265149

266150
// Then - verify the subscription data was extracted from metadata
267151
XCTAssertNotNil(productVariation.subscription)
268-
XCTAssertEqual(productVariation.subscription?.price, "20.00")
269-
XCTAssertEqual(productVariation.subscription?.period, .week)
152+
XCTAssertEqual(productVariation.subscription?.price, "15.00")
153+
XCTAssertEqual(productVariation.subscription?.period, .month)
270154
}
271155
}
272156

0 commit comments

Comments
 (0)