From 4efca57451b805f3a6a085dc11183b1d203db54d Mon Sep 17 00:00:00 2001 From: Aday BA Date: Wed, 7 Feb 2024 14:22:18 +0000 Subject: [PATCH] Improved testing for Project size feature (#3342) --- .../app/views/projects/show.json.jbuilder | 2 +- apps/dashboard/test/models/projects_test.rb | 16 ++++++++++++---- apps/dashboard/test/system/jobs_app_test.rb | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/apps/dashboard/app/views/projects/show.json.jbuilder b/apps/dashboard/app/views/projects/show.json.jbuilder index 167b28ba18..1f04d7e225 100644 --- a/apps/dashboard/app/views/projects/show.json.jbuilder +++ b/apps/dashboard/app/views/projects/show.json.jbuilder @@ -5,4 +5,4 @@ json.icon @project.icon json.directory @project.directory project_size = @project.size json.size project_size -json.human_size ::ApplicationController.helpers.number_to_human_size(project_size) +json.human_size number_to_human_size(project_size) diff --git a/apps/dashboard/test/models/projects_test.rb b/apps/dashboard/test/models/projects_test.rb index 79fbfc445f..1742a19fa2 100644 --- a/apps/dashboard/test/models/projects_test.rb +++ b/apps/dashboard/test/models/projects_test.rb @@ -34,12 +34,20 @@ class ProjectsTest < ActiveSupport::TestCase end test 'creates project' do + stub_du Dir.mktmpdir do |tmp| projects_path = Pathname.new(tmp) - project = create_project(projects_path) + project_id = Project.next_id + project = create_project(projects_path, id: project_id, name: 'MyLocalName', description: 'MyLocalDescription', icon: 'fas://test') assert project.errors.inspect - assert Dir.entries("#{projects_path}/projects").include?(project.id) + assert Dir.entries("#{projects_path}/projects").include?(project_id) + assert_equal project_id, project.id + assert_equal 'MyLocalName', project.name + assert_equal "#{projects_path}/projects/#{project_id}", project.directory + assert_equal 'MyLocalDescription', project.description + assert_equal 'fas://test', project.icon + assert_equal 2097152, project.size end end @@ -182,9 +190,9 @@ class ProjectsTest < ActiveSupport::TestCase end end - def create_project(projects_path, name: 'test-project', icon: 'fas://arrow-right', description: 'description', directory: nil, template: nil) + def create_project(projects_path, id: nil, name: 'test-project', icon: 'fas://arrow-right', description: 'description', directory: nil, template: nil) OodAppkit.stubs(:dataroot).returns(projects_path) - id = Project.next_id + id ||= Project.next_id attrs = { name: name, icon: icon, id: id, description: description, directory: directory, template: template } project = Project.new(attrs) assert project.save, project.collect_errors diff --git a/apps/dashboard/test/system/jobs_app_test.rb b/apps/dashboard/test/system/jobs_app_test.rb index 7513d33124..0c43a1e292 100644 --- a/apps/dashboard/test/system/jobs_app_test.rb +++ b/apps/dashboard/test/system/jobs_app_test.rb @@ -133,6 +133,25 @@ def add_bc_num_hours(project_id, script_id) find("[href='/projects/#{project_id}']").click assert_selector 'h1', text: 'Test Project' assert_selector '.btn.btn-default', text: 'Back' + # project size is hardcoded to 2MB with stub_du + assert_selector '#new-dir-btn', text: 'Project Directory (2 MB)' + end + end + + test 'project show should support JSON' do + Dir.mktmpdir do |dir| + project_id = setup_project(dir) + + visit project_path(project_id, format: :json) + json_response = find('body').text + project_data = JSON.parse(json_response, symbolize_names: true) + assert_equal project_id, project_data[:id] + assert_equal 'test-project', project_data[:name] + assert_equal 'test-description', project_data[:description] + assert_equal 'fas://arrow-right', project_data[:icon] + assert_equal "#{dir}/projects/#{project_id}", project_data[:directory] + assert_equal 2097152, project_data[:size] + assert_equal '2 MB', project_data[:human_size] end end