#!/bin/bash

#/**
#* Copyright IBM Corporation 2016
#*
#* Licensed under the Apache License, Version 2.0 (the "License");
#* you may not use this file except in compliance with the License.
#* You may obtain a copy of the License at
#*
#* http://www.apache.org/licenses/LICENSE-2.0
#*
#* Unless required by applicable law or agreed to in writing, software
#* distributed under the License is distributed on an "AS IS" BASIS,
#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#* See the License for the specific language governing permissions and
#* limitations under the License.
#**/

# Test AggregateColumnExpressions and ScalarColumnExpressions with Aggregate as their argument.

SCRIPT_DIR=$(dirname "$BASH_SOURCE")
cd "$SCRIPT_DIR"
CUR_DIR=$(pwd)

temp=$(dirname "${CUR_DIR}")
temp=$(dirname "${temp}")
PKG_DIR=$(dirname "${CUR_DIR}")

shopt -s nullglob

if ! [ -d "${PKG_DIR}/Tests/SwiftKueryTests" ]; then
echo "Failed to find ${PKG_DIR}/Tests/SwiftKueryTests"
exit 1
fi

INPUT_FILE="${PKG_DIR}/Scripts/TestAggregate.txt"
INPUT_SCALAR_FILE="${PKG_DIR}/Scripts/TestAggregateScalar.txt"
OUTPUT_FILE="${PKG_DIR}/Tests/SwiftKueryTests/TestAggregateFunctions.swift"

echo "--- Generating ${OUTPUT_FILE}"

cat <<'EOF' > ${OUTPUT_FILE}
/**
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import XCTest

@testable import SwiftKuery

// This test was generated by Scripts/testAggregate.sh.
class TestAggregateFunctions: XCTestCase {

    static var allTests: [(String, (TestAggregateFunctions) -> () throws -> Void)] {
        return [
            ("testFunctions", testFunctions),
        ]
    }

    class MyTable: Table {
        let a = Column("a")
        let b = Column("b")

        let tableName = "table"
    }

    func testFunctions() {
        let t = MyTable()
        let connection = createConnection()
        var s = Select(from: t)
        var kuery = ""
        var query = ""

EOF

while read -r LINE; do
    [ -z "$LINE" ] && continue
    [[ "$LINE" =~ ^#.*$ ]] && continue
    stringarray=($LINE)
    FUNCTION=${stringarray[0]}
    SQL=${stringarray[1]}

cat <<EOF >> ${OUTPUT_FILE}

        s = Select(t.a, from: t)
            .group(by: t.a)
            .having($FUNCTION(t.b) > 3)
        kuery = connection.descriptionOf(query: s)
        query = "SELECT table.a FROM table GROUP BY table.a HAVING $SQL(table.b) > 3"
        XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")
EOF

done < $INPUT_FILE

cat <<EOF >> ${OUTPUT_FILE}

        s = Select(t.a, from: t)
            .group(by: t.a)
            .having(countDistinct(t.b) != 0)
        kuery = connection.descriptionOf(query: s)
        query = "SELECT table.a FROM table GROUP BY table.a HAVING COUNT(DISTINCT(table.b)) <> 0"
        XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")
EOF

while read -r LINE; do
    [ -z "$LINE" ] && continue
    [[ "$LINE" =~ ^#.*$ ]] && continue
    stringarray=($LINE)
    FUNCTION=${stringarray[0]}
    SQL=${stringarray[1]}

cat <<EOF >> ${OUTPUT_FILE}

        s = Select(t.a, from: t)
            .group(by: t.a)
            .having($FUNCTION(sum(t.b)) <= -1)
        kuery = connection.descriptionOf(query: s)
        query = "SELECT table.a FROM table GROUP BY table.a HAVING $SQL(SUM(table.b)) <= -1"
        XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")
EOF
done < $INPUT_SCALAR_FILE

cat <<EOF >> ${OUTPUT_FILE}
        s = Select(t.a, from: t)
            .group(by: t.a)
            .having(round(sum(t.b), to: 2) >= 9.08)
        kuery = connection.descriptionOf(query: s)
        query = "SELECT table.a FROM table GROUP BY table.a HAVING ROUND(SUM(table.b), 2) >= 9.08"
        XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

        s = Select(t.a, from: t)
            .group(by: t.a)
            .having(mid(sum(t.b), start: 3, length: 6) <= -1)
        kuery = connection.descriptionOf(query: s)
        query = "SELECT table.a FROM table GROUP BY table.a HAVING MID(SUM(table.b), 3, 6) <= -1"
        XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")
EOF
echo "  }" >> ${OUTPUT_FILE}
echo "}" >> ${OUTPUT_FILE}



