Skip to content

Commit

Permalink
Add date, more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZekeSnider committed Feb 3, 2019
1 parent 1bd394d commit 099e2f5
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Pods/* linguist-vendored=true
Pods/* linguist-vendored
4 changes: 4 additions & 0 deletions Jared.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
B3EF1DAA2206E09B00953DE7 /* Jared.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3EF1DA92206E09B00953DE7 /* Jared.swift */; };
B3EF1DAC2206E17900953DE7 /* Entities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3EF1DAB2206E17900953DE7 /* Entities.swift */; };
B3EF1DAE2206E19100953DE7 /* Bodies.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3EF1DAD2206E19100953DE7 /* Bodies.swift */; };
B3EF1DB02206E4EB00953DE7 /* MessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3EF1DAF2206E4EB00953DE7 /* MessageTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -122,6 +123,7 @@
B3EF1DA92206E09B00953DE7 /* Jared.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Jared.swift; sourceTree = "<group>"; };
B3EF1DAB2206E17900953DE7 /* Entities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Entities.swift; sourceTree = "<group>"; };
B3EF1DAD2206E19100953DE7 /* Bodies.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bodies.swift; sourceTree = "<group>"; };
B3EF1DAF2206E4EB00953DE7 /* MessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageTests.swift; sourceTree = "<group>"; };
B85E2F9206C873C167385410 /* Pods-JaredUI.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JaredUI.debug.xcconfig"; path = "Pods/Target Support Files/Pods-JaredUI/Pods-JaredUI.debug.xcconfig"; sourceTree = "<group>"; };
C31CB7445EAF3431D6AA3FD4 /* Pods_JaredUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_JaredUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -215,6 +217,7 @@
isa = PBXGroup;
children = (
B3327DC722068147009DD882 /* WebhookTests.swift */,
B3EF1DAF2206E4EB00953DE7 /* MessageTests.swift */,
B3EF1DA3220687DD00953DE7 /* URLProtocolMock.swift */,
B3327DD222068176009DD882 /* Info.plist */,
);
Expand Down Expand Up @@ -541,6 +544,7 @@
buildActionMask = 2147483647;
files = (
B3327DD8220682ED009DD882 /* WebhookTests.swift in Sources */,
B3EF1DB02206E4EB00953DE7 /* MessageTests.swift in Sources */,
B3EF1DA4220687DD00953DE7 /* URLProtocolMock.swift in Sources */,
B3EF1DA02206849000953DE7 /* WebHookManager.swift in Sources */,
);
Expand Down
6 changes: 4 additions & 2 deletions Jared/DatabaseHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ class DatabaseHandler {
let start = Date()

let query = """
SELECT handle.id, message.text, message.ROWID, message.cache_roomnames, message.is_from_me, message.destination_caller_id
SELECT handle.id, message.text, message.ROWID, message.cache_roomnames, message.is_from_me, message.destination_caller_id,
message.date/1000000000 + strftime("%s", "2001-01-01")
FROM message INNER JOIN handle
ON message.handle_id = handle.ROWID
WHERE message.ROWID > ?
Expand All @@ -167,6 +168,7 @@ class DatabaseHandler {
let roomName = unwrapStringColumn(at: 3)
let isFromMe = sqlite3_column_int(statement, 4) == 1
let destinationOptional = unwrapStringColumn(at: 5)
let epochDate = TimeInterval(sqlite3_column_int64(statement, 6))

querySinceID = rowID;

Expand All @@ -191,7 +193,7 @@ class DatabaseHandler {
recipient = group ?? Person(givenName: myName, handle: destination, isMe: true, inGroup: nil)
}

let message = Message(body: TextBody(text), date: Date(), sender: sender, recipient: recipient)
let message = Message(body: TextBody(text), date: Date(timeIntervalSince1970: epochDate), sender: sender, recipient: recipient)
appDelegate.Router.route(message: message)
}
}
Expand Down
9 changes: 9 additions & 0 deletions Jared/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public struct Message: Encodable {
} else if let group = recipient as? Group {
try container.encode(group, forKey: .recipient)
}

if let notOptionalDate = date {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.locale = Locale(identifier: "en_US_POSIX")

try container.encode(formatter.string(from: notOptionalDate), forKey: .date)
}
}

public init (body: MessageBody, date: Date, sender: SenderEntity, recipient: RecipientEntity) {
Expand Down
4 changes: 4 additions & 0 deletions JaredFramework/Bodies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ public struct TextBody: MessageBody, Codable {

public struct ImageBody: MessageBody, Codable {
public var ImagePath: String

public init(_ path: String) {
ImagePath = path
}
}
16 changes: 14 additions & 2 deletions JaredFramework/Entities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public protocol SenderEntity: Codable {
var givenName: String? {get set}
}

public struct Person: SenderEntity, RecipientEntity, Codable {
public struct Person: SenderEntity, RecipientEntity, Codable, Equatable {
public var givenName: String?
public var handle: String
public var isMe: Bool = false
Expand All @@ -28,9 +28,15 @@ public struct Person: SenderEntity, RecipientEntity, Codable {
self.isMe = isMe
self.inGroup = inGroup
}

public static func == (lhs: Person, rhs: Person) -> Bool {
return lhs.givenName == rhs.givenName &&
lhs.handle == rhs.handle &&
lhs.isMe == rhs.isMe
}
}

public struct Group: RecipientEntity, Codable {
public struct Group: RecipientEntity, Codable, Equatable {
public var name: String?
public var handle: String
public var participants: [Person]
Expand All @@ -40,4 +46,10 @@ public struct Group: RecipientEntity, Codable {
self.handle = handle
self.participants = participants
}

public static func == (lhs: Group, rhs: Group) -> Bool {
return lhs.name == rhs.name &&
lhs.handle == rhs.handle &&
lhs.participants == rhs.participants
}
}
60 changes: 60 additions & 0 deletions JaredTests/MessageTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// MessageTests.swift
// JaredTests
//
// Created by Zeke Snider on 2/3/19.
// Copyright © 2019 Zeke Snider. All rights reserved.
//

import XCTest
import JaredFramework




class MessageTests: XCTestCase {
let textBody = TextBody("Hey Jared")

let jaredPerson = Person(givenName: "jared", handle: "[email protected]", isMe: false, inGroup: nil)
let mePerson = Person(givenName: "zeke", handle: "[email protected]", isMe: true, inGroup: nil)
let swiftPerson = Person(givenName: "taylor", handle: "[email protected]", isMe: false, inGroup: nil)

var sampleGroup: Group!

var sampleTextMessage: Message!
var sampleImageMesage: Message!
var messageFromMeToGroup: Message!
var messageFromMeToPerson: Message!
var messageFromPersonToGroup: Message!

override func setUp() {
sampleGroup = Group(name: "thank u next", handle: "chat1000", participants: [mePerson, jaredPerson, swiftPerson])

sampleTextMessage = Message(body: textBody, date: Date(), sender: mePerson, recipient: jaredPerson)
sampleImageMesage = Message(body: ImageBody("/users/zeke/goodJaredImage.jpg"), date: Date(), sender: mePerson, recipient: jaredPerson)

messageFromMeToGroup = Message(body: textBody, date: Date(), sender: mePerson, recipient: sampleGroup)
messageFromPersonToGroup = Message(body: textBody, date: Date(), sender: swiftPerson, recipient: sampleGroup)
messageFromMeToPerson = Message(body: textBody, date: Date(), sender: mePerson, recipient: swiftPerson)
}

override func tearDown() {
}

func testGetTextBody() {
XCTAssert(sampleTextMessage.getTextBody() == "Hey Jared", "getTextBody returns proper string")
XCTAssert(sampleImageMesage.getTextBody() == nil, "getTextBody returns nil for image body")
}

func testGetImageBody() {
XCTAssert(sampleTextMessage.getImageBody() == nil, "getTextBody returns nil for text body")
XCTAssert(sampleImageMesage.getImageBody() == "/users/zeke/goodJaredImage.jpg", "getTextBody returns proper path")
}

func testGetMessageResponse() {
XCTAssert(sampleTextMessage.RespondTo() as? Person == jaredPerson, "Message from me to person responds to recipient")
XCTAssert(messageFromMeToGroup.RespondTo() as? Group == sampleGroup, "Message from me to group responds to group")
XCTAssert(messageFromPersonToGroup.RespondTo() as? Group == sampleGroup, "Message from person to group responds to group")
XCTAssert(messageFromMeToPerson.RespondTo() as? Person == swiftPerson, "Message from me to person responds to person")
}
}
1 change: 1 addition & 0 deletions JaredTests/URLProtocolMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class URLProtocolMock: URLProtocol {
// if we have a valid URL…
if let url = request.url {
if let data = URLProtocolMock.testURLs[url] {
// let stringl = String(data: Data(reading: request.httpBodyStream!), encoding: .utf8)
if (data == Data(reading: request.httpBodyStream!)) {
URLProtocolMock.matchedDataURLs.append(url)
}
Expand Down
6 changes: 3 additions & 3 deletions JaredTests/WebhookTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import JaredFramework
class WebhookTests: XCTestCase {
let WEBHOOK_TEST_URL = "https://github.com/zekesnider/jaredwebhook"
let WEBHOOK_TEST_URL_TWO = "https://twitter.com/zekesnider/jaredwebhook"
let MESSAGE_SERIALIZED = "{\"sender\":{\"handle\":\"zeke@email.com\",\"givenName\":\"zeke\",\"isMe\":true},\"recipient\":{\"handle\":\"jared@email.com\",\"givenName\":\"jared\",\"isMe\":false},\"body\":{\"message\":\"hello there jared\"}}"
let SAMPLE_MESSAGE = Message(body: TextBody("hello there jared"), date: Date(), sender: Person(givenName: "zeke", handle: "[email protected]", isMe: true, inGroup: nil), recipient: Person(givenName: "jared", handle: "[email protected]", isMe: false, inGroup: nil))
let MESSAGE_SERIALIZED = "{\"body\":{\"message\":\"hello there jared\"},\"recipient\":{\"handle\":\"jared@email.com\",\"givenName\":\"jared\",\"isMe\":false},\"sender\":{\"handle\":\"zeke@email.com\",\"givenName\":\"zeke\",\"isMe\":true},\"date\":\"2017-05-17T22:57:21.000Z\"}"
let SAMPLE_MESSAGE = Message(body: TextBody("hello there jared"), date: Date(timeIntervalSince1970: TimeInterval(1495061841)), sender: Person(givenName: "zeke", handle: "[email protected]", isMe: true, inGroup: nil), recipient: Person(givenName: "jared", handle: "[email protected]", isMe: false, inGroup: nil))

var config: URLSessionConfiguration!

Expand All @@ -26,7 +26,7 @@ class WebhookTests: XCTestCase {
URLProtocolMock.matchedDataURLs = []
}

func callsWithValidURLs() {
func testvalidURLsCall() {
// set up first call to webhook with one url
let url = URL(string: WEBHOOK_TEST_URL)
URLProtocolMock.testURLs = [url: Data(MESSAGE_SERIALIZED.utf8)]
Expand Down
2 changes: 1 addition & 1 deletion JaredUI/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>251</string>
<string>286</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/ZekeSnider/Jared.svg?branch=master)](https://travis-ci.org/ZekeSnider/Jared.svg?branch=master)
[![Build Status](https://travis-ci.org/ZekeSnider/Jared.svg?branch=master)](https://travis-ci.org/ZekeSnider/Jared)

<a name='Jared'/>

Expand Down

0 comments on commit 099e2f5

Please sign in to comment.