I was able to verify the post_code_info view by comparing queries run on both the street_cafes table, and the post code info view and then comparing the return values.
SQL Below
CREATE VIEW post_code_info
AS
WITH summary AS (
SELECT
c.post_code,
c.number_of_chairs,
c.name,
ROW_NUMBER() OVER(PARTITION BY c.post_code ORDER BY c.number_of_chairs DESC) as ck
FROM street_cafes c)
SELECT s.post_code, agg_tbl.total_places, agg_tbl.total_chairs, agg_tbl.chairs_pct, s.name as place_with_max_chairs, s.number_of_chairs as max_chairs
FROM summary s
INNER JOIN (SELECT post_code,
SUM(number_of_chairs) as total_chairs,
CAST((CAST(sum(street_cafes.number_of_chairs) as Float) / CAST((SELECT SUM(number_of_chairs) FROM street_cafes) as Float) * 100) as DECIMAL(10,2)) as chairs_pct,
count(street_cafes.post_code) as total_places
FROM street_cafes GROUP BY post_code) agg_tbl ON (agg_tbl.post_code = s.post_code)
WHERE s.ck = 1;
SQL Below
CREATE VIEW categories_info
AS
SELECT street_cafes.category AS category,
Count(street_cafes.post_code) AS total_places,
Sum(street_cafes.number_of_chairs) AS total_chairs
FROM street_cafes
GROUP BY street_cafes.category;
Each script is written and executed within a rake task. They are tested at Unit and Integration levels The methods used in each script are located in the StreetCafe Model
Unit Testing
spec/models/street_cafe_spec.rb
Integration Testing
spec/tasks/rake_tasks_spec.rb
Rake Task Name
import:street_cafe_csv
Methods Used
with_post_code_prefix()
categorize_LS1_cafes
categorize_LS2_cafes
categorize_post_code_outliers
Rake Task Name
categorize:street_cafes
Methods Used
return_by_size()
write_to_csv()
Rake Task Name
export_and_delete:small_street_cafes
Concatenating the Category to the Beginning of the Name on all Street Cafes Categorized as Medium or Large
Methods Used
return_by_size
concat_category_and_name
Rake Task Name
concatenate:med_and_large_cafe_names
-
Clone the Repository
-
Run
bundle
for gems and dependencies -
Run
rails g rspec:install
for the RSpec test suite -
Run
rails db:create
thenrails db:migrate
-
At this point run
rake import:street_cafe_csv
to populate the database with the street cafe csv data -
The post_code_info view can now be accessed and queried via ``psql ps-code-challenge_development```
-
Run
categorize:street_cafes
to categorize street cafes based on post_code prefix and number of chairs -
The categories_info view can now be accessed and queried via
psql ps-code-challenge_development
-
Run
export_and_delete:small_street_cafes
to export the street cafes categorized as 'small' to a csv file, and then delete the records. The csv will generate within thecsv_export_files
folder. The full path will becsv_export_files/small_street_cafes
Additionally, you can pass in an optional argument when running this task, this argument allows the exported csv file to be renamed, as long as it is followed by
.csv
and is wrapped in single quotes. EX:rake 'export_and_delete:small_street_cafes['new_file.csv']'
- Run
concatenate:med_and_large_cafe_names
to concatenate the category to the beginning of the name on all street cafes categorized as medium or large
The testing suite can be run in its entirety by running rspec
For individual test files you can run rspec spec/<FILE_PATH>