Save automatically, improve mocks

This commit is contained in:
Christoph Hagen
2025-02-05 12:24:33 +01:00
parent d41c54d174
commit 5abe6e1a9f
55 changed files with 701 additions and 381 deletions

View File

@ -12,11 +12,14 @@ extension FileManager {
extension Content {
static let mock: Content = Content(
settings: .default,
posts: [.empty, .mock, .fullMock],
pages: [.empty],
tags: [.hiking, .mountains, .nature, .sports],
files: [],
tagOverview: nil)
static let mock: Content = {
let content = Content()
content.files = FileResource.Mock.mockData(content: content)
content.tags = Tag.Mock.mockData(content: content)
content.posts = Post.Mock.mockData(content: content)
content.pages = Page.Mock.mockData(content: content)
return content
}()
}

View File

@ -1,12 +1,25 @@
extension FileResource {
enum Mock {
static func mockData(content: Content) -> [FileResource] {
[
FileResource(content: content, resourceImage: "image1", type: .jpg),
FileResource(content: content, resourceImage: "image2", type: .jpg),
FileResource(content: content, resourceImage: "image3", type: .jpg),
FileResource(content: content, resourceImage: "image4", type: .jpg),
FileResource(
content: content,
id: "my-file.txt",
isExternallyStored: true,
english: "Some text file",
german: "Eine Textdatei")
]
}
}
static var mock: FileResource {
.init(
content: .mock,
id: "my-file.txt",
isExternallyStored: true,
english: "Some text file",
german: "Eine Textdatei")
Content.mock.files.first(where: { $0.id == "my-file.txt" })!
}
}

View File

@ -1,19 +0,0 @@
import SwiftUI
/// An image loaded from the app resources for test purposes
struct MockImage {
let name: String
static var images: [FileResource] {
["image1", "image2", "image3", "image4"]
.map { FileResource(resourceImage: $0, type: .jpg) }
}
}
extension MockImage: ExpressibleByStringLiteral {
init(stringLiteral value: StringLiteralType) {
self.name = value
}
}

View File

@ -2,36 +2,40 @@ import Foundation
extension Page {
static var empty: Page {
.init(
content: .mock,
id: "my-id",
externalLink: nil,
isDraft: true,
createdDate: Date(),
hideDate: false,
startDate: Date().addingTimeInterval(-86400),
endDate: nil,
german: .german,
english: .english,
tags: [.mock],
requiredFiles: [])
enum Mock {
static func mockData(content: Content) -> [Page] {
[
Page(
content: content,
id: "my-id",
externalLink: nil,
isDraft: true,
createdDate: Date(),
hideDate: false,
startDate: Date().addingTimeInterval(-86400),
endDate: nil,
german: LocalizedPage(
content: content,
urlString: "mein-projekt",
title: "Mein Erstes Projekt",
lastModified: nil,
originalUrl: "projects/electronics/my-first-project/de.html"),
english: LocalizedPage(
content: content,
urlString: "my-project",
title: "My First Project",
lastModified: nil,
originalUrl: "projects/electronics/my-first-project/en.html"),
tags: [
content.tags.first(where: { $0.id == "electronics" })!
],
requiredFiles: [])
]
}
static var empty: Page {
Content.mock.pages.first(where: { $0.id == "my-id" })!
}
}
}
extension LocalizedPage {
static let english = LocalizedPage(
content: .mock,
urlString: "my-project",
title: "My First Project",
lastModified: nil,
originalUrl: "projects/electronics/my-first-project/en.html")
static let german = LocalizedPage(
content: .mock,
urlString: "mein-projekt",
title: "Mein Erstes Projekt",
lastModified: nil,
originalUrl: "projects/electronics/my-first-project/de.html")
}

View File

@ -1,65 +1,90 @@
extension Post {
static var empty: Post {
.init(content: Content.mock,
id: "empty",
isDraft: true,
createdDate: .now,
startDate: .now,
endDate: nil,
tags: [],
german: .init(content: .mock,
text: "Text"),
english: .init(content: .mock,
text: "Text"),
linkedPage: nil)
}
enum Mock {
static var mock: Post {
Post(
content: Content.mock,
id: "mock",
isDraft: false,
createdDate: .now,
startDate: .now,
endDate: nil,
tags: [.nature, .sports, .hiking],
german: .init(
content: .mock,
title: "Der Titel",
text: "Sehr schöne und einsame Tour von Oberau zum Krottenkopf. Abwechslungsreich und stellenweise fordernd, aufgrund der Länge und der Höhenmeter auch anstrengend."),
english: .init(
content: .mock,
title: "The title",
text: "Very nice and solitary hike from Oberau to Krottenkopf. Challenging and rewarding, due to the length and height")
)
}
static func mockData(content: Content) -> [Post] {
[
Post(content: content,
id: "empty",
isDraft: true,
createdDate: .now,
startDate: .now,
endDate: nil,
tags: [],
german: .init(
content: content,
text: "Text"),
english: .init(
content: content,
text: "Text"),
linkedPage: nil),
Post(
content: content,
id: "hike",
isDraft: false,
createdDate: .now,
startDate: .now,
endDate: nil,
tags: [
content.tags.first(where: { $0.id == "nature" })!,
content.tags.first(where: { $0.id == "sports" })!,
content.tags.first(where: { $0.id == "hiking" })!
],
german: .init(
content: content,
title: "Eine Wanderung",
text: "Sehr schöne und einsame Tour von Oberau zum Krottenkopf. Abwechslungsreich und stellenweise fordernd, aufgrund der Länge und der Höhenmeter auch anstrengend."),
english: .init(
content: content,
title: "A hike",
text: "Very nice and solitary hike from Oberau to Krottenkopf. Challenging and rewarding, due to the length and height")
),
Post(
content: content,
id: "hike2",
isDraft: true,
createdDate: .now,
startDate: .now.addingTimeInterval(-86400), endDate: .now,
tags: [
content.tags.first(where: { $0.id == "nature" })!,
content.tags.first(where: { $0.id == "sports" })!,
content.tags.first(where: { $0.id == "hiking" })!,
content.tags.first(where: { $0.id == "mountains" })!
],
german: LocalizedPost(
content: content,
title: "Eine lange Wanderung",
text: "Sehr schöne und einsame Tour von Oberau zum Krottenkopf. Abwechslungsreich und stellenweise fordernd, aufgrund der Länge und der Höhenmeter auch anstrengend.",
images: [
content.files.first(where: { $0.id == "image1" })!,
content.files.first(where: { $0.id == "image2" })!,
content.files.first(where: { $0.id == "image3" })!,
content.files.first(where: { $0.id == "image4" })!
]),
english: LocalizedPost(
content: content,
title: "A longer hike",
text: "Very nice and solitary hike from Oberau to Krottenkopf. Challenging and rewarding, due to the length and height.",
images: [
content.files.first(where: { $0.id == "image1" })!,
content.files.first(where: { $0.id == "image2" })!,
content.files.first(where: { $0.id == "image3" })!,
content.files.first(where: { $0.id == "image4" })!
]))
]
}
static var fullMock: Post {
.init(
content: Content.mock,
id: "full",
isDraft: true,
createdDate: .now,
startDate: .now.addingTimeInterval(-86400), endDate: .now,
tags: [.nature, .sports, .hiking, .mountains],
german: .german,
english: .english)
static var empty: Post {
Content.mock.posts.first(where: { $0.id == "empty" })!
}
static var hike: Post {
Content.mock.posts.first(where: { $0.id == "hike" })!
}
static var hike2: Post {
Content.mock.posts.first(where: { $0.id == "hike2" })!
}
}
}
extension LocalizedPost {
static let german = LocalizedPost(
content: .mock,
title: "Ein langer Titel",
text: "Sehr schöne und einsame Tour von Oberau zum Krottenkopf. Abwechslungsreich und stellenweise fordernd, aufgrund der Länge und der Höhenmeter auch anstrengend.",
images: MockImage.images)
static let english = LocalizedPost(
content: .mock,
title: "A longer title",
text: "Very nice and solitary hike from Oberau to Krottenkopf. Challenging and rewarding, due to the length and height.",
images: MockImage.images)
}

View File

@ -2,56 +2,74 @@ import Foundation
extension Tag {
static let mock = Tag(
content: .mock,
id: "electronics",
german: .german,
english: .english)
enum Mock {
static let nature = Tag(
content: .mock,
id: "nature",
german: .init(content: .mock, urlComponent: "natur", name: "Natur"),
english: .init(content: .mock, urlComponent: "nature", name: "Nature")
)
static func mockData(content: Content) -> [Tag] {
[
Tag(content: content,
id: "electronics",
german: .init(
content: content,
urlComponent: "elektronik",
name: "Elektronik",
linkPreview: .init(description: "Eine Beschreibung des Tags",
image: content.files.first(where: { $0.id == "image2" })!),
originalUrl: "projects/electronics"
),
english: .init(
content: content,
urlComponent: "electronics",
name: "Electronics",
linkPreview: .init(
description: "Some description of the tag",
image: content.files.first(where: { $0.id == "image1" })!),
originalUrl: "projects/electronics")
),
Tag(
content: content,
id: "nature",
german: .init(content: content, urlComponent: "natur", name: "Natur"),
english: .init(content: content, urlComponent: "nature", name: "Nature")
),
Tag(
content: content,
id: "sports",
german: .init(content: content, urlComponent: "sport", name: "Sport"),
english: .init(content: content, urlComponent: "sports", name: "Sports")
),
Tag(
content: content,
id: "hiking",
german: .init(content: content, urlComponent: "wandern", name: "Wandern"),
english: .init(content: content, urlComponent: "hiking", name: "Hiking")
),
Tag(
content: content,
id: "mountains",
german: .init(content: content, urlComponent: "berge", name: "Berge"),
english: .init(content: content, urlComponent: "mountains", name: "Mountains")
)
]
}
static let sports = Tag(
content: .mock,
id: "sports",
german: .init(content: .mock, urlComponent: "sport", name: "Sport"),
english: .init(content: .mock, urlComponent: "sports", name: "Sports")
)
static let hiking = Tag(
content: .mock,
id: "hiking",
german: .init(content: .mock, urlComponent: "wandern", name: "Wandern"),
english: .init(content: .mock, urlComponent: "hiking", name: "Hiking")
)
static let mountains = Tag(
content: .mock,
id: "mountains",
german: .init(content: .mock, urlComponent: "berge", name: "Berge"),
english: .init(content: .mock, urlComponent: "mountains", name: "Mountains")
)
}
extension LocalizedTag {
static let english = LocalizedTag(
content: .mock,
urlComponent: "electronics",
name: "Electronics",
linkPreview: .init(description: "Some description of the tag",
image: FileResource(resourceImage: "image1", type: .jpg)),
originalUrl: "projects/electronics")
static let german = LocalizedTag(
content: .mock,
urlComponent: "elektronik",
name: "Elektronik",
linkPreview: .init(description: "Eine Beschreibung des Tags",
image: FileResource(resourceImage: "image2", type: .jpg)),
originalUrl: "projects/electronics")
static var electronics: Tag {
Content.mock.tags.first(where: { $0.id == "electronics" })!
}
static var nature: Tag {
Content.mock.tags.first(where: { $0.id == "nature" })!
}
static var sports: Tag {
Content.mock.tags.first(where: { $0.id == "sports" })!
}
static var hiking: Tag {
Content.mock.tags.first(where: { $0.id == "hiking" })!
}
static var mountains: Tag {
Content.mock.tags.first(where: { $0.id == "mountains" })!
}
}
}