From 61dba311fa7160cafa9d3f0be6d006958fc8dab6 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Mon, 21 Sep 2015 16:56:39 +0200 Subject: [PATCH] Further spec greening --- app/controllers/application_controller.rb | 4 +- bin/test_prepare | 1 + lib/qwaiter/supplier_base_serializer.rb | 3 +- .../suppliers/lists_controller_spec.rb | 2 +- .../suppliers/products_controller_spec.rb | 5 -- spec/models/list_spec.rb | 4 +- spec/models/user_spec.rb | 2 +- spec/routing/user_routing_spec.rb | 69 ------------------- .../join_request_serializer_spec.rb | 19 ----- .../suppliers/order_serializer_spec.rb | 4 +- .../users/join_request_serializer_spec.rb | 11 +++ .../serializers/users/list_serializer_spec.rb | 9 +-- .../users/order_serializer_spec.rb | 3 +- .../users/table_serializer_spec.rb | 2 - spec/support/serializers_test_helpers.rb | 14 ++-- 15 files changed, 33 insertions(+), 119 deletions(-) delete mode 100644 spec/routing/user_routing_spec.rb delete mode 100644 spec/serializers/join_request_serializer_spec.rb create mode 100644 spec/serializers/users/join_request_serializer_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 217ac332..d782d542 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -69,7 +69,9 @@ private "#{controller_path.classify}Serializer".constantize end end - options[:is_collection] = params[:id].blank? unless options.has_key?(:is_collection) + unless options.has_key?(:is_collection) + options[:is_collection] = params[:id].blank? && %w[new create].exclude?(action_name) + end JSONAPI::Serializer.serialize(resource, options).to_json end diff --git a/bin/test_prepare b/bin/test_prepare index 7d1eca53..022a90d8 100755 --- a/bin/test_prepare +++ b/bin/test_prepare @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require 'pathname' require 'active_support/all' +# rm log/test.log && rm -rf tmp/capybara/* && ./bin/test_prepare && touch public/user/cordova.js && rspec spec/acceptance/users/sign_up.feature:15 mozo_user_path = ENV['MOZO_USER_PATH'] || Pathname.new(File.expand_path('../../../mozo-user', __FILE__)) Dir.chdir mozo_user_path do `ember build --output-path=../mozo/public/user/ --environment=testexport` # not production, because then the production servers will be queried diff --git a/lib/qwaiter/supplier_base_serializer.rb b/lib/qwaiter/supplier_base_serializer.rb index 495b9980..4334ee7f 100644 --- a/lib/qwaiter/supplier_base_serializer.rb +++ b/lib/qwaiter/supplier_base_serializer.rb @@ -38,8 +38,7 @@ module Qwaiter::SupplierBaseSerializer def timestamp_attribute(attr) attribute attr do - return unless timestamp = object.public_send(attr) - timestamp.iso8601 + object.public_send(attr).try(:iso8601) end end diff --git a/spec/controllers/suppliers/lists_controller_spec.rb b/spec/controllers/suppliers/lists_controller_spec.rb index d097a9d8..da74b542 100644 --- a/spec/controllers/suppliers/lists_controller_spec.rb +++ b/spec/controllers/suppliers/lists_controller_spec.rb @@ -51,7 +51,7 @@ describe Suppliers::ListsController, type: :controller do it "renders the #show view" do list = create :list, supplier: @supplier get :show, id: list - response.should render_template :show + response.body.should include 'orders' end end diff --git a/spec/controllers/suppliers/products_controller_spec.rb b/spec/controllers/suppliers/products_controller_spec.rb index d03d499d..edbda994 100644 --- a/spec/controllers/suppliers/products_controller_spec.rb +++ b/spec/controllers/suppliers/products_controller_spec.rb @@ -120,11 +120,6 @@ describe Suppliers::ProductsController, type: :controller do }.to change(Product, :count).by(-1) end - it "redirects to products#index" do - delete :destroy, id: @product - response.should redirect_to [:suppliers, :products] - end - it "should not be possible to delete a product of another supplier" do product = create :product expect{ diff --git a/spec/models/list_spec.rb b/spec/models/list_spec.rb index 7cb4d1ef..6ec446c7 100644 --- a/spec/models/list_spec.rb +++ b/spec/models/list_spec.rb @@ -143,8 +143,8 @@ describe List do # }.to broadcast_to_user(user.id).message('orders_placed_count').with(count: 2) expect{ - list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 5}], user: user) - }.to broadcast_to_supplier(supplier.id).message('orders_placed_count').with(count: 2) + list.place_order(product_orders: [{'product_id' => product.id, 'quantity' => 5}], user: user, first_order: false) + }.to broadcast_to_supplier(supplier.id).message('supplier_orders_placed_count').with(count: 2) end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index eb1aa231..e0e284cd 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -10,7 +10,7 @@ describe User do end it 'falls back to unkown if not present' do - user.supplier_name.should == I18n.t('user.unknown_supplier_name') + user.supplier_name.should == I18n.t('supplier.unknown_user_name') end end diff --git a/spec/routing/user_routing_spec.rb b/spec/routing/user_routing_spec.rb deleted file mode 100644 index 82ff5547..00000000 --- a/spec/routing/user_routing_spec.rb +++ /dev/null @@ -1,69 +0,0 @@ -require "spec_helper" - -describe UserController, type: :routing do - describe "routing" do - - it "routes root to #index" do - get("/user").should route_to("user#index") - end - it "routes to #index" do - get("/user/index").should route_to("user#index") - end - it "routes to #active_list" do - get("/user/active_list").should route_to("user#active_list") - end - it "routes to #list_info" do - get("/user/list_info").should route_to("user#list_info") - end - it "routes to #needs_help" do - post("/user/needs_help").should route_to("user#needs_help") - end - - it "routes to #list_needs_payment" do - post("/user/list_needs_payment").should route_to("user#list_needs_payment") - end - - it "routes to #create_list" do - post("/user/create_list").should route_to("user#create_list") - end - it "routes to #list_products" do - get("/user/list_products").should route_to("user#list_products") - end - it "routes to #list_products_for_table" do - get("/user/list_products_for_table").should route_to("user#list_products_for_table") - end - it "routes to #list_history" do - get("/user/list_history").should route_to("user#list_history") - end - it "routes to #history_list" do - get("/user/history_list").should route_to("user#history_list") - end - it "routes to #order_selected_products" do - post("/user/order_selected_products").should route_to("user#order_selected_products") - end - it "routes to #move_table" do - post("/user/move_table").should route_to("user#move_table") - end - it "routes to #table_info" do - get("/user/table_info").should route_to("user#table_info") - end - it "routes to #join_occupied_table" do - post("/user/join_occupied_table").should route_to("user#request_to_join_occupied_table") - end - it "routes to #join_occupied_table" do - post("/user/join_occupied_table").should route_to("user#request_to_join_occupied_table") - end - it "routes to #reject_join_request" do - post("/user/reject_join_request").should route_to("user#reject_join_request") - end - it "routes to #approve_join_request" do - post("/user/approve_join_request").should route_to("user#approve_join_request") - end - it "routes to #check_table_join_status" do - post("/user/check_table_join_status").should route_to("user#check_table_join_status") - end - it "routes to #obtain_token" do - get("/user/obtain_token").should route_to("user#obtain_token") - end - end -end diff --git a/spec/serializers/join_request_serializer_spec.rb b/spec/serializers/join_request_serializer_spec.rb deleted file mode 100644 index 6c9f9fd3..00000000 --- a/spec/serializers/join_request_serializer_spec.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'spec_helper' - -describe JoinRequestSerializer do - it 'works' do - user = create :user - list = create :list - join_request = list.join_request_for_user(user) - JoinRequestSerializer.new(join_request).as_json.deep_symbolize_keys.should =~ { - users: [UserUserSerializer.new(user).as_json(root: false)], - join_request: { - id: "jr-#{user.id}", - created_at: nil, - updated_at: nil, - list_id: list.id, - user_id: user.id - } - } - end -end diff --git a/spec/serializers/suppliers/order_serializer_spec.rb b/spec/serializers/suppliers/order_serializer_spec.rb index 214ee604..79e713bd 100644 --- a/spec/serializers/suppliers/order_serializer_spec.rb +++ b/spec/serializers/suppliers/order_serializer_spec.rb @@ -6,8 +6,8 @@ describe Suppliers::OrderSerializer, type: :serializer do order = create :order, user: user order.product_orders # preload empty result = nil - expect{ result = JSONAPI::Serializer.serialize order, serializer: described_class, include: %w[user user.order] }.not_to perform_any_queries - result[:included].size.should eq 4 + expect{ result = described_class.serialize(order, include: %w[user]).deep_symbolize_keys }.not_to perform_any_queries + result[:included].size.should eq 1 end end diff --git a/spec/serializers/users/join_request_serializer_spec.rb b/spec/serializers/users/join_request_serializer_spec.rb new file mode 100644 index 00000000..36b4909f --- /dev/null +++ b/spec/serializers/users/join_request_serializer_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Users::JoinRequestSerializer do + it 'works' do + user = create :user + list = create :list + join_request = list.join_request_for_user(user) + result = described_class.serialize(join_request).deep_symbolize_keys + result[:data].should match hash_including(type: 'join-requests', id: "jr-#{user.id}") + end +end diff --git a/spec/serializers/users/list_serializer_spec.rb b/spec/serializers/users/list_serializer_spec.rb index d5678545..8aede6f4 100644 --- a/spec/serializers/users/list_serializer_spec.rb +++ b/spec/serializers/users/list_serializer_spec.rb @@ -12,15 +12,10 @@ describe Users::ListSerializer, type: :serializer do l.add_user create :user l.add_user create :user list = List.find(l.id) - [list].include_relations(:users) + list.users # cache users result = nil #result = expect{ JSONAPI::Serializer.serialize(list, serializer: described_class) }.not_to perform_any_queries - expect{ result = JSONAPI::Serializer.serialize(list, serializer: described_class, include: %w[supplier users]) }.not_to exceed_query_limit 1 # supplier - binding.pry - - - #expect{ object_as_json orders }.not_to exceed_query_limit 0 - #expect{ object_as_json list }.not_to exceed_query_limit 0 + expect{ result = described_class.serialize(list, include: %w[supplier users]) }.not_to exceed_query_limit 1 # supplier end end diff --git a/spec/serializers/users/order_serializer_spec.rb b/spec/serializers/users/order_serializer_spec.rb index 7a42f1ad..67cb27c2 100644 --- a/spec/serializers/users/order_serializer_spec.rb +++ b/spec/serializers/users/order_serializer_spec.rb @@ -13,8 +13,7 @@ describe Users::OrderSerializer, type: :serializer do orders = list.orders.include_relation(:product_orders) #expect{ object_as_json orders }.not_to exceed_query_limit 0 result = nil - expect{ result = JSONAPI::Serializer.serialize(orders, serializer: described_class, include: 'product_orders', is_collection: true)}.not_to perform_any_queries - binding.pry + expect{ result = described_class.serialize(orders, include: 'product_orders', is_collection: true).deep_symbolize_keys }.not_to perform_any_queries result[:included].size.should eq 4 end diff --git a/spec/serializers/users/table_serializer_spec.rb b/spec/serializers/users/table_serializer_spec.rb index eb1de8db..300f44bc 100644 --- a/spec/serializers/users/table_serializer_spec.rb +++ b/spec/serializers/users/table_serializer_spec.rb @@ -4,8 +4,6 @@ describe Users::TableSerializer, type: :serializer do it "does not perform extra queries" do table = create :table result = object_as_json(table) - binding.pry - result[:included].size.should eq 4 end end diff --git a/spec/support/serializers_test_helpers.rb b/spec/support/serializers_test_helpers.rb index f6017dc5..7cb14d69 100644 --- a/spec/support/serializers_test_helpers.rb +++ b/spec/support/serializers_test_helpers.rb @@ -1,14 +1,16 @@ module SerializersTestHelpers - def object_as_json(obj) - serializer = if obj.is_a?(Array) - ActiveModel::Serializer::ArraySerializer.new(obj, serializer: described_class, add_included: true) + def object_as_json(obj, options = {}) + if obj.is_a?(Array) + #ActiveModel::Serializer::ArraySerializer.new(obj, serializer: described_class, add_included: true) + described_class.serialize(obj, options.merge(is_collection: true)) else - serializer = described_class.new(obj) + #serializer = described_class.new(obj) + described_class.serialize(obj, options) end - adapter.new(serializer, include: %w[product_orders]).as_json + #adapter.new(serializer, include: %w[product_orders]).as_json end def adapter - @active_model_adapter = ActiveModel::Serializer.config.adapter + #@active_model_adapter = ActiveModel::Serializer.config.adapter end end