Skip to content

Commit

Permalink
Add Media tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachOrr committed Oct 18, 2018
1 parent 078ffc1 commit d3ca261
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 8 deletions.
126 changes: 126 additions & 0 deletions tba-unit-tests/Core Data/Media/Media_Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import XCTest
@testable import The_Blue_Alliance

class Media_TestCase: CoreDataTestCase {

var media: Media!

override func setUp() {
super.setUp()

media = Media.init(entity: Media.entity(), insertInto: persistentContainer.viewContext)
}

override func tearDown() {
media = nil

super.tearDown()
}

func test_image() {
let image = UIImage(data: Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwQAADsEBuJFr7QAAABl0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC4yMfEgaZUAAAEYSURBVFhH7ZVLD8IgEIRXoxf//w/07Emj8eZj2qkbHraFhaoxfJkYspQZNlgqjV9j+9IjEutLcZ/TIgQZE6pJYJ2iCqjXRWTXDw5OnWOCWTwD8eEi6K5GDCasx/CZsdlUuN6wffPCjpLF9rUlqYDnTZM8SlKVFpwCUqsEW0wKg9c1tv5ZeELUTeQkcu5/j/7UhIwd411UAkdIufp1N8x1yAAWq2HoWVNKUNdgrDV2jC+Pbjlwh5SgjuNQ3G9XHjAirjWlBPX9UO7GdnQx714q7kOnIKVOsIGiYHRg/Gf63VuwrS9NJbkudVIJvDbDcIaaqSDd7l+CQcrrUfQKjfG1YDB999pv5hTQU3yKKC7Vq4t7b1ONxjtEngWF2v3/EmI7AAAAAElFTkSuQmCC")!)

XCTAssertNil(media.image)

media.image = image
XCTAssertNotNil(media.image?.pngData())
XCTAssertEqual(image?.pngData(), media.image?.pngData())

media.image = nil
XCTAssertNil(media.image)
}

func test_imageError() {
let error = MediaError.error("Some media error")

XCTAssertNil(media.mediaError)

media.mediaError = error
XCTAssertNotNil(media.mediaError?.localizedDescription)
XCTAssertEqual(error.localizedDescription, media.mediaError?.localizedDescription)

media.mediaError = nil
XCTAssertNil(media.mediaError)
}

func test_youtubeKey() {
let foreignKey = "foreign_key"
media.foreignKey = foreignKey

XCTAssertNil(media.youtubeKey)

media.type = MediaType.youtubeVideo.rawValue
XCTAssertNotNil(media.youtubeKey)
XCTAssertEqual(media.youtubeKey, foreignKey)
}

func test_imageTypes_hasURL() {
media.foreignKey = "foreign_key"
// Setup details so we can get images
media.details = [
"image_partial": "test",
"model_image": "test"
]

for imageType in MediaType.imageTypes {
media.type = imageType
XCTAssertNotNil(media.viewImageURL)
XCTAssertNotNil(media.imageDirectURL)
}
}

func test_viewImageURL() {
media.foreignKey = "foreign_key"

// No type - nil
XCTAssertNil(media.viewImageURL)

// cdPhotoThread
media.type = MediaType.cdPhotoThread.rawValue
XCTAssertEqual(media.viewImageURL?.absoluteString, "http://www.chiefdelphi.com/media/photos/foreign_key")

// imgur
media.type = MediaType.imgur.rawValue
XCTAssertEqual(media.viewImageURL?.absoluteString, "https://imgur.com/foreign_key")

// instagramImage
media.type = MediaType.instagramImage.rawValue
XCTAssertEqual(media.viewImageURL?.absoluteString, "https://www.instagram.com/p/foreign_key")

// grabcad
media.type = MediaType.grabcad.rawValue
XCTAssertEqual(media.viewImageURL?.absoluteString, "https://grabcad.com/library/foreign_key")
}

func test_imageDirectURL() {
media.foreignKey = "foreign_key"

// No type - nil
XCTAssertNil(media.viewImageURL)

// cdPhotoThread
media.type = MediaType.cdPhotoThread.rawValue
media.details = [
"image_partial": "test_l"
]
XCTAssertEqual(media.imageDirectURL?.absoluteString, "http://www.chiefdelphi.com/media/img/test_m")

// imgur
media.type = MediaType.imgur.rawValue
media.details = nil
XCTAssertEqual(media.imageDirectURL?.absoluteString, "https://i.imgur.com/foreign_keyh.jpg")

// instagramImage
media.type = MediaType.instagramImage.rawValue
media.details = nil
XCTAssertEqual(media.imageDirectURL?.absoluteString, "https://www.instagram.com/p/foreign_key/media/?size=l")

// grabcad
media.type = MediaType.grabcad.rawValue
media.details = [
"model_image": "https://test.test.net/screenshots/pics/2850f4cfb0ca7a196d00e100c6bdd91b/card.jpg"
]
XCTAssertEqual(media.imageDirectURL?.absoluteString, "https://test.test.net/screenshots/pics/2850f4cfb0ca7a196d00e100c6bdd91b/large.png")
}

}
12 changes: 12 additions & 0 deletions the-blue-alliance-ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
9221C618209BFF8700477DE2 /* LoadingTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9221C617209BFF8700477DE2 /* LoadingTableViewCell.xib */; };
9223AC831EE7003C00F54F93 /* CollectionViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9223AC821EE7003C00F54F93 /* CollectionViewDataSource.swift */; };
9223AC851EE7214A00F54F93 /* MediaCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9223AC841EE7214A00F54F93 /* MediaCollectionViewCell.swift */; };
9236101F2178245C000CC5E9 /* Media_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9236101E2178245C000CC5E9 /* Media_Tests.swift */; };
923AEE87216583CA00EF50C8 /* Bundle+Version.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923AEE86216583CA00EF50C8 /* Bundle+Version.swift */; };
923AEE8A21658BC400EF50C8 /* Launch_UITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923AEE8921658BC400EF50C8 /* Launch_UITests.swift */; };
923AEE8C21658C5800EF50C8 /* Bundle+Version_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923AEE8B21658C5800EF50C8 /* Bundle+Version_Tests.swift */; };
Expand Down Expand Up @@ -251,6 +252,7 @@
9221C617209BFF8700477DE2 /* LoadingTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LoadingTableViewCell.xib; sourceTree = "<group>"; };
9223AC821EE7003C00F54F93 /* CollectionViewDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewDataSource.swift; sourceTree = "<group>"; };
9223AC841EE7214A00F54F93 /* MediaCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MediaCollectionViewCell.swift; sourceTree = "<group>"; };
9236101E2178245C000CC5E9 /* Media_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Media_Tests.swift; sourceTree = "<group>"; };
923AEE86216583CA00EF50C8 /* Bundle+Version.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "Bundle+Version.swift"; path = "Extensions/Bundle+Version.swift"; sourceTree = "<group>"; };
923AEE8921658BC400EF50C8 /* Launch_UITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Launch_UITests.swift; sourceTree = "<group>"; };
923AEE8B21658C5800EF50C8 /* Bundle+Version_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+Version_Tests.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -525,6 +527,14 @@
path = "Team@District";
sourceTree = "<group>";
};
9236101D217823F1000CC5E9 /* Media */ = {
isa = PBXGroup;
children = (
9236101E2178245C000CC5E9 /* Media_Tests.swift */,
);
path = Media;
sourceTree = "<group>";
};
923A0D7620D21D3800BF5E31 /* Launch Views */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -585,6 +595,7 @@
924459BF20CB796B0007570D /* Core Data */ = {
isa = PBXGroup;
children = (
9236101D217823F1000CC5E9 /* Media */,
92C1F2DD216994CF00BD8445 /* CoreDataTestCase.swift */,
926652622156C55C000E4F4F /* Event */,
92A2F5522156BD4B00267C63 /* Extensions */,
Expand Down Expand Up @@ -1742,6 +1753,7 @@
924459C420CB796B0007570D /* Int16+Suffix_Tests.swift in Sources */,
924459C820CB797E0007570D /* RetryService_Tests.swift in Sources */,
14B424F62167B01700E1904C /* Calendar+TBA_Tests.swift in Sources */,
9236101F2178245C000CC5E9 /* Media_Tests.swift in Sources */,
92C1F2E42169AA7200BD8445 /* TeamsContainerViewController_Tests.swift in Sources */,
92C1F2E62169AB9F00BD8445 /* DistrictsContainerViewController_Tests.swift in Sources */,
923AEE952167012000EF50C8 /* TBABackgroundService_Tests.swift in Sources */,
Expand Down
24 changes: 16 additions & 8 deletions the-blue-alliance-ios/Core Data/Media.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,36 @@ extension Media: Managed, Playable {
// https://github.com/the-blue-alliance/the-blue-alliance/blob/master/models/media.py#L92

public var viewImageURL: URL? {
if type! == MediaType.cdPhotoThread.rawValue {
guard let type = type else {
return nil
}

if type == MediaType.cdPhotoThread.rawValue {
return cdphotothreadThreadURL
} else if type! == MediaType.imgur.rawValue {
} else if type == MediaType.imgur.rawValue {
return imgurURL
} else if type! == MediaType.grabcad.rawValue {
} else if type == MediaType.grabcad.rawValue {
return grabcadURL
} else if type! == MediaType.instagramImage.rawValue {
} else if type == MediaType.instagramImage.rawValue {
return instagramURL
} else {
return nil
}
}

public var imageDirectURL: URL? {
guard let type = type else {
return nil
}

// Largest image that isn't max resolution (which can be arbitrarily huge)
if type! == MediaType.cdPhotoThread.rawValue {
if type == MediaType.cdPhotoThread.rawValue {
return cdphotothreadImageSize(.medium)
} else if type! == MediaType.imgur.rawValue {
} else if type == MediaType.imgur.rawValue {
return imgurImageSize(.direct)
} else if type! == MediaType.grabcad.rawValue {
} else if type == MediaType.grabcad.rawValue {
return grabcadDirectURL
} else if type! == MediaType.instagramImage.rawValue {
} else if type == MediaType.instagramImage.rawValue {
return instagramDirectURL(.large)
} else {
return nil
Expand Down

0 comments on commit d3ca261

Please sign in to comment.