From 80f32f22787bed560bf1dc2350d6dc3b3937f2ba Mon Sep 17 00:00:00 2001
From: Mauricio Giacomini Girardello <mauriciogiacomini4@gmail.com>
Date: Wed, 28 Oct 2015 11:56:10 -0200
Subject: [PATCH] adding collection model unit testing

---
 .../orient_db/learning_object_repository.rb   | 34 ++++++++--------
 test/models/collection_test.rb                | 40 +++++++++++++++++++
 2 files changed, 57 insertions(+), 17 deletions(-)
 create mode 100644 test/models/collection_test.rb

diff --git a/app/repositories/orient_db/learning_object_repository.rb b/app/repositories/orient_db/learning_object_repository.rb
index 4c2ebfa26..3765fe6cd 100644
--- a/app/repositories/orient_db/learning_object_repository.rb
+++ b/app/repositories/orient_db/learning_object_repository.rb
@@ -75,17 +75,17 @@ module OrientDb
 
     #it could perhaps be in the GenericMethods, if we'd like to extend to User counting.
     def get_number_of_non_visualised
-      (connection.query("SELECT COUNT(*) FROM LearningObject WHERE in('Views').size() = 0"))[0]["COUNT"]
+      (connection.query("SELECT COUNT(*) FROM LearningObject WHERE in('Views').size() = 0"))[0]["COUNT"].to_i
     end
 
     #get the ten most visualised. It's probably making too many useless accesses to orientDB...
     def get_most_visualised
       result = connection.query("SELECT FROM (SELECT @rid,in('Views').size() AS views FROM LearningObject) ORDER BY views DESC LIMIT 10")
-      @most_visualised = []
+      most_visualised = []
       result.each do |result|
-        (@most_visualised ||= []).push(get_by_rid(result["rid"]))
+        most_visualised.push(get_by_rid(result["rid"]))
       end
-      build_objects @most_visualised
+      build_objects most_visualised
     end
 
     def get_publisher(learning_object)
@@ -140,7 +140,7 @@ module OrientDb
         edges << create_edges_from_array("HasAttr", learning_object.id, learning_object.attributes, true)
       end
 
-      if !learning_object.publisher.nil? && !edge_exists?("PublishedBy",learning_object.id,learning_object.publisher.id)
+      if !learning_object.publisher.nil? && !edge_exists?("PublishedBy", learning_object.id, learning_object.publisher.id)
         edges << create_edge("PublishedBy", learning_object.id, learning_object.publisher.id)
       end
       edges.flatten
@@ -194,17 +194,17 @@ module OrientDb
 
         order = params[:order]
         order = case order
-        when 'author'
-          'author'
-        when 'publicationasc'
-          'published_at'
-        when 'publicationdesc'
-          'published_at DESC'
-        when 'title'
-          'name ASC'
-        else
-          'score DESC'
-        end
+                  when 'author'
+                    'author'
+                  when 'publicationasc'
+                    'published_at'
+                  when 'publicationdesc'
+                    'published_at DESC'
+                  when 'title'
+                    'name ASC'
+                  else
+                    'score DESC'
+                end
 
         query = "SELECT FROM LearningObject WHERE [name, description] LUCENE '#{qry}'"
         query = query + " AND out('IsAbout') CONTAINS (name in ['" + params[:subject].join("','") + "'])" unless params[:subject].blank?
@@ -216,7 +216,7 @@ module OrientDb
 
         # return only rids with their modification time
         learning_objects_hash.map do |e|
-          {'@rid': e['rid'], 'last_modified': e['last_modified']}
+          {'@rid' => e['rid'], 'last_modified' => e['last_modified']}
         end
       end
     end
diff --git a/test/models/collection_test.rb b/test/models/collection_test.rb
new file mode 100644
index 000000000..3a4da2742
--- /dev/null
+++ b/test/models/collection_test.rb
@@ -0,0 +1,40 @@
+require 'test_helper'
+
+class CollectionTest < ActiveSupport::TestCase
+  ## Errors messages are missing, and they are required to shoulda matachers pass
+
+  #should validate_presence_of :name
+  #should validate_presence_of :created_at
+  #should validate_presence_of :owner
+  #should validate_presence_of :learning_objects
+
+  test 'collection is invalid when name attribute is empty' do
+    collection = Collection.new
+    assert !collection.valid?
+  end
+
+  test 'collection is valid when name attribute is not empty' do
+    collection = Collection.new(name: 'Minha coleção de teste')
+    assert collection.valid?
+  end
+
+  test 'append learning objects to collection' do
+    collection = Collection.new(name: 'Minha coleção de teste')
+    collection.add LearningObject.new
+    collection.add LearningObject.new
+
+    assert_count 2, collection.learning_objects
+  end
+
+  test 'remove learning object from collection' do
+    lo1 = LearningObject.new
+    collection = Collection.new(name: 'Minha coleção de teste')
+    collection.add lo1
+    collection.add LearningObject.new
+    assert_count 2, collection.learning_objects
+
+    collection.remove lo1
+    assert_count 1, collection.learning_objects
+  end
+
+end
\ No newline at end of file
-- 
GitLab