101 lines
3.6 KiB
Swift
101 lines
3.6 KiB
Swift
//
|
|
// StorageServiceTests+Events.swift
|
|
// AutoCatCoreTests
|
|
//
|
|
// Created by Selim Mustafaev on 13.12.2024.
|
|
// Copyright © 2024 Selim Mustafaev. All rights reserved.
|
|
//
|
|
|
|
import Testing
|
|
import AutoCatCore
|
|
|
|
extension StorageServiceTests {
|
|
|
|
@Test("Add event")
|
|
func addEvent() async throws {
|
|
|
|
let event = VehicleEventDto(lat: testLat, lon: testLon, addedBy: nil)
|
|
|
|
let vehicle = try await storageService.add(event: event, to: existingVehicleNumber)
|
|
|
|
#expect(vehicle.events.count == 1)
|
|
#expect(vehicle.events.first?.latitude == testLat)
|
|
#expect(vehicle.events.first?.longitude == testLon)
|
|
#expect(vehicle.updatedDate != 0)
|
|
}
|
|
|
|
@Test("Add event to non-existent vehicle")
|
|
func addEventToNonExistentVehicle() async throws {
|
|
|
|
let event = VehicleEventDto(lat: testLat, lon: testLon, addedBy: nil)
|
|
|
|
await #expect(throws: StorageError.vehicleNotFound) {
|
|
_ = try await storageService.add(event: event, to: nonExistingVehicleNumber)
|
|
}
|
|
}
|
|
|
|
@Test("Remove event")
|
|
func removeEvent() async throws {
|
|
|
|
let event = VehicleEventDto(lat: testLat, lon: testLon, addedBy: nil)
|
|
var vehicle = try await storageService.add(event: event, to: existingVehicleNumber)
|
|
let id = try #require(vehicle.events.first { $0.latitude == testLat && $0.longitude == testLon }?.id)
|
|
|
|
vehicle = try await storageService.remove(event: id, from: existingVehicleNumber)
|
|
|
|
#expect(vehicle.events.isEmpty)
|
|
#expect(vehicle.updatedDate != 0)
|
|
}
|
|
|
|
@Test("Remove event from non-existent vehicle")
|
|
func removeEventFromNonExistentVehicle() async throws {
|
|
|
|
await #expect(throws: StorageError.vehicleNotFound) {
|
|
_ = try await storageService.remove(event: "", from: nonExistingVehicleNumber)
|
|
}
|
|
}
|
|
|
|
@Test("Remove non-existent event")
|
|
func removeNonExistentEvent() async throws {
|
|
|
|
await #expect(throws: StorageError.eventNotFound) {
|
|
_ = try await storageService.remove(event: "", from: existingVehicleNumber)
|
|
}
|
|
}
|
|
|
|
@Test("Edit event")
|
|
func editEvent() async throws {
|
|
|
|
let event = VehicleEventDto(lat: 0, lon: 0, addedBy: nil)
|
|
var vehicle = try await storageService.add(event: event, to: existingVehicleNumber)
|
|
|
|
var editedEvent = VehicleEventDto(lat: testLat, lon: testLon, addedBy: nil)
|
|
editedEvent.id = try #require(vehicle.events.first?.id)
|
|
|
|
vehicle = try await storageService.edit(event: editedEvent, for: existingVehicleNumber)
|
|
|
|
let resultEvent = try #require(vehicle.events.first { $0.id == editedEvent.id })
|
|
#expect(resultEvent.latitude == testLat && resultEvent.longitude == testLon)
|
|
#expect(vehicle.events.count == 1)
|
|
#expect(vehicle.updatedDate != 0)
|
|
}
|
|
|
|
@Test("Edit non-existent event")
|
|
func editNonExistentEvent() async throws {
|
|
|
|
let event = VehicleEventDto(lat: 0, lon: 0, addedBy: nil)
|
|
await #expect(throws: StorageError.eventNotFound) {
|
|
_ = try await storageService.edit(event: event, for: existingVehicleNumber)
|
|
}
|
|
}
|
|
|
|
@Test("Edit event in non-existent vehicle")
|
|
func editEventInNonExistentVehicle() async throws {
|
|
|
|
let event = VehicleEventDto(lat: 0, lon: 0, addedBy: nil)
|
|
await #expect(throws: StorageError.vehicleNotFound) {
|
|
_ = try await storageService.edit(event: event, for: nonExistingVehicleNumber)
|
|
}
|
|
}
|
|
}
|