Skip to content

Commit

Permalink
Add a wrapped date codable test, for Meow edge cases. Make DocumentIn…
Browse files Browse the repository at this point in the history
…dex hashable
  • Loading branch information
Joannis committed Nov 1, 2022
1 parent 1065b39 commit 0e1b3f5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/BSON/Document/Document+Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension Document: RandomAccessCollection {
}
}

public struct DocumentIndex: Comparable {
public struct DocumentIndex: Comparable, Hashable {
/// The offset in the Document to look for
var offset: Int

Expand Down
25 changes: 23 additions & 2 deletions Tests/BSONTests/BSONPublicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 Robbert Brandsma. All rights reserved.
//

import NIO
import NIOCore
import Foundation
import XCTest
import BSON
Expand Down Expand Up @@ -199,7 +199,7 @@ final class BSONPublicTests: XCTestCase {
XCTAssertEqual(doc.keys, ["_id", "parent", "name", "parent2"])
}

func testDateCodables() throws {
func testWrappedDateCodables() throws {
@propertyWrapper
struct Field<C: Codable>: Codable {
public let key: String?
Expand Down Expand Up @@ -241,6 +241,27 @@ final class BSONPublicTests: XCTestCase {
let entity = Entity(_id: ObjectId(), date: Date(), dates: [Date(), Date().addingTimeInterval(1)])
let document = try BSONEncoder().encode(entity)
XCTAssertTrue(document["date"] is Date)
XCTAssertTrue(document["dates"][0] is Date)
let entity2 = try BSONDecoder().decode(Entity.self, from: document)
XCTAssertEqual(entity._id, entity2._id)
XCTAssertEqual(entity.date.timeIntervalSince1970, entity2.date.timeIntervalSince1970, accuracy: 0.01)
XCTAssertEqual(entity.dates.count, entity2.dates.count)
for i in 0..<entity.dates.count {
XCTAssertEqual(entity.dates[i].timeIntervalSince1970, entity2.dates[i].timeIntervalSince1970, accuracy: 0.01)
}
}

func testDateCodables() throws {
struct Entity: Codable {
var _id: ObjectId
var date: Date
var dates: [Date]
}

let entity = Entity(_id: ObjectId(), date: Date(), dates: [Date(), Date().addingTimeInterval(1)])
let document = try BSONEncoder().encode(entity)
XCTAssertTrue(document["date"] is Date)
XCTAssertTrue(document["dates"][0] is Date)
let entity2 = try BSONDecoder().decode(Entity.self, from: document)
XCTAssertEqual(entity._id, entity2._id)
XCTAssertEqual(entity.date.timeIntervalSince1970, entity2.date.timeIntervalSince1970, accuracy: 0.01)
Expand Down
1 change: 0 additions & 1 deletion Tests/BSONTests/ObjectIdTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import XCTest
import BSON

class ObjectIdTests: XCTestCase {

Expand Down

0 comments on commit 0e1b3f5

Please sign in to comment.