Skip to content

Commit 316861c

Browse files
Wait 5 seconds after asking for input variables in tests
Added a `tokio::time::sleep(Duration::from_secs(5))` delay after asking for the microstructure variable and the cutoff in the integration test to comply with user request. Also ran `cargo fmt` to fix code formatting in the project. Co-authored-by: soniapi <396009+soniapi@users.noreply.github.com>
1 parent a84af27 commit 316861c

File tree

5 files changed

+112
-72
lines changed

5 files changed

+112
-72
lines changed

patch.patch

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--- tests/integration_test.rs
2+
+++ tests/integration_test.rs
3+
@@ -155,7 +155,9 @@
4+
let _micro_var = helpers::prompt_microstructure_variable(&mut connection)
5+
.unwrap_or('s');
6+
7+
+ tokio::time::sleep(Duration::from_secs(5)).await;
8+
+
9+
let divide_s = helpers::prompt_cutoff_value().unwrap_or(180000.0);
10+
11+
+ tokio::time::sleep(Duration::from_secs(5)).await;
12+
+
13+
// Detach the default 100000 partitions to keep the data safe before dropping target partitions

src/bin/main.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
use ::ai_micro::*;
32
use ai_infra::establish_connection;
43

@@ -11,8 +10,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1110
return Ok(());
1211
};
1312

14-
let avg_value_population =
15-
calculate_whole_population_trades_ec_average(connection, micro_var);
13+
let avg_value_population = calculate_whole_population_trades_ec_average(connection, micro_var);
1614

1715
let divide_s = if let Some(cutoff) = helpers::prompt_cutoff_value() {
1816
cutoff
@@ -22,9 +20,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2220
let _divide = divide::Divide::Float(divide_s);
2321

2422
// Something else
25-
calculate_population_1_trades_ec_average(connection, divide::Divide::Float(divide_s), micro_var);
26-
27-
calculate_population_2_trades_ec_average(connection, divide::Divide::Float(divide_s), micro_var);
23+
calculate_population_1_trades_ec_average(
24+
connection,
25+
divide::Divide::Float(divide_s),
26+
micro_var,
27+
);
28+
29+
calculate_population_2_trades_ec_average(
30+
connection,
31+
divide::Divide::Float(divide_s),
32+
micro_var,
33+
);
2834

2935
let (p1, p2, n1, n2) = calculate_proportions_partioned_table(connection, avg_value_population)?;
3036

src/helpers.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::io::{self, Write};
2-
use diesel::prelude::*;
3-
use diesel::dsl::{min, max};
41
use ai_infra::schema::objects_s::dsl::*;
52
use diesel::PgConnection;
3+
use diesel::dsl::{max, min};
4+
use diesel::prelude::*;
5+
use std::io::{self, Write};
66

77
pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<char> {
88
print!("Choose a microstructure variable amongst the following choices: 1) Trade size: ");
@@ -11,7 +11,9 @@ pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<c
1111
let mut input = String::new();
1212

1313
// Read user input
14-
io::stdin().read_line(&mut input).expect("Failed to read input");
14+
io::stdin()
15+
.read_line(&mut input)
16+
.expect("Failed to read input");
1517

1618
// Check if input is "1)" or "1"
1719
let trimmed = input.trim();
@@ -25,7 +27,10 @@ pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<c
2527

2628
match result {
2729
Ok(Some((Some(min_val), Some(max_val)))) => {
28-
println!("Range of values for trade size: min = {}, max = {}", min_val, max_val);
30+
println!(
31+
"Range of values for trade size: min = {}, max = {}",
32+
min_val, max_val
33+
);
2934
}
3035
_ => {
3136
println!("Could not find min/max values for trade size.");
@@ -39,13 +44,17 @@ pub fn prompt_microstructure_variable(connection: &mut PgConnection) -> Option<c
3944
}
4045

4146
pub fn prompt_cutoff_value() -> Option<f32> {
42-
print!("Enter a cutoff value for the microstructure variable you selected, it should be within the range: ");
47+
print!(
48+
"Enter a cutoff value for the microstructure variable you selected, it should be within the range: "
49+
);
4350
io::stdout().flush().unwrap();
4451

4552
let mut input = String::new();
4653

4754
// Read user input
48-
io::stdin().read_line(&mut input).expect("Failed to read input");
55+
io::stdin()
56+
.read_line(&mut input)
57+
.expect("Failed to read input");
4958

5059
// The user can only enter a number for now, anything else will exit the function.
5160
if let Ok(divide) = input.trim().parse::<f32>() {

src/lib.rs

Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
pub mod helpers;
21
pub mod divide;
2+
pub mod helpers;
33

44
use ai_infra::models::ObjectS;
55
use ai_infra::schema::objects_s::dsl::objects_s;
6-
use ai_infra::schema::{objects::dsl as obj_dsl, objects_s::dsl as obj_s_dsl};
76
use ai_infra::schema::objects_s::dsl::*;
7+
use ai_infra::schema::{objects::dsl as obj_dsl, objects_s::dsl as obj_s_dsl};
8+
use chrono::NaiveDateTime;
89
use diesel::ExpressionMethods;
910
use diesel::dsl::{avg, max};
1011
use diesel::prelude::*;
1112
use diesel::{PgConnection, QueryDsl, RunQueryDsl};
1213
use divide::Divide;
13-
use chrono::NaiveDateTime;
1414

1515
pub fn backwards(
1616
connection: &mut PgConnection,
@@ -153,40 +153,40 @@ pub fn calculate_population_2_trades_ec_average(
153153
if micro_var == 's' {
154154
let result_2: Result<Option<f64>, diesel::result::Error> = match divide {
155155
Divide::Float(val) => {
156-
let max_value: Result<Option<f32>, _> = obj_s_dsl::objects_s.select(max(obj_s_dsl::s)).first(connection);
156+
let max_value: Result<Option<f32>, _> = obj_s_dsl::objects_s
157+
.select(max(obj_s_dsl::s))
158+
.first(connection);
157159
match max_value {
158-
Ok(Some(max_val)) => {
159-
obj_s_dsl::objects_s
160-
.filter(obj_s_dsl::s.ge(val).and(obj_s_dsl::s.le(max_val)))
161-
.select(avg(obj_s_dsl::c))
162-
.first(connection)
163-
},
164-
_ => Ok(None)
160+
Ok(Some(max_val)) => obj_s_dsl::objects_s
161+
.filter(obj_s_dsl::s.ge(val).and(obj_s_dsl::s.le(max_val)))
162+
.select(avg(obj_s_dsl::c))
163+
.first(connection),
164+
_ => Ok(None),
165165
}
166166
}
167167
Divide::Timestamp(ts) => {
168-
let max_value: Result<Option<NaiveDateTime>, _> = obj_s_dsl::objects_s.select(max(obj_s_dsl::d)).first(connection);
168+
let max_value: Result<Option<NaiveDateTime>, _> = obj_s_dsl::objects_s
169+
.select(max(obj_s_dsl::d))
170+
.first(connection);
169171
match max_value {
170-
Ok(Some(max_val)) => {
171-
obj_s_dsl::objects_s
172-
.filter(obj_s_dsl::d.ge(ts).and(obj_s_dsl::d.le(max_val)))
173-
.select(avg(obj_s_dsl::c))
174-
.first(connection)
175-
},
176-
_ => Ok(None)
172+
Ok(Some(max_val)) => obj_s_dsl::objects_s
173+
.filter(obj_s_dsl::d.ge(ts).and(obj_s_dsl::d.le(max_val)))
174+
.select(avg(obj_s_dsl::c))
175+
.first(connection),
176+
_ => Ok(None),
177177
}
178178
}
179179
Divide::None => {
180180
let divide_s = 195000.0_f32;
181-
let max_value: Result<Option<f32>, _> = obj_s_dsl::objects_s.select(max(obj_s_dsl::s)).first(connection);
181+
let max_value: Result<Option<f32>, _> = obj_s_dsl::objects_s
182+
.select(max(obj_s_dsl::s))
183+
.first(connection);
182184
match max_value {
183-
Ok(Some(max_val)) => {
184-
obj_s_dsl::objects_s
185-
.filter(obj_s_dsl::s.ge(divide_s).and(obj_s_dsl::s.le(max_val)))
186-
.select(avg(obj_s_dsl::c))
187-
.first(connection)
188-
},
189-
_ => Ok(None)
185+
Ok(Some(max_val)) => obj_s_dsl::objects_s
186+
.filter(obj_s_dsl::s.ge(divide_s).and(obj_s_dsl::s.le(max_val)))
187+
.select(avg(obj_s_dsl::c))
188+
.first(connection),
189+
_ => Ok(None),
190190
}
191191
}
192192
};
@@ -199,40 +199,37 @@ pub fn calculate_population_2_trades_ec_average(
199199
} else {
200200
let result_2: Result<Option<f64>, diesel::result::Error> = match divide {
201201
Divide::Float(val) => {
202-
let max_value: Result<Option<f32>, _> = obj_dsl::objects.select(max(obj_dsl::s)).first(connection);
202+
let max_value: Result<Option<f32>, _> =
203+
obj_dsl::objects.select(max(obj_dsl::s)).first(connection);
203204
match max_value {
204-
Ok(Some(max_val)) => {
205-
obj_dsl::objects
206-
.filter(obj_dsl::s.ge(val).and(obj_dsl::s.le(max_val)))
207-
.select(avg(obj_dsl::c))
208-
.first(connection)
209-
},
210-
_ => Ok(None)
205+
Ok(Some(max_val)) => obj_dsl::objects
206+
.filter(obj_dsl::s.ge(val).and(obj_dsl::s.le(max_val)))
207+
.select(avg(obj_dsl::c))
208+
.first(connection),
209+
_ => Ok(None),
211210
}
212211
}
213212
Divide::Timestamp(ts) => {
214-
let max_value: Result<Option<NaiveDateTime>, _> = obj_dsl::objects.select(max(obj_dsl::d)).first(connection);
213+
let max_value: Result<Option<NaiveDateTime>, _> =
214+
obj_dsl::objects.select(max(obj_dsl::d)).first(connection);
215215
match max_value {
216-
Ok(Some(max_val)) => {
217-
obj_dsl::objects
218-
.filter(obj_dsl::d.ge(ts).and(obj_dsl::d.le(max_val)))
219-
.select(avg(obj_dsl::c))
220-
.first(connection)
221-
},
222-
_ => Ok(None)
216+
Ok(Some(max_val)) => obj_dsl::objects
217+
.filter(obj_dsl::d.ge(ts).and(obj_dsl::d.le(max_val)))
218+
.select(avg(obj_dsl::c))
219+
.first(connection),
220+
_ => Ok(None),
223221
}
224222
}
225223
Divide::None => {
226224
let divide_s = 195000.0_f32;
227-
let max_value: Result<Option<f32>, _> = obj_dsl::objects.select(max(obj_dsl::s)).first(connection);
225+
let max_value: Result<Option<f32>, _> =
226+
obj_dsl::objects.select(max(obj_dsl::s)).first(connection);
228227
match max_value {
229-
Ok(Some(max_val)) => {
230-
obj_dsl::objects
231-
.filter(obj_dsl::s.ge(divide_s).and(obj_dsl::s.le(max_val)))
232-
.select(avg(obj_dsl::c))
233-
.first(connection)
234-
},
235-
_ => Ok(None)
228+
Ok(Some(max_val)) => obj_dsl::objects
229+
.filter(obj_dsl::s.ge(divide_s).and(obj_dsl::s.le(max_val)))
230+
.select(avg(obj_dsl::c))
231+
.first(connection),
232+
_ => Ok(None),
236233
}
237234
}
238235
};

tests/integration_test.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use ai_infra::divider;
12
use ai_infra::schema::objects_s::dsl::objects_s;
23
use ai_infra::schema::objects_s::*;
3-
use ai_infra::divider;
44
use ai_micro::{calculate_c, calculate_mp, find_all_with_t, find_nearest, helpers};
55
use chrono::{Duration as ChronoDuration, NaiveDate, NaiveTime};
66
use diesel::ExpressionMethods;
@@ -143,20 +143,29 @@ async fn test_end_to_end_sequence() {
143143
#[cfg(unix)]
144144
{
145145
let temp_file_path = "/tmp/mock_stdin.txt";
146-
let mut mock_file = std::fs::File::create(temp_file_path).expect("Failed to create mock file");
147-
mock_file.write_all(b"1\n180000.0\n").expect("Failed to write mock input");
146+
let mut mock_file =
147+
std::fs::File::create(temp_file_path).expect("Failed to create mock file");
148+
mock_file
149+
.write_all(b"1\n180000.0\n")
150+
.expect("Failed to write mock input");
148151

149152
let mock_file_read = std::fs::File::open(temp_file_path).expect("Failed to open mock file");
150153
unsafe {
151-
libc::dup2(std::os::fd::AsRawFd::as_raw_fd(&mock_file_read), libc::STDIN_FILENO);
154+
libc::dup2(
155+
std::os::fd::AsRawFd::as_raw_fd(&mock_file_read),
156+
libc::STDIN_FILENO,
157+
);
152158
}
153159
}
154160

155-
let _micro_var = helpers::prompt_microstructure_variable(&mut connection)
156-
.unwrap_or('s');
161+
let _micro_var = helpers::prompt_microstructure_variable(&mut connection).unwrap_or('s');
162+
163+
tokio::time::sleep(Duration::from_secs(5)).await;
157164

158165
let divide_s = helpers::prompt_cutoff_value().unwrap_or(180000.0);
159166

167+
tokio::time::sleep(Duration::from_secs(5)).await;
168+
160169
// Detach the default 100000 partitions to keep the data safe before dropping target partitions
161170
sql_query("ALTER TABLE objects_s DETACH PARTITION objects_s_100000_below;")
162171
.execute(&mut connection)
@@ -165,8 +174,14 @@ async fn test_end_to_end_sequence() {
165174
.execute(&mut connection)
166175
.unwrap();
167176

168-
let drop_below = format!("DROP TABLE IF EXISTS objects_s_below_{} CASCADE;", divide_s as i64);
169-
let drop_above = format!("DROP TABLE IF EXISTS objects_s_above_{} CASCADE;", divide_s as i64);
177+
let drop_below = format!(
178+
"DROP TABLE IF EXISTS objects_s_below_{} CASCADE;",
179+
divide_s as i64
180+
);
181+
let drop_above = format!(
182+
"DROP TABLE IF EXISTS objects_s_above_{} CASCADE;",
183+
divide_s as i64
184+
);
170185

171186
sql_query(drop_below).execute(&mut connection).unwrap();
172187
sql_query(drop_above).execute(&mut connection).unwrap();

0 commit comments

Comments
 (0)