|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | #
|
3 | 3 | # Copyright (C) 2021 TU Wien.
|
| 4 | +# Copyright (C) 2024 KTH Royal Institute of Technology. |
4 | 5 | #
|
5 | 6 | # Invenio App RDM is free software; you can redistribute it and/or modify it
|
6 | 7 | # under the terms of the MIT License; see LICENSE file for more details.
|
|
9 | 10 |
|
10 | 11 | from datetime import datetime
|
11 | 12 |
|
| 13 | +import pytest |
| 14 | + |
12 | 15 | from invenio_app_rdm.records_ui.utils import set_default_value
|
| 16 | +from invenio_app_rdm.utils.utils import build_broker_url, build_db_uri, build_redis_url |
13 | 17 |
|
14 | 18 |
|
15 | 19 | def test_set_default_value__value():
|
@@ -65,3 +69,123 @@ def test_set_default_value__explicit_and_automatic_prefix():
|
65 | 69 | dict1["metadata"]["publication_date"] == dict2["metadata"]["publication_date"]
|
66 | 70 | )
|
67 | 71 | assert dict1["metadata"]["publication_date"] == value
|
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize( |
| 75 | + "env_vars, expected_uri", |
| 76 | + [ |
| 77 | + ( |
| 78 | + { |
| 79 | + "DB_USER": "testuser", |
| 80 | + "DB_PASSWORD": "testpassword", |
| 81 | + "DB_HOST": "testhost", |
| 82 | + "DB_PORT": "5432", |
| 83 | + "DB_NAME": "testdb", |
| 84 | + }, |
| 85 | + "postgresql+psycopg2://testuser:testpassword@testhost:5432/testdb", |
| 86 | + ), |
| 87 | + ( |
| 88 | + {"SQLALCHEMY_DATABASE_URI": "sqlite:///test.db"}, |
| 89 | + "sqlite:///test.db", |
| 90 | + ), |
| 91 | + ( |
| 92 | + {}, |
| 93 | + "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm", |
| 94 | + ), |
| 95 | + ], |
| 96 | +) |
| 97 | +def test_build_db_uri(monkeypatch, env_vars, expected_uri): |
| 98 | + """Test building database URI.""" |
| 99 | + for key in [ |
| 100 | + "DB_USER", |
| 101 | + "DB_PASSWORD", |
| 102 | + "DB_HOST", |
| 103 | + "DB_PORT", |
| 104 | + "DB_NAME", |
| 105 | + "SQLALCHEMY_DATABASE_URI", |
| 106 | + ]: |
| 107 | + monkeypatch.delenv(key, raising=False) |
| 108 | + for key, value in env_vars.items(): |
| 109 | + monkeypatch.setenv(key, value) |
| 110 | + |
| 111 | + assert build_db_uri() == expected_uri |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.parametrize( |
| 115 | + "env_vars, expected_url", |
| 116 | + [ |
| 117 | + ( |
| 118 | + { |
| 119 | + "BROKER_USER": "testuser", |
| 120 | + "BROKER_PASSWORD": "testpassword", |
| 121 | + "BROKER_HOST": "testhost", |
| 122 | + "BROKER_PORT": "5672", |
| 123 | + }, |
| 124 | + "amqp://testuser:testpassword@testhost:5672/", |
| 125 | + ), |
| 126 | + ( |
| 127 | + {"BROKER_URL": "amqp://guest:guest@localhost:5672/"}, |
| 128 | + "amqp://guest:guest@localhost:5672/", |
| 129 | + ), |
| 130 | + ( |
| 131 | + {}, |
| 132 | + "amqp://guest:guest@localhost:5672/", |
| 133 | + ), |
| 134 | + ], |
| 135 | +) |
| 136 | +def test_build_broker_url(monkeypatch, env_vars, expected_url): |
| 137 | + """Test building broker URL.""" |
| 138 | + for key in [ |
| 139 | + "BROKER_USER", |
| 140 | + "BROKER_PASSWORD", |
| 141 | + "BROKER_HOST", |
| 142 | + "BROKER_PORT", |
| 143 | + "BROKER_URL", |
| 144 | + ]: |
| 145 | + monkeypatch.delenv(key, raising=False) |
| 146 | + for key, value in env_vars.items(): |
| 147 | + monkeypatch.setenv(key, value) |
| 148 | + |
| 149 | + assert build_broker_url() == expected_url |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.parametrize( |
| 153 | + "env_vars, db, expected_url", |
| 154 | + [ |
| 155 | + ( |
| 156 | + { |
| 157 | + "REDIS_HOST": "testhost", |
| 158 | + "REDIS_PORT": "6379", |
| 159 | + "REDIS_PASSWORD": "testpassword", |
| 160 | + }, |
| 161 | + 2, |
| 162 | + "redis://:testpassword@testhost:6379/2", |
| 163 | + ), |
| 164 | + ( |
| 165 | + { |
| 166 | + "REDIS_HOST": "testhost", |
| 167 | + "REDIS_PORT": "6379", |
| 168 | + }, |
| 169 | + 1, |
| 170 | + "redis://testhost:6379/1", |
| 171 | + ), |
| 172 | + ( |
| 173 | + {"BROKER_URL": "redis://localhost:6379/0"}, |
| 174 | + None, |
| 175 | + "redis://localhost:6379/0", |
| 176 | + ), |
| 177 | + ( |
| 178 | + {}, |
| 179 | + 4, |
| 180 | + "redis://localhost:6379/4", |
| 181 | + ), |
| 182 | + ], |
| 183 | +) |
| 184 | +def test_build_redis_url(monkeypatch, env_vars, db, expected_url): |
| 185 | + """Test building Redis URL.""" |
| 186 | + for key in ["REDIS_HOST", "REDIS_PORT", "REDIS_PASSWORD", "BROKER_URL"]: |
| 187 | + monkeypatch.delenv(key, raising=False) |
| 188 | + for key, value in env_vars.items(): |
| 189 | + monkeypatch.setenv(key, value) |
| 190 | + |
| 191 | + assert build_redis_url(db=db) == expected_url |
0 commit comments