Skip to content

Commit

Permalink
Merge pull request #39 from phiHero/conversation
Browse files Browse the repository at this point in the history
fix: type `conversationHistory` should be an array & integer parameters incorrectly converted to boolean
  • Loading branch information
jasonbosco authored Nov 5, 2024
2 parents 67cc20a + f86f66f commit 0f6c550
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Sources/Typesense/Models/SearchResultConversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import Foundation
public struct SearchResultConversation: Codable {

public var answer: String
public var conversationHistory: [String: String]
public var conversationHistory: [[String: String]]
public var conversationId: String
public var query: String

public init(answer: String, conversationHistory: [String: String], conversationId: String, query: String) {
public init(answer: String, conversationHistory: [[String: String]], conversationId: String, query: String) {
self.answer = answer
self.conversationHistory = conversationHistory
self.conversationId = conversationId
Expand Down
2 changes: 1 addition & 1 deletion Sources/Typesense/utils/CreateURLQueryParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func createURLQuery<T: Codable>(forSchema: T?) throws -> [URLQueryItem]{
var stringVal = ""
if let str = val as? String {
stringVal = str
} else if let bool = val as? Bool {
} else if type(of: val) == type(of: NSNumber(booleanLiteral: true)) || type(of: val) == type(of: NSNumber(booleanLiteral: false)), let bool = val as? Bool{
stringVal = String(bool)
} else if let int = val as? Int {
stringVal = String(int)
Expand Down
34 changes: 34 additions & 0 deletions Tests/TypesenseTests/ConversationModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,38 @@ final class ConversationModelTests: XCTestCase {
}
}

func testConversationSearch() async {
do {
let string = """
{
"conversation": {
"answer": " context information, I' unable to suggest an is information given about specific context action as,, specific titles If a preference for particular genre length of, please that information and I' try my best to suggestions.",
"conversation_history": [
{
"user": "can you suggest an action series"
},
{
"assistant": " context information, I' unable to suggest an is information given about specific context action as,, specific titles If a preference for particular genre length of, please that information and I' try my best to suggestions."
}
],
"conversation_id": "123",
"query": "can you suggest an action series"
},
"results": [
{
"code": 404,
"error": "Not found."
}
]
}
"""
let data = string.data(using: .utf8)!
let _ = try decoder.decode(MultiSearchResult<Never>.self, from: data)
XCTAssertTrue(true)
}catch (let error) {
print(error)
XCTAssertTrue(false)
}
}

}
11 changes: 10 additions & 1 deletion Tests/TypesenseTests/CreateURLQueryParamsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,41 @@ final class CreateURLQueryParamsTests: XCTestCase {
var name = "Fast"
var age: Int = 20
var price: Int64 = 2000
var page: Int? = 1
var groupLimit: Int64? = 0
var speed: Double = 120.555
var mileage: Float = 120.5
var isElectric = true
var inStock: Bool = false
var empty: String? = nil

enum CodingKeys: String, CodingKey {
case name
case age
case price
case page
case groupLimit = "group_limit"
case speed
case mileage
case isElectric = "is_electric"
case inStock = "in_stock"
case empty
}
}
func testCreateURLQueryParams() {
do {
let queryParams = try createURLQuery(forSchema: Car())
print(queryParams)
XCTAssertEqual(queryParams.count, 6)
XCTAssertEqual(queryParams.count, 9)
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "name", value: "Fast")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "age", value: "20")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "price", value: "2000")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "page", value: "1")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "group_limit", value: "0")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "speed", value: "120.555")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "mileage", value: "120.5")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "is_electric", value: "true")))
XCTAssertTrue(queryParams.contains(URLQueryItem(name: "in_stock", value: "false")))
XCTAssertFalse(queryParams.contains(URLQueryItem(name: "empty", value: "null")))
} catch (let error) {
print(error)
Expand Down
2 changes: 1 addition & 1 deletion Tests/TypesenseTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ final class DocumentTests: XCTestCase {
}

func testDocumentSearch() async {
let searchParams = SearchParameters(q: "stark", queryBy: "company_name", filterBy: "num_employees:>100", sortBy: "num_employees:desc")
let searchParams = SearchParameters(q: "*", queryBy: "company_name", page: 0, groupBy: "country", groupLimit: 1)

do {
try await createDocument()
Expand Down

0 comments on commit 0f6c550

Please sign in to comment.