From 76113570578b0e76553c3ac9f35a0bf459355fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Wed, 27 Nov 2019 10:36:09 +0100 Subject: [PATCH] test fixes --- Gemfile.lock | 2 +- .../navigation_map_settings_form/show.erb | 2 -- .../decidim/navigation_maps/save_area.rb | 2 +- .../navigation_maps/admin/areas_controller.rb | 2 +- config/locales/en.yml | 28 +++++++++--------- ...decidim_navigation_maps_blueprint_areas.rb | 22 -------------- ...decidim_navigation_maps_blueprint_areas.rb | 2 ++ lib/decidim/navigation_maps/test/factories.rb | 11 +++++-- spec/commands/save_area_spec.rb | 20 ++++++++----- .../{decidim => }/area_controller_spec.rb | 9 +++--- .../blueprint_controller_spec.rb | 4 +-- spec/model/blueprint_area_spec.rb | 9 ++++-- spec/model/blueprint_spec.rb | 29 +++++++++++++------ 13 files changed, 73 insertions(+), 69 deletions(-) rename spec/controllers/{decidim => }/area_controller_spec.rb (87%) rename spec/controllers/{decidim => }/blueprint_controller_spec.rb (96%) diff --git a/Gemfile.lock b/Gemfile.lock index b7bc7c5..b728126 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -758,7 +758,7 @@ DEPENDENCIES web-console (~> 3.5) RUBY VERSION - ruby 2.6.5p114 + ruby 2.6.3p62 BUNDLED WITH 2.0.2 diff --git a/app/cells/decidim/navigation_maps/content_blocks/navigation_map_settings_form/show.erb b/app/cells/decidim/navigation_maps/content_blocks/navigation_map_settings_form/show.erb index d060506..f9b2c5d 100644 --- a/app/cells/decidim/navigation_maps/content_blocks/navigation_map_settings_form/show.erb +++ b/app/cells/decidim/navigation_maps/content_blocks/navigation_map_settings_form/show.erb @@ -15,8 +15,6 @@ <%= render partial: "tabs", locals: { tabs: blueprints } %> <%= render partial: "tabs_content", locals: { tabs: blueprints } %> - -
diff --git a/app/commands/decidim/navigation_maps/save_area.rb b/app/commands/decidim/navigation_maps/save_area.rb index b3dd459..e6f97b5 100644 --- a/app/commands/decidim/navigation_maps/save_area.rb +++ b/app/commands/decidim/navigation_maps/save_area.rb @@ -32,7 +32,7 @@ def call attr_reader :form, :current_blueprint def initialize_area - @area = BlueprintArea.find_or_initialize_by(area_id: form.area_id, blueprint: @form.context.current_blueprint); + @area = BlueprintArea.find_or_initialize_by(area_id: form.area_id, blueprint: @form.context.current_blueprint) end def update_area diff --git a/app/controllers/decidim/navigation_maps/admin/areas_controller.rb b/app/controllers/decidim/navigation_maps/admin/areas_controller.rb index 4385224..8d2004d 100644 --- a/app/controllers/decidim/navigation_maps/admin/areas_controller.rb +++ b/app/controllers/decidim/navigation_maps/admin/areas_controller.rb @@ -55,7 +55,7 @@ def update def parse_areas return unless params[:blueprint_area] - return unless params[:blueprint_area][:area].present? + return if params[:blueprint_area][:area].blank? feature = JSON.parse params[:blueprint_area][:area] params[:blueprint_area][:area] = feature["geometry"] if feature["geometry"] diff --git a/config/locales/en.yml b/config/locales/en.yml index 5fa59ee..9bf2d82 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -7,35 +7,35 @@ en: navigation_maps: admin: areas: + create: + error: An error ocurred while creating the area + success: Area created successfully new: area: New area details - save: Save cancel: Cancel + save: Save show: area: Area details - save: Save cancel: Cancel + save: Save update: - success: Area saved successfully error: An error ocurred while saving the area - create: - success: Area created successfully - error: An error ocurred while creating the area + success: Area saved successfully content_blocks: name: Navigation Maps navigation_map_settings_form: + add: Add blueprint_image: Blueprint image - image_missing: Please add a blueprint image to start - upload_image: Upload image - id: Id - type: Type - link: Link - title: Title description: Description - add: Add - info: General info editor: Map Editor + id: Id + image_missing: Please add a blueprint image to start + info: General info + link: Link remove_blueprint: Delete this blueprint + title: Title + type: Type + upload_image: Upload image create: error: Error creating blueprint success: Blueprint created successfully diff --git a/db/migrate/20191125142751_create_decidim_navigation_maps_blueprint_areas.rb b/db/migrate/20191125142751_create_decidim_navigation_maps_blueprint_areas.rb index ec5b50c..f70a227 100644 --- a/db/migrate/20191125142751_create_decidim_navigation_maps_blueprint_areas.rb +++ b/db/migrate/20191125142751_create_decidim_navigation_maps_blueprint_areas.rb @@ -1,14 +1,6 @@ # frozen_string_literal: true class CreateDecidimNavigationMapsBlueprintAreas < ActiveRecord::Migration[5.2] - class Blueprint < ApplicationRecord - self.table_name = "decidim_navigation_maps_blueprints" - end - - class Area < ApplicationRecord - self.table_name = "decidim_navigation_maps_blueprint_areas" - end - def change create_table :decidim_navigation_maps_blueprint_areas do |t| t.jsonb :area @@ -21,20 +13,6 @@ def change t.timestamps end - # Search areas and create distributed entries - Blueprint.find_each do |blueprint| - next unless blueprint.blueprint - - blueprint.blueprint.each do |_key, area| - Area.create!( - area: area["geometry"], - decidim_navigation_maps_blueprint_id: blueprint.id, - area_type: area["type"], - url: area["properties"]["link"] - ) - end - end - remove_column :decidim_navigation_maps_blueprints, :blueprint, :jsonb end end diff --git a/db/migrate/20191126154019_add_area_id_to_decidim_navigation_maps_blueprint_areas.rb b/db/migrate/20191126154019_add_area_id_to_decidim_navigation_maps_blueprint_areas.rb index b5dbe16..b914529 100644 --- a/db/migrate/20191126154019_add_area_id_to_decidim_navigation_maps_blueprint_areas.rb +++ b/db/migrate/20191126154019_add_area_id_to_decidim_navigation_maps_blueprint_areas.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AddAreaIdToDecidimNavigationMapsBlueprintAreas < ActiveRecord::Migration[5.2] class Area < ApplicationRecord self.table_name = "decidim_navigation_maps_blueprint_areas" diff --git a/lib/decidim/navigation_maps/test/factories.rb b/lib/decidim/navigation_maps/test/factories.rb index 67dc541..f501a6f 100644 --- a/lib/decidim/navigation_maps/test/factories.rb +++ b/lib/decidim/navigation_maps/test/factories.rb @@ -12,9 +12,16 @@ factory :blueprint_area, class: Decidim::NavigationMaps::BlueprintArea do blueprint { create(:blueprint) } - area { { x: 1, y: 1 } } + area do + { + "x" => 1, + "y" => 1 + } + end area_type { "Feature" } - link { "#" } + area_id { "1" } + link { "#link" } + link_type { "link" } title { Decidim::Faker::Localized.word } description { generate_localized_title } end diff --git a/spec/commands/save_area_spec.rb b/spec/commands/save_area_spec.rb index 66e25fc..0f94c56 100644 --- a/spec/commands/save_area_spec.rb +++ b/spec/commands/save_area_spec.rb @@ -8,22 +8,26 @@ module Decidim::NavigationMaps let(:blueprint) { create :blueprint } let(:form) do - double( - AreaForm, + AreaForm + .from_params(attributes) + .with_context(current_blueprint: blueprint) + end + let(:attributes) do + { area: data, - id: id, + area_id: area_id, title: title, description: title, link: link, current_blueprint: blueprint - ) + } end let(:data) do { x: 0.5, y: 0.6 } end let(:title) { Decidim::Faker::Localized.sentence(2) } let(:description) { Decidim::Faker::Localized.paragraph } - let(:id) { nil } + let(:area_id) { nil } let(:link) { "#link" } before do @@ -42,20 +46,20 @@ module Decidim::NavigationMaps context "when id exists" do let!(:blueprint_area) { create(:blueprint_area, blueprint: blueprint) } - let(:id) { blueprint_area.id } + let(:area_id) { blueprint_area.area_id } it "broadcasts ok" do expect { subject.call }.to broadcast(:ok) end - it "creates the area" do + it "updates the area" do expect { subject.call }.to change(BlueprintArea, :count).by(0) end end context "when id does not exist" do let(:blueprint_area) { build(:blueprint_area, id: id, blueprint: blueprint) } - let(:id) { 11101 } + let(:id) { 11_101 } it "broadcasts ok" do expect { subject.call }.to broadcast(:ok) diff --git a/spec/controllers/decidim/area_controller_spec.rb b/spec/controllers/area_controller_spec.rb similarity index 87% rename from spec/controllers/decidim/area_controller_spec.rb rename to spec/controllers/area_controller_spec.rb index 2fbdbd8..6ad327b 100644 --- a/spec/controllers/decidim/area_controller_spec.rb +++ b/spec/controllers/area_controller_spec.rb @@ -10,7 +10,7 @@ module Decidim::NavigationMaps::Admin let(:user) { create(:user, :confirmed, :admin, organization: organization) } let(:blueprint) { create(:blueprint, organization: organization) } - let(:params) do + let(:attributes) do { blueprint_area: { area: data, @@ -48,13 +48,14 @@ module Decidim::NavigationMaps::Admin let!(:area) { create(:blueprint_area, blueprint: blueprint) } it "returns http success" do - get :show, params: { blueprint_id: blueprint.id, id: area.id } + get :show, params: { blueprint_id: blueprint.id, area_id: area.area_id } expect(response).to have_http_status(:success) end end + context "when area does not exist" do it "redirects to new" do - get :show, params: { blueprint_id: blueprint.id, id: 1111 } + get :show, params: { blueprint_id: blueprint.id, area_id: 1111 } expect(response).to have_http_status(:success) end end @@ -64,7 +65,7 @@ module Decidim::NavigationMaps::Admin let!(:area) { create(:blueprint_area, blueprint: blueprint) } it "returns http success" do - post :update, params: { blueprint_id: blueprint.id, id: area.id } + post :update, params: { blueprint_id: blueprint.id, area_id: area.area_id } expect(response).to have_http_status(:success) end end diff --git a/spec/controllers/decidim/blueprint_controller_spec.rb b/spec/controllers/blueprint_controller_spec.rb similarity index 96% rename from spec/controllers/decidim/blueprint_controller_spec.rb rename to spec/controllers/blueprint_controller_spec.rb index 9a567de..c1e381a 100644 --- a/spec/controllers/decidim/blueprint_controller_spec.rb +++ b/spec/controllers/blueprint_controller_spec.rb @@ -7,9 +7,7 @@ module Decidim::NavigationMaps::Admin routes { Decidim::NavigationMaps::AdminEngine.routes } let(:user) { create(:user, :confirmed, :admin, organization: organization) } - let(:organization) do - create(:organization) - end + let(:organization) { create(:organization) } let(:params) do { blueprints: { diff --git a/spec/model/blueprint_area_spec.rb b/spec/model/blueprint_area_spec.rb index 24eb36b..d5f622c 100644 --- a/spec/model/blueprint_area_spec.rb +++ b/spec/model/blueprint_area_spec.rb @@ -9,13 +9,14 @@ module NavigationMaps let(:organization) { create(:organization) } let(:blueprint) { create(:blueprint, organization: organization) } - let(:blueprint_area) { build(:blueprint_area, title: title, description: description, link: link, area: area, blueprint: blueprint) } + let(:blueprint_area) { build(:blueprint_area, blueprint: blueprint) } let(:area) do { "x" => "coord x", "y" => "coord y" } end + let(:area_id) { "101" } let(:title) { Decidim::Faker::Localized.sentence(2) } let(:description) { Decidim::Faker::Localized.paragraph } let(:link) { "#link" } @@ -29,18 +30,21 @@ module NavigationMaps # TODO: validate json area context "when all fields are specified" do + let!(:blueprint_area) { create(:blueprint_area, title: title, description: description, link: link, area: area, area_id: area_id, blueprint: blueprint) } + it "saves data correctly" do subject.save subject.reload expect(subject.title).to eq(title) expect(subject.description).to eq(description) expect(subject.link).to eq(link) + expect(subject.area_id).to eq(area_id) expect(subject.area).to eq(area) end end context "when no area" do - let!(:blueprint_area) { create(:blueprint_area, title: title, description: description, link: link, area: area, blueprint: blueprint) } + let!(:blueprint_area) { create(:blueprint_area, area: area, area_id: area_id, blueprint: blueprint) } it "save data without area" do a = BlueprintArea.find(blueprint_area.id) @@ -52,6 +56,7 @@ module NavigationMaps expect(a.title).to eq(title) expect(a.description).to eq(description) expect(a.link).to eq(link) + expect(a.area_id).to eq(area_id) expect(a.area).to eq(area) end end diff --git a/spec/model/blueprint_spec.rb b/spec/model/blueprint_spec.rb index 9a17c90..5002e23 100644 --- a/spec/model/blueprint_spec.rb +++ b/spec/model/blueprint_spec.rb @@ -38,24 +38,35 @@ module NavigationMaps context "when areas are defined" do let!(:blueprint) { create(:blueprint, organization: organization) } - let!(:area1) { create(:blueprint_area, area: data1, blueprint: blueprint) } - let!(:area2) { create(:blueprint_area, area: data2, blueprint: blueprint) } + let!(:area1) { create(:blueprint_area, area: data, area_id: "101", link: "#link1", blueprint: blueprint) } + let!(:area2) { create(:blueprint_area, area: data, area_id: "102", link: "#link1", blueprint: blueprint) } let(:blueprint_object) do { - area1.id.to_s => { + "101" => { type: "Feature", - geometry: data1 + geometry: data, + properties: properties }, - area2.id.to_s => { + "102" => { type: "Feature", - geometry: data2 + geometry: data, + properties: properties } } end - let(:area3) { create(:blueprint_area, area: data2) } - let(:data1) { { "x" => "coord x", "y" => "coord y" } } - let(:data2) { { "x" => "coord x", "y" => "coord y" } } + let(:area3) { create(:blueprint_area, area: data) } + let(:data) do + { + "x" => "coord x", + "y" => "coord y" + } + end + let(:properties) do + { + link: "#link1" + } + end it { is_expected.to be_valid }