summaryrefslogtreecommitdiff
path: root/sandbox/src/test/kotlin/DBTest.kt
blob: bc5a33c5b455279b240b427830b0a9d8de402394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2020 Taler Systems S.A.
 *
 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.
 *
 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.Test
import tech.libeufin.sandbox.*
import tech.libeufin.util.connectWithSchema
import tech.libeufin.util.getCurrentUser
import tech.libeufin.util.getJdbcConnectionFromPg
import tech.libeufin.util.millis
import java.io.File
import java.time.LocalDateTime
import kotlin.reflect.KProperty
import kotlin.reflect.typeOf

/**
 * Run a block after connecting to the test database.
 * Cleans up the DB file afterwards.
 */
fun withTestDatabase(f: () -> Unit) {
    dbDropTables("postgresql:///libeufincheck")
    dbCreateTables("postgresql:///libeufincheck")
    f()
}

class DBTest {
    private var config = DemobankConfig(
        currency = "EUR",
        bankDebtLimit = 1000000,
        usersDebtLimit = 10000,
        allowRegistrations = true,
        demobankName = "default",
        withSignupBonus = false,
    )

    /**
     * This tests the conversion from a Postgres connection
     * string to a JDBC one.
     */
    @Test
    fun connectionStringTest() {
        getJdbcConnectionFromPg("postgres://auditor-basedb")
        var conv = getJdbcConnectionFromPg("postgresql:///libeufincheck")
        connectWithSchema(getJdbcConnectionFromPg("postgres:///libeufincheck"))
        connectWithSchema(conv)
        conv = getJdbcConnectionFromPg("postgresql://localhost:5432/libeufincheck?user=${System.getProperty("user.name")}")
        connectWithSchema(conv)
        conv = getJdbcConnectionFromPg("postgresql:///libeufincheck?host=/tmp/libeufin")
        var exception: Exception? = null
        try {
            connectWithSchema(conv)
        } catch (e: Exception) {
            exception = e
        }
        assert(exception is UtilError)
    }

    /**
     * Storing configuration values into the database,
     * then extract them and check that they equal the
     * configuration model object.
     */
    @Test
    fun insertPairsTest() {
        withTestDatabase {
            // Config model.
            val config = DemobankConfig(
                currency = "EUR",
                bankDebtLimit = 1,
                usersDebtLimit = 2,
                allowRegistrations = true,
                demobankName = "default",
                withSignupBonus = true
            )
            transaction {
                DemobankConfigEntity.new { name = "default" }
                insertConfigPairs(config)
                val db = getDefaultDemobank()
                /**
                 * db.config extracts config values from the database
                 * and puts them in a fresh config model object.
                 */
                assert(config.hashCode() == db.config.hashCode())
            }
        }
    }

    @Test
    fun betweenDates() {
        withTestDatabase {
            transaction {
                insertConfigPairs(config)
                val demobank = DemobankConfigEntity.new {
                    name = "default"
                }
                val bankAccount = BankAccountEntity.new {
                    iban = "iban"
                    bic = "bic"
                    label = "label"
                    owner = "test"
                    demoBank = demobank
                }
                BankAccountTransactionEntity.new {
                    account = bankAccount
                    creditorIban = "earns"
                    creditorBic = "BIC"
                    creditorName = "Creditor Name"
                    debtorIban = "spends"
                    debtorBic = "BIC"
                    debtorName = "Debitor Name"
                    subject = "deal"
                    amount = "EUR:1"
                    date = LocalDateTime.now().millis()
                    currency = "EUR"
                    pmtInfId = "0"
                    direction = "DBIT"
                    accountServicerReference = "test-account-servicer-reference"
                    this.demobank = demobank
                }
            }
            // The block below tests the date range in the database query
            transaction {
                addLogger(StdOutSqlLogger)
                BankAccountTransactionEntity.find {
                    BankAccountTransactionsTable.date.between(
                        0, // 1970-01-01
                        LocalDateTime.now().millis() //
                    )
                }.apply {
                    assert(this.count() == 1L)
                }
            }
        }
    }
}