Skip to content

Commit

Permalink
Updated tests to match new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorcorrea committed May 17, 2022
1 parent 46f4c1b commit a6b2257
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 232 deletions.
5 changes: 2 additions & 3 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index

def new
default_collection_id = current_user.default_collection.id
work = Work.create_skeleton("New Work", current_user.id, default_collection_id, "DATASET")
work = Work.create_skeleton("New Dataset", current_user.id, default_collection_id, "DATASET")
redirect_to edit_work_path(work)
end

Expand Down Expand Up @@ -45,8 +45,7 @@ def update
format.html { redirect_to work_url(@work), notice: "Work was successfully updated." }
format.json { render :show, status: :ok, location: @work }
else
work = Work.find(@dataset.work_id)
@datacite = Datacite::Resource.new_from_json(work.data_cite)
@datacite = Datacite::Resource.new_from_json(@work.data_cite)
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ def self.research_data
create_defaults
Collection.where(code: "RD").first
end

def self.library_resources
create_defaults
Collection.where(code: "LIB").first
end
end
18 changes: 17 additions & 1 deletion app/models/work.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# rubocop:disable Metrics/ClassLength
class Work < ApplicationRecord
include Rails.application.routes.url_helpers

Expand All @@ -13,7 +14,9 @@ class Work < ApplicationRecord
end

before_update do |ds|
if ds.ark.blank?
if dublin_core.present?
# we don't mint ARKs for these records
elsif ds.ark.blank?
ds.ark = Ark.mint
end
end
Expand Down Expand Up @@ -50,6 +53,18 @@ def self.create_skeleton(title, user_id, collection_id, work_type)
work
end

# def self.create_dataset(title, user_id, collection_id, work_type)
# work = Work.new(
# title: title,
# created_by_user_id: user_id,
# collection_id: collection_id,
# work_type: "DATASET",
# state: "AWAITING-APPROVAL"
# )
# work.save!
# work
# end

def dataset_id
Dataset.where(work_id: id).first&.id
end
Expand Down Expand Up @@ -150,3 +165,4 @@ def self.admin_works_by_user_state(user, state)
works
end
end
# rubocop:ensable Metrics/ClassLength
2 changes: 2 additions & 0 deletions app/values/dublin_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ def initialize(json)
@json = json
end

delegate :present?, to: :attributes

def attributes
@attributes ||= json_object
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

require "rails_helper"

RSpec.describe DatasetsController, mock_ezid_api: true do
RSpec.describe WorksController, mock_ezid_api: true do
before do
Collection.create_defaults
user
end
let(:user) { FactoryBot.create(:user) }
let(:collection) { Collection.first }
let(:ds) { Dataset.create_skeleton("test dataset", user.id, collection.id) }
let(:ezid) { ds.ark }
let(:work) { ds.work }
let(:work) { Work.create_skeleton("test dataset", user.id, collection.id, "DATASET") }

context "valid user login" do
it "handles the index page" do
Expand All @@ -22,80 +20,72 @@

it "handles the show page" do
sign_in user
get :show, params: { id: ds.id }
get :show, params: { id: work.id }
expect(response).to render_template("show")
end

it "renders the edit page when creating a new dataset" do
sign_in user
post :new
expect(response.status).to be 302
expect(response.location.start_with?("http://test.host/datasets/")).to be true
expect(response.location.start_with?("http://test.host/works/")).to be true
end

it "renders the edit page on edit" do
sign_in user
get :edit, params: { id: ds.id }
get :edit, params: { id: work.id }
expect(response).to render_template("edit")
end

it "handles the update page" do
params = {
"dataset" => {
"work_id" => ds.work.id
},
"title" => "test dataset updated",
"collection_id" => ds.work.collection.id,
"collection_id" => work.collection.id,
"commit" => "Update Dataset",
"controller" => "datasets",
"controller" => "works",
"action" => "update",
"id" => ds.id.to_s
"id" => work.id.to_s
}
sign_in user
post :update, params: params
expect(response.status).to be 302
expect(response.location).to eq "http://test.host/datasets/#{ds.id}"
expect(response.location).to eq "http://test.host/works/#{work.id}"
end

it "handles aprovals" do
sign_in user
post :approve, params: { id: ds.id }
post :approve, params: { id: work.id }
expect(response.status).to be 302
expect(response.location).to eq "http://test.host/datasets/#{ds.id}"
expect(response.location).to eq "http://test.host/works/#{work.id}"
end

it "handles withdraw" do
sign_in user
post :withdraw, params: { id: ds.id }
post :withdraw, params: { id: work.id }
expect(response.status).to be 302
expect(response.location).to eq "http://test.host/datasets/#{ds.id}"
expect(response.location).to eq "http://test.host/works/#{work.id}"
end

it "handles resubmit" do
sign_in user
post :resubmit, params: { id: ds.id }
post :resubmit, params: { id: work.id }
expect(response.status).to be 302
expect(response.location).to eq "http://test.host/datasets/#{ds.id}"
expect(response.location).to eq "http://test.host/works/#{work.id}"
end

it "handles the show page" do
sign_in user
get :datacite, params: { id: ds.id }
get :datacite, params: { id: work.id }
expect(response.body.start_with?('<?xml version="1.0"?>')).to be true
end
end

describe "#update" do
let(:params) do
{
id: ds.id,
title: ds.title,
id: work.id,
title: work.title,
collection_id: collection.id,
dataset: {
id: ds.id,
work_id: work.id,
format: format
},
new_title_1: "the subtitle",
new_title_type_1: "Subtitle",
existing_title_count: "1",
Expand All @@ -118,7 +108,7 @@
before do
sign_in user
allow(Work).to receive(:find).and_return(work)
allow_any_instance_of(Dataset).to receive(:update).and_return(false)
allow_any_instance_of(Work).to receive(:update).and_return(false)
patch :update, params: params
end

Expand All @@ -136,7 +126,7 @@
before do
sign_in user
allow(Work).to receive(:find).and_return(work)
allow_any_instance_of(Dataset).to receive(:update).and_return(false)
allow_any_instance_of(Work).to receive(:update).and_return(false)
patch :update, params: params
end

Expand Down
59 changes: 0 additions & 59 deletions spec/factories/dataset.rb

This file was deleted.

18 changes: 18 additions & 0 deletions spec/factories/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,72 @@
factory :shakespeare_and_company_work do
title { "Shakespeare and Company Project Dataset: Lending Library Members, Books, Events" }
collection { Collection.research_data }
doi { "https://doi.org/10.34770/pe9w-x904" }
ark { "ark:/88435/dsp01zc77st047" }
created_by_user_id { FactoryBot.create(:user).id }
end

factory :sowing_the_seeds_work do
title { "Sowing the Seeds for More Usable Web Archives: A Usability Study of Archive-It" }
collection { Collection.research_data }
doi { "" } # no DOI associated with this dataset
ark { "ark:/88435/dsp01d791sj97j" }
created_by_user_id { FactoryBot.create(:user).id }
end

factory :distinct_cytoskeletal_proteins_work do
title { "Distinct cytoskeletal proteins define zones of enhanced cell wall synthesis in Helicobacter pylori" }
collection { Collection.research_data }
doi { "https://doi.org/10.34770/r2dz-ys12" }
ark { "ark:/88435/dsp01h415pd457" }
created_by_user_id { FactoryBot.create(:user).id }
end

factory :attention_and_awareness_work do
title { "Attention and awareness in the dorsal attention network" }
collection { Collection.research_data }
doi { "https://doi.org/10.34770/9425-b553" }
ark { "ark:/88435/dsp01xp68kk27p" }
created_by_user_id { FactoryBot.create(:user).id }
end

factory :femtosecond_xray_work do
title { "Femtosecond X-ray Diffraction of Laser-shocked Forsterite (Mg2SiO4) to 122 GPa" }
collection { Collection.research_data }
doi { "https://doi.org/10.34770/gg40-tc15" }
ark { "ark:/88435/dsp01rj4307478" }
created_by_user_id { FactoryBot.create(:user).id }
end

factory :bitklavier_work do
title { "bitKlavier Grand Sample Library—Piano Bar Mic Image" }
collection { Collection.research_data }
doi { "https://doi.org/10.34770/r75s-9j74" }
ark { "ark:/88435/dsp015999n653h" }
created_by_user_id { FactoryBot.create(:user).id }
end

factory :design_and_simulation_of_the_snowflake_work do
title { "Design and simulation of the snowflake divertor control for NSTX-U" }
collection { Collection.research_data }
doi { "" } # no DOI associated with this dataset
ark { "ark:/88435/dsp01jm214r94t" }
created_by_user_id { FactoryBot.create(:user).id }
end

factory :whistler_wave_generation_work do
title { "Whistler wave generation by anisotropic tail electrons during asymmetric magnetic reconnection in space and laboratory" }
collection { Collection.research_data }
doi { "" } # no DOI associated with this dataset
ark { "ark:/88435/dsp01t148fk89s" }
created_by_user_id { FactoryBot.create(:user).id }
end

factory :supplemental_data_work do
title { "Supplementary data for thesis: The Evolution and Regulation of Morphological Complexity in the Vibrios" }
collection { Collection.research_data }
doi { "https://doi.org/10.34770/gk6n-gj34" }
ark { "ark:/88435/dsp01vq27zr562" }
created_by_user_id { FactoryBot.create(:user).id }
end

Expand Down
Loading

0 comments on commit a6b2257

Please sign in to comment.