Skip to content

Commit d00caf7

Browse files
authored
Remove unnecessary panic for unreachable code (#26)
1 parent 10208aa commit d00caf7

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed

src/connection/mod.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,19 +500,24 @@ impl Connection {
500500
Err(err)
501501
}
502502
},
503-
false => match &mut self.results_iter {
504-
Some(it) => match it.next() {
505-
Some(x) => Ok(Some(x)),
506-
None => {
507-
self.status = ConnectionStatus::Ready;
508-
Ok(None)
509-
}
510-
},
511-
None => panic!(),
503+
false => match self.next_record() {
504+
Some(x) => Ok(Some(x)),
505+
None => {
506+
self.status = ConnectionStatus::Ready;
507+
Ok(None)
508+
}
512509
},
513510
}
514511
}
515512

513+
fn next_record(&mut self) -> Option<Record> {
514+
if let Some(iter) = self.results_iter.as_mut() {
515+
iter.next()
516+
} else {
517+
None
518+
}
519+
}
520+
516521
/// Returns next rows of query results.
517522
///
518523
/// The number of rows to fetch is specified either by `size` or

src/connection/tests.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ fn from_connect_fetchone_address() {
184184

185185
#[test]
186186
#[serial]
187-
#[should_panic(expected = "explicit panic")]
188-
fn from_connect_fetchone_explicit_panic() {
187+
fn from_connect_fetchone_no_data() {
189188
initialize();
190189
execute_query(String::from(
191190
"CREATE (u:User {name: 'Alice'})-[:Likes]->(m:Software {name: 'Memgraph'})",
@@ -200,39 +199,19 @@ fn from_connect_fetchone_explicit_panic() {
200199
..Default::default()
201200
};
202201
let mut connection = get_connection(connect_prms);
203-
let params = get_params("name".to_string(), "Alice".to_string());
202+
let params = get_params("name".to_string(), "Something".to_string());
204203

205204
let query = String::from("MATCH (n:User) WHERE n.name = $name RETURN n LIMIT 5");
206205
match connection.execute(&query, Some(&params)) {
207206
Ok(x) => x,
208207
Err(err) => panic!("Query failed: {}", err),
209208
};
210-
connection.results_iter = None;
211-
loop {
212-
match connection.fetchone() {
213-
Ok(res) => match res {
214-
Some(x) => {
215-
for val in &x.values {
216-
let values = vec![String::from("User")];
217-
let mg_map = hashmap! {
218-
String::from("name") => Value::String("Alice".to_string()),
219-
};
220-
let node = Value::Node(Node {
221-
id: match val {
222-
Value::Node(x) => x.id,
223-
_ => 1,
224-
},
225-
label_count: 1,
226-
labels: values,
227-
properties: mg_map,
228-
});
229-
assert_eq!(&node, val);
230-
}
231-
}
232-
None => break,
233-
},
234-
Err(err) => panic!("Fetch failed: {}", err),
235-
}
209+
210+
let first = connection.fetchone();
211+
if let Ok(rec) = first {
212+
assert!(rec.is_none());
213+
} else {
214+
panic!("First fetched record should be None")
236215
}
237216
}
238217

0 commit comments

Comments
 (0)