Skip to content

Commit

Permalink
fix Tests compilation: move the DEBUG assert into testing module & ad…
Browse files Browse the repository at this point in the history
…d ios min v13.0 to ModerationsResult
  • Loading branch information
James J Kalafus committed Feb 15, 2024
1 parent c5bd8c2 commit 4b69b8e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions Sources/OpenAI/OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ final public class OpenAI: OpenAIProtocol {
performRequest(request: JSONRequest<ModelsResult>(url: buildURL(path: .models), method: "GET"), completion: completion)
}

@available(iOS 13.0, *)
public func moderations(query: ModerationsQuery, completion: @escaping (Result<ModerationsResult, Error>) -> Void) {
performRequest(request: JSONRequest<ModerationsResult>(body: query, url: buildURL(path: .moderations)), completion: completion)
}
Expand Down
12 changes: 2 additions & 10 deletions Sources/OpenAI/Public/Models/ModerationsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation

@available(iOS 13.0, *)
public struct ModerationsResult: Codable, Equatable {

public struct Moderation: Codable, Equatable {
Expand Down Expand Up @@ -52,11 +53,6 @@ public struct ModerationsResult: Codable, Equatable {

public func makeIterator() -> IndexingIterator<[(String, Bool)]> {
return Mirror(reflecting: self).children.enumerated().map { (index, element) in
#if DEBUG
if #available(iOS 16.0, *) {
assert(element.label!.lowercased() == CodingKeys.allCases[index].stringValue.replacing(try! Regex("[/-]"), with: { _ in "" }))
}
#endif
return (CodingKeys.allCases[index].stringValue, element.value) as! (String, Bool)
}.makeIterator()
}
Expand Down Expand Up @@ -103,11 +99,6 @@ public struct ModerationsResult: Codable, Equatable {

public func makeIterator() -> IndexingIterator<[(String, Bool)]> {
return Mirror(reflecting: self).children.enumerated().map { (index, element) in
#if DEBUG
if #available(iOS 16.0, *) {
assert(element.label!.lowercased() == CodingKeys.allCases[index].stringValue.replacing(try! Regex("[/-]"), with: { _ in "" }))
}
#endif
return (CodingKeys.allCases[index].stringValue, element.value) as! (String, Bool)
}.makeIterator()
}
Expand All @@ -132,4 +123,5 @@ public struct ModerationsResult: Codable, Equatable {
public let results: [Self.Moderation]
}

@available(iOS 13.0, *)
extension ModerationsResult: Identifiable {}
1 change: 1 addition & 0 deletions Sources/OpenAI/Public/Protocols/OpenAIProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public protocol OpenAIProtocol {
- query: A `ModerationsQuery` object containing the input parameters for the API request. This includes the input text and optionally the model to be used.
- completion: A closure which receives the result when the API request finishes. The closure's parameter, `Result<ModerationsResult, Error>`, will contain either the `ModerationsResult` object with the list of category results, or an error if the request failed.
**/
@available(iOS 13.0, *)
func moderations(query: ModerationsQuery, completion: @escaping (Result<ModerationsResult, Error>) -> Void)

/**
Expand Down
16 changes: 15 additions & 1 deletion Tests/OpenAITests/OpenAITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,21 @@ class OpenAITests: XCTestCase {
let result = try await openAI.moderations(query: query)
XCTAssertEqual(result, moderationsResult)
}


func testModerationsIterable() {
let categories = ModerationsResult.Moderation.Categories(harassment: false, harassmentThreatening: false, hate: false, hateThreatening: false, selfHarm: false, selfHarmIntent: false, selfHarmInstructions: false, sexual: false, sexualMinors: false, violence: false, violenceGraphic: false)
Mirror(reflecting: categories).children.enumerated().forEach { index, element in
let label = ModerationsResult.Moderation.Categories.CodingKeys.allCases[index].stringValue.replacing(try! Regex("[/-]"), with: { _ in "" })
XCTAssertEqual(label, element.label!.lowercased())
}

let categoryScores = ModerationsResult.Moderation.CategoryScores(harassment: 0.1, harassmentThreatening: 0.1, hate: 0.1, hateThreatening: 0.1, selfHarm: 0.1, selfHarmIntent: 0.1, selfHarmInstructions: 0.1, sexual: 0.1, sexualMinors: 0.1, violence: 0.1, violenceGraphic: 0.1)
Mirror(reflecting: categoryScores).children.enumerated().forEach { index, element in
let label = ModerationsResult.Moderation.CategoryScores.CodingKeys.allCases[index].stringValue.replacing(try! Regex("[/-]"), with: { _ in "" })
XCTAssertEqual(label, element.label!.lowercased())
}
}

func testModerationsError() async throws {
let query = ModerationsQuery(input: "Hello, world!")
let inError = APIError(message: "foo", type: "bar", param: "baz", code: "100")
Expand Down

0 comments on commit 4b69b8e

Please sign in to comment.