|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved. |
| 3 | +// https://nautechsystems.io |
| 4 | +// |
| 5 | +// Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); |
| 6 | +// You may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// ------------------------------------------------------------------------------------------------- |
| 15 | + |
| 16 | + |
| 17 | +use tokio::sync::Mutex; |
| 18 | +use sqlx::PgPool; |
| 19 | +use nautilus_infrastructure::sql::cache_database::PostgresCacheDatabase; |
| 20 | +use nautilus_infrastructure::sql::pg::{connect_pg, delete_nautilus_postgres_tables, drop_postgres, init_postgres, PostgresConnectOptions}; |
| 21 | + |
| 22 | +static INITlIZED: Mutex<bool> = Mutex::const_new(false); |
| 23 | + |
| 24 | + |
| 25 | +pub fn get_test_pg_connect_options() -> PostgresConnectOptions { |
| 26 | + PostgresConnectOptions::new( |
| 27 | + "localhost".to_string(), |
| 28 | + 5432, |
| 29 | + "nautilus".to_string(), |
| 30 | + "pass".to_string(), |
| 31 | + "nautilus".to_string(), |
| 32 | + ) |
| 33 | +} |
| 34 | +pub async fn get_pg() -> PgPool { |
| 35 | + let pg_connect_options = get_test_pg_connect_options(); |
| 36 | + connect_pg(pg_connect_options.into()).await.unwrap() |
| 37 | +} |
| 38 | + |
| 39 | +pub async fn initialize() -> anyhow::Result<()>{ |
| 40 | + let pg_pool = get_pg().await; |
| 41 | + let mut initialized = INITlIZED.lock().await; |
| 42 | + // 1. check if we need to init schema |
| 43 | + if !*initialized { |
| 44 | + // drop and init postgres commands dont throw, they just log |
| 45 | + // se we can use them here in init login in this order |
| 46 | + drop_postgres(&pg_pool, "nautilus".to_string()).await.unwrap(); |
| 47 | + init_postgres(&pg_pool, "nautilus".to_string(), "pass".to_string()).await.unwrap(); |
| 48 | + *initialized = true; |
| 49 | + } |
| 50 | + // truncate all table |
| 51 | + println!("deleting all tables"); |
| 52 | + delete_nautilus_postgres_tables(&pg_pool).await.unwrap(); |
| 53 | + Ok(()) |
| 54 | +} |
| 55 | + |
| 56 | +pub async fn get_pg_cache_database() -> anyhow::Result<PostgresCacheDatabase> { |
| 57 | + initialize().await.unwrap(); |
| 58 | + let connect_options = get_test_pg_connect_options(); |
| 59 | + Ok( |
| 60 | + PostgresCacheDatabase::connect( |
| 61 | + Some(connect_options.host), |
| 62 | + Some(connect_options.port), |
| 63 | + Some(connect_options.username), |
| 64 | + Some(connect_options.password), |
| 65 | + Some(connect_options.database), |
| 66 | + ) |
| 67 | + .await.unwrap() |
| 68 | + ) |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +mod tests{ |
| 73 | + use std::time::Duration; |
| 74 | + use crate::get_pg_cache_database; |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + /// ----------------------------------- General ----------------------------------- |
| 79 | + #[tokio::test] |
| 80 | + async fn test_load_general_objects_when_nothing_in_cache_returns_empty_hashmap(){ |
| 81 | + let pg_cache = get_pg_cache_database().await.unwrap(); |
| 82 | + let result = pg_cache.load().await.unwrap(); |
| 83 | + println!("1: {:?}",result); |
| 84 | + assert_eq!(result.len(), 0); |
| 85 | + } |
| 86 | + |
| 87 | + #[tokio::test] |
| 88 | + async fn test_add_general_object_adds_to_cache(){ |
| 89 | + let pg_cache = get_pg_cache_database().await.unwrap(); |
| 90 | + let test_id_value = String::from("test_value").into_bytes(); |
| 91 | + pg_cache.add(String::from("test_id"),test_id_value.clone()).await.unwrap(); |
| 92 | + // sleep with tokio |
| 93 | + tokio::time::sleep(Duration::from_secs(1)).await; |
| 94 | + let result = pg_cache.load().await.unwrap(); |
| 95 | + println!("2: {:?}",result); |
| 96 | + assert_eq!(result.keys().len(), 1); |
| 97 | + assert_eq!(result.keys().cloned().collect::<Vec<String>>(), vec![String::from("test_id")]); // assert_eq!(result.get(&test_id_key).unwrap().to_owned(),&test_id_value.clone()); |
| 98 | + assert_eq!(result.get("test_id").unwrap().to_owned(), test_id_value); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments