Skip to content

Commit

Permalink
fix and update mock data tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kiliankoe committed Nov 15, 2024
1 parent 4754ce2 commit a88e05d
Show file tree
Hide file tree
Showing 7 changed files with 813 additions and 133 deletions.
2 changes: 1 addition & 1 deletion Sources/EmealKit/Mensa/MealFeed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Meal {
}
}

public static func rssData(session: URLSession = .shared) async throws -> [RSSMeal] {
public static func rssData() async throws -> [RSSMeal] {
let feedURL = URL(string: "https://www.studentenwerk-dresden.de/feeds/speiseplan.rss")!
let parser = FeedParser(URL: feedURL)
return try await withCheckedThrowingContinuation { continuation in
Expand Down
28 changes: 17 additions & 11 deletions Sources/EmealKit/Mensa/MensaAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ internal extension URL {
// MARK: - Canteens

extension Canteen {
public static func all(session: URLSession = .shared) async throws(EmealError) -> [Canteen] {
public static func all(session: URLSessionProtocol = URLSession.shared) async throws(EmealError) -> [Canteen] {
Logger.emealKit.debug("Fetching all canteens")
do {
let (data, _) = try await session.data(from: URL.Mensa.canteens)
print(String(data: data, encoding: .utf8)!)
let canteens = try JSONDecoder().decode([Canteen].self, from: data)
Logger.emealKit.debug("Successfully fetched \(canteens.count) canteens")
return canteens
Expand All @@ -31,30 +32,35 @@ extension Canteen {
// MARK: - Meals

extension Meal {
public static func `for`(canteen: Int, on date: Date, session: URLSession = .shared) async throws(EmealError) -> [Meal] {
public static func `for`(canteen: Int, on date: Date, session: URLSessionProtocol = URLSession.shared) async throws(EmealError) -> [Meal] {
Logger.emealKit.debug("Fetching meals for canteen \(canteen) on \(date)")
do {
let (data, _) = try await session.data(from: URL.Mensa.meals(canteen: canteen, date: date))
let meals = try JSONDecoder().decode([Meal].self, from: data)
Logger.emealKit.debug("Successfully fetched \(meals.count) meals")

let feedItems = try await Self.rssData()
return meals.map { meal in
var meal = meal
let matchingItem = feedItems.first { $0.matches(meal: meal) }
if let matchingItem {
Logger.emealKit.debug("Found matching feeditem for \(meal.id)")
meal.isSoldOut = matchingItem.isSoldOut
do {
let feedItems = try await Self.rssData()
return meals.map { meal in
var meal = meal
let matchingItem = feedItems.first { $0.matches(meal: meal) }
if let matchingItem {
Logger.emealKit.debug("Found matching feeditem for \(meal.id)")
meal.isSoldOut = matchingItem.isSoldOut
}
return meal
}
return meal
} catch (let error) {
Logger.emealKit.log("Failed to fetch rss data, continuing without: \(String(describing: error))")
return meals
}
} catch (let error) {
Logger.emealKit.error("Failed to fetch meal data: \(String(describing: error))")
throw .other(error)
}
}

public static func `for`(canteen: CanteenId, on date: Date, session: URLSession = .shared) async throws(EmealError) -> [Meal] {
public static func `for`(canteen: CanteenId, on date: Date, session: URLSessionProtocol = URLSession.shared) async throws(EmealError) -> [Meal] {
try await Self.for(canteen: canteen.rawValue, on: date, session: session)
}
}
9 changes: 9 additions & 0 deletions Sources/EmealKit/URLSessionProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

public protocol URLSessionProtocol {
func data(from url: URL) async throws -> (Data, URLResponse)
}

extension URLSession: URLSessionProtocol {

}
4 changes: 2 additions & 2 deletions Tests/EmealKitTests/CanteenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import CoreLocation
class CanteenTests: XCTestCase {
@available(macOS 12.0, iOS 15.0, *)
func testMockFetchAndDecode() async throws {
let canteens = try await Canteen.all(session: MockURLSession(mockData: .canteens))
XCTAssertEqual(canteens.count, 21)
let canteens = try await Canteen.all(session: MockURLSession(data: .canteens))
XCTAssertEqual(canteens.count, 16)
}

func testLocation() {
Expand Down
26 changes: 21 additions & 5 deletions Tests/EmealKitTests/MealTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import EmealKit
class MealTests: XCTestCase {
@available(macOS 12.0, iOS 15.0, *)
func testMockFetchAndDecode() async throws {
let meals = try await Meal.for(canteen: .alteMensa, on: Date(), session: MockURLSession(mockData: .meals))
XCTAssertEqual(meals.count, 5)
let meals = try await Meal.for(canteen: .alteMensa, on: Date(), session: MockURLSession(data: .meals))
XCTAssertEqual(meals.count, 4)
}

func testPlaceholderImage() {
let meal = Meal(id: 0, name: "", notes: [], prices: nil, category: "",
image: URL(string: "https://static.studentenwerk-dresden.de/bilder/mensen/studentenwerk-dresden-lieber-mensen-gehen.jpg")!,
url: URL(string: "q")!)
let meal = Meal(
id: 0,
name: "",
notes: [],
prices: nil,
category: "",
image: URL(string: "https://static.studentenwerk-dresden.de/bilder/mensen/studentenwerk-dresden-lieber-mensen-gehen.jpg")!,
url: URL(string: "q")!
)
XCTAssert(meal.imageIsPlaceholder)
}

Expand Down Expand Up @@ -78,5 +84,15 @@ class MealTests: XCTestCase {
XCTAssertEqual(prices2.students, 1.0)
XCTAssertEqual(prices2.employees, 1.0)
}

func testFeedData() async throws {
// Unfortunately we can't really test this with mock data since there's no way to inject anything into FeedKit.
let feedItems = try await Meal.rssData()
XCTAssertGreaterThan(feedItems.count, 0)
}

func testSoldOut() async throws {
// see above
}
}

Loading

0 comments on commit a88e05d

Please sign in to comment.