func handleBetween()

in AmplifyPlugins/Core/AWSPluginsCoreTests/Query/QueryPredicateEvaluateGenerator.swift [242:337]


    func handleBetween(type1: String, val1: String, type2: String, val2: String, val3: String, operation: String) -> Bool {
        guard let oper = operationMap[operation],
            let fieldName = fieldForType[type1] else {
                print("Failed to look up operation")
                return false
        }

        let v1FnName = val1
            .replacingOccurrences(of: ".", with: "_")
            .replacingOccurrences(of: "\"", with: "")
            .replacingOccurrences(of: "(", with: "")
            .replacingOccurrences(of: ")", with: "")
            .replacingOccurrences(of: ":", with: "")
            .replacingOccurrences(of: ",", with: "")
            .replacingOccurrences(of: " ", with: "")

        let v2FnName = val2
            .replacingOccurrences(of: ".", with: "_")
            .replacingOccurrences(of: "\"", with: "")
            .replacingOccurrences(of: "(", with: "")
            .replacingOccurrences(of: ")", with: "")
            .replacingOccurrences(of: ":", with: "")
            .replacingOccurrences(of: ",", with: "")
            .replacingOccurrences(of: " ", with: "")
        let type1 = type1.replacingOccurrences(of: ".", with: "")
        let type2 = type2.replacingOccurrences(of: ".", with: "")

        //In cases of between, we should check we have v1, v2 and v3, E.g.: (v1 < v3 && v3 > v2)
        if val2 == "" {
            return false
        }

        let v3FnName = val3.replacingOccurrences(of: ".", with: "_")
            .replacingOccurrences(of: "\"", with: "")
            .replacingOccurrences(of: "(", with: "")
            .replacingOccurrences(of: ")", with: "")
            .replacingOccurrences(of: ":", with: "")
            .replacingOccurrences(of: ",", with: "")
            .replacingOccurrences(of: " ", with: "")

        print("func test\(operation)\(type1)\(v1FnName)\(operation)\(type2)\(v2FnName)with\(v3FnName)() throws {")

        if type1 == "TemporalDateTime" {
            print("   let dateTimeNow = Temporal.DateTime.now()")
        } else if type1 == "TemporalTime" {
            print("   let timeNow = try Temporal.Time.init(iso8601String: \"10:16:44\")")
        }
        let v1LocalRef = val1
            .replacingOccurrences(of: "Temporal.DateTime.now()",
                                  with: "dateTimeNow")
            .replacingOccurrences(of: "Temporal.Time.now()",
                                  with: "timeNow")
        let v2LocalRef = val2
            .replacingOccurrences(of: "Temporal.DateTime.now()",
                                  with: "dateTimeNow")
            .replacingOccurrences(of: "Temporal.Time.now()",
                                  with: "timeNow")
        let v3LocalRef = val3
            .replacingOccurrences(of: "Temporal.DateTime.now()",
                                  with: "dateTimeNow")
            .replacingOccurrences(of: "Temporal.Time.now()",
                                  with: "timeNow")

        print("   let predicate = QPredGen.keys.\(fieldName).\(oper)(start: \(v1LocalRef), end: \(v2LocalRef))")
        if val3 != "" {
            print("   var instance = QPredGen(name: \"test\")")
            print("   instance.\(fieldName) = \(v3LocalRef)")
        } else {
            print("   let instance = QPredGen(name: \"test\")")
        }
        print("")
        print("   let evaluation = try predicate.evaluate(target: instance.eraseToAnyModel().instance)")
        print("")
        if type1 == "String" {
            if attemptToResolveBetweenString(val1, val2, val3) {
                print("   XCTAssert(evaluation)")
            } else {
                print("   XCTAssertFalse(evaluation)")
            }
        } else if type1.contains("Temporal") {
            if attemptToResolveBetweenTemporal(type1, val1, type2, val2, val3) {
                print("   XCTAssert(evaluation)")
            } else {
                print("   XCTAssertFalse(evaluation)")
            }
        } else {
            if attemptToResolveBetweenDouble(val1, val2, val3) {
                print("   XCTAssert(evaluation)")
            } else {
                print("   XCTAssertFalse(evaluation)")
            }
        }
        print("}")
        print("")
        return true
    }