Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - senza-lnd.check_enqueued_projects_worker.in_progress_projects #48

Open
tomsmith8 opened this issue Nov 12, 2024 · 0 comments

Comments

@tomsmith8
Copy link
Owner

Unit Test Coverage for " senza-lnd.check_enqueued_projects_worker.in_progress_projects"


Stakwork Run


Unit Test Code


require 'rspec'
require 'active_record'
require 'date'

# Assuming the necessary models and the function are already defined and available
# For example:
# class Customer < ActiveRecord::Base
#   has_many :projects
# end
#
# class Project < ActiveRecord::Base
#   enum workflow_state: { in_progress: 0, completed: 1, pending: 2 }
# end

RSpec.describe 'in_progress_projects' do
let(:customer) { Customer.create }
let(:last_month) { Date.today.prev_month }

before do
  # Clean up the database before each test
  Project.delete_all
  Customer.delete_all
end

context 'Basic Functionality' do
  it 'returns projects in progress created after last_month' do
    project1 = customer.projects.create(workflow_state: 'in_progress', created_at: Date.today)
    project2 = customer.projects.create(workflow_state: 'completed', created_at: Date.today)
    project3 = customer.projects.create(workflow_state: 'in_progress', created_at: last_month - 1)

    result = in_progress_projects(customer, last_month)
    expect(result).to contain_exactly(project1.id)
  end

  it 'returns an empty list when no projects are in progress' do
    customer.projects.create(workflow_state: 'completed', created_at: Date.today)

    result = in_progress_projects(customer, last_month)
    expect(result).to be_empty
  end
end

context 'Edge Cases' do
  it 'returns an empty list when all projects are created before last_month' do
    customer.projects.create(workflow_state: 'in_progress', created_at: last_month - 1)

    result = in_progress_projects(customer, last_month)
    expect(result).to be_empty
  end

  it 'returns an empty list when projects are created exactly on last_month' do
    customer.projects.create(workflow_state: 'in_progress', created_at: last_month)

    result = in_progress_projects(customer, last_month)
    expect(result).to be_empty
  end

  it 'returns an empty list when customer has no projects' do
    result = in_progress_projects(customer, last_month)
    expect(result).to be_empty
  end
end

context 'Error Conditions' do
  it 'raises an error with invalid date for last_month' do
    expect { in_progress_projects(customer, 'invalid_date') }.to raise_error(ArgumentError)
  end

  it 'raises an error with null customer' do
    expect { in_progress_projects(nil, last_month) }.to raise_error(NoMethodError)
  end
end

context 'Performance and Scale' do
  it 'handles a large number of projects efficiently' do
    1000.times do
      customer.projects.create(workflow_state: 'in_progress', created_at: Date.today)
    end

    result = in_progress_projects(customer, last_month)
    expect(result.size).to eq(1000)
  end
end

context 'Special Cases' do
  it 'returns projects with future creation dates' do
    future_project = customer.projects.create(workflow_state: 'in_progress', created_at: Date.today + 1)

    result = in_progress_projects(customer, last_month)
    expect(result).to contain_exactly(future_project.id)
  end

  it 'returns only projects associated with the given customer' do
    other_customer = Customer.create
    shared_project = customer.projects.create(workflow_state: 'in_progress', created_at: Date.today)
    other_customer.projects << shared_project

    result = in_progress_projects(customer, last_month)
    expect(result).to contain_exactly(shared_project.id)
  end
end

context 'Additional Test Cases' do
  it 'returns only the correct projects with mixed states and dates' do
    project1 = customer.projects.create(workflow_state: 'in_progress', created_at: Date.today)
    project2 = customer.projects.create(workflow_state: 'completed', created_at: Date.today)
    project3 = customer.projects.create(workflow_state: 'in_progress', created_at: last_month - 1)

    result = in_progress_projects(customer, last_month)
    expect(result).to contain_exactly(project1.id)
  end

  it 'handles boundary test for last_month correctly' do
    boundary_project = customer.projects.create(workflow_state: 'in_progress', created_at: last_month + 1)

    result = in_progress_projects(customer, last_month)
    expect(result).to contain_exactly(boundary_project.id)
  end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant