myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2862] branches/datasets: More tests + fixing dep


From: noreply
Subject: [myexperiment-hackers] [2862] branches/datasets: More tests + fixing deprecation warnings
Date: Mon, 5 Dec 2011 11:16:12 -0500 (EST)

Revision
2862
Author
fbacall
Date
2011-12-05 11:16:11 -0500 (Mon, 05 Dec 2011)

Log Message

More tests + fixing deprecation warnings

Modified Paths

Added Paths

Diff

Modified: branches/datasets/app/controllers/data_items_controller.rb (2861 => 2862)


--- branches/datasets/app/controllers/data_items_controller.rb	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/app/controllers/data_items_controller.rb	2011-12-05 16:16:11 UTC (rev 2862)
@@ -15,12 +15,9 @@
     @errors = []
     @data_item = Relationship.new
 
-    data = ""
-    port = get_port
-    predicate = Predicate.find(params[:predicate_id])
-
-    if @errors.empty? && @data_item.update_attributes(:subject => data, :objekt => port, :predicate => predicate,
-                                                      :context => @data_set)
+    if (port = get_port) && (data = "" &&
+       @data_item.update_attributes(:subject => data, :objekt => port,
+                                    :predicate => Predicate.find(params[:predicate_id]), :context => @data_set)
       respond_to do |format|
         format.html {render :partial => "data_sets/data_set", :object => @data_set}
       end
@@ -35,17 +32,16 @@
   def update
     @errors = []
 
-    data = ""
-    port = get_port
-    predicate = Predicate.find(params[:predicate_id])
-
-    if @errors.empty? && @data_item.update_attributes(:subject => data, :objekt => port, :predicate => predicate)
+    if (port = get_port) && (data = "" &&
+       @data_item.update_attributes(:subject => data, :objekt => port,
+                                    :predicate => Predicate.find(params[:predicate_id]), :context => @data_set)
       respond_to do |format|
         format.html {render :partial => "data_sets/data_item", :object => @data_item,
                             :locals => {:port_type => params[:workflow_port_type]}}
       end
     else
       @errors = @errors + @data_item.errors.full_messages
+      @errors.each {|e| puts e}
       respond_to do |format|
         format.html {render :partial => "data_sets/errors", :status => 400}
       end
@@ -110,12 +106,14 @@
         data = "" => params[:data], :data_set => @data_set)
         unless data.save
           @errors = @errors + data.errors.full_messages
+          return false
         end
       #Otherwise just update the existing text data
       else
         data = ""
         unless data.update_attributes(:data ="" params[:data])
           @errors = @errors + data.errors.full_messages
+          return false
         end
       end
     elsif params[:data_type] == "file"
@@ -126,7 +124,7 @@
 
       data = ""
     else
-      @errors << "Invalid data type specified."
+      raise "Invalid data type specified."
     end
 
     data
@@ -141,9 +139,7 @@
     if @data_item.objekt
       #But only if port wasn't changed in the form
       if (params[:workflow_port] == @data_item.objekt.name) &&
-         (port_type == @data_item.objekt.port_type.to_s) &&
-         (version == @data_item.objekt.workflow_version)
-
+         (port_type == @data_item.objekt.port_type.to_s)
         port = @data_item.objekt
       end
     end
@@ -153,6 +149,14 @@
       port = WorkflowPort.find(:first,
                                :conditions => ["name = ? AND port_type = ? AND workflow_id = ? AND workflow_version = ?",
                                                              params[:workflow_port], port_type, @workflow.id, version])
+
+      #If we found a port, check if there isn't already some data connected to it within this data set
+      if port && Relationship.find(:first, :conditions => ["context_type = 'DataSet' AND context_id = ? AND " +
+                                                   "objekt_type = 'WorkflowPort' AND objekt_id = ?",
+                                                   @data_set.id, port.id])
+        @errors << "Data already exists for this port."
+        return false
+      end
     end
 
     #If we still don't have a port, make one
@@ -166,9 +170,11 @@
                                 :port_type => port_type, :workflow_version => version)
         unless port.save
           @errors = @errors + port.errors.full_messages
+          return false
         end
       else
         @errors << "Invalid port name specified."
+        return false
       end
     end
 
@@ -199,10 +205,11 @@
   end
 
   def auth
-    unless Authorization.is_authorized?(action_name, nil, @workflow, current_user)
+    unless Authorization.is_authorized?("edit", nil, @data_set, current_user)
       respond_to do |format|
         format.html { render :nothing => true, :status => 403 }
       end
     end
   end
+
 end
\ No newline at end of file

Modified: branches/datasets/app/helpers/application_helper.rb (2861 => 2862)


--- branches/datasets/app/helpers/application_helper.rb	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/app/helpers/application_helper.rb	2011-12-05 16:16:11 UTC (rev 2862)
@@ -749,6 +749,8 @@
       return "famfamfam_silk/award_star_delete.png"
     when "service"
       return "biocat_icon.png"
+    when "dataset"
+      return "redmond_studio/copy_16.png"
     else
       return Conf.label_icons[method.to_s] if Conf.label_icons[method.to_s]
     end

Modified: branches/datasets/app/views/data_sets/_data_item.rhtml (2861 => 2862)


--- branches/datasets/app/views/data_sets/_data_item.rhtml	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/app/views/data_sets/_data_item.rhtml	2011-12-05 16:16:11 UTC (rev 2862)
@@ -25,7 +25,7 @@
         <% can_view = can_download || Authorization.is_authorized?("view", nil, data, current_user) %>
 			  <% if can_view -%>
           <div style="float:left">
-            <b>Title:</b> <%= link_to "#{h truncate(data.label, 70)}", file_path(data), :class => "file_link" %><br/>
+            <b>Title:</b> <%= link_to "#{h truncate(data.label, :length => 70)}", file_path(data), :class => "file_link" %><br/>
             <b>Type: </b> <%= h data.content_type.title %><br/>
             <b>Size: </b> <%= number_to_human_size(data.content_blob.data.size) %><br/>
           </div>

Modified: branches/datasets/app/views/data_sets/_data_item_form.rhtml (2861 => 2862)


--- branches/datasets/app/views/data_sets/_data_item_form.rhtml	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/app/views/data_sets/_data_item_form.rhtml	2011-12-05 16:16:11 UTC (rev 2862)
@@ -7,13 +7,13 @@
 
   port_names_for_select = @port_names.map do |p|
     %(<option #{"selected=\"selected\"" if (port && port.name == p)}
-        value="#{p}" title="#{h p}">#{h truncate(p,20)}</option>)
-  end
+        value="#{p}" title="#{h p}">#{h truncate(p, :length => 20)}</option>)
+  end.join.html_safe
 
   blob_names_for_select = @blobs.map do |b|
     %(<option #{"selected=\"selected\"" if (data == b)}
-        value="#{b.id}" title="#{h b.title}">#{h truncate(b.title,30)}</option>)
-  end
+        value="#{b.id}" title="#{h b.title}">#{h truncate(b.title, :length => 30)}</option>)
+  end.join.html_safe
 
   container_element = @data_item.id.nil? ? "data_item_#{port_type}_form" : "address@hidden"
   unique_id = (@data_item.id || port_type).to_s

Modified: branches/datasets/app/views/data_sets/show.rhtml (2861 => 2862)


--- branches/datasets/app/views/data_sets/show.rhtml	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/app/views/data_sets/show.rhtml	2011-12-05 16:16:11 UTC (rev 2862)
@@ -1,7 +1,7 @@
 <div id="data_sets_container">
   <div id="data_set_selector" style="float: left" class="sectionIcons">
     Jump to data set:
-    <%= select_tag :data_set, (@data_sets.collect {|d| "<option #{"selected = \"selected\"" if d == @data_set}value=\"#{data_set_url(d.id)}\">#{truncate h(d.title), 25}</option>"}),
+    <%= select_tag :data_set, (@data_sets.map {|d| "<option #{"selected = \"selected\"" if d == @data_set}value=\"#{data_set_url(d.id)}\">#{truncate h(d.title), :length => 25}</option>"}).join.html_safe,
                    : "window.location = $F('data_set');",
                    :style => "width: 15em"%>
   </div>

Modified: branches/datasets/test/fixtures/contributions.yml (2861 => 2862)


--- branches/datasets/test/fixtures/contributions.yml	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/test/fixtures/contributions.yml	2011-12-05 16:16:11 UTC (rev 2862)
@@ -129,4 +129,16 @@
   contributor_id: 1 # John
   contributor_type: User
   contributable: string_concat_v1_example (DataSet)
+  policy: only_john_can_edit
+
+contribution_data_set_string_concat_v1_empty:
+  contributor_id: 1 # John
+  contributor_type: User
+  contributable: string_concat_v1_empty (DataSet)
+  policy: only_john_can_edit
+
+contribution_data_set_string_concat_v2_empty:
+  contributor_id: 1 # John
+  contributor_type: User
+  contributable: string_concat_v2_empty (DataSet)
   policy: only_john_can_edit
\ No newline at end of file

Modified: branches/datasets/test/fixtures/data_sets.yml (2861 => 2862)


--- branches/datasets/test/fixtures/data_sets.yml	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/test/fixtures/data_sets.yml	2011-12-05 16:16:11 UTC (rev 2862)
@@ -1,6 +1,23 @@
 string_concat_v1_example:
+  title: Example Data for String Concat
   workflow: string_concat
   workflow_version: 1
   category: example_data
   contributor_id: 1 # John
+  contributor_type: User
+
+string_concat_v1_empty:
+  title: Empty Data Set
+  workflow: string_concat
+  workflow_version: 1
+  category: example_data
+  contributor_id: 1 # John
+  contributor_type: User
+
+string_concat_v2_empty:
+  title: Empty Data Set
+  workflow: string_concat
+  workflow_version: 2
+  category: example_data
+  contributor_id: 1 # John
   contributor_type: User
\ No newline at end of file

Added: branches/datasets/test/functional/data_items_controller_test.rb (0 => 2862)


--- branches/datasets/test/functional/data_items_controller_test.rb	                        (rev 0)
+++ branches/datasets/test/functional/data_items_controller_test.rb	2011-12-05 16:16:11 UTC (rev 2862)
@@ -0,0 +1,140 @@
+# myExperiment: test/functional/data_items_controller_test.rb
+#
+# Copyright (c) 2011 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+require File.dirname(__FILE__) + '/../test_helper'
+
+class DataItemsControllerTest < ActionController::TestCase
+
+  test "can add text data to a data set" do
+    data_set = data_sets(:string_concat_v1_empty)
+
+    login_as(:john)
+
+    assert_no_difference("WorkflowPort.count") do # Port should already exist, so this shouldn't change
+    assert_difference("TextData.count", 1) do
+    assert_difference("Relationship.count", 1) do
+      post :create, :data_set_id => data_set.id,
+          :workflow_port => "string1",
+          :predicate_id => predicates(:input_data_to).id,
+          :workflow_port_type => "input",
+          :data_type => "text",
+          :data ="" "test"
+    end
+    end
+    end
+
+    assert_response :success
+    assert_equal "test", assigns(:data_item).subject.data
+  end
+
+  test "can add file data to a data set" do
+    data_set = data_sets(:string_concat_v1_empty)
+
+    login_as(:john)
+
+    assert_no_difference("WorkflowPort.count") do # Port should already exist, so this shouldn't change
+    assert_difference("Relationship.count", 1) do
+      post :create, :data_set_id => data_set.id,
+          :workflow_port => "string1",
+          :predicate_id => predicates(:input_data_to).id,
+          :workflow_port_type => "input",
+          :data_type => "file",
+          :file_id => blobs(:picture).id
+    end
+    end
+
+    assert_response :success
+    assert_equal blobs(:picture), assigns(:data_item).subject
+  end
+
+  test "can't add items to an already occupied port in a data set" do
+    data_set = data_sets(:string_concat_v1_example)
+
+    login_as(:john)
+
+    assert_no_difference("TextData.count") do
+    assert_no_difference("Relationship.count") do
+      post :create, :data_set_id => data_set.id,
+          :workflow_port => "string1",
+          :predicate_id => predicates(:input_data_to).id,
+          :workflow_port_type => "input",
+          :data_type => "text",
+          :data ="" "test"
+    end
+    end
+
+    assert_response 400
+  end
+
+  test "can't add items to data sets if not authorized" do
+    data_set = data_sets(:string_concat_v1_example)
+
+    login_as(:jane)
+
+    assert_no_difference("TextData.count") do
+    assert_no_difference("Relationship.count") do
+      post :create, :data_set_id => data_set.id,
+          :workflow_port => "string1",
+          :predicate_id => predicates(:input_data_to).id,
+          :workflow_port_type => "input",
+          :data_type => "text",
+          :data ="" "test"
+    end
+    end
+
+    assert_response 403
+  end
+
+  test "can replace data in a data set" do
+    data_item = relationships(:string_concat_v1_example_text_data2)
+
+    login_as(:john)
+
+    assert_no_difference("WorkflowPort.count") do # Port should already exist, so this shouldn't change
+    assert_difference("TextData.count", -1) do
+    assert_no_difference("Relationship.count") do
+      put :update,
+          :id => data_item.id,
+          :workflow_port => "result",
+          :predicate_id => predicates(:output_data_from).id,
+          :workflow_port_type => "output",
+          :data_type => "file",
+          :file_id => blobs(:picture).id
+    end
+    end
+    end
+
+    assert_response :success
+    assert_equal blobs(:picture), assigns(:data_item).subject
+  end
+
+  test "can delete data in a data set" do
+    data_item = relationships(:string_concat_v1_example_text_data2)
+
+    login_as(:john)
+
+    assert_no_difference("WorkflowPort.count") do # Port should already exist, so this shouldn't change
+    assert_difference("TextData.count", -1) do
+    assert_difference("Relationship.count", -1) do
+      delete :destroy, :id => data_item.id
+    end
+    end
+    end
+
+    assert_response :success
+  end
+
+  test "can fetch data item form with correct port names for workflow version" do
+    v2_data_set = data_sets(:string_concat_v2_empty)
+
+    login_as(:john)
+
+    get :new, :data_set_id => v2_data_set.id, :port_type => "input"
+    assert_response :success
+    # Using (array1 == array1 & array2) to compare array contents regardless of order
+    assert_equal ["string1", "string2", "string3"], assigns(:port_names) & ["string1", "string2", "string3"]
+  end
+
+end

Modified: branches/datasets/test/functional/data_sets_controller_test.rb (2861 => 2862)


--- branches/datasets/test/functional/data_sets_controller_test.rb	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/test/functional/data_sets_controller_test.rb	2011-12-05 16:16:11 UTC (rev 2862)
@@ -1,6 +1,6 @@
 # myExperiment: test/functional/data_sets_controller_test.rb
 #
-# Copyright (c) 2011University of Manchester and the University of Southampton.
+# Copyright (c) 2011 University of Manchester and the University of Southampton.
 # See license.txt for details.
 
 require File.dirname(__FILE__) + '/../test_helper'
@@ -42,6 +42,24 @@
     assert_redirected_to workflow_data_sets_path(workflow)
   end
 
+  test "can destroy data set without leaving orphaned resources" do
+    data_set = data_sets(:string_concat_v1_example)
+    item_count = data_set.data_items.size
+    text_data_count = data_set.data_items.select {|i| i.subject_type == "TextData"}.size
+
+    login_as(:john)
+
+    assert_difference("TextData.count", -text_data_count) do
+    assert_difference("Relationship.count", -item_count) do
+    assert_difference("DataSet.count", -1) do
+      delete :destroy, :id => data_set.id
+    end
+    end
+    end
+
+    assert_redirected_to workflow_data_sets_url(data_set.workflow)
+  end
+
   test "can't destroy data sets if not authorized" do
     data_set = data_sets(:string_concat_v1_example)
 
@@ -55,22 +73,26 @@
     assert_redirected_to data_set_url(data_set)
   end
 
-  test "can destroy data set without leaving orphaned resources" do
+  test "can view data set" do
     data_set = data_sets(:string_concat_v1_example)
-    item_count = data_set.data_items.size
-    text_data_count = data_set.data_items.select {|i| i.subject_type == "TextData"}.size
 
-    login_as(:john)
+    login_as(:jane)
 
-    assert_difference("TextData.count", -text_data_count) do
-      assert_difference("Relationship.count", -item_count) do
-        assert_difference("DataSet.count", -1) do
-          delete :destroy, :id => data_set.id
-        end
-      end
-    end
+    get :show, :id => data_set.id
 
-    assert_redirected_to workflow_data_sets_url(data_set.workflow)
+    assert_response :success
+    assert_equal data_set, assigns(:data_set)
   end
 
+  test "can view data sets" do
+    workflow = workflows(:string_concat)
+
+    login_as(:jane)
+
+    get :index, :workflow_id => workflow.id
+
+    assert_response :success
+    assert_equal 3, assigns(:data_sets).size
+  end
+
 end

Modified: branches/datasets/test/unit/data_set_test.rb (2861 => 2862)


--- branches/datasets/test/unit/data_set_test.rb	2011-12-05 11:56:29 UTC (rev 2861)
+++ branches/datasets/test/unit/data_set_test.rb	2011-12-05 16:16:11 UTC (rev 2862)
@@ -1,15 +1,35 @@
 require File.dirname(__FILE__) + '/../test_helper'
 require 'digest/md5'
+require 'zip/zip'
 
 class DataSetTest < ActiveSupport::TestCase
 
-  # Uses MD5 checksum to determine zipping was successful - maybe too fragile?
   test "can create a zip file" do
     data_set = data_sets(:string_concat_v1_example)
+
     zip_data = data_set.create_zip(users(:john)).string
 
-    assert_equal("fcb670d73a2c7798576974a1c4f61b5c", Digest::MD5.hexdigest(zip_data))
-  end
+    zip_file = Tempfile.open("test.zip", "tmp")
+    zip_file.write(zip_data)
+    zip_file.close
 
+    Zip::ZipFile.open(zip_file.path) do |zipfile|
+      assert_equal 4, zipfile.entries.size
+      #Zip file entries seemed to be ordered according to their size
 
+      assert_equal "inputs/string2 - file_picture.png", zipfile.entries[0].name
+      assert_equal 2728, zipfile.entries[0].size
+
+      assert_equal "_metadata.txt", zipfile.entries[1].name
+      assert zipfile.entries[1].size > 0 # _metadata.txt size might vary as the IDs it contains are (pseudo)randomly generated
+
+      assert_equal "outputs/result - text.txt", zipfile.entries[2].name
+      assert_equal 11, zipfile.entries[2].size
+
+      assert_equal "inputs/string1 - text.txt", zipfile.entries[3].name
+      assert_equal 5, zipfile.entries[3].size
+    end
+
+    zip_file.unlink
+  end
 end

reply via email to

[Prev in Thread] Current Thread [Next in Thread]