From 446bb160b02df2c5966188f18e28632e70723861 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Thu, 17 May 2012 16:25:47 +0900 Subject: [PATCH] test update --- app/controllers/common_licenses_controller.rb | 77 +-- app/controllers/home_controller.rb | 2 +- app/controllers/licenses_controller.rb | 16 +- app/controllers/original_licenses_controller.rb | 34 +- app/controllers/original_pictures_controller.rb | 8 +- app/controllers/panels_controller.rb | 2 +- .../speach_balloon_templates_controller.rb | 114 ++++ app/helpers/speach_balloon_templates_helper.rb | 2 + app/models/common_license.rb | 139 ++++- app/models/license.rb | 73 ++- app/models/original_license.rb | 137 ++++- app/models/original_picture.rb | 19 +- app/models/panel.rb | 129 +++- app/models/resource_picture.rb | 8 +- app/models/speach_balloon_template.rb | 12 + app/views/common_licenses/list.html.erb | 4 + app/views/common_licenses/result.html.erb | 2 + app/views/original_licenses/edit.js.erb | 1 + app/views/original_licenses/new.js.erb | 1 + app/views/speach_balloon_templates/_form.html.erb | 17 + app/views/speach_balloon_templates/browse.html.erb | 31 + app/views/speach_balloon_templates/index.html.erb | 26 + app/views/speach_balloon_templates/list.html.erb | 28 + app/views/speach_balloon_templates/show.html.erb | 31 + config/environment.rb | 5 + config/environments/development.rb | 9 +- config/environments/production.rb | 2 - config/environments/test.rb | 9 +- config/routes.rb | 26 +- config/test_layout | 0 .../20111206105022_create_original_licenses.rb | 21 +- db/migrate/20111206111202_create_panels.rb | 10 +- ...120424100555_create_speach_balloon_templates.rb | 8 + db/schema.rb | 18 +- .../balloon_templates_controller_spec.rb | 8 +- .../controllers/common_licenses_controller_spec.rb | 439 ++++++++++---- spec/controllers/licenses_controller_spec.rb | 126 +++- .../original_licenses_controller_spec.rb | 457 +++++++++++++- spec/controllers/panels_controller_spec.rb | 11 +- .../speach_balloon_templates_controller_spec.rb | 157 +++++ .../speach_templates_controller_spec.rb | 10 +- spec/factories.rb | 49 +- .../speach_balloon_templates_helper_spec.rb | 15 + spec/json/common_license.json | 12 + spec/json/common_licenses.json | 26 + spec/json/invalid_common_licenses.json | 38 ++ spec/json/invalid_original_licenses.json | 38 ++ spec/json/original_license.json | 12 + spec/json/original_licenses.json | 26 + spec/models/balloon_spec.rb | 259 +++++++- spec/models/common_license_spec.rb | 556 ++++++++++++++++- spec/models/license_spec.rb | 219 ++++++- spec/models/original_license_spec.rb | 555 ++++++++++++++++- spec/models/original_picture_spec.rb | 38 +- spec/models/panel_spec.rb | 661 ++++++++++++++++++++- spec/models/resource_picture_spec.rb | 113 ++++ spec/models/speach_balloon_template_spec.rb | 5 + spec/models/speach_spec.rb | 260 +++++++- spec/support/controller_macros.rb | 11 - 59 files changed, 4772 insertions(+), 350 deletions(-) create mode 100644 app/controllers/speach_balloon_templates_controller.rb create mode 100644 app/helpers/speach_balloon_templates_helper.rb create mode 100644 app/models/speach_balloon_template.rb create mode 100644 app/views/common_licenses/result.html.erb create mode 100644 app/views/original_licenses/edit.js.erb create mode 100644 app/views/original_licenses/new.js.erb create mode 100644 app/views/speach_balloon_templates/_form.html.erb create mode 100644 app/views/speach_balloon_templates/browse.html.erb create mode 100644 app/views/speach_balloon_templates/index.html.erb create mode 100644 app/views/speach_balloon_templates/list.html.erb create mode 100644 app/views/speach_balloon_templates/show.html.erb create mode 100644 config/test_layout create mode 100644 db/migrate/20120424100555_create_speach_balloon_templates.rb create mode 100644 spec/controllers/speach_balloon_templates_controller_spec.rb create mode 100644 spec/helpers/speach_balloon_templates_helper_spec.rb create mode 100644 spec/json/common_license.json create mode 100644 spec/json/common_licenses.json create mode 100644 spec/json/invalid_common_licenses.json create mode 100644 spec/json/invalid_original_licenses.json create mode 100644 spec/json/original_license.json create mode 100644 spec/json/original_licenses.json create mode 100644 spec/models/speach_balloon_template_spec.rb diff --git a/app/controllers/common_licenses_controller.rb b/app/controllers/common_licenses_controller.rb index cefb6d66..674e1612 100644 --- a/app/controllers/common_licenses_controller.rb +++ b/app/controllers/common_licenses_controller.rb @@ -1,26 +1,41 @@ class CommonLicensesController < ApplicationController + layout 'test' if Pettanr::TestLayout before_filter :authenticate_user!, :only => [:index, :show] - before_filter :authenticate_admin!, :only => [:list, :browse, :new, :edit, :create, :update, :destroy] - + before_filter :authenticate_admin!, :only => [:list, :browse, :import, :destroy] + + private + + def set_data(file) + if file.respond_to?(:read) + file.read + else + file + end + end + + public + # GET /common_licenses # GET /common_licenses.json def index - @common_licenses = CommonLicense.all + @page = CommonLicense.page params[:page] + @page_size = CommonLicense.page_size params[:page_size] + @common_licenses = CommonLicense.list({}, @page, @page_size) respond_to do |format| format.html # index.html.erb - format.json { render json: @common_licenses } + format.json { render json: @common_licenses.to_json(CommonLicense.list_json_opt) } end end # GET /common_licenses/1 # GET /common_licenses/1.json def show - @common_license = CommonLicense.find(params[:id]) + @common_license = CommonLicense.show(params[:id]) respond_to do |format| format.html # show.html.erb - format.json { render json: @common_license } + format.json { render json: @common_license.to_json(CommonLicense.show_json_include_opt) } end end @@ -42,52 +57,18 @@ class CommonLicensesController < ApplicationController end end - # GET /common_licenses/new - # GET /common_licenses/new.json - def new - @common_license = CommonLicense.new - - respond_to do |format| - format.html # new.html.erb - format.json { render json: @common_license } - end - end - - # GET /common_licenses/1/edit - def edit - @common_license = CommonLicense.find(params[:id]) - end - # POST /common_licenses # POST /common_licenses.json - def create - @common_license = CommonLicense.new(params[:common_license]) - - respond_to do |format| - CommonLicense.transaction do - if @common_license.save_save - format.html { redirect_to :action => :browse, :id => @common_license.id, notice: 'Common license was successfully created.' } - format.json { render json: @common_license, status: :created, location: @common_license } - else - format.html { render action: "new" } - format.json { render json: @common_license.errors, status: :unprocessable_entity } - end - end - end - end - - # PUT /common_licenses/1 - # PUT /common_licenses/1.json - def update - @common_license = CommonLicense.find(params[:id]) - + def import + @data = set_data params[:file] respond_to do |format| - if @common_license.update_attributes(params[:common_license]) - format.html { redirect_to :action => :browse, :id => @common_license.id, notice: 'Common license was successfully updated.' } - format.json { head :ok } + @errors = CommonLicense.import @data + if @errors and @errors.empty? + format.html { redirect_to :action => :list } + format.json { render text: 'ok', status: :created } else - format.html { render action: "edit" } - format.json { render json: @common_license.errors, status: :unprocessable_entity } + format.html { render action: "result" } + format.json { render json: @errors, status: :unprocessable_entity } end end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index c2981994..edd47d0a 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,5 +1,5 @@ class HomeController < ApplicationController -layout 'test' + layout 'test' if Pettanr::TestLayout before_filter :authenticate_user!, :only => [ :index, :show, :profile, :configure, :create_token, :delete_token, :step2, :save_step2, :step3, :save_step3, :comic, :picture diff --git a/app/controllers/licenses_controller.rb b/app/controllers/licenses_controller.rb index b92b0b99..2943122c 100644 --- a/app/controllers/licenses_controller.rb +++ b/app/controllers/licenses_controller.rb @@ -1,26 +1,26 @@ class LicensesController < ApplicationController - before_filter :authenticate_user!, :only => [:index, :show] before_filter :authenticate_admin!, :only => [:list, :browse] # GET /licenses # GET /licenses.json def index - @licenses = License.all + @page = License.page params[:page] + @page_size = License.page_size params[:page_size] + @licenses = License.list({}, @page, @page_size) respond_to do |format| format.html # index.html.erb - format.json { render json: @licenses } + format.json { render json: @licenses.to_json(License.list_json_opt) } end end # GET /licenses/1 # GET /licenses/1.json def show - @license = License.find(params[:id]) - if @license.common_license - redirect_to :controller => 'common_licenses', :action => :browse, :id => @license.common_license.id - else - redirect_to :controller => 'original_licenses', :action => :browse, :id => @license.original_license.id + @license = License.show(params[:id]) + respond_to do |format| + format.html # show.html.erb + format.json { render json: @license.to_json(License.show_json_include_opt) } end end diff --git a/app/controllers/original_licenses_controller.rb b/app/controllers/original_licenses_controller.rb index a88d3ea2..a6bf378c 100644 --- a/app/controllers/original_licenses_controller.rb +++ b/app/controllers/original_licenses_controller.rb @@ -1,26 +1,27 @@ class OriginalLicensesController < ApplicationController - before_filter :authenticate_user!, :only => [:index, :show, :new, :edit, :create, :update, :destroy] - before_filter :authenticate_admin!, :only => [:list, :browse] + before_filter :authenticate_admin!, :only => [:list, :browse, :new, :edit, :create, :update, :destroy] # GET /original_licenses # GET /original_licenses.json def index - @original_licenses = OriginalLicense.all + @page = OriginalLicense.page params[:page] + @page_size = OriginalLicense.page_size params[:page_size] + @original_licenses = OriginalLicense.list({}, @page, @page_size) respond_to do |format| format.html # index.html.erb - format.json { render json: @original_licenses } + format.json { render json: @original_licenses.to_json(OriginalLicense.list_json_opt) } end end # GET /original_licenses/1 # GET /original_licenses/1.json def show - @original_license = OriginalLicense.find(params[:id]) + @original_license = OriginalLicense.show(params[:id]) respond_to do |format| format.html # show.html.erb - format.json { render json: @original_license } + format.json { render json: @original_license.to_json(OriginalLicense.show_json_include_opt) } end end @@ -46,25 +47,31 @@ class OriginalLicensesController < ApplicationController # GET /original_licenses/new.json def new @original_license = OriginalLicense.new - + @original_license.supply_default respond_to do |format| format.html # new.html.erb + format.js format.json { render json: @original_license } end end # GET /original_licenses/1/edit def edit - @original_license = OriginalLicense.find(params[:id]) + @original_license = OriginalLicense.show(params[:id]) + respond_to do |format| + format.html + format.js + end end # POST /original_licenses # POST /original_licenses.json def create - @original_license = OriginalLicense.new(params[:original_license]) - @original_license.artist_id = @author.artist.id + @original_license = OriginalLicense.new + @original_license.supply_default + @original_license.attributes = params[:original_license] respond_to do |format| - if @original_license.save_save + if @original_license.store format.html { redirect_to @original_license, notice: 'Original license was successfully created.' } format.json { render json: @original_license, status: :created, location: @original_license } else @@ -77,10 +84,11 @@ class OriginalLicensesController < ApplicationController # PUT /original_licenses/1 # PUT /original_licenses/1.json def update - @original_license = OriginalLicense.find(params[:id]) + @original_license = OriginalLicense.show(params[:id]) + @original_license.attributes = params[:original_license] respond_to do |format| - if @original_license.update_attributes(params[:original_license]) + if @original_license.store format.html { redirect_to @original_license, notice: 'Original license was successfully updated.' } format.json { head :ok } else diff --git a/app/controllers/original_pictures_controller.rb b/app/controllers/original_pictures_controller.rb index ef1ff8d9..31b3ca14 100644 --- a/app/controllers/original_pictures_controller.rb +++ b/app/controllers/original_pictures_controller.rb @@ -46,11 +46,7 @@ class OriginalPicturesController < ApplicationController # GET /original_pictures/1 # GET /original_pictures/1.json def show - @original_picture = OriginalPicture.show(params[:id], @author) -# if params[:subdir] == 'refresh' -# refresh -# return -# end + @original_picture = OriginalPicture.show(params[:id], @artist) respond_to do |format| opt = {:type => @original_picture.mime_type, :disposition=>"inline"} @@ -58,7 +54,7 @@ class OriginalPicturesController < ApplicationController format.gif { send_data(@original_picture.restore, opt ) } format.jpeg { send_data(@original_picture.restore, opt ) } format.html # show.html.erb - format.json { render json: @original_picture} + format.json { render json: @original_picture.to_json(OriginalPicture.show_json_include_opt)} end end diff --git a/app/controllers/panels_controller.rb b/app/controllers/panels_controller.rb index d84482f4..50f45f6f 100644 --- a/app/controllers/panels_controller.rb +++ b/app/controllers/panels_controller.rb @@ -1,5 +1,5 @@ class PanelsController < ApplicationController -layout 'test' + layout 'test' if Pettanr::TestLayout before_filter :authenticate_user!, :only => [:index, :show, :create, :update, :destroy] before_filter :authenticate_admin!, :only => [:list, :browse] diff --git a/app/controllers/speach_balloon_templates_controller.rb b/app/controllers/speach_balloon_templates_controller.rb new file mode 100644 index 00000000..cb059917 --- /dev/null +++ b/app/controllers/speach_balloon_templates_controller.rb @@ -0,0 +1,114 @@ +class SpeachBalloonTemplatesController < ApplicationController + before_filter :authenticate_user!, :only => [:index, :show] + before_filter :authenticate_admin!, :only => [:list, :browse, :import, :destroy] + + private + + def validate_param(ft) + res = nil + bl = ft[:size_count].to_i + tl = ft[:tail_count].to_i + if bl * tl == ft[:balloon_templates_attributes].size + flag = [] + bl.times do |i| + flag[i] = [] + tl.times do |j| + flag[i][j] = true + end + end + ft[:balloon_templates_attributes].each do |k, t| + flag[t[:size].to_i - 1][t[:tail].to_i - 1] = nil + end + if flag.flatten.compact.size == 0 + else + res = "invalid variation" + end + else + res = "invalid template size" + end + res + end + + public + # GET /speach_balloon_templates + # GET /speach_balloon_templates.json + def index + @speach_balloon_templates = SpeachBalloon.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @speach_balloon_templates } + end + end + + # GET /speach_balloon_templates/1 + # GET /speach_balloon_templates/1.json + def show + @speach_balloon_template = SpeachBalloon.find(params[:id], include: {:balloon_templates => :speach_templates}) + + respond_to do |format| + format.html # show.html.erb + format.json { + render :json => @speach_balloon_template.to_json(include: { + :balloon_templates => {:include => :speach_templates} + }) + } + format.jsonp { + render :json => "callback(" + @speach_balloon_template.to_json(include: { + :balloon_templates => {:include => :speach_templates} + }) + ")" + } + end + end + + def list + @speach_balloon_templates = SpeachBalloon.all + + respond_to do |format| + format.html { render layout: 'system' } + format.json { render json: @speach_balloon_templates } + end + end + + def browse + @speach_balloon_template = SpeachBalloon.find(params[:id]) + + respond_to do |format| + format.html { render layout: 'system' } + format.json { render json: @speach_balloon_template } + end + end + + # POST /speach_balloon_templates + # POST /speach_balloon_templates.json + def create + @speach_balloon_template = SpeachBalloon.new(params[:speach_balloon_template]) + if em = validate_param(params[:speach_balloon_template]) + respond_to do |format| + format.json { render json: em, status: :unprocessable_entity } + end + else + respond_to do |format| + if @speach_balloon_template.save + format.html { redirect_to @speach_balloon_template, notice: 'Speach balloon was successfully created.' } + format.json { render json: @speach_balloon_template, status: :created, location: @speach_balloon_template } + else + format.html { render action: "new" } + format.json { render json: @speach_balloon_template.errors, status: :unprocessable_entity } + end + end + end + end + + # DELETE /speach_balloon_templates/1 + # DELETE /speach_balloon_templates/1.json + def destroy + @speach_balloon_template = SpeachBalloon.find(params[:id]) + @speach_balloon_template.destroy + + respond_to do |format| + format.html { redirect_to speach_balloon_templates_url } + format.json { head :ok } + end + end +end diff --git a/app/helpers/speach_balloon_templates_helper.rb b/app/helpers/speach_balloon_templates_helper.rb new file mode 100644 index 00000000..9ba05085 --- /dev/null +++ b/app/helpers/speach_balloon_templates_helper.rb @@ -0,0 +1,2 @@ +module SpeachBalloonTemplatesHelper +end diff --git a/app/models/common_license.rb b/app/models/common_license.rb index d4804378..906fbfd3 100644 --- a/app/models/common_license.rb +++ b/app/models/common_license.rb @@ -1,26 +1,133 @@ +#コモンライセンス class CommonLicense < ActiveRecord::Base belongs_to :license - def save_save - license = License.new( { - :name => self.name, :url => self.url, :cc_by => self.cc_by, :cc_sa => self.cc_sa, :cc_nd => self.cc_nd, :cc_nc => self.cc_nc, - :no_resize => self.no_resize, :no_flip => self.no_flip, :no_convert => self.no_convert, :keep_aspect_ratio => self.keep_aspect_ratio - }) - self.license_id = 0 - res = self.save - return false unless res - return false unless license.save - self.license_id = license.id - res = self.save + validates :name, :presence => true, :length => {:maximum => 50} + validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true + validates :license_id, :presence => true, :numericality => true, :existence => true + + def self.default_page_size + 25 + end + + def self.max_page_size + 100 + end + + def self.page prm = nil + page = prm.to_i + page = 1 if page < 1 + page + end + + def self.page_size prm = self.default_page_size + page_size = prm.to_i + page_size = self.max_page_size if page_size > self.max_page_size + page_size = self.default_page_size if page_size < 1 + page_size + end + + def self.list opt = {}, page = 1, page_size = self.default_page_size + opt.merge!(self.list_opt) unless opt[:include] + opt.merge!({ + :order => 'name', :limit => page_size, :offset => (page -1) * page_size} + ) + CommonLicense.find(:all, opt) + end + + def self.list_opt + {:include => [:license]} + end + + def self.list_json_opt + {:include => [:license]} + end + + def self.show cid, opt = {} + cl = CommonLicense.find(cid, :include => self.show_include_opt(opt)) +# raise ActiveRecord::Forbidden unless pic.own?(author) + cl + end + + def self.show_include_opt opt = {} + res = [:license] + res.push(opt[:include]) if opt[:include] + res + end + + def self.show_json_include_opt + {:include => [:license]} + end + + def self.update_common_license data + if r = CommonLicense.find_by_url(data[:url]) + r.attributes = data + else + r = CommonLicense.new data + end + r + end + + def store + res = nil + CommonLicense.transaction do + l = License.update_license self + if res = l.save + self.license_id = l.id + res = self.save + raise ActiveRecord::Rollback unless res + else + raise ActiveRecord::Rollback + end + end + res + end + + def self.store params + cl = CommonLicense.update_common_license params + cl.store + cl + end + + def self.each_license data + data = [data] if data.is_a?(Hash) + data.each do |d| + yield d + end + end + + def self.parse(data) + res = data + begin + res = JSON.parse(data) if data.is_a?(String) + rescue + return false + end + res + end + + def self.import(data) + d = CommonLicense.parse(data) + return false unless d + res = [] + CommonLicense.transaction do + CommonLicense.each_license(d) do |lch| + cl = CommonLicense.store lch + res.push(cl) unless cl.valid? + end + raise ActiveRecord::Rollback unless res.empty? + end res end def self.import_file(filename) - t = File.open(filename, 'r').read - j = JSON.parse t - l = CommonLicense.new j - l.save_save - p l.id + t = nil + begin + t = File.open(filename, 'r').read + rescue + return false + end + CommonLicense.import t end end diff --git a/app/models/license.rb b/app/models/license.rb index 668f289c..fd286d1f 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -1,7 +1,76 @@ class License < ActiveRecord::Base has_one :original_license has_one :common_license - accepts_nested_attributes_for :original_license - accepts_nested_attributes_for :common_license + validates :name, :presence => true, :length => {:maximum => 50} + validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true + + def self.update_license cl + l = License.find_by_url cl.url + l = License.new unless l + l.attributes = { + :name => cl.name, :url => cl.url, :cc_by => cl.cc_by, :cc_sa => cl.cc_sa, :cc_nd => cl.cc_nd, :cc_nc => cl.cc_nc, + :no_resize => cl.no_resize, :no_flip => cl.no_flip, :no_convert => cl.no_convert, :keep_aspect_ratio => cl.keep_aspect_ratio + } + l + end + + def self.default_page_size + 25 + end + + def self.max_page_size + 100 + end + + def self.page prm = nil + page = prm.to_i + page = 1 if page < 1 + page + end + + def self.page_size prm = self.default_page_size + page_size = prm.to_i + page_size = self.max_page_size if page_size > self.max_page_size + page_size = self.default_page_size if page_size < 1 + page_size + end + + def self.offset cnt, prm = nil + offset = prm.to_i + offset = cnt - 1 if offset >= cnt + offset = cnt - offset.abs if offset < 0 + offset = 0 if offset < 0 + offset + end + + def self.list opt = {}, page = 1, page_size = self.default_page_size + opt.merge!(self.list_opt) unless opt[:include] + opt.merge!({:order => 'name', :limit => page_size, :offset => (page -1) * page_size}) + License.find(:all, opt) + end + + def self.list_opt + {:include => {:common_license => {}, :original_license => {}}} + end + + def self.list_json_opt + {:include => {:common_license => {}, :original_license => {}}} + end + + def self.show rid, opt = {} + r = License.find(rid, :include => self.show_include_opt(opt)) +# raise ActiveRecord::Forbidden unless c.visible?(au) + r + end + + def self.show_include_opt opt = {} + res = [:common_license, :original_license] + res.push(opt[:include]) if opt[:include] + res + end + + def self.show_json_include_opt + {:include => {:common_license => {}, :original_license => {}}} + end end diff --git a/app/models/original_license.rb b/app/models/original_license.rb index ebc0b410..62035def 100644 --- a/app/models/original_license.rb +++ b/app/models/original_license.rb @@ -1,16 +1,137 @@ +#オリジナルライセンス class OriginalLicense < ActiveRecord::Base belongs_to :license + validates :name, :presence => true, :length => {:maximum => 50} + validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true + validates :license_id, :presence => true, :numericality => true, :existence => true - def save_save - license = License.new( { - :name => self.name, :url => self.url, :cc_by => self.cc_by, :cc_sa => self.cc_sa, :cc_nd => self.cc_nd, :cc_nc => self.cc_nc, - :no_resize => self.no_resize, :no_flip => self.no_flip, :no_convert => self.no_convert, :keep_aspect_ratio => self.keep_aspect_ratio - }) - return false unless license.save - self.license_id = license.id - res = self.save + def supply_default + + end + + def self.default_page_size + 25 + end + + def self.max_page_size + 100 + end + + def self.page prm = nil + page = prm.to_i + page = 1 if page < 1 + page + end + + def self.page_size prm = self.default_page_size + page_size = prm.to_i + page_size = self.max_page_size if page_size > self.max_page_size + page_size = self.default_page_size if page_size < 1 + page_size + end + + def self.list opt = {}, page = 1, page_size = self.default_page_size + opt.merge!(self.list_opt) unless opt[:include] + opt.merge!({ + :order => 'name', :limit => page_size, :offset => (page -1) * page_size} + ) + OriginalLicense.find(:all, opt) + end + + def self.list_opt + {:include => [:license]} + end + + def self.list_json_opt + {:include => [:license]} + end + + def self.show cid, opt = {} + ol = OriginalLicense.find(cid, :include => self.show_include_opt(opt)) +# raise ActiveRecord::Forbidden unless pic.own?(author) + ol + end + + def self.show_include_opt opt = {} + res = [:license] + res.push(opt[:include]) if opt[:include] res end + def self.show_json_include_opt + {:include => [:license]} + end + + def self.update_original_license data + if r = OriginalLicense.find_by_url(data[:url]) + r.attributes = data + else + r = OriginalLicense.new data + end + r + end + + def store + res = nil + OriginalLicense.transaction do + l = License.update_license self + if res = l.save + self.license_id = l.id + res = self.save + raise ActiveRecord::Rollback unless res + else + raise ActiveRecord::Rollback + end + end + res + end + + def self.store params + cl = OriginalLicense.update_original_license params + cl.store + cl + end + + def self.each_license data + data = [data] if data.is_a?(Hash) + data.each do |d| + yield d + end + end + + def self.parse(data) + res = data + begin + res = JSON.parse(data) if data.is_a?(String) + rescue + return false + end + res + end + + def self.import(data) + d = OriginalLicense.parse(data) + return false unless d + res = [] + OriginalLicense.transaction do + OriginalLicense.each_license(d) do |lch| + ol = OriginalLicense.store lch + res.push(ol) unless ol.valid? + end + raise ActiveRecord::Rollback unless res.empty? + end + res + end + + def self.import_file(filename) + t = nil + begin + t = File.open(filename, 'r').read + rescue + return false + end + OriginalLicense.import t + end + end diff --git a/app/models/original_picture.rb b/app/models/original_picture.rb index e323eb64..91a67918 100644 --- a/app/models/original_picture.rb +++ b/app/models/original_picture.rb @@ -50,27 +50,27 @@ class OriginalPicture < ActiveRecord::Base end def self.list_opt - {:include => [:artist, :license, :resource_picture]} + {:include => [:license, :resource_picture]} end def self.list_json_opt - {:include => [:resource_picture, :artist, :license]} + {:include => [:license, :resource_picture]} end - def self.show cid, author, opt = {} + def self.show cid, artist, opt = {} pic = OriginalPicture.find(cid, :include => self.show_include_opt(opt)) - raise ActiveRecord::Forbidden unless pic.own?(author) + raise ActiveRecord::Forbidden unless pic.own?(artist) pic end def self.show_include_opt opt = {} - res = [:license] + res = [:license, :resource_picture] res.push(opt[:include]) if opt[:include] res end def self.show_json_include_opt - {:include => :license} + {:include => [:license, :resource_picture]} end def destroy_with_file @@ -136,10 +136,9 @@ class OriginalPicture < ActiveRecord::Base PictureIO.original_picture_io.get self.filename, subdir end - def own? author - return false unless author - return false unless author.artist? - self.artist_id == author.artist.id + def own? art + return false unless art + self.artist_id == art.id end end diff --git a/app/models/panel.rb b/app/models/panel.rb index c383e1a2..2d7de5b8 100644 --- a/app/models/panel.rb +++ b/app/models/panel.rb @@ -1,13 +1,42 @@ class Panel < ActiveRecord::Base belongs_to :comic belongs_to :author + belongs_to :resource_picture +# belongs_to :background_picture, :class_name => 'ResourcePicture' has_many :panel_pictures, :dependent => :destroy has_many :balloons, :dependent => :destroy accepts_nested_attributes_for :panel_pictures, :allow_destroy => true accepts_nested_attributes_for :balloons, :allow_destroy => true + validates :comic_id, :presence => true, :numericality => true, :existence => true, :uniqueness => {:scope => :t} + validates :resource_picture_id, :numericality => {:allow_blank => true} + validates :width, :presence => true, :numericality => true, :natural_number => true + validates :height, :presence => true, :numericality => true, :natural_number => true + validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0} + validates :x, :numericality => {:allow_blank => true} + validates :y, :numericality => {:allow_blank => true} + validates :z, :numericality => {:allow_blank => true, :greater_than => 0} + validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0} + validates :author_id, :presence => true, :numericality => true, :existence => true + + def supply_default au + c = self.comic_id ? Comic.show(self.comic_id, au) : nil + if c + self.width = c.width if self.width.blank? + self.height = c.height if self.height.blank? + self.t = Panel.new_t(c.id) if self.t.blank? and self.new_record? + end + self.border = 0 if self.border.blank? + self.author_id = au.id + end + + def self.new_t comic_id + r = Panel.max_t(comic_id) + r.blank? ? 0 : r.to_i + 1 + end + def self.max_t comic_id - Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id]).to_i + Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id]) end def self.find_t comic_id, t @@ -18,12 +47,11 @@ class Panel < ActiveRecord::Base def vdt_save f = nil - max_t = Panel.max_t self.comic_id f = Panel.find_t(self.comic_id, self.t) if self.t if f Panel.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t]) else - self.t = max_t + 1 + self.t = Panel.new_t self.comic_id end self.save end @@ -34,7 +62,7 @@ class Panel < ActiveRecord::Base Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t]) else nf = Panel.find_t(self.comic_id, new_t) - max_t = Panel.max_t self.comic_id + max_t = Panel.max_t.to_i self.comic_id new_t = max_t if new_t > max_t Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t]) end @@ -47,6 +75,99 @@ class Panel < ActiveRecord::Base self.destroy end + def self.default_page_size + 25 + end + + def self.max_page_size + 100 + end + + def self.page prm = nil + page = prm.to_i + page = 1 if page < 1 + page + end + + def self.page_size prm = self.default_page_size + page_size = prm.to_i + page_size = self.max_page_size if page_size > self.max_page_size + page_size = self.default_page_size if page_size < 1 + page_size + end + + def self.offset cnt, prm = nil + offset = prm.to_i + offset = cnt - 1 if offset >= cnt + offset = cnt - offset.abs if offset < 0 + offset = 0 if offset < 0 + offset + end + + def self.list opt = {}, page = 1, page_size = self.default_page_size + opt.merge!(self.list_opt) unless opt[:include] + opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size}) + Panel.find(:all, opt) + end + + def self.list_opt + {:include => { + :comic => :author, + :panel_pictures => { + :resource_picture => {:artist => {}, :license => {}} + }, + :balloons => {:speaches => {}}, + :author => {} + }} + end + + def self.list_json_opt + {:include => { + :comic => :author, + :panel_pictures => { + :resource_picture => {:artist => {}, :license => {}} + }, + :balloons => {:speaches => {}}, + :author => {} + }} + end + + def self.show rid, au, opt = {} + r = Panel.find(rid, :include => self.show_include_opt(opt)) + raise ActiveRecord::Forbidden unless r.visible?(au) + r + end + + def self.show_include_opt opt = {} + res = { + :comic => :author, + :panel_pictures => { + :resource_picture => {:artist => {}, :license => {}} + }, + :balloons => {:speaches => {}}, + :author => {} + } + res.merge!(opt[:include]) if opt[:include] + res + end + + def self.show_json_include_opt + {:include => { + :comic => :author, + :panel_pictures => { + :resource_picture => {:artist => {}, :license => {}} + }, + :balloons => {:speaches => {}}, + :author => {} + }} + end + + def visible? au + return false unless au + return false unless self.comic + self.comic.visible?(au) or self.author_id == au.id + end + def own? author return false unless author self.author_id == author.id diff --git a/app/models/resource_picture.rb b/app/models/resource_picture.rb index 68ce690a..7d320923 100644 --- a/app/models/resource_picture.rb +++ b/app/models/resource_picture.rb @@ -171,11 +171,11 @@ class ResourcePicture < ActiveRecord::Base end def self.list_opt - {:include => [:artist, :license]} + {:include => [:license, :artist]} end def self.list_json_opt - {:include => [:artist, :license]} + {:include => [:license, :artist]} end def self.show rid, opt = {} @@ -185,13 +185,13 @@ class ResourcePicture < ActiveRecord::Base end def self.show_include_opt opt = {} - res = [:artist, :license] + res = [:license, :artist] res.push(opt[:include]) if opt[:include] res end def self.show_json_include_opt - {:include => [:artist, :license]} + {:include => [:license, :artist]} end def self.visible_count diff --git a/app/models/speach_balloon_template.rb b/app/models/speach_balloon_template.rb new file mode 100644 index 00000000..3af508ba --- /dev/null +++ b/app/models/speach_balloon_template.rb @@ -0,0 +1,12 @@ +class SpeachBalloonTemplate < ActiveRecord::Base + has_many :balloon_templates, :dependent => :destroy + has_many :speach_templates, :dependent => :destroy + accepts_nested_attributes_for :balloon_templates + accepts_nested_attributes_for :speach_templates + before_create :supply_t + + def supply_t + self.t = SpeachBalloon.maximum(:t).to_i + 1 + end + +end diff --git a/app/views/common_licenses/list.html.erb b/app/views/common_licenses/list.html.erb index 607c09e8..550a40d5 100644 --- a/app/views/common_licenses/list.html.erb +++ b/app/views/common_licenses/list.html.erb @@ -44,3 +44,7 @@
<%= link_to 'New Common_license', new_common_license_path %> + <%= form_tag( {:action => "import"} , { :multipart => true }) do %> + <%= file_field_tag "file" %> + <%= submit_tag 'upload' -%> + <% end -%> diff --git a/app/views/common_licenses/result.html.erb b/app/views/common_licenses/result.html.erb new file mode 100644 index 00000000..4e27dac2 --- /dev/null +++ b/app/views/common_licenses/result.html.erb @@ -0,0 +1,2 @@ +<% @errors.each do |common_license| %> +<% end %> diff --git a/app/views/original_licenses/edit.js.erb b/app/views/original_licenses/edit.js.erb new file mode 100644 index 00000000..ac928fd8 --- /dev/null +++ b/app/views/original_licenses/edit.js.erb @@ -0,0 +1 @@ +$("#uploader").html("<%= escape_javascript(render('ol')) -%>"); diff --git a/app/views/original_licenses/new.js.erb b/app/views/original_licenses/new.js.erb new file mode 100644 index 00000000..ac928fd8 --- /dev/null +++ b/app/views/original_licenses/new.js.erb @@ -0,0 +1 @@ +$("#uploader").html("<%= escape_javascript(render('ol')) -%>"); diff --git a/app/views/speach_balloon_templates/_form.html.erb b/app/views/speach_balloon_templates/_form.html.erb new file mode 100644 index 00000000..3a61fdb1 --- /dev/null +++ b/app/views/speach_balloon_templates/_form.html.erb @@ -0,0 +1,17 @@ +<%= form_for(@speach_balloon_template) do |f| %> + <% if @speach_balloon_template.errors.any? %> +
+

<%= pluralize(@speach_balloon_template.errors.count, "error") %> prohibited this speach_balloon_template from being saved:

+ + +
+ <% end %> + +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/speach_balloon_templates/browse.html.erb b/app/views/speach_balloon_templates/browse.html.erb new file mode 100644 index 00000000..f6e2392c --- /dev/null +++ b/app/views/speach_balloon_templates/browse.html.erb @@ -0,0 +1,31 @@ +

<%= notice %>

+ +

+ Name: + <%= h @speach_balloon.name %> +

+ +

+ Tail count: + <%= @speach_balloon.tail_count %> +

+ +

+ size count: + <%= @speach_balloon.size_count %> +

+ +

+ t: + <%= @speach_balloon.t %> +

+<% @speach_balloon.balloon_templates.each do |balloon_template| -%> +
+

+ Tail:<%= balloon_template.tail -%> + Size:<%= balloon_template.size -%> +

+ +
+<% end -%> +<%= link_to 'Back', :action => :list %> diff --git a/app/views/speach_balloon_templates/index.html.erb b/app/views/speach_balloon_templates/index.html.erb new file mode 100644 index 00000000..d9aa256f --- /dev/null +++ b/app/views/speach_balloon_templates/index.html.erb @@ -0,0 +1,26 @@ +

Listing speach_balloons

+ + + + + + + + + + +<% @speach_balloons.each do |speach_balloon| %> + + + + + + + + +<% end %> +
NameTail countsize countt
<%= h speach_balloon.name %><%= speach_balloon.tail_count %><%= speach_balloon.size_count %><%= speach_balloon.t %><%= link_to 'show', speach_balloon %> + <% if admin_signed_in? -%> + <%= link_to 'Destroy', speach_balloon, confirm: 'Are you sure?', method: :delete %> + <% end -%> +
diff --git a/app/views/speach_balloon_templates/list.html.erb b/app/views/speach_balloon_templates/list.html.erb new file mode 100644 index 00000000..d3e2e480 --- /dev/null +++ b/app/views/speach_balloon_templates/list.html.erb @@ -0,0 +1,28 @@ +

Listing speach_balloons

+ + + + + + + + + + + + +<% @speach_balloons.each do |speach_balloon| %> + + + + + + + + + + +<% end %> +
idNameTail countsize counttcreated_atupdated_at
<%= link_to speach_balloon.id, :action => :browse, :id => speach_balloon.id %><%= h speach_balloon.name %><%= speach_balloon.tail_count %><%= speach_balloon.size_count %><%= speach_balloon.t %><%= speach_balloon.created_at %><%= speach_balloon.updated_at %> + <%= link_to 'Destroy', speach_balloon, confirm: 'Are you sure?', method: :delete %> +
diff --git a/app/views/speach_balloon_templates/show.html.erb b/app/views/speach_balloon_templates/show.html.erb new file mode 100644 index 00000000..12b2e8ea --- /dev/null +++ b/app/views/speach_balloon_templates/show.html.erb @@ -0,0 +1,31 @@ +

<%= notice %>

+ +

+ Name: + <%= h @speach_balloon.name %> +

+ +

+ Tail count: + <%= @speach_balloon.tail_count %> +

+ +

+ size count: + <%= @speach_balloon.size_count %> +

+ +

+ t: + <%= @speach_balloon.t %> +

+<% @speach_balloon.balloon_templates.each do |balloon_template| -%> +
+

+ Tail:<%= balloon_template.tail -%> + Size:<%= balloon_template.size -%> +

+ +
+<% end -%> +<%= link_to 'Back', speach_balloons_path %> diff --git a/config/environment.rb b/config/environment.rb index 904ba8d2..a32bd2f8 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,5 +1,10 @@ # Load the rails application require File.expand_path('../application', __FILE__) +begin + require 'RMagick' +rescue LoadError + require 'DMagick' +end # Initialize the rails application Pettanr::Application.initialize! diff --git a/config/environments/development.rb b/config/environments/development.rb index 1f7eb0e5..6820832d 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,5 +1,3 @@ -require 'DMagick' - Pettanr::Application.configure do # Settings specified here will take precedence over those in config/application.rb @@ -37,3 +35,10 @@ PictureIO.setup do |config| config.resource_picture_io = PictureIO::LocalPicture.new '/sites/resource/pettanr/' config.system_picture_io = PictureIO::LocalPicture.new '/sites/system/pettanr/' end +module Pettanr + if File.exist? Rails.root + 'config/test_layout' + TestLayout = 'test' + else + TestLayout = false + end +end diff --git a/config/environments/production.rb b/config/environments/production.rb index a063e948..bce780b3 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -1,5 +1,3 @@ -#require 'RMagick' -require 'DMagick' Pettanr::Application.configure do # Settings specified here will take precedence over those in config/application.rb diff --git a/config/environments/test.rb b/config/environments/test.rb index ce3efa42..d50c3847 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -1,5 +1,3 @@ -require 'DMagick' - Pettanr::Application.configure do # Settings specified here will take precedence over those in config/application.rb @@ -46,3 +44,10 @@ PictureIO.setup do |config| config.resource_picture_io = PictureIO::LocalPicture.new '/sites/resource/pettanr/' config.system_picture_io = PictureIO::LocalPicture.new '/sites/system/pettanr/' end +module Pettanr + if File.exist? Rails.root + 'config/test_layout' + TestLayout = 'test' + else + TestLayout = false + end +end diff --git a/config/routes.rb b/config/routes.rb index 76eded74..298471fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,7 +33,7 @@ Pettanr::Application.routes.draw do delete :destroy end end - resources :speaches do + resources :speach_balloon_templates do collection do get :index get :show @@ -41,9 +41,10 @@ Pettanr::Application.routes.draw do get :browse end member do + delete :destroy end end - resources :balloons do + resources :speach_templates do collection do get :index get :show @@ -53,7 +54,7 @@ Pettanr::Application.routes.draw do member do end end - resources :speach_templates do + resources :balloon_templates do collection do get :index get :show @@ -63,10 +64,16 @@ Pettanr::Application.routes.draw do member do end end - resources :balloon_templates do + resources :speaches do + collection do + get :list + get :browse + end + member do + end + end + resources :balloons do collection do - get :index - get :show get :list get :browse end @@ -75,15 +82,10 @@ Pettanr::Application.routes.draw do end resources :speach_balloons do collection do - get :index - get :show - post :create get :list get :browse end member do - put :update - delete :destroy end end resources :panel_pictures do @@ -177,7 +179,7 @@ Pettanr::Application.routes.draw do collection do get :index get :show - post :create + post :import get :list get :browse end diff --git a/config/test_layout b/config/test_layout new file mode 100644 index 00000000..e69de29b diff --git a/db/migrate/20111206105022_create_original_licenses.rb b/db/migrate/20111206105022_create_original_licenses.rb index a74aef23..3003d490 100644 --- a/db/migrate/20111206105022_create_original_licenses.rb +++ b/db/migrate/20111206105022_create_original_licenses.rb @@ -1,18 +1,17 @@ class CreateOriginalLicenses < ActiveRecord::Migration def change create_table :original_licenses do |t| - t.integer :artist_id, :null => false t.integer :license_id, :null => false - t.string :name, :null => false - t.string :url, :null => false - t.integer :cc_by, :null => false, :default => 0 - t.integer :cc_sa, :null => false, :default => 0 - t.integer :cc_nd, :null => false, :default => 0 - t.integer :cc_nc, :null => false, :default => 0 - t.integer :no_resize, :null => false, :default => 0 - t.integer :no_flip, :null => false, :default => 0 - t.integer :no_convert, :null => false, :default => 0 - t.integer :keep_aspect_ratio, :null => false, :default => 0 + t.string :name, :null => false + t.string :url, :null => false + t.integer :cc_by, :null => false, :default => 0 + t.integer :cc_sa, :null => false, :default => 0 + t.integer :cc_nd, :null => false, :default => 0 + t.integer :cc_nc, :null => false, :default => 0 + t.integer :no_resize, :null => false, :default => 0 + t.integer :no_flip, :null => false, :default => 0 + t.integer :no_convert, :null => false, :default => 0 + t.integer :keep_aspect_ratio, :null => false, :default => 0 t.timestamps end diff --git a/db/migrate/20111206111202_create_panels.rb b/db/migrate/20111206111202_create_panels.rb index d277f915..bcf7b3ee 100644 --- a/db/migrate/20111206111202_create_panels.rb +++ b/db/migrate/20111206111202_create_panels.rb @@ -6,11 +6,11 @@ class CreatePanels < ActiveRecord::Migration t.integer :width, :null => false, :default => 200 t.integer :height, :null => false, :default => 80 t.integer :border, :null => false, :default => 1 - t.integer :x, :null => false - t.integer :y, :null => false - t.integer :z, :null => false - t.integer :t, :null => false, :default => 1 - t.integer :author_id + t.integer :x + t.integer :y + t.integer :z + t.integer :t, :null => false + t.integer :author_id, :null => false t.timestamps end diff --git a/db/migrate/20120424100555_create_speach_balloon_templates.rb b/db/migrate/20120424100555_create_speach_balloon_templates.rb new file mode 100644 index 00000000..9071ee24 --- /dev/null +++ b/db/migrate/20120424100555_create_speach_balloon_templates.rb @@ -0,0 +1,8 @@ +class CreateSpeachBalloonTemplates < ActiveRecord::Migration + def change + create_table :speach_balloon_templates do |t| + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 6d51ff26..0f629490 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120306231650) do +ActiveRecord::Schema.define(:version => 20120424100555) do create_table "admins", :force => true do |t| t.string "email", :default => "", :null => false @@ -141,7 +141,6 @@ ActiveRecord::Schema.define(:version => 20120306231650) do add_index "licenses", ["url"], :name => "index_licenses_on_url", :unique => true create_table "original_licenses", :force => true do |t| - t.integer "artist_id", :null => false t.integer "license_id", :null => false t.string "name", :null => false t.string "url", :null => false @@ -192,11 +191,11 @@ ActiveRecord::Schema.define(:version => 20120306231650) do t.integer "width", :default => 200, :null => false t.integer "height", :default => 80, :null => false t.integer "border", :default => 1, :null => false - t.integer "x", :null => false - t.integer "y", :null => false - t.integer "z", :null => false - t.integer "t", :default => 1, :null => false - t.integer "author_id" + t.integer "x" + t.integer "y" + t.integer "z" + t.integer "t", :null => false + t.integer "author_id", :null => false t.datetime "created_at" t.datetime "updated_at" end @@ -216,6 +215,11 @@ ActiveRecord::Schema.define(:version => 20120306231650) do t.datetime "updated_at" end + create_table "speach_balloon_templates", :force => true do |t| + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "speach_balloons", :force => true do |t| t.string "name", :null => false t.integer "tail_count", :default => 1, :null => false diff --git a/spec/controllers/balloon_templates_controller_spec.rb b/spec/controllers/balloon_templates_controller_spec.rb index 3659cbff..930a3e54 100644 --- a/spec/controllers/balloon_templates_controller_spec.rb +++ b/spec/controllers/balloon_templates_controller_spec.rb @@ -3,11 +3,5 @@ require 'spec_helper' describe BalloonTemplatesController do - describe "GET index" do - it "assigns all baloon_templates as @baloon_templates" do - baloon_template = BaloonTemplate.create! valid_attributes - get :index - assigns(:baloon_templates).should eq([baloon_template]) - end - end + #データはすべてSpeachBalloonsControllerから管理するので、ここから何かを要求することはない。 end diff --git a/spec/controllers/common_licenses_controller_spec.rb b/spec/controllers/common_licenses_controller_spec.rb index 57f7bdaf..5dcdf254 100644 --- a/spec/controllers/common_licenses_controller_spec.rb +++ b/spec/controllers/common_licenses_controller_spec.rb @@ -1,157 +1,338 @@ +# -*- encoding: utf-8 -*- +#コモンライセンス require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe CommonLicensesController do - - # This should return the minimal set of attributes required to create a valid - # CommonLicense. As you add validations to CommonLicense, be sure to - # update the return value of this method accordingly. - def valid_attributes - {} + before do + @admin = Factory :admin + @lc = Factory :license + @cl = Factory :common_license, :license_id => @lc.id + @user = Factory( :user_yas) + @author = @user.author + @artist = Factory :artist_yas, :author_id => @author.id end - - describe "GET index" do - it "assigns all common_licenses as @common_licenses" do - common_license = CommonLicense.create! valid_attributes - get :index - assigns(:common_licenses).should eq([common_license]) +=begin + describe '一覧表示に於いて' do + before do + sign_in @admin + sign_in @user + CommonLicense.stub(:list).and_return([@cl, @cl, @cl]) end - end - - describe "GET show" do - it "assigns the requested common_license as @common_license" do - common_license = CommonLicense.create! valid_attributes - get :show, :id => common_license.id - assigns(:common_license).should eq(common_license) + context 'パラメータpageについて' do + it '@pageに値が入る' do + get :index, :page => 5 + assigns(:page).should eq 5 + end + it '省略されると@pageに1値が入る' do + get :index + assigns(:page).should eq 1 + end + it '与えられたpage_sizeがセットされている' do + get :index, :page_size => 15 + assigns(:page_size).should eq 15 + end + it '省略されると@page_sizeにデフォルト値が入る' do + get :index + assigns(:page_size).should eq CommonLicense.default_page_size + end + it '最大を超えると@page_sizeにデフォルト最大値が入る' do + get :index, :page_size => 1500 + assigns(:page_size).should eq CommonLicense.max_page_size + end + it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do + get :index, :page_size => 0 + assigns(:page_size).should eq CommonLicense.default_page_size + end end - end - - describe "GET new" do - it "assigns a new common_license as @common_license" do - get :new - assigns(:common_license).should be_a_new(CommonLicense) + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + it 'コモンライセンスモデルに一覧を問い合わせている' do + CommonLicense.should_receive(:list).exactly(1) + get :index + end + it '@common_licensesにリストを取得している' do + get :index + assigns(:common_licenses).should have_at_least(3).items + end + context 'html形式' do + it 'indexテンプレートを描画する' do + get :index + response.should render_template("index") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :index, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがリスト構造になっている' do + get :index, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + it 'リストの先頭くらいはコモンライセンスっぽいものであって欲しい' do + get :index, :format => :json + json = JSON.parse response.body + json.first.has_key?("url").should be_true + end + end end - end - - describe "GET edit" do - it "assigns the requested common_license as @common_license" do - common_license = CommonLicense.create! valid_attributes - get :edit, :id => common_license.id - assigns(:common_license).should eq(common_license) + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :index + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :index + response.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :index, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :index, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '管理者権限がないとき' do + before do + sign_out @admin + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :index, :format => :json + response.should be_success + end + end end end - - describe "POST create" do - describe "with valid params" do - it "creates a new CommonLicense" do - expect { - post :create, :common_license => valid_attributes - }.to change(CommonLicense, :count).by(1) + + describe '単体表示に於いて' do + before do + sign_in @admin + sign_in @user + CommonLicense.stub(:show).and_return(@cl) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @cl.id + response.should be_success end - - it "assigns a newly created common_license as @common_license" do - post :create, :common_license => valid_attributes - assigns(:common_license).should be_a(CommonLicense) - assigns(:common_license).should be_persisted + it 'コモンライセンスモデルに単体取得を問い合わせている' do + CommonLicense.should_receive(:show).exactly(1) + get :show end - - it "redirects to the created common_license" do - post :create, :common_license => valid_attributes - response.should redirect_to(CommonLicense.last) + it '@common_licenseにアレを取得している' do + get :show, :id => @cl.id + assigns(:common_license).id.should eq(@cl.id) + end + context 'html形式' do + it 'showテンプレートを描画する' do + get :show, :id => @cl.id + response.should render_template("show") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :id => @cl.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + get :show, :id => @cl.id, :format => :json + json = JSON.parse response.body + json["name"].should match(/peta/) + end end end - - describe "with invalid params" do - it "assigns a newly created but unsaved common_license as @common_license" do - # Trigger the behavior that occurs when invalid params are submitted - CommonLicense.any_instance.stub(:save).and_return(false) - post :create, :common_license => {} - assigns(:common_license).should be_a_new(CommonLicense) + context '作家権限がないとき' do + before do + sign_out @user end - - it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted - CommonLicense.any_instance.stub(:save).and_return(false) - post :create, :common_license => {} - response.should render_template("new") + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :show, :id => @cl.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :show, :id => @cl.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :show, :id => @cl.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :show, :id => @cl.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '管理者権限がないとき' do + before do + sign_out @admin + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @cl.id + response.should be_success + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @cl.id, :format => :json + response.should be_success + end end end end - - describe "PUT update" do - describe "with valid params" do - it "updates the requested common_license" do - common_license = CommonLicense.create! valid_attributes - # Assuming there are no other common_licenses in the database, this - # specifies that the CommonLicense created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. - CommonLicense.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) - put :update, :id => common_license.id, :common_license => {'these' => 'params'} +=end + + describe 'インポートに於いて' do + before do + sign_in @admin + sign_in @user + #テストデータを用意してね + @f = Rails.root + 'spec/json/common_license.json' + @t = File.open(@f, 'r').read + @j = JSON.parse @t + @fs = Rails.root + 'spec/json/common_licenses.json' + @ts = File.open(@fs, 'r').read + @js = JSON.parse @ts + @fes = Rails.root + 'spec/json/invalid_common_licenses.json' + @tes = File.open(@fes, 'r').read + @jes = JSON.parse @tes + end + context '事前チェックしておく' do + before do + #異常な行を返すから、正常の意味で空を返す + CommonLicense.stub(:import).with(any_args()).and_return([]) end - - it "assigns the requested common_license as @common_license" do - common_license = CommonLicense.create! valid_attributes - put :update, :id => common_license.id, :common_license => valid_attributes - assigns(:common_license).should eq(common_license) + it "@dataに渡したデータを保持している" do + post :import, :file => @t + assigns(:data).should_not be_nil end - - it "redirects to the common_license" do - common_license = CommonLicense.create! valid_attributes - put :update, :id => common_license.id, :common_license => valid_attributes - response.should redirect_to(common_license) + it 'モデルにインポート依頼する' do + CommonLicense.should_receive(:import).with(any_args()).exactly(1) + post :import, :file => @t + end + it "@errorsに結果を保持している" do + post :import, :file => @t + assigns(:errors).should eq [] end end - - describe "with invalid params" do - it "assigns the common_license as @common_license" do - common_license = CommonLicense.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - CommonLicense.any_instance.stub(:save).and_return(false) - put :update, :id => common_license.id, :common_license => {} - assigns(:common_license).should eq(common_license) + context 'つつがなく終わるとき' do + before do end - - it "re-renders the 'edit' template" do - common_license = CommonLicense.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - CommonLicense.any_instance.stub(:save).and_return(false) - put :update, :id => common_license.id, :common_license => {} - response.should render_template("edit") + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :import, :file => @t + response.status.should eq 302 + end + it '管理者向けコモンライセンス一覧ページへ遷移する' do + post :import, :file => @t + response.should redirect_to('/common_licenses/list') + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + post :import, :file => @t, :format => :json + response.should be_success + end end end - end - - describe "DELETE destroy" do - it "destroys the requested common_license" do - common_license = CommonLicense.create! valid_attributes - expect { - delete :destroy, :id => common_license.id - }.to change(CommonLicense, :count).by(-1) + context '管理者権限がないとき' do + before do + sign_out @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :import, :file => @t + response.status.should eq 302 + end + it '管理者サインインページへ遷移する' do + post :import, :file => @t + response.body.should redirect_to '/admins/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :import, :file => @t, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :import, :file => @t, :format => :json + response.message.should match(/Unauthorized/) + end + end end - - it "redirects to the common_licenses list" do - common_license = CommonLicense.create! valid_attributes - delete :destroy, :id => common_license.id - response.should redirect_to(common_licenses_url) + context '作家権限がないとき' do + #必要なのは管理者権限であって作家権限ではない。成功を見届ける + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :import, :file => @t + response.status.should eq 302 + end + it '管理者向けコモンライセンス一覧ページへ遷移する' do + post :import, :file => @t + response.should redirect_to('/common_licenses/list') + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + post :import, :file => @t, :format => :json + response.should be_success + end + end + end + context '検証、保存に失敗した' do + before do + #異常な行を返す + CommonLicense.stub(:import).with(any_args()).and_return( + [CommonLicense.new(Factory.attributes_for(:common_license))] + ) + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + post :import, :file => @t + response.status.should eq 200 + end + it 'resultページを描画する' do + post :import, :file => @t + response.should render_template("result") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + post :import, :file => @t, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + post :import, :file => @t, :format => :json + response.message.should match(/Unprocessable/) + end + end end end - end diff --git a/spec/controllers/licenses_controller_spec.rb b/spec/controllers/licenses_controller_spec.rb index ca1249c3..9b3c5cdf 100644 --- a/spec/controllers/licenses_controller_spec.rb +++ b/spec/controllers/licenses_controller_spec.rb @@ -4,19 +4,137 @@ require 'spec_helper' describe LicensesController do before do - Factory :admin + @admin = Factory :admin + @lc = Factory :license + @cl = Factory :common_license, :license_id => @lc.id @user = Factory( :user_yas) @author = @user.author @artist = Factory :artist_yas, :author_id => @author.id end - describe 'システム初期化要請に於いて' do + describe '一覧表示に於いて' do before do - @op = Factory :original_picture, :artist_id => @artist.id sign_in @user - OriginalPicture.stub(:list).and_return([@op, @op, @op]) + License.stub(:list).and_return([@lc, @lc, @lc]) end context 'パラメータpageについて' do + it '@pageに値が入る' do + get :index, :page => 5 + assigns(:page).should eq 5 + end + it '省略されると@pageに1値が入る' do + get :index + assigns(:page).should eq 1 + end + it '与えられたpage_sizeがセットされている' do + get :index, :page_size => 15 + assigns(:page_size).should eq 15 + end + it '省略されると@page_sizeにデフォルト値が入る' do + get :index + assigns(:page_size).should eq License.default_page_size + end + it '最大を超えると@page_sizeにデフォルト最大値が入る' do + get :index, :page_size => 1500 + assigns(:page_size).should eq License.max_page_size + end + it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do + get :index, :page_size => 0 + assigns(:page_size).should eq License.default_page_size + end + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + it 'ライセンスモデルに一覧を問い合わせている' do + License.should_receive(:list).exactly(1) + get :index + end + it '@licensesにリストを取得している' do + get :index + assigns(:licenses).should have_at_least(3).items + end + context 'html形式' do + it 'indexテンプレートを描画する' do + get :index + response.should render_template("index") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :index, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがリスト構造になっている' do + get :index, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + it 'リストの先頭くらいはライセンスっぽいものであって欲しい' do + get :index, :format => :json + json = JSON.parse response.body + json.first.has_key?("url").should be_true + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + it 'ステータスコード200 okを返す' do + get :index + response.status.should eq 200 + end + end + end + + describe '単体表示に於いて' do + before do + sign_in @user + License.stub(:show).and_return(@lc) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @lc.id + response.should be_success + end + it 'ライセンスモデルに単体取得を問い合わせている' do + License.should_receive(:show).exactly(1) + get :show + end + it '@licenseにアレを取得している' do + get :show, :id => @lc.id + assigns(:license).id.should eq(@lc.id) + end + context 'html形式' do + it 'showテンプレートを描画する' do + get :show, :id => @lc.id + response.should render_template("show") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :id => @lc.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + get :show, :id => @lc.id, :format => :json + json = JSON.parse response.body + json["name"].should match(/peta/) + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + it 'ステータスコード200 okを返す' do + get :show, :id => @lc.id + response.status.should eq 200 + end end end + end diff --git a/spec/controllers/original_licenses_controller_spec.rb b/spec/controllers/original_licenses_controller_spec.rb index d4eae4b3..b6f01ebb 100644 --- a/spec/controllers/original_licenses_controller_spec.rb +++ b/spec/controllers/original_licenses_controller_spec.rb @@ -3,12 +3,459 @@ require 'spec_helper' describe OriginalLicensesController do + before do + @admin = Factory :admin + @lc = Factory :license + @user = Factory( :user_yas) + @author = @user.author + @artist = Factory :artist_yas, :author_id => @author.id + end + describe '一覧表示に於いて' do + before do + sign_in @user + @ol = Factory :original_license, :license_id => @lc.id + OriginalLicense.stub(:list).and_return([@ol, @ol, @ol]) + end + context 'パラメータpageについて' do + it '@pageに値が入る' do + get :index, :page => 5 + assigns(:page).should eq 5 + end + it '省略されると@pageに1値が入る' do + get :index + assigns(:page).should eq 1 + end + it '与えられたpage_sizeがセットされている' do + get :index, :page_size => 15 + assigns(:page_size).should eq 15 + end + it '省略されると@page_sizeにデフォルト値が入る' do + get :index + assigns(:page_size).should eq OriginalLicense.default_page_size + end + it '最大を超えると@page_sizeにデフォルト最大値が入る' do + get :index, :page_size => 1500 + assigns(:page_size).should eq OriginalLicense.max_page_size + end + it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do + get :index, :page_size => 0 + assigns(:page_size).should eq OriginalLicense.default_page_size + end + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + it 'ライセンスモデルに一覧を問い合わせている' do + OriginalLicense.should_receive(:list).exactly(1) + get :index + end + it '@original_licensesにリストを取得している' do + get :index + assigns(:original_licenses).should have_at_least(3).items + end + context 'html形式' do + it 'indexテンプレートを描画する' do + get :index + response.should render_template("index") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :index, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがリスト構造になっている' do + get :index, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + it 'リストの先頭くらいはライセンスっぽいものであって欲しい' do + get :index, :format => :json + json = JSON.parse response.body + json.first.has_key?("url").should be_true + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + it 'ステータスコード200 okを返す' do + get :index + response.status.should eq 200 + end + end + end + + describe '単体表示に於いて' do + before do + sign_in @user + @ol = Factory :original_license, :license_id => @lc.id + OriginalLicense.stub(:show).and_return(@ol) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @ol.id + response.should be_success + end + it 'ライセンスモデルに単体取得を問い合わせている' do + OriginalLicense.should_receive(:show).exactly(1) + get :show + end + it '@original_licenseにアレを取得している' do + get :show, :id => @ol.id + assigns(:original_license).id.should eq(@ol.id) + end + context 'html形式' do + it 'showテンプレートを描画する' do + get :show, :id => @ol.id + response.should render_template("show") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :id => @ol.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + get :show, :id => @ol.id, :format => :json + json = JSON.parse response.body + json["name"].should match(/peta/) + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + it 'ステータスコード200 okを返す' do + get :show, :id => @ol.id + response.status.should eq 200 + end + end + end + + describe '新規作成フォーム表示に於いて' do + before do + sign_in @admin + sign_in @user + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :new + response.should be_success + end + it '@original_licenseに新規データを用意している' do + get :new + assigns(:original_license).should be_a_new(OriginalLicense) + end + it 'オリジナルライセンスモデルにデフォルト値補充を依頼している' do + OriginalLicense.any_instance.should_receive(:supply_default).exactly(1) + get :new + end + context 'html形式' do + it 'newテンプレートを描画する' do + get :new + response.should render_template("new") + end + end + context 'js形式' do + it 'new.jsテンプレートを描画する' do + get :new, :format => :js + response.should render_template("new") + end + end + end + context '管理者権限がないとき' do + before do + sign_out @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :new + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :new + response.body.should redirect_to '/admins/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :new, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :new, :format => :js + response.message.should match(/Unauthorized/) + end + end + end + end - describe "GET index" do - it "assigns all original_licenses as @original_licenses" do - original_license = OriginalLicense.create! valid_attributes - get :index - assigns(:original_licenses).should eq([original_license]) + describe '新規作成に於いて' do + before do + sign_in @admin + sign_in @user + end + context 'つつがなく終わるとき' do + it 'モデルに保存依頼する' do + OriginalLicense.any_instance.should_receive(:store).exactly(1) + post :create, :original_license => Factory.attributes_for(:original_license) + end + it "@original_licenseに作成されたオリジナルライセンスを保持していて、それがDBにある" do + post :create, :original_license => Factory.attributes_for(:original_license) + assigns(:original_license).should be_a(OriginalLicense) + assigns(:original_license).should be_persisted + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + OriginalLicense.any_instance.stub(:store).and_return(true) + post :create, :original_license => Factory.attributes_for(:original_license) + response.status.should eq 302 + end + it '作成されたオリジナルライセンスの表示ページへ遷移する' do +# OriginalLicense.any_instance.stub(:save).and_return(true) + post :create, :original_license => Factory.attributes_for(:original_license) + response.should redirect_to(OriginalLicense.last) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do +# OriginalLicense.any_instance.stub(:save).and_return(true) + post :create, :original_license => Factory.attributes_for(:original_license), :format => :json + response.should be_success + end + it '作成されたオリジナルライセンスをjsonデータで返す' do + post :create, :original_license => Factory.attributes_for(:original_license), :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + post :create, :original_license => Factory.attributes_for(:original_license), :format => :json + json = JSON.parse response.body + json["name"].should match(/peta/) + end + end + end + context '管理者権限がないとき' do + before do + sign_out @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :create, :original_license => Factory.attributes_for(:original_license) + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + post :create, :original_license => Factory.attributes_for(:original_license) + response.body.should redirect_to '/admins/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :create, :original_license => Factory.attributes_for(:original_license), :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :create, :original_license => Factory.attributes_for(:original_license), :format => :json + response.message.should match(/Unauthorized/) + end + end end + context '検証、保存に失敗した' do + before do + OriginalLicense.any_instance.stub(:save).and_return(false) + end + it "未保存のオリジナルライセンスを保持している" do + post :create, :original_license => {} + assigns(:original_license).should be_a_new(OriginalLicense) + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + post :create, :original_license => {} + response.status.should eq 200 + end + it '新規ページを描画する' do + post :create, :original_license => {} + response.should render_template("new") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + post :create, :original_license => {}, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + post :create, :original_license => {}, :format => :json + response.message.should match(/Unprocessable/) + end + end + end + end + + describe '編集フォーム表示に於いて' do + before do + @ol = Factory :original_license, :license_id => @lc.id + sign_in @admin + sign_in @user + OriginalLicense.stub(:show).and_return(@ol) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :edit, :id => @ol.id + response.should be_success + end + it 'オリジナルライセンスモデルに単体取得を問い合わせている' do + OriginalLicense.should_receive(:show).exactly(1) + get :edit, :id => @ol.id + end + it '@original_licenseにデータを用意している' do + get :edit, :id => @ol.id + assigns(:original_license).should eq @ol + end + context 'html形式' do + it 'editテンプレートを描画する' do + get :edit, :id => @ol.id + response.should render_template("edit") + end + end + context 'js形式' do + it 'edit.jsテンプレートを描画する' do + get :edit, :id => @ol.id, :format => :js + response.should render_template("edit") + end + end + end + context '管理者権限がないとき' do + before do + sign_out @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :edit, :id => @ol.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :edit, :id => @ol.id + response.body.should redirect_to '/admins/sign_in' + end + end + context 'js形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :edit, :id => @ol.id, :format => :js + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :edit, :id => @ol.id, :format => :js + response.message.should match(/Unauthorized/) + end + end + end + end + + describe '更新に於いて' do + before do + @ol = Factory :original_license, :license_id => @lc.id + sign_in @admin + sign_in @user + @attr = Factory.attributes_for(:original_license, :name => 'new lc') + end + context '事前チェックしておく' do + it 'オリジナルライセンスモデルに単体取得を問い合わせている' do + OriginalLicense.stub(:show).with(any_args()).and_return @ol + OriginalLicense.should_receive(:show).exactly(1) + put :update, :id => @ol.id, :original_license => @attr + end + it 'モデルに更新を依頼する' do + OriginalLicense.any_instance.should_receive(:store).with(any_args) + put :update, :id => @ol.id, :original_license => @attr + end + it '@original_licenseにアレを取得している' do + put :update, :id => @ol.id, :original_license => @attr + assigns(:original_license).id.should eq(@ol.id) + end + end + context 'つつがなく終わるとき' do + it '更新される' do + put :update, :id => @ol.id, :original_license => @attr + OriginalLicense.find(@ol.id).name.should eq 'new lc' + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + OriginalLicense.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @ol.id, :original_license => @attr + response.status.should eq 302 + end + it '更新されたオリジナルライセンスの表示ページへ遷移する' do + put :update, :id => @ol.id, :original_license => @attr + response.should redirect_to(@ol) + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + OriginalLicense.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @ol.id, :original_license => @attr, :format => :json + response.should be_success + end + it 'ページ本体は特に返さない' do + OriginalLicense.any_instance.stub(:store).with(any_args()).and_return(true) + put :update, :id => @ol.id, :original_license => @attr, :format => :json + response.body.should match /./ + end + end + end + context '管理者権限がないとき' do + before do + sign_out @admin + end + it 'ステータスコード302 Foundを返す' do + put :update, :id => @ol.id, :original_license => @attr + response.status.should eq 302 + end + context 'html形式' do + it 'サインインページへ遷移する' do + put :update, :id => @ol.id, :original_license => @attr + response.body.should redirect_to '/admins/sign_in' + end + end + context 'json形式' do + it '応答メッセージにUnauthorizedを返す' do + put :update, :id => @ol.id, :original_license => @attr, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '検証、保存に失敗したとき' do + before do + OriginalLicense.any_instance.stub(:store).and_return(false) + end + context 'html形式' do + it 'ステータスコード200 Okを返す' do + put :update, :id => @ol.id, :original_license => @attr + response.status.should eq 200 + end + it '編集ページを描画する' do + put :update, :id => @ol.id, :original_license => @attr + response.should render_template("edit") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + OriginalLicense.any_instance.stub(:store).and_return(false) + put :update, :id => @ol.id, :original_license => @attr, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + put :update, :id => @ol.id, :original_license => @attr, :format => :json + response.message.should match(/Unprocessable/) + end + end + end + end + + describe 'インポートに於いて' do end + end diff --git a/spec/controllers/panels_controller_spec.rb b/spec/controllers/panels_controller_spec.rb index 2a2ae43c..fbc8e267 100644 --- a/spec/controllers/panels_controller_spec.rb +++ b/spec/controllers/panels_controller_spec.rb @@ -7,11 +7,12 @@ describe PanelsController do @license = Factory :license @user = Factory :user_yas @author = @user.author #ユーザ作成時に連動して作成される + @comic = Factory :comic, :author_id => @author.id end describe '一覧表示に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id + @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id Panel.stub(:list).and_return([@panel, @panel, @panel]) sign_in @user end @@ -106,7 +107,7 @@ describe PanelsController do describe '単体表示に於いて' do before do - @panel = Factory :panel, :author_id => @user.author.id + @panel = Factory :panel, :author_id => @user.author.id, :comic_id => @comic.id Panel.stub(:show).and_return(@panel) sign_in @user end @@ -289,7 +290,7 @@ describe PanelsController do describe '新規作成に於いて' do before do - @panel = Factory :normal_panel, :author_id => @user.author.id + @panel = Factory :normal_panel, :author_id => @user.author.id, :comic_id => @comic.id sign_in @user end context '事前チェックする' do @@ -410,7 +411,7 @@ describe PanelsController do describe '編集フォーム表示に於いて' do before do - @panel = Factory :panel, :author_id => @author.id + @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id sign_in @user Panel.stub(:show).and_return(@panel) end @@ -469,7 +470,7 @@ describe PanelsController do describe '更新に於いて' do before do - @panel = Factory :panel, :author => @author.id + @panel = Factory :panel, :author => @author.id, :comic_id => @comic.id sign_in @user end context '事前チェックしておく' do diff --git a/spec/controllers/speach_balloon_templates_controller_spec.rb b/spec/controllers/speach_balloon_templates_controller_spec.rb new file mode 100644 index 00000000..96aac188 --- /dev/null +++ b/spec/controllers/speach_balloon_templates_controller_spec.rb @@ -0,0 +1,157 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe SpeachBalloonTemplatesController do + + # This should return the minimal set of attributes required to create a valid + # SpeachBalloonTemplate. As you add validations to SpeachBalloonTemplate, be sure to + # update the return value of this method accordingly. + def valid_attributes + {} + end + + describe "GET index" do + it "assigns all speach_balloon_templates as @speach_balloon_templates" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + get :index + assigns(:speach_balloon_templates).should eq([speach_balloon_template]) + end + end + + describe "GET show" do + it "assigns the requested speach_balloon_template as @speach_balloon_template" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + get :show, :id => speach_balloon_template.id + assigns(:speach_balloon_template).should eq(speach_balloon_template) + end + end + + describe "GET new" do + it "assigns a new speach_balloon_template as @speach_balloon_template" do + get :new + assigns(:speach_balloon_template).should be_a_new(SpeachBalloonTemplate) + end + end + + describe "GET edit" do + it "assigns the requested speach_balloon_template as @speach_balloon_template" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + get :edit, :id => speach_balloon_template.id + assigns(:speach_balloon_template).should eq(speach_balloon_template) + end + end + + describe "POST create" do + describe "with valid params" do + it "creates a new SpeachBalloonTemplate" do + expect { + post :create, :speach_balloon_template => valid_attributes + }.to change(SpeachBalloonTemplate, :count).by(1) + end + + it "assigns a newly created speach_balloon_template as @speach_balloon_template" do + post :create, :speach_balloon_template => valid_attributes + assigns(:speach_balloon_template).should be_a(SpeachBalloonTemplate) + assigns(:speach_balloon_template).should be_persisted + end + + it "redirects to the created speach_balloon_template" do + post :create, :speach_balloon_template => valid_attributes + response.should redirect_to(SpeachBalloonTemplate.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved speach_balloon_template as @speach_balloon_template" do + # Trigger the behavior that occurs when invalid params are submitted + SpeachBalloonTemplate.any_instance.stub(:save).and_return(false) + post :create, :speach_balloon_template => {} + assigns(:speach_balloon_template).should be_a_new(SpeachBalloonTemplate) + end + + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + SpeachBalloonTemplate.any_instance.stub(:save).and_return(false) + post :create, :speach_balloon_template => {} + response.should render_template("new") + end + end + end + + describe "PUT update" do + describe "with valid params" do + it "updates the requested speach_balloon_template" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + # Assuming there are no other speach_balloon_templates in the database, this + # specifies that the SpeachBalloonTemplate created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + SpeachBalloonTemplate.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, :id => speach_balloon_template.id, :speach_balloon_template => {'these' => 'params'} + end + + it "assigns the requested speach_balloon_template as @speach_balloon_template" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + put :update, :id => speach_balloon_template.id, :speach_balloon_template => valid_attributes + assigns(:speach_balloon_template).should eq(speach_balloon_template) + end + + it "redirects to the speach_balloon_template" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + put :update, :id => speach_balloon_template.id, :speach_balloon_template => valid_attributes + response.should redirect_to(speach_balloon_template) + end + end + + describe "with invalid params" do + it "assigns the speach_balloon_template as @speach_balloon_template" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + SpeachBalloonTemplate.any_instance.stub(:save).and_return(false) + put :update, :id => speach_balloon_template.id, :speach_balloon_template => {} + assigns(:speach_balloon_template).should eq(speach_balloon_template) + end + + it "re-renders the 'edit' template" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + SpeachBalloonTemplate.any_instance.stub(:save).and_return(false) + put :update, :id => speach_balloon_template.id, :speach_balloon_template => {} + response.should render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested speach_balloon_template" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + expect { + delete :destroy, :id => speach_balloon_template.id + }.to change(SpeachBalloonTemplate, :count).by(-1) + end + + it "redirects to the speach_balloon_templates list" do + speach_balloon_template = SpeachBalloonTemplate.create! valid_attributes + delete :destroy, :id => speach_balloon_template.id + response.should redirect_to(speach_balloon_templates_url) + end + end + +end diff --git a/spec/controllers/speach_templates_controller_spec.rb b/spec/controllers/speach_templates_controller_spec.rb index bc6d6fc2..468cd6de 100644 --- a/spec/controllers/speach_templates_controller_spec.rb +++ b/spec/controllers/speach_templates_controller_spec.rb @@ -3,13 +3,5 @@ require 'spec_helper' describe SpeachTemplatesController do - - describe "GET index" do - it "assigns all speach_templates as @speach_templates" do - speach_template = SpeachTemplate.create! valid_attributes - get :index - assigns(:speach_templates).should eq([speach_template]) - end - end - + #データはすべてSpeachBalloonsControllerから管理するので、ここから何かを要求することはない。 end diff --git a/spec/factories.rb b/spec/factories.rb index 489f50d9..ee5ac25e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,7 +1,8 @@ Factory.define :admin, :class => Admin do |admin| - admin.email "admin@gmail.com" + admin.sequence(:email) { |n| "admin#{n}@gmail.com"} admin.password "admin0100" admin.password_confirmation "admin0100" + admin.approve 1 end Factory.define :user_yas, :class => User do |user| @@ -37,7 +38,7 @@ end Factory.define :license, :class => License do |license| license.name 'peta2.5' - license.url 'test' + license.url 'http://test.lc/' license.cc_by 0 license.cc_sa 0 license.cc_nd 0 @@ -48,6 +49,42 @@ Factory.define :license, :class => License do |license| license.keep_aspect_ratio 0 end +Factory.define :common_license, :class => CommonLicense do |license| + license.license_id 0 + license.name 'peta2.1' + license.url 'http://test.lc/' + license.cc_by 0 + license.cc_sa 0 + license.cc_nd 0 + license.cc_nc 0 + license.no_resize 0 + license.no_flip 0 + license.no_convert 0 + license.keep_aspect_ratio 0 +end + +Factory.define :original_license, :class => OriginalLicense do |license| + license.license_id 0 + license.name 'peta2.1' + license.url 'http://test.olc/' + license.cc_by 0 + license.cc_sa 0 + license.cc_nd 0 + license.cc_nc 0 + license.no_resize 0 + license.no_flip 0 + license.no_convert 0 + license.keep_aspect_ratio 0 +end + +Factory.define :comic, :class => Comic do |comic| + comic.title "comic" + comic.width 100 + comic.height 10 + comic.visible 3 + comic.editable 3 +end + Factory.define :normal_comic, :class => Comic do |comic| comic.title "normal_comic" comic.width 100 @@ -145,11 +182,11 @@ end Factory.define :panel, :class => Panel do |panel| panel.comic_id 1 - panel.resource_picture_id 1 + panel.resource_picture_id nil panel.border 1 - panel.x 111 - panel.y 200 - panel.z 1 + panel.x nil + panel.y nil + panel.z nil panel.t 0 panel.width 100 panel.height 300 diff --git a/spec/helpers/speach_balloon_templates_helper_spec.rb b/spec/helpers/speach_balloon_templates_helper_spec.rb new file mode 100644 index 00000000..6dc42fff --- /dev/null +++ b/spec/helpers/speach_balloon_templates_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the SpeachBalloonTemplatesHelper. For example: +# +# describe SpeachBalloonTemplatesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe SpeachBalloonTemplatesHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/json/common_license.json b/spec/json/common_license.json new file mode 100644 index 00000000..20889ebf --- /dev/null +++ b/spec/json/common_license.json @@ -0,0 +1,12 @@ +{ + "name": "Public Domain", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +} diff --git a/spec/json/common_licenses.json b/spec/json/common_licenses.json new file mode 100644 index 00000000..4919d4f6 --- /dev/null +++ b/spec/json/common_licenses.json @@ -0,0 +1,26 @@ +[ +{ + "name": "Public Domain", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +}, +{ + "name": "Public Domain2", + "url": "http://pedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +} +] diff --git a/spec/json/invalid_common_licenses.json b/spec/json/invalid_common_licenses.json new file mode 100644 index 00000000..744a83d8 --- /dev/null +++ b/spec/json/invalid_common_licenses.json @@ -0,0 +1,38 @@ +[ +{ + "name": "Public Domain", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +}, +{ + "name": "fail1", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +}, +{ + "name": "fail2", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +} +] diff --git a/spec/json/invalid_original_licenses.json b/spec/json/invalid_original_licenses.json new file mode 100644 index 00000000..744a83d8 --- /dev/null +++ b/spec/json/invalid_original_licenses.json @@ -0,0 +1,38 @@ +[ +{ + "name": "Public Domain", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +}, +{ + "name": "fail1", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +}, +{ + "name": "fail2", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +} +] diff --git a/spec/json/original_license.json b/spec/json/original_license.json new file mode 100644 index 00000000..20889ebf --- /dev/null +++ b/spec/json/original_license.json @@ -0,0 +1,12 @@ +{ + "name": "Public Domain", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +} diff --git a/spec/json/original_licenses.json b/spec/json/original_licenses.json new file mode 100644 index 00000000..4919d4f6 --- /dev/null +++ b/spec/json/original_licenses.json @@ -0,0 +1,26 @@ +[ +{ + "name": "Public Domain", + "url": "http://ja.wikipedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +}, +{ + "name": "Public Domain2", + "url": "http://pedia.org/wiki/%E3%83%91%E3%83%96%E3%83%AA%E3%83%83%E3%82%AF%E3%83%89%E3%83%A1%E3%82%A4%E3%83%B3", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +} +] diff --git a/spec/models/balloon_spec.rb b/spec/models/balloon_spec.rb index a9c7e3b3..ca5cd86b 100644 --- a/spec/models/balloon_spec.rb +++ b/spec/models/balloon_spec.rb @@ -1,5 +1,262 @@ +# -*- encoding: utf-8 -*- require 'spec_helper' describe Balloon do - pending "add some examples to (or delete) #{__FILE__}" + before do + Factory :admin + @license = Factory :license + @user = Factory( :user_yas) + @author = @user.author + @artist = Factory :artist_yas, :author_id => @author.id + @other_user = Factory( :user_yas) + @other_author = @other_user.author + @other_artist = Factory :artist_yas, :author_id => @other_author.id + + end + + describe '検証に於いて' do + before do + @comic = Factory :comic, :author_id => @author.id + end + + it 'オーソドックスなデータなら通る' do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id +# @panel.should be_valid + @panel.save! + end + + context 'comic_idを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => nil + end + it 'テストデータの確認' do + @panel.comic_id = @comic.id + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.comic_id = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.comic_id = 'a' + @panel.should_not be_valid + end + it '存在するコミックでなければ失敗する' do + @panel.comic_id = 0 + @panel.should_not be_valid + end + end + context 'resource_picture_idを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.resource_picture_id = 1 + @panel.should be_valid + end + it 'nullなら通る' do + @panel.resource_picture_id = nil + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.resource_picture_id = 'a' + @panel.should_not be_valid + end + it '存在する素材でなければ失敗する' do + @panel.resource_picture_id = 0 + @panel.should_not be_valid + end + end + context 'widthを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.width = 1 + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.width = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.width = 'a' + @panel.should_not be_valid + end + it '0なら失敗する' do + @panel.width = '0' + @panel.should_not be_valid + end + it '負でも失敗する' do + @panel.width = -1 + @panel.should_not be_valid + end + end + context 'heightを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.height = '1' + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.height = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.height = 'a' + @panel.should_not be_valid + end + it '0なら失敗する' do + @panel.height = '0' + @panel.should_not be_valid + end + it '負でも失敗する' do + @panel.height = -1 + @panel.should_not be_valid + end + end + context 'borderを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.border = '1' + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.border = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.border = 'a' + @panel.should_not be_valid + end + it '負なら失敗する' do + @panel.border = '-1' + @panel.should_not be_valid + end + it '0なら通る' do + @panel.border = 0 + @panel.should be_valid + end + end + context 'xを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.x = '1' + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.x = 'a' + @panel.should_not be_valid + end + it '0なら通る' do + @panel.x = '0' + @panel.should be_valid + end + it '負でも通る' do + @panel.x = -1 + @panel.should be_valid + end + end + context 'yを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.y = '1' + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.y = 'a' + @panel.should_not be_valid + end + it '0なら通る' do + @panel.y = '0' + @panel.should be_valid + end + it '負でも通る' do + @panel.y = -1 + @panel.should be_valid + end + end + context 'zを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.z = '1' + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.z = 'a' + @panel.should_not be_valid + end + it '0なら失敗する' do + @panel.z = '0' + @panel.should_not be_valid + end + it '負なら失敗する' do + @panel.z = -1 + @panel.should_not be_valid + end + end + context 'tを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.t = '1' + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.t = 'a' + @panel.should_not be_valid + end + it '0なら通る' do + @panel.t = '0' + @panel.should be_valid + end + it '負でも失敗する' do + @panel.t = -1 + @panel.should_not be_valid + end + end + context 'author_idを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.author_id = @author.id + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.author_id = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.author_id = 'a' + @panel.should_not be_valid + end + it '存在する絵師でなければ失敗する' do + @panel.author_id = 0 + @panel.should_not be_valid + end + end + context '全体を検証するとき' do + before do + @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'コミックidとtが重複していると失敗する' do + @panel2 = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + @panel2.save + @panel2.should_not be_valid + end + end + end + end diff --git a/spec/models/common_license_spec.rb b/spec/models/common_license_spec.rb index 352eb924..de13ece0 100644 --- a/spec/models/common_license_spec.rb +++ b/spec/models/common_license_spec.rb @@ -1,5 +1,559 @@ +# -*- encoding: utf-8 -*- +#コモンライセンス require 'spec_helper' describe CommonLicense do - pending "add some examples to (or delete) #{__FILE__}" + before do + #テストデータを用意してね + @f = Rails.root + 'spec/json/common_license.json' + @t = File.open(@f, 'r').read + @j = JSON.parse @t + @fs = Rails.root + 'spec/json/common_licenses.json' + @ts = File.open(@fs, 'r').read + @js = JSON.parse @ts + @fes = Rails.root + 'spec/json/invalid_common_licenses.json' + @tes = File.open(@fes, 'r').read + @jes = JSON.parse @tes + end + describe '検証に於いて' do + before do + @l = Factory :license + end + + it 'オーソドックスなデータなら通る' do + @cl = Factory.build :common_license, :license_id => @l.id + @cl.should be_valid + end + + context 'nameを検証するとき' do + before do + @cl = Factory.build :common_license, :license_id => @l.id + end + it 'テストデータの確認' do + @cl.name = 'CC by' + @cl.should be_valid + end + it 'nullなら失敗する' do + @cl.name = '' + @cl.should_not be_valid + end + it '51文字以上なら失敗する' do + @cl.name = 'a'*51 + @cl.should_not be_valid + end + end + context 'urlを検証するとき' do + before do + @cl = Factory.build :common_license, :license_id => @l.id + end + it 'テストデータの確認' do + @cl.url = 'CC by' + @cl.should be_valid + end + it 'nullなら失敗する' do + @cl.url = '' + @cl.should_not be_valid + end + it '201文字以上なら失敗する' do + @cl.url = 'a'*201 + @cl.should_not be_valid + end + it '重複していたら失敗する' do + cl = Factory :common_license, :license_id => @l.id + @cl.should_not be_valid + end + it 'url形式でなら失敗する' do + @cl.url = '' + pending + end + end + context 'license_idを検証するとき' do + before do + @cl = Factory.build :common_license + end + it 'テストデータの確認' do + @cl.license_id = @l.id + @cl.should be_valid + end + it 'nullなら失敗する' do + @cl.license_id = nil + @cl.should_not be_valid + end + it '数値でなければ失敗する' do + @cl.license_id = 'a' + @cl.should_not be_valid + end + it '存在するlicenseでなければ失敗する' do + @cl.license_id = 0 + @cl.should_not be_valid + end + end + end + + describe '対象コモンライセンスの取得に於いて' do + before do + @lc = Factory :license + @cl = Factory :common_license, :license_id => @lc.id + end + context 'urlが一致するコモンライセンスがないとき' do + it '新規コモンライセンスを準備して返す' do + r = CommonLicense.update_common_license Factory.attributes_for(:common_license, :url => 'http://domain.no') + r.should be_a_new CommonLicense + end + end + context 'urlが一致するコモンライセンスがあるとき' do + it '該当コモンライセンスを返す' do + prm = Factory.attributes_for(:common_license) + r = CommonLicense.update_common_license prm + r.is_a?(CommonLicense).should be_true + r.should_not be_a_new CommonLicense + r[:url].should eq prm[:url] + end + end + end + + describe 'コモンライセンス更新に於いて' do + before do + @lc = Factory :license + @cl = Factory :common_license, :license_id => @lc.id + @attr = Factory.attributes_for(:common_license, :name => 'exist case') + @newattr = Factory.attributes_for(:common_license, :url => 'http://domain.no') + end + context 'つつがなく終わるとき' do + it '対象コモンライセンスを問い合わせている' do + CommonLicense.stub(:update_common_license).with(any_args).and_return(CommonLicense.new(@attr)) + CommonLicense.should_receive(:update_common_license).exactly(1) + CommonLicense.store @attr + end + context '新規のとき' do + it 'コモンライセンスを保存しようとしている' do + CommonLicense.any_instance.should_receive(:save).exactly(1) + CommonLicense.store @newattr + end + it 'コモンライセンスが作成されている' do + lambda { + CommonLicense.store @newattr + }.should change CommonLicense, :count + end + context 'ライセンスとの連動' do + it 'ライセンスが作成されている' do + lambda { + CommonLicense.store @newattr + }.should change License, :count + end + it '両者がリンクされている' do + r = CommonLicense.store @newattr + l = License.find(r.license_id) + l.should be_true + end + it '属性が一致している' do + r = CommonLicense.store @newattr + l = License.find(r.license_id) + l.url.should eq r.url + l.name.should eq r.name + end + end + end + context '更新のとき' do + it 'コモンライセンスを保存しようとしている' do + CommonLicense.any_instance.should_receive(:save).exactly(1) + CommonLicense.store @attr + end + it 'コモンライセンスの数に変化がない' do + lambda { + CommonLicense.store @attr + }.should_not change CommonLicense, :count + end + context 'ライセンスとの連動' do + it 'ライセンスの数に変化がない' do + lambda { + CommonLicense.store @attr + }.should_not change License, :count + end + it '両者がリンクされている' do + r = CommonLicense.store @attr + l = License.find(r.license_id) + l.should be_true + end + it '属性が一致している' do + r = CommonLicense.store @attr + l = License.find(r.license_id) + l.url.should eq r.url + l.name.should eq r.name + end + end + end + it '属性が一致している' do + r = CommonLicense.store @newattr + r.url.should eq @newattr[:url] + r.name.should eq @newattr[:name] + end + it '保存されたCommonLicenseオブジェクトを返す' do + r = CommonLicense.store @newattr + r.should_not be_a_new CommonLicense + end + end + context 'ライセンスの作成に失敗するとき' do + before do + License.any_instance.stub(:save).with(any_args).and_return(false) + end + context '新規のとき' do + it 'ライセンスに変化がない' do + lambda { + r = CommonLicense.store @newattr + }.should_not change License, :count + end + it 'コモンライセンスに変化がない' do + lambda { + r = CommonLicense.store @newattr + }.should_not change CommonLicense, :count + end + end + context '更新のとき' do + it 'コモンライセンス属性に変化がない' do + lambda { + r = CommonLicense.store @attr + }.should_not change License.find(@cl.id), :name + end + it 'ライセンス属性に変化がない' do + lambda { + r = CommonLicense.store @attr + }.should_not change License.find(@lc.id), :name + end + end + end + context 'コモンライセンスの作成に失敗するとき' do + before do + CommonLicense.any_instance.stub(:save).with(any_args).and_return(false) + end + context '新規のとき' do + it 'ライセンスに変化がない' do + lambda { + r = CommonLicense.store @newattr + }.should_not change License, :count + end + it 'コモンライセンスに変化がない' do + lambda { + r = CommonLicense.store @newattr + }.should_not change CommonLicense, :count + end + end + context '更新のとき' do + it 'コモンライセンス属性に変化がない' do + lambda { + r = CommonLicense.store @attr + }.should_not change License.find(@cl.id), :name + end + it 'ライセンス属性に変化がない' do + lambda { + r = CommonLicense.store @attr + }.should_not change License.find(@lc.id), :name + end + end + end + end +=begin + describe '単体取得に於いて' do + before do + @cl = Factory.build :common_license + @cl.store + end + it '指定のライセンスを返す' do + l = CommonLicense.show @cl.id + l.should eq @cl + end + context '関連テーブルオプションがないとき' do + it 'ライセンスだけを含んでいる' do + r = CommonLicense.show_include_opt + r.should eq [:license] + end + end + context '関連テーブルオプションでコマを含ませたとき' do + it 'ライセンスと作家を含んでいる' do + r = CommonLicense.show_include_opt(:include => :author) + r.should eq [:license, :author] + end + end + end + describe '一覧取得に於いて' do + before do + @cl = Factory.build :common_license + @cl.store + end + context 'page補正について' do + it '文字列から数値に変換される' do + CommonLicense.page('8').should eq 8 + end + it 'nilの場合は1になる' do + CommonLicense.page().should eq 1 + end + it '0以下の場合は1になる' do + CommonLicense.page('0').should eq 1 + end + end + context 'page_size補正について' do + it '文字列から数値に変換される' do + CommonLicense.page_size('7').should eq 7 + end + it 'nilの場合はCommonLicense.default_page_sizeになる' do + CommonLicense.page_size().should eq CommonLicense.default_page_size + end + it '0以下の場合はCommonLicense.default_page_sizeになる' do + CommonLicense.page_size('0').should eq CommonLicense.default_page_size + end + it 'CommonLicense.max_page_sizeを超えた場合はCommonLicense.max_page_sizeになる' do + CommonLicense.page_size('1000').should eq CommonLicense.max_page_size + end + end + it 'リストを返す' do + l = CommonLicense.list + l.should eq [@cl] + end + it '名前順で並んでいる' do + n = Factory.build :common_license, :url => 'tes.to', :name => 'peta2.2' + n.store + l = CommonLicense.list + l.should eq [@cl, n] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + @license2 = Factory.build :common_license, :url => 'tes.to2', :name => 'peta2.2' + @license2.store + @license3 = Factory.build :common_license, :url => 'tes.to3', :name => 'peta2.3' + @license3.store + @license4 = Factory.build :common_license, :url => 'tes.to4', :name => 'peta2.4' + @license4.store + @license5 = Factory.build :common_license, :url => 'tes.to5', :name => 'peta2.5' + @license5.store + CommonLicense.stub(:default_page_size).and_return(2) + end + it '通常は2件を返す' do + l = CommonLicense.list + l.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + l = CommonLicense.list({}, 1) + l.should eq [@cl, @license2] + end + it 'page=2なら中間2件を返す' do + l = CommonLicense.list({}, 2) + l.should eq [@license3, @license4] + end + it 'page=3なら先頭1件を返す' do + l = CommonLicense.list({}, 3) + l.should eq [@license5] + end + end + end +=end + + describe 'Json解析に於いて' do + before do + end + context 'テキストを渡されたとき' do + it 'Json解析している' do + JSON.should_receive(:parse).with(@t).exactly(1) + r = CommonLicense.parse @t + end + it '単数データならHashで返す' do + r = CommonLicense.parse @t + r.is_a?(Hash).should be_true + end + it '複数データならArrayで返す' do + r = CommonLicense.parse @ts + r.is_a?(Array).should be_true + end + end + context 'パース失敗したとき' do + it 'Falseを返す' do + JSON.should_receive(:parse).with(any_args).and_raise('StandardError') + r = CommonLicense.parse @t + r.should be_false + end + end + end + + describe 'Jsonの繰り返し処理に於いて' do + before do + end + context '単体データを渡されたとき' do + it '一回処理' do + r = [] + CommonLicense.each_license @j do |i| + r << i + end + r.size.should eq 1 + end + end + context '複数を渡されたとき' do + it '二回処理' do + r = [] + CommonLicense.each_license @js do |i| + r << i + end + r.size.should eq 2 + end + end + end + + describe 'テキスト取り込みに於いて' do + #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す + #Licenseとの連動が完成していること + before do + end + context 'つつがなく終わるとき' do + it 'Json解析を依頼する' do + CommonLicense.should_receive(:parse).with(any_args).exactly(1) + CommonLicense.stub(:parse).with(any_args).and_return(@j) + CommonLicense.import(@t) + end + it '繰り返し処理を依頼する' do + CommonLicense.should_receive(:each_license).with(any_args).exactly(1) + CommonLicense.import(@t) + end + it 'ライセンス更新を一回依頼する' do + CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new) + CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true) + CommonLicense.should_receive(:store).with(any_args).exactly(1) + CommonLicense.import(@t) + end + it 'コモンライセンスが追加される' do + lambda { + CommonLicense.import(@t) + }.should change CommonLicense, :count + end + it '[]を返す' do + CommonLicense.import(@t).should eq [] + end + end + context '複数データがつつがなく終わるとき' do + it 'ライセンス更新を二回依頼する' do + CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new) + CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true) + CommonLicense.should_receive(:store).with(any_args).exactly(2) + CommonLicense.import(@ts) + end + it 'コモンライセンスが二個追加される' do + lambda { + CommonLicense.import(@ts) + }.should change(CommonLicense, :count).by 2 + end + it '[]を返す' do + CommonLicense.import(@ts).should eq [] + end + end + #例外ケース + context 'Json解析に失敗したとき' do + before do + CommonLicense.stub(:parse).with(any_args).and_return(false) + end + it 'コモンライセンスの数に変化がない' do + lambda { + CommonLicense.import(@t) + }.should_not change CommonLicense, :count + end + it 'Falseを返す' do + CommonLicense.import(@t).should be_false + end + end + context 'コモンライセンス作成に失敗したとき' do + before do + CommonLicense.any_instance.stub(:save).with(any_args).and_return(false) + CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(false) + end + it 'コモンライセンスの数に変化がない' do + lambda { + CommonLicense.import(@t) + }.should_not change CommonLicense, :count + end + it '配列を返す' do + r = CommonLicense.import(@t) + r.is_a?(Array).should be_true + end + it '配列の中身は一件' do + r = CommonLicense.import(@t) + r.should have(1).items + end + it 'コモンライセンスオブジェクトが入っている' do + r = CommonLicense.import(@t) + r.first.is_a?(CommonLicense).should be_true + end + end + context '複数のコモンライセンス作成に失敗したとき' do + #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する + it 'コモンライセンスの数に変化がない' do + lambda { + CommonLicense.import(@tes) + }.should_not change CommonLicense, :count + end + it '途中で保存に失敗しても全件更新依頼する' do + CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new) + CommonLicense.should_receive(:store).with(any_args).exactly(3) + CommonLicense.import(@tes) + end + it '配列を返す' do + r = CommonLicense.import(@tes) + r.is_a?(Array).should be_true + end + it '配列の中身は2件' do + r = CommonLicense.import(@tes) + r.should have(2).items + end + it '配列の中身は失敗したコモンライセンスオブジェクトが入っている' do + r = CommonLicense.import(@tes) + r[0].is_a?(CommonLicense).should be_true + r[0]["name"].should eq 'fail1' + r[1].is_a?(CommonLicense).should be_true + r[1]["name"].should eq 'fail2' + end + end + end + + describe 'ファイル取り込みに於いて' do + before do + CommonLicense.stub(:import).with(any_args).and_return(true) + end + context 'つつがなく終わるとき' do + before do + CommonLicense.stub(:import).with(any_args).and_return(true) + end + it 'ファイルを開いてテキストを読む' do + File.should_receive(:open).with(@f, 'r').exactly(1) + CommonLicense.import_file(@f) + end + it 'テキスト取り込みを依頼する' do + CommonLicense.should_receive(:import).with(any_args).exactly(1) + CommonLicense.import_file(@f) + end + #テキスト取り込み成功でTrueが返る + it 'Trueを返す' do + CommonLicense.import_file(@f).should be_true + end + end + context 'ファイルが開けないとき' do + before do + File.stub(:open).with(any_args).and_raise('StandardError') + end + it 'ファイルエラーのメッセージを出力する' do + pending + end + it 'Falseを返す' do + CommonLicense.import_file(@f).should be_false + end + end + #失敗したときは、失敗したライセンスが配列で返る + context 'テキスト取り込みが失敗したとき' do + before do + CommonLicense.stub(:import).with(any_args).and_return(false) + end + it '各コモンライセンスのエラーメッセージを出力する' do + pending + end + it 'Falseを返す' do + CommonLicense.import_file(@f).should be_false + end + end + end + end diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index 8027ddb4..ea5a8d9d 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -1,5 +1,222 @@ +# -*- encoding: utf-8 -*- +#ライセンス require 'spec_helper' describe License do - pending "add some examples to (or delete) #{__FILE__}" + before do + end + describe '検証に於いて' do + before do + @l = Factory.build :license + end + + it 'オーソドックスなデータなら通る' do + @l.should be_valid + end + + context 'nameを検証するとき' do + it 'nullなら失敗する' do + @l.name = '' + @l.should_not be_valid + end + it '51文字以上なら失敗する' do + @l.name = 'a'*51 + @l.should_not be_valid + end + end + context 'urlを検証するとき' do + it 'nullなら失敗する' do + @l.url = '' + @l.should_not be_valid + end + it '201文字以上なら失敗する' do + @l.url = 'a'*201 + @l.should_not be_valid + end + it '重複していたら失敗する' do + lc = Factory :license + @l.should_not be_valid + end + it 'url形式でなら失敗する' do + @l.url = '' + pending + end + end + end + + describe '対象ライセンスの取得に於いて' do + before do + @lc = Factory :license + end + context 'urlが一致するライセンスがないとき' do + it '新規ライセンスを準備して返す' do + cl = Factory.build(:common_license, :url => 'http://domain.no') + r = License.update_license cl + r.should be_a_new License + end + end + context 'urlが一致するライセンスがあるとき' do + it '該当ライセンスを返す' do + r = License.update_license @lc + r.is_a?(License).should be_true + r.should_not be_a_new License + r.url.should eq @lc.url + r.name.should eq @lc.name + end + end + end + + #作成が + describe '単体取得に於いて' do + before do + @lcl = Factory :license + @cl = Factory :common_license, :license_id => @lcl.id + @lol = Factory :license, :url => 'http://test.ptn/10' + @ol = Factory :original_license, :license_id => @lol.id, :url => 'http://test.ptn/10' + end + it '指定のコマを返す' do + l = License.show @lcl.id + l.should eq @lcl + end + end + describe '関連テーブルプションに於いて' do + context 'オプションがないとき' do + it 'コモンライセンスとオリジナルライセンスを含んでいる' do + r = License.show_include_opt + r.should eq [:common_license, :original_license] + end + end + context 'オプションで原画を含ませたとき' do + it 'コモンライセンスとオリジナルライセンスと原画データを含んでいる' do + r = License.show_include_opt(:include => :original_picture) + r.should eq [:common_license, :original_license, :original_picture] + end + end + end + describe 'json単体出力オプションに於いて' do + it 'includeキーを含んでいる' do + r = License.show_json_include_opt + r.has_key?(:include).should be_true + end + it '2つの項目を含んでいる' do + r = License.show_json_include_opt[:include] + r.should have(2).items + end + it 'コモンライセンスを含んでいる' do + r = License.show_json_include_opt[:include] + r.has_key?(:common_license).should be_true + end + it 'オリジナルライセンスを含んでいる' do + r = License.show_json_include_opt[:include] + r.has_key?(:original_license).should be_true + end + end + describe '一覧取得に於いて' do + before do + @lcl = Factory :license, :name => 'peta2.0' + @cl = Factory :common_license, :license_id => @lcl.id + end + context 'page補正について' do + it '文字列から数値に変換される' do + License.page('8').should eq 8 + end + it 'nilの場合は1になる' do + License.page().should eq 1 + end + it '0以下の場合は1になる' do + License.page('0').should eq 1 + end + end + context 'page_size補正について' do + it '文字列から数値に変換される' do + License.page_size('7').should eq 7 + end + it 'nilの場合はLicense.default_page_sizeになる' do + License.page_size().should eq License.default_page_size + end + it '0以下の場合はLicense.default_page_sizeになる' do + License.page_size('0').should eq License.default_page_size + end + it 'License.max_page_sizeを超えた場合はLicense.max_page_sizeになる' do + License.page_size('1000').should eq License.max_page_size + end + end + it 'リストを返す' do + pl = License.list + pl.should eq [@lcl] + end + it '名前順で並んでいる' do + @lol = Factory :license, :name => 'peta1.0', :url => 'http://test.ptn/10' + @ol = Factory :original_license, :license_id => @lol.id, :name => 'peta1.0', :url => 'http://test.ptn/10' + l = License.list + l.should eq [@lol, @lcl] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + @lol2 = Factory :license, :name => 'peta2.1', :url => 'http://test.ptn/21' + @ol2 = Factory :original_license, :license_id => @lol2.id, :name => 'peta2.1', :url => 'http://test.ptn/21' + @lol3 = Factory :license, :name => 'peta2.2', :url => 'http://test.ptn/22' + @ol3 = Factory :original_license, :license_id => @lol3.id, :name => 'peta2.2', :url => 'http://test.ptn/22' + @lol4 = Factory :license, :name => 'peta2.3', :url => 'http://test.ptn/23' + @ol4 = Factory :original_license, :license_id => @lol4.id, :name => 'peta2.3', :url => 'http://test.ptn/23' + @lol5 = Factory :license, :name => 'peta2.4', :url => 'http://test.ptn/24' + @ol5 = Factory :original_license, :license_id => @lol5.id, :name => 'peta2.4', :url => 'http://test.ptn/24' + License.stub(:default_page_size).and_return(2) + end + it '通常は2件を返す' do + l = License.list + l.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #名前順で並んでいる + l = License.list( {}, 1) + l.should eq [@lcl, @lol2] + end + it 'page=2なら中間2件を返す' do + l = License.list({}, 2) + l.should eq [@lol3, @lol4] + end + it 'page=3なら先頭1件を返す' do + l = License.list({}, 3) + l.should eq [@lol5] + end + end + end + describe 'list関連テーブルプションに於いて' do + it 'includeキーを含んでいる' do + r = License.list_opt + r.has_key?(:include).should be_true + end + it '2つの項目を含んでいる' do + r = License.list_opt[:include] + r.should have(2).items + end + it 'コモンライセンスを含んでいる' do + r = License.list_opt[:include] + r.has_key?(:common_license).should be_true + end + it 'オリジナルライセンスを含んでいる' do + r = License.list_opt[:include] + r.has_key?(:original_license).should be_true + end + end + describe 'json一覧出力オプションに於いて' do + it 'includeキーを含んでいる' do + r = License.list_json_opt + r.has_key?(:include).should be_true + end + it '2つの項目を含んでいる' do + r = License.list_json_opt[:include] + r.should have(2).items + end + it 'コモンライセンスを含んでいる' do + r = License.list_json_opt[:include] + r.has_key?(:common_license).should be_true + end + it 'オリジナルライセンスを含んでいる' do + r = License.list_json_opt[:include] + r.has_key?(:original_license).should be_true + end + end + end diff --git a/spec/models/original_license_spec.rb b/spec/models/original_license_spec.rb index 36b21b52..d8f30108 100644 --- a/spec/models/original_license_spec.rb +++ b/spec/models/original_license_spec.rb @@ -1,5 +1,558 @@ +# -*- encoding: utf-8 -*- +#オリジナルライセンス require 'spec_helper' describe OriginalLicense do - pending "add some examples to (or delete) #{__FILE__}" + before do + #テストデータを用意してね + @f = Rails.root + 'spec/json/original_license.json' + @t = File.open(@f, 'r').read + @j = JSON.parse @t + @fs = Rails.root + 'spec/json/original_licenses.json' + @ts = File.open(@fs, 'r').read + @js = JSON.parse @ts + @fes = Rails.root + 'spec/json/invalid_original_licenses.json' + @tes = File.open(@fes, 'r').read + @jes = JSON.parse @tes + end + describe '検証に於いて' do + before do + @l = Factory :license + end + + it 'オーソドックスなデータなら通る' do + @ol = Factory.build :original_license, :license_id => @l.id + @ol.should be_valid + end + + context 'nameを検証するとき' do + before do + @ol = Factory.build :original_license, :license_id => @l.id + end + it 'テストデータの確認' do + @ol.name = 'CC by' + @ol.should be_valid + end + it 'nullなら失敗する' do + @ol.name = '' + @ol.should_not be_valid + end + it '51文字以上なら失敗する' do + @ol.name = 'a'*51 + @ol.should_not be_valid + end + end + context 'urlを検証するとき' do + before do + @ol = Factory.build :original_license, :license_id => @l.id + end + it 'テストデータの確認' do + @ol.url = 'CC by' + @ol.should be_valid + end + it 'nullなら失敗する' do + @ol.url = '' + @ol.should_not be_valid + end + it '201文字以上なら失敗する' do + @ol.url = 'a'*201 + @ol.should_not be_valid + end + it '重複していたら失敗する' do + cl = Factory :original_license, :license_id => @l.id + @ol.should_not be_valid + end + it 'url形式でなら失敗する' do + @ol.url = '' + pending + end + end + context 'license_idを検証するとき' do + before do + @ol = Factory.build :original_license + end + it 'テストデータの確認' do + @ol.license_id = @l.id + @ol.should be_valid + end + it 'nullなら失敗する' do + @ol.license_id = nil + @ol.should_not be_valid + end + it '数値でなければ失敗する' do + @ol.license_id = 'a' + @ol.should_not be_valid + end + it '存在するlicenseでなければ失敗する' do + @ol.license_id = 0 + @ol.should_not be_valid + end + end + end + + describe '対象オリジナルライセンスの取得に於いて' do + before do + @lc = Factory :license + @ol = Factory :original_license, :license_id => @lc.id + end + context 'urlが一致するオリジナルライセンスがないとき' do + it '新規オリジナルライセンスを準備して返す' do + r = OriginalLicense.update_original_license Factory.attributes_for(:original_license, :url => 'http://domain.no') + r.should be_a_new OriginalLicense + end + end + context 'urlが一致するオリジナルライセンスがあるとき' do + it '該当オリジナルライセンスを返す' do + prm = Factory.attributes_for(:original_license) + r = OriginalLicense.update_original_license prm + r.is_a?(OriginalLicense).should be_true + r.should_not be_a_new OriginalLicense + r[:url].should eq prm[:url] + end + end + end + + describe 'オリジナルライセンス更新に於いて' do + before do + @lc = Factory :license + @ol = Factory :original_license, :license_id => @lc.id, :url => @lc.url + @attr = Factory.attributes_for(:original_license, :name => 'exist case', :url => @lc.url) + @newattr = Factory.attributes_for(:original_license, :url => 'http://domain.no') + end + context 'つつがなく終わるとき' do + it '対象オリジナルライセンスを問い合わせている' do + OriginalLicense.stub(:update_original_license).with(any_args).and_return(OriginalLicense.new(@attr)) + OriginalLicense.should_receive(:update_original_license).exactly(1) + OriginalLicense.store @attr + end + context '新規のとき' do + it 'オリジナルライセンスを保存しようとしている' do + OriginalLicense.any_instance.should_receive(:save).exactly(1) + OriginalLicense.store @newattr + end + it 'オリジナルライセンスが作成されている' do + lambda { + OriginalLicense.store @newattr + }.should change OriginalLicense, :count + end + context 'ライセンスとの連動' do + it 'ライセンスが作成されている' do + lambda { + OriginalLicense.store @newattr + }.should change License, :count + end + it '両者がリンクされている' do + r = OriginalLicense.store @newattr + l = License.find(r.license_id) + l.should be_true + end + it '属性が一致している' do + r = OriginalLicense.store @newattr + l = License.find(r.license_id) + l.url.should eq r.url + l.name.should eq r.name + end + end + end + context '更新のとき' do + it 'オリジナルライセンスを保存しようとしている' do + OriginalLicense.any_instance.should_receive(:save).exactly(1) + OriginalLicense.store @attr + end + it 'オリジナルライセンスの数に変化がない' do + lambda { + OriginalLicense.store @attr + }.should_not change OriginalLicense, :count + end + context 'ライセンスとの連動' do + it 'ライセンスの数に変化がない' do + lambda { + OriginalLicense.store @attr + }.should_not change License, :count + end + it '両者がリンクされている' do + r = OriginalLicense.store @attr + l = License.find(r.license_id) + l.should be_true + end + it '属性が一致している' do + r = OriginalLicense.store @attr + l = License.find(r.license_id) + l.url.should eq r.url + l.name.should eq r.name + end + end + end + it '属性が一致している' do + r = OriginalLicense.store @newattr + r.url.should eq @newattr[:url] + r.name.should eq @newattr[:name] + end + it '保存されたOriginalLicenseオブジェクトを返す' do + r = OriginalLicense.store @newattr + r.should_not be_a_new OriginalLicense + end + end + context 'ライセンスの作成に失敗するとき' do + before do + License.any_instance.stub(:save).with(any_args).and_return(false) + end + context '新規のとき' do + it 'ライセンスに変化がない' do + lambda { + r = OriginalLicense.store @newattr + }.should_not change License, :count + end + it 'オリジナルライセンスに変化がない' do + lambda { + r = OriginalLicense.store @newattr + }.should_not change OriginalLicense, :count + end + end + context '更新のとき' do + it 'オリジナルライセンス属性に変化がない' do + lambda { + r = OriginalLicense.store @attr + }.should_not change License.find(@ol.id), :name + end + it 'ライセンス属性に変化がない' do + lambda { + r = OriginalLicense.store @attr + }.should_not change License.find(@lc.id), :name + end + end + end + context 'オリジナルライセンスの作成に失敗するとき' do + before do + OriginalLicense.any_instance.stub(:save).with(any_args).and_return(false) + end + context '新規のとき' do + it 'ライセンスに変化がない' do + lambda { + r = OriginalLicense.store @newattr + }.should_not change License, :count + end + it 'オリジナルライセンスに変化がない' do + lambda { + r = OriginalLicense.store @newattr + }.should_not change OriginalLicense, :count + end + end + context '更新のとき' do + it 'オリジナルライセンス属性に変化がない' do + lambda { + r = OriginalLicense.store @attr + }.should_not change License.find(@ol.id), :name + end + it 'ライセンス属性に変化がない' do + lambda { + r = OriginalLicense.store @attr + }.should_not change License.find(@lc.id), :name + end + end + end + end + + describe '単体取得に於いて' do + before do + @ol = Factory.build :original_license + @ol.store + end + it '指定のライセンスを返す' do + l = OriginalLicense.show @ol.id + l.should eq @ol + end + context '関連テーブルオプションがないとき' do + it 'ライセンスだけを含んでいる' do + r = OriginalLicense.show_include_opt + r.should eq [:license] + end + end + context '関連テーブルオプションで作家を含ませたとき' do + it 'ライセンスと作家を含んでいる' do + r = OriginalLicense.show_include_opt(:include => :author) + r.should eq [:license, :author] + end + end + end + describe '一覧取得に於いて' do + before do + @ol = Factory.build :original_license + @ol.store + end + context 'page補正について' do + it '文字列から数値に変換される' do + OriginalLicense.page('8').should eq 8 + end + it 'nilの場合は1になる' do + OriginalLicense.page().should eq 1 + end + it '0以下の場合は1になる' do + OriginalLicense.page('0').should eq 1 + end + end + context 'page_size補正について' do + it '文字列から数値に変換される' do + OriginalLicense.page_size('7').should eq 7 + end + it 'nilの場合はOriginalLicense.default_page_sizeになる' do + OriginalLicense.page_size().should eq OriginalLicense.default_page_size + end + it '0以下の場合はOriginalLicense.default_page_sizeになる' do + OriginalLicense.page_size('0').should eq OriginalLicense.default_page_size + end + it 'OriginalLicense.max_page_sizeを超えた場合はOriginalLicense.max_page_sizeになる' do + OriginalLicense.page_size('1000').should eq OriginalLicense.max_page_size + end + end + it 'リストを返す' do + l = OriginalLicense.list + l.should eq [@ol] + end + it '名前順で並んでいる' do + n = Factory.build :original_license, :url => 'http://tes.to', :name => 'peta2.2' + n.store + l = OriginalLicense.list + l.should eq [@ol, n] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + @license2 = Factory.build :original_license, :url => 'http://tes.to2', :name => 'peta2.2' + @license2.store + @license3 = Factory.build :original_license, :url => 'http://tes.to3', :name => 'peta2.3' + @license3.store + @license4 = Factory.build :original_license, :url => 'http://tes.to4', :name => 'peta2.4' + @license4.store + @license5 = Factory.build :original_license, :url => 'http://tes.to5', :name => 'peta2.5' + @license5.store + OriginalLicense.stub(:default_page_size).and_return(2) + end + it '通常は2件を返す' do + l = OriginalLicense.list + l.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + l = OriginalLicense.list({}, 1) + l.should eq [@ol, @license2] + end + it 'page=2なら中間2件を返す' do + l = OriginalLicense.list({}, 2) + l.should eq [@license3, @license4] + end + it 'page=3なら先頭1件を返す' do + l = OriginalLicense.list({}, 3) + l.should eq [@license5] + end + end + end + + describe 'Json解析に於いて' do + before do + end + context 'テキストを渡されたとき' do + it 'Json解析している' do + JSON.should_receive(:parse).with(@t).exactly(1) + r = OriginalLicense.parse @t + end + it '単数データならHashで返す' do + r = OriginalLicense.parse @t + r.is_a?(Hash).should be_true + end + it '複数データならArrayで返す' do + r = OriginalLicense.parse @ts + r.is_a?(Array).should be_true + end + end + context 'パース失敗したとき' do + it 'Falseを返す' do + JSON.should_receive(:parse).with(any_args).and_raise('StandardError') + r = OriginalLicense.parse @t + r.should be_false + end + end + end + + describe 'Jsonの繰り返し処理に於いて' do + before do + end + context '単体データを渡されたとき' do + it '一回処理' do + r = [] + OriginalLicense.each_license @j do |i| + r << i + end + r.size.should eq 1 + end + end + context '複数を渡されたとき' do + it '二回処理' do + r = [] + OriginalLicense.each_license @js do |i| + r << i + end + r.size.should eq 2 + end + end + end + + describe 'テキスト取り込みに於いて' do + #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す + #Licenseとの連動が完成していること + before do + end + context 'つつがなく終わるとき' do + it 'Json解析を依頼する' do + OriginalLicense.should_receive(:parse).with(any_args).exactly(1) + OriginalLicense.stub(:parse).with(any_args).and_return(@j) + OriginalLicense.import(@t) + end + it '繰り返し処理を依頼する' do + OriginalLicense.should_receive(:each_license).with(any_args).exactly(1) + OriginalLicense.import(@t) + end + it 'ライセンス更新を一回依頼する' do + OriginalLicense.stub(:store).with(any_args).and_return(OriginalLicense.new) + OriginalLicense.any_instance.stub(:valid?).with(any_args).and_return(true) + OriginalLicense.should_receive(:store).with(any_args).exactly(1) + OriginalLicense.import(@t) + end + it 'オリジナルライセンスが追加される' do + lambda { + OriginalLicense.import(@t) + }.should change OriginalLicense, :count + end + it '[]を返す' do + OriginalLicense.import(@t).should eq [] + end + end + context '複数データがつつがなく終わるとき' do + it 'ライセンス更新を二回依頼する' do + OriginalLicense.stub(:store).with(any_args).and_return(OriginalLicense.new) + OriginalLicense.any_instance.stub(:valid?).with(any_args).and_return(true) + OriginalLicense.should_receive(:store).with(any_args).exactly(2) + OriginalLicense.import(@ts) + end + it 'オリジナルライセンスが二個追加される' do + lambda { + OriginalLicense.import(@ts) + }.should change(OriginalLicense, :count).by 2 + end + it '[]を返す' do + OriginalLicense.import(@ts).should eq [] + end + end + #例外ケース + context 'Json解析に失敗したとき' do + before do + OriginalLicense.stub(:parse).with(any_args).and_return(false) + end + it 'オリジナルライセンスの数に変化がない' do + lambda { + OriginalLicense.import(@t) + }.should_not change OriginalLicense, :count + end + it 'Falseを返す' do + OriginalLicense.import(@t).should be_false + end + end + context 'オリジナルライセンス作成に失敗したとき' do + before do + OriginalLicense.any_instance.stub(:save).with(any_args).and_return(false) + OriginalLicense.any_instance.stub(:valid?).with(any_args).and_return(false) + end + it 'オリジナルライセンスの数に変化がない' do + lambda { + OriginalLicense.import(@t) + }.should_not change OriginalLicense, :count + end + it '配列を返す' do + r = OriginalLicense.import(@t) + r.is_a?(Array).should be_true + end + it '配列の中身は一件' do + r = OriginalLicense.import(@t) + r.should have(1).items + end + it 'オリジナルライセンスオブジェクトが入っている' do + r = OriginalLicense.import(@t) + r.first.is_a?(OriginalLicense).should be_true + end + end + context '複数のオリジナルライセンス作成に失敗したとき' do + #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する + it 'オリジナルライセンスの数に変化がない' do + lambda { + OriginalLicense.import(@tes) + }.should_not change OriginalLicense, :count + end + it '途中で保存に失敗しても全件更新依頼する' do + OriginalLicense.stub(:store).with(any_args).and_return(OriginalLicense.new) + OriginalLicense.should_receive(:store).with(any_args).exactly(3) + OriginalLicense.import(@tes) + end + it '配列を返す' do + r = OriginalLicense.import(@tes) + r.is_a?(Array).should be_true + end + it '配列の中身は2件' do + r = OriginalLicense.import(@tes) + r.should have(2).items + end + it '配列の中身は失敗したオリジナルライセンスオブジェクトが入っている' do + r = OriginalLicense.import(@tes) + r[0].is_a?(OriginalLicense).should be_true + r[0]["name"].should eq 'fail1' + r[1].is_a?(OriginalLicense).should be_true + r[1]["name"].should eq 'fail2' + end + end + end + + describe 'ファイル取り込みに於いて' do + before do + OriginalLicense.stub(:import).with(any_args).and_return(true) + end + context 'つつがなく終わるとき' do + before do + OriginalLicense.stub(:import).with(any_args).and_return(true) + end + it 'ファイルを開いてテキストを読む' do + File.should_receive(:open).with(@f, 'r').exactly(1) + OriginalLicense.import_file(@f) + end + it 'テキスト取り込みを依頼する' do + OriginalLicense.should_receive(:import).with(any_args).exactly(1) + OriginalLicense.import_file(@f) + end + #テキスト取り込み成功でTrueが返る + it 'Trueを返す' do + OriginalLicense.import_file(@f).should be_true + end + end + context 'ファイルが開けないとき' do + before do + File.stub(:open).with(any_args).and_raise('StandardError') + end + it 'ファイルエラーのメッセージを出力する' do + pending + end + it 'Falseを返す' do + OriginalLicense.import_file(@f).should be_false + end + end + #失敗したときは、失敗したライセンスが配列で返る + context 'テキスト取り込みが失敗したとき' do + before do + OriginalLicense.stub(:import).with(any_args).and_return(false) + end + it '各オリジナルライセンスのエラーメッセージを出力する' do + pending + end + it 'Falseを返す' do + OriginalLicense.import_file(@f).should be_false + end + end + end + end diff --git a/spec/models/original_picture_spec.rb b/spec/models/original_picture_spec.rb index 8366b4da..f61ef845 100644 --- a/spec/models/original_picture_spec.rb +++ b/spec/models/original_picture_spec.rb @@ -193,23 +193,43 @@ describe OriginalPicture do before do @op = Factory :original_picture, :artist_id => @artist.id end - it '指定のコミックを返す' do - pic = OriginalPicture.show @op.id, @author + it '指定の原画を返す' do + pic = OriginalPicture.show @op.id, @artist pic.should eq @op end context '関連テーブルオプションがないとき' do - it 'ライセンスデータだけを含んでいる' do + it 'ライセンスと素材を含んでいる' do r = OriginalPicture.show_include_opt - r.should eq [:license] + r.should eq [:license, :resource_picture] end end context '関連テーブルオプションで絵師を含ませたとき' do - it 'ライセンスデータと作者データを含んでいる' do + it 'ライセンスと素材と作者データを含んでいる' do r = OriginalPicture.show_include_opt(:include => :artist) - r.should eq [:license, :artist] + r.should eq [:license, :resource_picture, :artist] + end + end + context '他人の原画を開こうとしたとき' do + it '403Forbidden例外を返す' do + lambda{ + pic = OriginalPicture.show @op.id, @other_artist + }.should raise_error(ActiveRecord::Forbidden) + end + end + context '存在しない原画を開こうとしたとき' do + it '404RecordNotFound例外を返す' do + lambda{ + pic = OriginalPicture.show 0, @artist + }.should raise_error(ActiveRecord::RecordNotFound) end end end + describe 'json単体出力オプションに於いて' do + it 'includeキーがライセンスと素材を含んでいる' do + r = OriginalPicture.show_json_include_opt + r[:include].should eq [:license, :resource_picture] + end + end describe '一覧取得に於いて' do before do @op = Factory :original_picture, :artist_id => @artist.id @@ -280,6 +300,12 @@ describe OriginalPicture do end end end + describe 'json一覧出力オプションに於いて' do + it 'includeキーがライセンスと素材を含んでいる' do + r = OriginalPicture.list_json_opt + r[:include].should eq [:license, :resource_picture] + end + end describe 'RMagick変換に於いて' do before do @op = Factory.build :original_picture, :artist_id => @artist.id diff --git a/spec/models/panel_spec.rb b/spec/models/panel_spec.rb index ad5959d8..86d775e5 100644 --- a/spec/models/panel_spec.rb +++ b/spec/models/panel_spec.rb @@ -1,5 +1,664 @@ +# -*- encoding: utf-8 -*- require 'spec_helper' describe Panel do - pending "add some examples to (or delete) #{__FILE__}" + before do + Factory :admin + @license = Factory :license + @user = Factory( :user_yas) + @author = @user.author + @artist = Factory :artist_yas, :author_id => @author.id + @other_user = Factory( :user_yas) + @other_author = @other_user.author + @other_artist = Factory :artist_yas, :author_id => @other_author.id + + end + + describe '検証に於いて' do + before do + @comic = Factory :comic, :author_id => @author.id + end + + it 'オーソドックスなデータなら通る' do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + @panel.should be_valid + end + + context 'comic_idを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => nil + end + it 'テストデータの確認' do + @panel.comic_id = @comic.id + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.comic_id = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.comic_id = 'a' + @panel.should_not be_valid + end + it '存在するコミックでなければ失敗する' do + @panel.comic_id = 0 + @panel.should_not be_valid + end + it 'コミックidとtが重複していると失敗する' do + @panel.save + @panel2 = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + @panel2.should_not be_valid + end + end + context 'resource_picture_idを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.resource_picture_id = 1 + @panel.should be_valid + end + it 'nullなら通る' do + @panel.resource_picture_id = nil + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.resource_picture_id = 'a' + @panel.should_not be_valid + end + it '存在する素材でなければ失敗する' do + @panel.resource_picture_id = 0 + @panel.should_not be_valid + end + end + context 'widthを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.width = 1 + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.width = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.width = 'a' + @panel.should_not be_valid + end + it '0なら失敗する' do + @panel.width = '0' + @panel.should_not be_valid + end + it '負でも失敗する' do + @panel.width = -1 + @panel.should_not be_valid + end + end + context 'heightを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.height = '1' + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.height = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.height = 'a' + @panel.should_not be_valid + end + it '0なら失敗する' do + @panel.height = '0' + @panel.should_not be_valid + end + it '負でも失敗する' do + @panel.height = -1 + @panel.should_not be_valid + end + end + context 'borderを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.border = '1' + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.border = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.border = 'a' + @panel.should_not be_valid + end + it '負なら失敗する' do + @panel.border = '-1' + @panel.should_not be_valid + end + it '0なら通る' do + @panel.border = 0 + @panel.should be_valid + end + end + context 'xを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.x = '1' + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.x = 'a' + @panel.should_not be_valid + end + it '0なら通る' do + @panel.x = '0' + @panel.should be_valid + end + it '負でも通る' do + @panel.x = -1 + @panel.should be_valid + end + end + context 'yを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.y = '1' + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.y = 'a' + @panel.should_not be_valid + end + it '0なら通る' do + @panel.y = '0' + @panel.should be_valid + end + it '負でも通る' do + @panel.y = -1 + @panel.should be_valid + end + end + context 'zを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.z = '1' + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.z = 'a' + @panel.should_not be_valid + end + it '0なら失敗する' do + @panel.z = '0' + @panel.should_not be_valid + end + it '負なら失敗する' do + @panel.z = -1 + @panel.should_not be_valid + end + end + context 'tを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.t = '1' + @panel.should be_valid + end + it '数値でなければ失敗する' do + @panel.t = 'a' + @panel.should_not be_valid + end + it '0なら通る' do + @panel.t = '0' + @panel.should be_valid + end + it '負でも失敗する' do + @panel.t = -1 + @panel.should_not be_valid + end + end + context 'author_idを検証するとき' do + before do + @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @panel.author_id = @author.id + @panel.should be_valid + end + it 'nullなら失敗する' do + @panel.author_id = nil + @panel.should_not be_valid + end + it '数値でなければ失敗する' do + @panel.author_id = 'a' + @panel.should_not be_valid + end + it '存在する絵師でなければ失敗する' do + @panel.author_id = 0 + @panel.should_not be_valid + end + end + context '全体を検証するとき' do + before do + @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + end + end + end + + describe 'データ補充に於いて' do + before do + @comic = Factory :comic, :author_id => @author.id + @panel = Factory.build :panel, :comic_id => @comic.id + end + context 'widthを補充' do + it '空の時はコミックから補充する' do + @panel.width = nil + @panel.supply_default @author + @panel.width.should eq @comic.width + end + it '空の時でもコミックが不在なら補充しない' do + @panel.width = nil + @panel.comic_id = nil + @panel.supply_default @author + @panel.width.should be_nil + end + it '空でない時は変化しない' do + @panel.width = 45 + lambda { + @panel.supply_default @author + }.should_not change @panel, :width + end + end + context 'heightを補充' do + it '空の時はコミックから補充する' do + @panel.height = nil + @panel.supply_default @author + @panel.height.should eq @comic.height + end + it '空の時でもコミックが不在なら補充しない' do + @panel.height = nil + @panel.comic_id = nil + @panel.supply_default @author + @panel.height.should be_nil + end + it '空でない時は変化しない' do + @panel.height = 87 + lambda { + @panel.supply_default @author + }.should_not change @panel, :height + end + end + context 'borderを補充' do + it '空の時は0を補充する' do + @panel.border = nil + @panel.supply_default @author + @panel.border.should eq 0 + end + it '空でない時は変化しない' do + @panel.border = 1 + lambda { + @panel.supply_default @author + }.should_not change @panel, :border + end + end + context 'tを補充' do + it '空の時はコミック内のtの最大値+1を補充する' do + pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 0 + @panel.t = nil + @panel.supply_default @author + @panel.t.should eq 1 + end + it '空でコミック初のコマなら0を補充する' do + @panel.t = nil + @panel.supply_default @author + @panel.t.should eq 0 + end + it '空の時でも更新ケースなら補充しない' do + pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 1 + pl.t = nil + lambda { + pl.supply_default @author + }.should_not change pl, :t + end + it '空でない時は変化しない' do + @panel.t = 1 + lambda { + @panel.supply_default @author + }.should_not change @panel, :t + end + end + context 'author_idを補充' do + it 'ログイン中の作家idを補充する' do + @panel.author_id = nil + @panel.supply_default @author + @panel.author_id.should eq @author.id + end + end + + end + + describe '作者判定に於いて' do + before do + @comic = Factory :comic, :author_id => @author.id + end + it '自作のコマならyes' do + panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + panel.own?(@author).should == true + end + it '他人のコマならno' do + panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id + panel.own?(@author).should == false + end + it '作家が不明ならno' do + panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + panel.own?(nil).should == false + end + end + describe '閲覧許可に於いて' do + before do + @comic = Factory :comic, :author_id => @author.id + end + it '自作の公開コミックのコマを見るときは許可する' do + Comic.any_instance.stub(:visible?).and_return(true) + panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + panel.visible?(@author).should == true + end + it '自作のコマは非公開コミックでも許可する' do + Comic.any_instance.stub(:visible?).and_return(false) + panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + panel.visible?(@author).should == true + end + it '他人のコマでも公開コミックなら許可する' do + Comic.any_instance.stub(:visible?).and_return(true) + panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id + panel.visible?(@author).should == true + end + it '他人のコマで非公開コミックなら許可しない' do + Comic.any_instance.stub(:visible?).and_return(false) + panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id + panel.visible?(@author).should == false + end + end + describe '単体取得に於いて' do + before do + @comic = Factory :comic, :author_id => @author.id + @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id + end + it '指定のコマを返す' do + Comic.any_instance.stub(:visible?).and_return(true) + pl = Panel.show @panel.id, @author + pl.should eq @panel + end + context '他人の非公開コミックのコマを開こうとしたとき' do + it '403Forbidden例外を返す' do + Panel.any_instance.stub(:visible?).and_return(false) + lambda{ + Panel.show @panel.id, @author + }.should raise_error(ActiveRecord::Forbidden) + end + end + context '存在しないコマを開こうとしたとき' do + it '404RecordNotFound例外を返す' do + lambda{ + Panel.show 110, @author + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + describe '関連テーブルプションに於いて' do + context 'オプションがないとき' do + it '4つの項目を含んでいる' do + r = Panel.show_include_opt + r.should have(4).items + end + it 'コミックを含んでいる' do + r = Panel.show_include_opt + r.has_key?(:comic).should be_true + end + it 'コマ絵を含んでいる' do + r = Panel.show_include_opt + r.has_key?(:panel_pictures).should be_true + end + it 'コマ絵は素材を含んでいる' do + r = Panel.show_include_opt + r[:panel_pictures].has_key?(:resource_picture).should be_true + end + it '素材は絵師を含んでいる' do + r = Panel.show_include_opt + r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true + end + it '素材はライセンスを含んでいる' do + r = Panel.show_include_opt + r[:panel_pictures][:resource_picture].has_key?(:license).should be_true + end + it 'フキダシを含んでいる' do + r = Panel.show_include_opt + r.has_key?(:balloons).should be_true + end + it 'フキダシはセリフを含んでいる' do + r = Panel.show_include_opt + r[:balloons].has_key?(:speaches).should be_true + end + it '作家を含んでいる' do + r = Panel.show_include_opt + r.has_key?(:author).should be_true + end + end + context 'オプションで原画を含ませたとき' do + it '5つの項目を含んでいる' do + r = Panel.show_include_opt(:include => {:test => {}}) + r.should have(5).items + end + it 'testを含んでいる' do + r = Panel.show_include_opt(:include => {:test => {}}) + r.has_key?(:test).should be_true + end + end + end + describe 'json単体出力オプションに於いて' do + it 'includeキーを含んでいる' do + r = Panel.show_json_include_opt + r.has_key?(:include).should be_true + end + it '4つの項目を含んでいる' do + r = Panel.show_json_include_opt[:include] + r.should have(4).items + end + it 'コミックを含んでいる' do + r = Panel.show_json_include_opt[:include] + r.has_key?(:comic).should be_true + end + it 'コマ絵を含んでいる' do + r = Panel.show_json_include_opt[:include] + r.has_key?(:panel_pictures).should be_true + end + it 'コマ絵は素材を含んでいる' do + r = Panel.show_json_include_opt[:include] + r[:panel_pictures].has_key?(:resource_picture).should be_true + end + it '素材は絵師を含んでいる' do + r = Panel.show_json_include_opt[:include] + r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true + end + it '素材はライセンスを含んでいる' do + r = Panel.show_json_include_opt[:include] + r[:panel_pictures][:resource_picture].has_key?(:license).should be_true + end + it 'フキダシを含んでいる' do + r = Panel.show_json_include_opt[:include] + r.has_key?(:balloons).should be_true + end + it 'フキダシはセリフを含んでいる' do + r = Panel.show_json_include_opt[:include] + r[:balloons].has_key?(:speaches).should be_true + end + it '作家を含んでいる' do + r = Panel.show_json_include_opt[:include] + r.has_key?(:author).should be_true + end + end + describe '一覧取得に於いて' do + before do + @comic = Factory :comic, :author_id => @author.id + @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id + end + context 'page補正について' do + it '文字列から数値に変換される' do + Panel.page('8').should eq 8 + end + it 'nilの場合は1になる' do + Panel.page().should eq 1 + end + it '0以下の場合は1になる' do + Panel.page('0').should eq 1 + end + end + context 'page_size補正について' do + it '文字列から数値に変換される' do + Panel.page_size('7').should eq 7 + end + it 'nilの場合はPanel.default_page_sizeになる' do + Panel.page_size().should eq Panel.default_page_size + end + it '0以下の場合はPanel.default_page_sizeになる' do + Panel.page_size('0').should eq Panel.default_page_size + end + it 'Panel.max_page_sizeを超えた場合はPanel.max_page_sizeになる' do + Panel.page_size('1000').should eq Panel.max_page_size + end + end + it 'リストを返す' do + pl = Panel.list + pl.should eq [@panel] + end + it '時系列で並んでいる' do + npl = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1 + pl = Panel.list + pl.should eq [npl, @panel] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + @npl2 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1 + @npl3 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 2 + @npl4 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 3 + @npl5 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 4 + Panel.stub(:default_page_size).and_return(2) + end + it '通常は2件を返す' do + pl = Panel.list + pl.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + pl = Panel.list( {}, 1) + pl.should eq [@npl5, @npl4] + end + it 'page=2なら中間2件を返す' do + pl = Panel.list({}, 2) + pl.should eq [@npl3, @npl2] + end + it 'page=3なら先頭1件を返す' do + pl = Panel.list({}, 3) + pl.should eq [@panel] + end + end + end + describe 'list関連テーブルプションに於いて' do + it 'includeキーを含んでいる' do + r = Panel.list_opt + r.has_key?(:include).should be_true + end + it '4つの項目を含んでいる' do + r = Panel.list_opt[:include] + r.should have(4).items + end + it 'コミックを含んでいる' do + r = Panel.list_opt[:include] + r.has_key?(:comic).should be_true + end + it 'コマ絵を含んでいる' do + r = Panel.list_opt[:include] + r.has_key?(:panel_pictures).should be_true + end + it 'コマ絵は素材を含んでいる' do + r = Panel.list_opt[:include] + r[:panel_pictures].has_key?(:resource_picture).should be_true + end + it '素材は絵師を含んでいる' do + r = Panel.list_opt[:include] + r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true + end + it '素材はライセンスを含んでいる' do + r = Panel.list_opt[:include] + r[:panel_pictures][:resource_picture].has_key?(:license).should be_true + end + it 'フキダシを含んでいる' do + r = Panel.list_opt[:include] + r.has_key?(:balloons).should be_true + end + it 'フキダシはセリフを含んでいる' do + r = Panel.list_opt[:include] + r[:balloons].has_key?(:speaches).should be_true + end + it '作家を含んでいる' do + r = Panel.list_opt[:include] + r.has_key?(:author).should be_true + end + end + describe 'json一覧出力オプションに於いて' do + it 'includeキーを含んでいる' do + r = Panel.list_json_opt + r.has_key?(:include).should be_true + end + it '4つの項目を含んでいる' do + r = Panel.list_json_opt[:include] + r.should have(4).items + end + it 'コミックを含んでいる' do + r = Panel.list_json_opt[:include] + r.has_key?(:comic).should be_true + end + it 'コマ絵を含んでいる' do + r = Panel.list_json_opt[:include] + r.has_key?(:panel_pictures).should be_true + end + it 'コマ絵は素材を含んでいる' do + r = Panel.list_json_opt[:include] + r[:panel_pictures].has_key?(:resource_picture).should be_true + end + it '素材は絵師を含んでいる' do + r = Panel.list_json_opt[:include] + r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true + end + it '素材はライセンスを含んでいる' do + r = Panel.list_json_opt[:include] + r[:panel_pictures][:resource_picture].has_key?(:license).should be_true + end + it 'フキダシを含んでいる' do + r = Panel.list_json_opt[:include] + r.has_key?(:balloons).should be_true + end + it 'フキダシはセリフを含んでいる' do + r = Panel.list_json_opt[:include] + r[:balloons].has_key?(:speaches).should be_true + end + it '作家を含んでいる' do + r = Panel.list_json_opt[:include] + r.has_key?(:author).should be_true + end + end + end diff --git a/spec/models/resource_picture_spec.rb b/spec/models/resource_picture_spec.rb index 9f6a1f32..76e117c5 100644 --- a/spec/models/resource_picture_spec.rb +++ b/spec/models/resource_picture_spec.rb @@ -208,6 +208,119 @@ describe ResourcePicture do end + describe '単体取得に於いて' do + before do + @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id + @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id + end + it '指定の素材を返す' do + pic = ResourcePicture.show @rp.id + pic.should eq @rp + end + context '関連テーブルオプションがないとき' do + it 'ライセンスと絵師を含んでいる' do + r = ResourcePicture.show_include_opt + r.should eq [:license, :artist] + end + end + context '関連テーブルオプションで原画を含ませたとき' do + it 'ライセンスと絵師と原画データを含んでいる' do + r = ResourcePicture.show_include_opt(:include => :original_picture) + r.should eq [:license, :artist, :original_picture] + end + end + context '存在しない素材を開こうとしたとき' do + it '404RecordNotFound例外を返す' do + lambda{ + pic = ResourcePicture.show 0 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + describe 'json単体出力オプションに於いて' do + it 'includeキーがライセンスと絵師を含んでいる' do + r = ResourcePicture.show_json_include_opt + r[:include].should eq [:license, :artist] + end + end + describe '一覧取得に於いて' do + before do + @op = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id + @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id + end + context 'page補正について' do + it '文字列から数値に変換される' do + ResourcePicture.page('8').should eq 8 + end + it 'nilの場合は1になる' do + ResourcePicture.page().should eq 1 + end + it '0以下の場合は1になる' do + ResourcePicture.page('0').should eq 1 + end + end + context 'page_size補正について' do + it '文字列から数値に変換される' do + ResourcePicture.page_size('7').should eq 7 + end + it 'nilの場合はResourcePicture.default_page_sizeになる' do + ResourcePicture.page_size().should eq ResourcePicture.default_page_size + end + it '0以下の場合はResourcePicture.default_page_sizeになる' do + ResourcePicture.page_size('0').should eq ResourcePicture.default_page_size + end + it 'ResourcePicture.max_page_sizeを超えた場合はResourcePicture.max_page_sizeになる' do + ResourcePicture.page_size('1000').should eq ResourcePicture.max_page_size + end + end + it 'リストを返す' do + pic = ResourcePicture.list + pic.should eq [@rp] + end + it '時系列で並んでいる' do + nop = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id + nrp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id + pic = ResourcePicture.list + pic.should eq [nrp, @rp] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + nop2 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id + @nrp2 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id + nop3 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id + @nrp3 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id + nop4 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id + @nrp4 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id + nop5 = Factory :original_picture, :artist_id => @artist.id, :license_id => @license.id + @nrp5 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id + ResourcePicture.stub(:default_page_size).and_return(2) + end + it '通常は2件を返す' do + pic = ResourcePicture.list + pic.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + pic = ResourcePicture.list({}, 1) + pic.should eq [@nrp5, @nrp4] + end + it 'page=2なら中間2件を返す' do + pic = ResourcePicture.list({}, 2) + pic.should eq [@nrp3, @nrp2] + end + it 'page=3なら先頭1件を返す' do + pic = ResourcePicture.list({}, 3) + pic.should eq [@rp] + end + end + end + describe 'json一覧出力オプションに於いて' do + it 'includeキーがライセンスと絵師を含んでいる' do + r = ResourcePicture.list_json_opt + r[:include].should eq [:license, :artist] + end + end + describe 'サイズの調整に於いて' do before do end diff --git a/spec/models/speach_balloon_template_spec.rb b/spec/models/speach_balloon_template_spec.rb new file mode 100644 index 00000000..c37394b8 --- /dev/null +++ b/spec/models/speach_balloon_template_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe SpeachBalloonTemplate do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/speach_spec.rb b/spec/models/speach_spec.rb index 6ac39920..660be6dd 100644 --- a/spec/models/speach_spec.rb +++ b/spec/models/speach_spec.rb @@ -1,5 +1,263 @@ +# -*- encoding: utf-8 -*- require 'spec_helper' describe Speach do - pending "add some examples to (or delete) #{__FILE__}" + before do + Factory :admin + @license = Factory :license + @user = Factory( :user_yas) + @author = @user.author + @artist = Factory :artist_yas, :author_id => @author.id + @other_user = Factory( :user_yas) + @other_author = @other_user.author + @other_artist = Factory :artist_yas, :author_id => @other_author.id + + end + + describe '検証に於いて' do + before do + @st = Factory :speach_template + @comic = Factory :comic, :author_id => @author.id + @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + @balloon = Factory :balloon, :panel_id => @panel.id, :t => 0 + @speach = Factory.build :speach, :balloon => @balloon.id, :t => 0 + end + + it 'オーソドックスなデータなら通る' do + @speach.should be_valid + end + + context 'balloon_idを検証するとき' do + before do + end + it 'テストデータの確認' do + @speach.comic_id = @comic.id + @speach.should be_valid + end + it 'nullなら失敗する' do + @speach.comic_id = nil + @speach.should_not be_valid + end + it '数値でなければ失敗する' do + @speach.comic_id = 'a' + @speach.should_not be_valid + end + it '存在するコミックでなければ失敗する' do + @speach.comic_id = 0 + @speach.should_not be_valid + end + it 'コミックidとtが重複していると失敗する' do + @speach.save + @speach2 = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + @speach2.should_not be_valid + end + end + context 'resource_picture_idを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.resource_picture_id = 1 + @speach.should be_valid + end + it 'nullなら通る' do + @speach.resource_picture_id = nil + @speach.should be_valid + end + it '数値でなければ失敗する' do + @speach.resource_picture_id = 'a' + @speach.should_not be_valid + end + it '存在する素材でなければ失敗する' do + @speach.resource_picture_id = 0 + @speach.should_not be_valid + end + end + context 'widthを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.width = 1 + @speach.should be_valid + end + it 'nullなら失敗する' do + @speach.width = nil + @speach.should_not be_valid + end + it '数値でなければ失敗する' do + @speach.width = 'a' + @speach.should_not be_valid + end + it '0なら失敗する' do + @speach.width = '0' + @speach.should_not be_valid + end + it '負でも失敗する' do + @speach.width = -1 + @speach.should_not be_valid + end + end + context 'heightを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.height = '1' + @speach.should be_valid + end + it 'nullなら失敗する' do + @speach.height = nil + @speach.should_not be_valid + end + it '数値でなければ失敗する' do + @speach.height = 'a' + @speach.should_not be_valid + end + it '0なら失敗する' do + @speach.height = '0' + @speach.should_not be_valid + end + it '負でも失敗する' do + @speach.height = -1 + @speach.should_not be_valid + end + end + context 'borderを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.border = '1' + @speach.should be_valid + end + it 'nullなら失敗する' do + @speach.border = nil + @speach.should_not be_valid + end + it '数値でなければ失敗する' do + @speach.border = 'a' + @speach.should_not be_valid + end + it '負なら失敗する' do + @speach.border = '-1' + @speach.should_not be_valid + end + it '0なら通る' do + @speach.border = 0 + @speach.should be_valid + end + end + context 'xを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.x = '1' + @speach.should be_valid + end + it '数値でなければ失敗する' do + @speach.x = 'a' + @speach.should_not be_valid + end + it '0なら通る' do + @speach.x = '0' + @speach.should be_valid + end + it '負でも通る' do + @speach.x = -1 + @speach.should be_valid + end + end + context 'yを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.y = '1' + @speach.should be_valid + end + it '数値でなければ失敗する' do + @speach.y = 'a' + @speach.should_not be_valid + end + it '0なら通る' do + @speach.y = '0' + @speach.should be_valid + end + it '負でも通る' do + @speach.y = -1 + @speach.should be_valid + end + end + context 'zを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.z = '1' + @speach.should be_valid + end + it '数値でなければ失敗する' do + @speach.z = 'a' + @speach.should_not be_valid + end + it '0なら失敗する' do + @speach.z = '0' + @speach.should_not be_valid + end + it '負なら失敗する' do + @speach.z = -1 + @speach.should_not be_valid + end + end + context 'tを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.t = '1' + @speach.should be_valid + end + it '数値でなければ失敗する' do + @speach.t = 'a' + @speach.should_not be_valid + end + it '0なら通る' do + @speach.t = '0' + @speach.should be_valid + end + it '負でも失敗する' do + @speach.t = -1 + @speach.should_not be_valid + end + end + context 'author_idを検証するとき' do + before do + @speach = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id + end + it 'テストデータの確認' do + @speach.author_id = @author.id + @speach.should be_valid + end + it 'nullなら失敗する' do + @speach.author_id = nil + @speach.should_not be_valid + end + it '数値でなければ失敗する' do + @speach.author_id = 'a' + @speach.should_not be_valid + end + it '存在する絵師でなければ失敗する' do + @speach.author_id = 0 + @speach.should_not be_valid + end + end + context '全体を検証するとき' do + before do + @speach = Factory :panel, :author_id => @author.id, :comic_id => @comic.id + end + end + end + end diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb index 47080ad5..3b4d0e72 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -1,13 +1,2 @@ module ControllerMacros - def login_user - before(:each) do - @request.env["devise.mapping"] = Devise.mappings[:user] - @user = Admin.create(:email => "hoge@gmail.com", - :screen_name => "naberon", - :password => "user0100", - :password_confirmation => "user0100") -# @admin.confirm! - sign_in @user - end - end end -- 2.11.0