-
Notifications
You must be signed in to change notification settings - Fork 1
[TO CLOSE] Add pagination parameter for GET sentences #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#![feature(plugin)] | ||
#![feature(plugin, custom_derive)] | ||
#![plugin(rocket_codegen)] | ||
|
||
extern crate rocket; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,10 @@ extern crate uuid; | |
extern crate xml; | ||
|
||
use rocket::Response; | ||
use rocket_contrib::Json; | ||
use rocket_contrib::{ | ||
Json, | ||
UUID, | ||
}; | ||
use rocket::http::{ | ||
Status, | ||
ContentType, | ||
|
@@ -13,6 +16,7 @@ use postgres::error::{ | |
}; | ||
use self::xml::reader::EventReader; | ||
use self::xml::reader::XmlEvent::{Characters, Whitespace}; | ||
use self::uuid::Uuid; | ||
|
||
use std::io::Cursor; | ||
|
||
|
@@ -26,6 +30,11 @@ pub struct Sentence { | |
pub structure: Option<String>, | ||
} | ||
|
||
#[derive(FromForm)] | ||
struct LastId { | ||
pub id: UUID, | ||
} | ||
|
||
#[post("/sentences", format="application/json", data="<sentence>")] | ||
fn create_sentence<'r>( | ||
connection: db::DbConnection, | ||
|
@@ -113,12 +122,14 @@ fn create_sentence<'r>( | |
.finalize() | ||
} | ||
|
||
|
||
#[get("/sentences")] | ||
#[get("/sentences?<last_id>")] | ||
fn get_all_sentences<'r>( | ||
connection: db::DbConnection, | ||
last_id: LastId, | ||
) -> Response<'r> { | ||
|
||
let real_uuid : Uuid = *last_id.id; | ||
|
||
let result = connection.query( | ||
r#" | ||
SELECT | ||
|
@@ -128,19 +139,19 @@ fn get_all_sentences<'r>( | |
structure::text | ||
FROM sentence | ||
JOIN language ON (sentence.language_id = language.id) | ||
WHERE sentence.id > $1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. your ids are not sequential , so if a new id get inserted you will certainly miss it. |
||
ORDER BY | ||
added_at, | ||
sentence.id | ||
LIMIT 100 | ||
LIMIT 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why changing the limit ? |
||
"#, | ||
&[], | ||
&[&real_uuid], | ||
); | ||
|
||
let rows = result.expect("problem while getting sentence"); | ||
|
||
let mut sentences : Vec<Sentence> = Vec::with_capacity(100); | ||
|
||
|
||
for row in rows.iter() { | ||
let sentence = Sentence { | ||
id: row.get(0), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,12 @@ extern crate uuid; | |
|
||
#[macro_use] extern crate serde_derive; | ||
|
||
use reqwest::StatusCode; | ||
use reqwest::{ | ||
StatusCode, | ||
Url, | ||
}; | ||
|
||
use std::str::FromStr; | ||
|
||
mod db; | ||
|
||
|
@@ -51,7 +56,12 @@ fn test_get_all_sentences_returns_200() { | |
tests_commons::SERVICE_URL, | ||
); | ||
|
||
let mut response = reqwest::get(&url).unwrap(); | ||
let url = Url::parse_with_params( | ||
&url, | ||
&[("id", "00000000-0000-0000-0000-000000000000")], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not a valid uuid per-rfc so it should not work actually... |
||
).unwrap(); | ||
|
||
let mut response = reqwest::get(url).unwrap(); | ||
|
||
assert_eq!( | ||
response.status(), | ||
|
@@ -65,3 +75,88 @@ fn test_get_all_sentences_returns_200() { | |
2, | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_get_paginated_sentences() { | ||
|
||
let connection = db::get_connection(); | ||
db::clear(&connection); | ||
|
||
let english_iso639_3 = "eng"; | ||
db::insert_language( | ||
&connection, | ||
&english_iso639_3, | ||
); | ||
|
||
let base_url = format!( | ||
"{}/sentences", | ||
tests_commons::SERVICE_URL, | ||
); | ||
|
||
/* insert multiple sentences with different | ||
content and "consecutives" uuids, | ||
from a full-zeros uuid to x-x-x-x-...13 */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. something is wrong if you need to make consecutive uuid (see my comment above) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. something is wrong if you need to make consecutive uuid (see my comment above) , i.e you're tricking the reality to please your test though you should do the oppositve (i.e in reality multiple sentences add in order A B C don't have any garantee to have uuid in order A B C ) |
||
|
||
let uuid_common = "00000000-0000-0000-0000-0000000000"; | ||
|
||
const SENTENCE_MAX_INDEX: usize = 15; | ||
for id in 1..SENTENCE_MAX_INDEX { | ||
|
||
/* ensure the uuids strings are all valid uuids */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is the code below ensuring that ? |
||
|
||
const UUIDS_PER_SET: usize = 10; | ||
let uuid = if id >= UUIDS_PER_SET { | ||
format!("{}{}0", uuid_common, id - UUIDS_PER_SET) | ||
} else { | ||
format!("{}0{}", uuid_common, id) | ||
}; | ||
|
||
db::insert_sentence( | ||
&connection, | ||
&uuid::Uuid::from_str(&uuid).unwrap(), | ||
&format!("Sentence {}", id), | ||
&english_iso639_3, | ||
); | ||
} | ||
|
||
let url = Url::parse_with_params( | ||
&base_url, | ||
&[("id", "00000000-0000-0000-0000-000000000000")], | ||
).unwrap(); | ||
|
||
let mut response = reqwest::get(url).unwrap(); | ||
|
||
assert_eq!( | ||
response.status(), | ||
StatusCode::Ok, | ||
); | ||
|
||
let sentences = response.json::<tests_commons::Sentences>().unwrap(); | ||
|
||
assert_eq!( | ||
sentences.len(), | ||
10, | ||
); | ||
|
||
let last_sentence = sentences.last().unwrap(); | ||
let last_id = last_sentence.id.unwrap().to_string(); | ||
|
||
let url = Url::parse_with_params( | ||
&base_url, | ||
&[("id", last_id)], | ||
).unwrap(); | ||
|
||
let mut response = reqwest::get(url).unwrap(); | ||
|
||
assert_eq!( | ||
response.status(), | ||
StatusCode::Ok, | ||
); | ||
|
||
let sentences = response.json::<tests_commons::Sentences>().unwrap(); | ||
|
||
assert_eq!( | ||
sentences.len(), | ||
3, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 80 lines for a test, with a lot of mundane (i.e low non-functional details ) happening , makes the test very obscure to understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't hesitate to create helper functions to outsource low details outside of the "reading" path. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, will be done in a separated dedicated PR #62. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need this because we can't implement a trait (FromForm) on a type ( UUID) that don't belong to us ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if so, it should be an issue raised to the rocket_contrib/uuid crate , as this crate is specifically made to ease interaction with uuid in rocket, so it should do that for us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna do a second check on this point.