-
Notifications
You must be signed in to change notification settings - Fork 642
Simplify and optimize Categories::toplevel
#594
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
Merged
carols10cents
merged 1 commit into
rust-lang:master
from
sgrif:sg-simplify-categoires-toplevel
Mar 8, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ opt-level = 2 | |
|
||
[lib] | ||
name = "cargo_registry" | ||
test = false | ||
doctest = false | ||
|
||
[[test]] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,16 +157,13 @@ impl Category { | |
// Collect all the top-level categories and sum up the crates_cnt of | ||
// the crates in all subcategories | ||
let stmt = conn.prepare(&format!( | ||
"SELECT c.id, c.category, c.slug, c.description, c.created_at, \ | ||
COALESCE (( \ | ||
SELECT sum(c2.crates_cnt)::int \ | ||
FROM categories as c2 \ | ||
WHERE c2.slug = c.slug \ | ||
OR c2.slug LIKE c.slug || '::%' \ | ||
), 0) as crates_cnt \ | ||
FROM categories as c \ | ||
WHERE c.category NOT LIKE '%::%' {} \ | ||
LIMIT $1 OFFSET $2", | ||
"SELECT c.id, c.category, c.slug, c.description, c.created_at, | ||
sum(c2.crates_cnt)::int as crates_cnt | ||
FROM categories as c | ||
INNER JOIN categories c2 ON split_part(c2.slug, '::', 1) = c.slug | ||
WHERE split_part(c.slug, '::', 1) = c.slug | ||
GROUP BY c.id | ||
{} LIMIT $1 OFFSET $2", | ||
sort_sql | ||
))?; | ||
|
||
|
@@ -278,3 +275,110 @@ pub fn slugs(req: &mut Request) -> CargoResult<Response> { | |
struct R { category_slugs: Vec<Slug> } | ||
Ok(req.json(&R { category_slugs: slugs })) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
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. Yay more tests!!! 😻 😻 😻 😻 |
||
use super::*; | ||
use pg::{Connection, TlsMode}; | ||
use dotenv::dotenv; | ||
use std::env; | ||
|
||
fn pg_connection() -> Connection { | ||
let _ = dotenv(); | ||
let database_url = env::var("TEST_DATABASE_URL") | ||
.expect("TEST_DATABASE_URL must be set to run tests"); | ||
let conn = Connection::connect(database_url, TlsMode::None).unwrap(); | ||
// These tests deadlock if run concurrently | ||
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. 💩 |
||
conn.batch_execute("BEGIN; LOCK categories IN ACCESS EXCLUSIVE MODE").unwrap(); | ||
conn | ||
} | ||
|
||
#[test] | ||
fn category_toplevel_excludes_subcategories() { | ||
let conn = pg_connection(); | ||
conn.batch_execute("INSERT INTO categories (category, slug) VALUES | ||
('Cat 2', 'cat2'), ('Cat 1', 'cat1'), ('Cat 1::sub', 'cat1::sub') | ||
").unwrap(); | ||
|
||
let categories = Category::toplevel(&conn, "", 10, 0).unwrap() | ||
.into_iter().map(|c| c.category).collect::<Vec<_>>(); | ||
let expected = vec!["Cat 1".to_string(), "Cat 2".to_string()]; | ||
assert_eq!(expected, categories); | ||
} | ||
|
||
#[test] | ||
fn category_toplevel_orders_by_crates_cnt_when_sort_given() { | ||
let conn = pg_connection(); | ||
conn.batch_execute("INSERT INTO categories (category, slug, crates_cnt) VALUES | ||
('Cat 1', 'cat1', 0), ('Cat 2', 'cat2', 2), ('Cat 3', 'cat3', 1) | ||
").unwrap(); | ||
|
||
let categories = Category::toplevel(&conn, "crates", 10, 0).unwrap() | ||
.into_iter().map(|c| c.category).collect::<Vec<_>>(); | ||
let expected = vec!["Cat 2".to_string(), "Cat 3".to_string(), "Cat 1".to_string()]; | ||
assert_eq!(expected, categories); | ||
} | ||
|
||
#[test] | ||
fn category_toplevel_applies_limit_and_offset() { | ||
let conn = pg_connection(); | ||
conn.batch_execute("INSERT INTO categories (category, slug) VALUES | ||
('Cat 1', 'cat1'), ('Cat 2', 'cat2') | ||
").unwrap(); | ||
|
||
let categories = Category::toplevel(&conn, "", 1, 0).unwrap() | ||
.into_iter().map(|c| c.category).collect::<Vec<_>>(); | ||
let expected = vec!["Cat 1".to_string()]; | ||
assert_eq!(expected, categories); | ||
|
||
let categories = Category::toplevel(&conn, "", 1, 1).unwrap() | ||
.into_iter().map(|c| c.category).collect::<Vec<_>>(); | ||
let expected = vec!["Cat 2".to_string()]; | ||
assert_eq!(expected, categories); | ||
} | ||
|
||
#[test] | ||
fn category_toplevel_includes_subcategories_in_crate_cnt() { | ||
let conn = pg_connection(); | ||
conn.batch_execute("INSERT INTO categories (category, slug, crates_cnt) VALUES | ||
('Cat 1', 'cat1', 1), ('Cat 1::sub', 'cat1::sub', 2), | ||
('Cat 2', 'cat2', 3), ('Cat 2::Sub 1', 'cat2::sub1', 4), | ||
('Cat 2::Sub 2', 'cat2::sub2', 5), ('Cat 3', 'cat3', 6) | ||
").unwrap(); | ||
|
||
let categories = Category::toplevel(&conn, "crates", 10, 0).unwrap() | ||
.into_iter().map(|c| (c.category, c.crates_cnt)).collect::<Vec<_>>(); | ||
let expected = vec![ | ||
("Cat 2".to_string(), 12), | ||
("Cat 3".to_string(), 6), | ||
("Cat 1".to_string(), 3), | ||
]; | ||
assert_eq!(expected, categories); | ||
} | ||
|
||
#[test] | ||
fn category_toplevel_applies_limit_and_offset_after_grouping() { | ||
let conn = pg_connection(); | ||
conn.batch_execute("INSERT INTO categories (category, slug, crates_cnt) VALUES | ||
('Cat 1', 'cat1', 1), ('Cat 1::sub', 'cat1::sub', 2), | ||
('Cat 2', 'cat2', 3), ('Cat 2::Sub 1', 'cat2::sub1', 4), | ||
('Cat 2::Sub 2', 'cat2::sub2', 5), ('Cat 3', 'cat3', 6) | ||
").unwrap(); | ||
|
||
let categories = Category::toplevel(&conn, "crates", 2, 0).unwrap() | ||
.into_iter().map(|c| (c.category, c.crates_cnt)).collect::<Vec<_>>(); | ||
let expected = vec![ | ||
("Cat 2".to_string(), 12), | ||
("Cat 3".to_string(), 6), | ||
]; | ||
assert_eq!(expected, categories); | ||
|
||
let categories = Category::toplevel(&conn, "crates", 2, 1).unwrap() | ||
.into_iter().map(|c| (c.category, c.crates_cnt)).collect::<Vec<_>>(); | ||
let expected = vec![ | ||
("Cat 3".to_string(), 6), | ||
("Cat 1".to_string(), 3), | ||
]; | ||
assert_eq!(expected, categories); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Cool!!