Skip to content

Commit

Permalink
Fix pagination filter
Browse files Browse the repository at this point in the history
  • Loading branch information
JRomainG committed May 14, 2021
1 parent 7bb1f80 commit b546c85
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Sources/Objects/MDFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MDPaginationFilter: Encodable {
public var offset: Int?

/// Get the query parameters for this filter to encode them in a URL
public func getParameters() -> [URLQueryItem] {
internal func getParameters() -> [URLQueryItem] {
// Convert the filter to a dictionary which is easy to encode in the URL
let data: [String: Any]
do {
Expand All @@ -45,8 +45,14 @@ public class MDPaginationFilter: Encodable {
/// Custom `encode` implementation
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(limit, forKey: .limit)
try container.encode(offset, forKey: .offset)

// Convert the integers to strings to make our lives easier in `getParameters`
if limit != nil {
try container.encode(String(limit!), forKey: .limit)
}
if offset != nil {
try container.encode(String(offset!), forKey: .offset)
}
}

/// Coding keys to map our struct to JSON data
Expand Down Expand Up @@ -366,7 +372,6 @@ public class MDFeedFilter: MDPaginationFilter {
/// Custom `encode` implementation
override public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(locales, forKey: .locales)
try container.encode(createdAtSince, forKey: .createdAtSince)
try container.encode(updatedAtSince, forKey: .updatedAtSince)
try container.encode(publishedAtSince, forKey: .publishedAtSince)
Expand Down
5 changes: 5 additions & 0 deletions Tests/MangaDexLibTests/MDLibApiTests+Author.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ extension MDLibApiTests {

func testSearchAuthors() throws {
let filter = MDAuthorFilter(name: "ONE")
filter.limit = 4
filter.offset = 0

let expectation = self.expectation(description: "Get a list of authors")
api.searchAuthors(filter: filter) { (result, error) in
XCTAssertNil(error)
XCTAssertNotNil(result)
XCTAssert(result!.results.count > 0)
XCTAssertNotNil(result?.results.first?.object)
XCTAssertNotNil(result?.results.first?.object?.data)
XCTAssertEqual(result?.limit, filter.limit)
XCTAssertEqual(result?.offset, filter.offset)
expectation.fulfill()
}
waitForExpectations(timeout: 15, handler: nil)
Expand Down
4 changes: 4 additions & 0 deletions Tests/MangaDexLibTests/MDLibApiTests+Chapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ extension MDLibApiTests {
func testSearchChapters() throws {
let filter = MDChapterFilter(title: "Oneshot")
filter.createdAtSince = .init(timeIntervalSince1970: 0)
filter.limit = 3
filter.offset = 7

let expectation = self.expectation(description: "Get a list of chapters")
api.searchChapters(filter: filter) { (result, error) in
Expand All @@ -35,6 +37,8 @@ extension MDLibApiTests {
XCTAssert(result!.results.count > 0)
XCTAssertNotNil(result?.results.first?.object)
XCTAssertNotNil(result?.results.first?.object?.data)
XCTAssertEqual(result?.limit, filter.limit)
XCTAssertEqual(result?.offset, filter.offset)
expectation.fulfill()
}
waitForExpectations(timeout: 15, handler: nil)
Expand Down
5 changes: 5 additions & 0 deletions Tests/MangaDexLibTests/MDLibApiTests+Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ extension MDLibApiTests {

func testSearchScanlationGroups() throws {
let filter = MDGroupFilter(name: "mangadex")
filter.limit = 2
filter.offset = 1

let expectation = self.expectation(description: "Get a list of scanlation groups")
api.searchGroups(filter: filter) { (result, error) in
XCTAssertNil(error)
XCTAssertNotNil(result)
XCTAssert(result!.results.count > 0)
XCTAssertNotNil(result?.results.first?.object)
XCTAssertNotNil(result?.results.first?.object?.data)
XCTAssertEqual(result?.limit, filter.limit)
XCTAssertEqual(result?.offset, filter.offset)
expectation.fulfill()
}
waitForExpectations(timeout: 15, handler: nil)
Expand Down
4 changes: 4 additions & 0 deletions Tests/MangaDexLibTests/MDLibApiTests+Manga.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ extension MDLibApiTests {
func testSearchMangas() throws {
let filter = MDMangaFilter(title: "Solo leveling")
filter.createdAtSince = .init(timeIntervalSince1970: 0)
filter.limit = 8
filter.offset = 22

let expectation = self.expectation(description: "Get a list of mangas")
api.searchMangas(filter: filter) { (result, error) in
Expand All @@ -35,6 +37,8 @@ extension MDLibApiTests {
XCTAssert(result!.results.count > 0)
XCTAssertNotNil(result?.results.first?.object)
XCTAssertNotNil(result?.results.first?.object?.data)
XCTAssertEqual(result?.limit, filter.limit)
XCTAssertEqual(result?.offset, filter.offset)
expectation.fulfill()
}
waitForExpectations(timeout: 15, handler: nil)
Expand Down

0 comments on commit b546c85

Please sign in to comment.