Created
January 13, 2016 05:45
-
-
Save jkopelioff/349f92770dd061fdc831 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import XCTest | |
| @testable import Society6 | |
| class ModelFactoryTests: XCTestCase { | |
| override func setUp() { | |
| super.setUp() | |
| // Put setup code here. This method is called before the invocation of each test method in the class. | |
| } | |
| override func tearDown() { | |
| // Put teardown code here. This method is called after the invocation of each test method in the class. | |
| super.tearDown() | |
| } | |
| func testCreateProduct() { | |
| // This is an example of a functional test case. | |
| if let path = NSBundle(forClass: ModelFactoryTests.self).pathForResource("product", ofType: "json") | |
| { | |
| if let jsonData = NSData(contentsOfFile: path) | |
| { | |
| do { | |
| if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary | |
| { | |
| print(jsonResult.valueForKeyPathWithIndexes("previews[0].s6image")) | |
| let model = try ModelFactory.createModel(jsonResult, modelType: Product.self) | |
| XCTAssert(model.id == "s6-3353104p26a18v126a25v193", "id is s6-3353104p26a18v126a25v193") | |
| XCTAssert(model.price == 20.00, "price == 20.00") | |
| XCTAssert(model.aspectRatio == 1.0 , "aspectRatio == 1.0") | |
| XCTAssert(model.productType == "Throw Pillow", "productType is 'Throw Pillow'") | |
| return | |
| } | |
| } catch { | |
| XCTAssert(false, "Failed") | |
| } | |
| } | |
| } | |
| XCTAssert(false, "Failed") | |
| } | |
| func testCreateProducts() { | |
| // This is an example of a functional test case. | |
| if let path = NSBundle(forClass: ModelFactoryTests.self).pathForResource("products", ofType: "json") | |
| { | |
| if let jsonData = NSData(contentsOfFile: path) | |
| { | |
| do { | |
| if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary | |
| { | |
| if let products = jsonResult["products"] as? [NSDictionary] { | |
| let models = try ModelFactory.createModels(products, modelType: Product.self) | |
| XCTAssert(models.count == 3, "Model count is 3") | |
| XCTAssert(models[0].id == "s6-1041826p26a18v126a25v193", "Models[0] id is s6-1041826p26a18v126a25v193") | |
| XCTAssert(models[0].userFollowers == 23464, "Models[0] followers is 23464") | |
| XCTAssert(models[1].id == "s6-3353104p26a18v126a25v193", "Models[1] id is s6-3353104p26a18v126a25v193") | |
| XCTAssert(models[1].userFollowers == 34, "Models[1] followers is 34") | |
| XCTAssert(models[2].id == "s6-1681104p26a18v126a25v193", "Models[2] id is s6-1681104p26a18v126a25v193") | |
| XCTAssert(models[2].userFollowers == 6904, "Models[2] followers is 6904") | |
| return | |
| } | |
| } | |
| } catch { | |
| XCTAssert(false, "Failed") | |
| } | |
| } | |
| } | |
| XCTAssert(false, "Failed") | |
| } | |
| func testCreatePost() { | |
| // This is an example of a functional test case. | |
| if let path = NSBundle(forClass: ModelFactoryTests.self).pathForResource("s6post", ofType: "json") | |
| { | |
| if let jsonData = NSData(contentsOfFile: path) | |
| { | |
| do { | |
| if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary | |
| { | |
| if let product = jsonResult["data"] as? NSDictionary { | |
| let model = try ModelFactory.createModel(product, modelType: Post.self) | |
| XCTAssert(model.postId == "3579203", "Post id is 3579203") | |
| XCTAssert(model.artistName! == "Henn Kim", "Artist name is 'Henn Kim'") | |
| XCTAssert(model.numComments == 2, "numComments == 2") | |
| XCTAssert(model.numPromotes == 30, "numPromotes == 30") | |
| XCTAssert(model.promoted == false, "promoted == false") | |
| XCTAssert(model.commentList?.count == 2, "commentList?.count == 2") | |
| if let comments = model.commentList { | |
| XCTAssert(comments[0].commentId == "5229910", "Comment[0] id is 5229910") | |
| XCTAssert(comments[1].commentId == "5227497", "Comment[0] id is 5227497") | |
| } | |
| return | |
| } | |
| } | |
| } catch { | |
| XCTAssert(false, "Failed") | |
| } | |
| } | |
| } | |
| XCTAssert(false, "Failed") | |
| } | |
| func testCreatePosts() { | |
| // This is an example of a functional test case. | |
| if let path = NSBundle(forClass: ModelFactoryTests.self).pathForResource("s6posts", ofType: "json") | |
| { | |
| if let jsonData = NSData(contentsOfFile: path) | |
| { | |
| do { | |
| if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary | |
| { | |
| if let product = jsonResult["data"] as? [NSDictionary] { | |
| let model = try ModelFactory.createModels(product, modelType: Post.self) | |
| XCTAssert(model.count == 10, "model.count == 10") | |
| XCTAssert(model[0].postId == "3579203", "Post[0] id is 3579203") | |
| XCTAssert(model[1].postId == "3580552", "Post[1] id is 3580552") | |
| XCTAssert(model[2].postId == "3578431", "Post[2] id is 3578431") | |
| return | |
| } | |
| } | |
| } catch { | |
| XCTAssert(false, "Failed") | |
| } | |
| } | |
| } | |
| XCTAssert(false, "Failed") | |
| } | |
| func testCreatePostFromComments() { | |
| // This is an example of a functional test case. | |
| if let path = NSBundle(forClass: ModelFactoryTests.self).pathForResource("s6comments", ofType: "json") | |
| { | |
| if let jsonData = NSData(contentsOfFile: path) | |
| { | |
| do { | |
| if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary | |
| { | |
| if let json = jsonResult.valueForKeyPath("data.post") as? NSDictionary { | |
| let post = try ModelFactory.createModel(json, modelType: Post.self) | |
| XCTAssert(post.postId == "3579203", "post?.postId == \"3579203\"") | |
| XCTAssert(post.imageUrl == "https:\\/\\/cdn.society6.com\\/cdn\\/0053\\/p\\/22708065_2074605_pm.jpg", "imageUrl valid") | |
| } | |
| if let comments = jsonResult.valueForKeyPath("data.comments") as? [NSDictionary] { | |
| let model = try ModelFactory.createModels(comments, modelType: Comment.self) | |
| XCTAssert(model.count == 7, "Looking for 7 comments, found \(model.count)") | |
| return | |
| } | |
| } | |
| } catch { | |
| XCTAssert(false, "Failed") | |
| } | |
| } | |
| } | |
| XCTAssert(false, "Failed") | |
| } | |
| func testCreateArtistFromSearch() { | |
| // This is an example of a functional test case. | |
| if let path = NSBundle(forClass: ModelFactoryTests.self).pathForResource("s6searchartist", ofType: "json") | |
| { | |
| if let jsonData = NSData(contentsOfFile: path) | |
| { | |
| do { | |
| if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary | |
| { | |
| if let json = jsonResult.valueForKeyPath("items") as? [NSDictionary] { | |
| let artists = try ModelFactory.createModels(json, modelType: User.self) | |
| XCTAssert(artists.count == 25, "25 artists") | |
| XCTAssert(artists[0].id ?? 0 == "nanlawson", "first artist is nanlawson") | |
| return | |
| } | |
| } | |
| } catch { | |
| XCTAssert(false, "Failed") | |
| } | |
| } | |
| } | |
| XCTAssert(false, "Failed") | |
| } | |
| func testCreateArtistPromotions() { | |
| if let path = NSBundle(forClass: ModelFactoryTests.self).pathForResource("s6promotions", ofType: "json") | |
| { | |
| if let jsonData = NSData(contentsOfFile: path) | |
| { | |
| do { | |
| if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary | |
| { | |
| if let json = jsonResult.valueForKeyPath("data.post") as? NSDictionary { | |
| let post = try ModelFactory.createModel(json, modelType: Post.self) | |
| XCTAssert(post.postId == "1095706", "post?.postId == \"1095706\"") | |
| XCTAssert(post.imageUrl == "https://cdn.society6.com/cdn/0027/p/12982539_5898141_pm.jpg", "imageUrl valid") | |
| } | |
| if let promotions = jsonResult.valueForKeyPath("data.promotions") as? [NSDictionary] { | |
| let model = try ModelFactory.createModels(promotions, modelType: User.self) | |
| XCTAssert(model.count == 36, "Looking for 36 promotions, found \(model.count)") | |
| return | |
| } | |
| } | |
| } catch { | |
| XCTAssert(false, "Failed") | |
| } | |
| } | |
| } | |
| XCTAssert(false, "Failed") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment