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

Refactor/job application application url uniqueness #34

Merged
merged 6 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ Unsuccessful Response:
{:message=>"Company must exist and Position title can't be blank", :status=>400}
```

Unsuccessful Response(pre-existing application for user):
```
{:message=>"Application url You already have an application with this URL", :status=>400}
```

### Companies

Get login credentials
Expand Down
2 changes: 1 addition & 1 deletion app/models/job_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class JobApplication < ApplicationRecord
validates :date_applied, presence: true
validates :status, presence: true
validates :job_description, presence: true
validates :application_url, presence: true
validates :application_url, presence: true, uniqueness: { scope: :user_id, message: "already exists for the user, try making a new application with a new URL." }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work adding this particular validation. Now we can ensure that every application record is unique per user while allowing other users to have the same url. This will help maintain data integrity and avoid duplicate entries for the same job application.

validates :contact_information, presence: true
validates :company_id, presence: true
end
35 changes: 35 additions & 0 deletions spec/models/job_application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@
it { should validate_presence_of(:application_url) }
it { should validate_presence_of(:contact_information) }
it { should validate_presence_of(:company_id) }

subject {
user = User.create!(
name: "Yolonda Aberdeer",
email: "[email protected]",
password: "nuggetbisket"
)

google = Company.create!(
user_id: user.id,
name: "Google",
website: "google.com",
street_address: "1600 Amphitheatre Parkway",
city: "Mountain View",
state: "CA",
zip_code: "94043",
notes: "Search engine"
)

JobApplication.create!(
position_title: "Jr. CTO",
date_applied: "2024-10-31",
status: 1,
notes: "Fingers crossed!",
job_description: "Looking for Turing grad/jr dev to be CTO",
application_url: "www.example.com",
contact_information: "[email protected]",
company_id: google.id,
user_id: user.id
)
}

it "validates uniqueness of job application for a given user" do
expect(subject).to validate_uniqueness_of(:application_url).scoped_to(:user_id).with_message("already exists for the user, try making a new application with a new URL.")
end
Comment on lines +44 to +46
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot goes into testing the validity scoped to a user. I appreciate the clean, one-line test, and a very user-friendly error message. Excellent work.

end

describe "associations" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
expect(response.status).to eq(200)

jobApp = JSON.parse(response.body, symbolize_names: true)

expect(jobApp[:data][:type]).to eq("job_application")
expect(jobApp[:data][:id]).to eq(JobApplication.last.id.to_s)
expect(jobApp[:data][:attributes][:position_title]).to eq(job_application_params[:position_title])
Expand All @@ -44,6 +44,20 @@
expect(jobApp[:data][:attributes][:contact_information]).to eq(job_application_params[:contact_information])
expect(jobApp[:data][:attributes][:company_id]).to eq(job_application_params[:company_id])
end
it "Allows 2 unique users to create job applications with the same URL" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an excellent test to include, not only does it test the flexibility of the validation, but this functionality is necessary for the foundation of this application.


post "/api/v1/users/#{@user.id}/job_applications",
params: { job_application: job_application_params }
expect(response).to be_successful
expect(response.status).to eq(200)

user_2 = User.create!(name: "Daniel Averdaniel", email: "[email protected]", password: "nuggetonnabiscut")

post "/api/v1/users/#{user_2.id}/job_applications",
params: { job_application: job_application_params }
expect(response).to be_successful
expect(response.status).to eq(200)
end
end

context "sad path" do
Expand Down Expand Up @@ -92,6 +106,23 @@
expect(json[:message]).to eq("Company must exist and Position title can't be blank")
expect(json[:status]).to eq(400)
end

it "returns a error message if a user tries to create multiple job applications with the same URL" do
post "/api/v1/users/#{@user.id}/job_applications",
params: { job_application: job_application_params }
expect(response).to be_successful
expect(response.status).to eq(200)

post "/api/v1/users/#{@user.id}/job_applications",
params: { job_application: job_application_params }
expect(response).to_not be_successful
expect(response.status).to eq(400)

json = JSON.parse(response.body, symbolize_names: true)

expect(json[:message]).to eq("Application url already exists for the user, try making a new application with a new URL.")
expect(json[:status]).to eq(400)
end
Comment on lines +110 to +125
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you added and tested this functionality, verifying that a user cannot make multiple entries of the same job application. This is a excellent enhancement for maintaining clean and accurate data being stored in the database. From a user perspective, I would appreciate this functionality, as I could be creating a new job application online, yet have the wrong URL in my clipboard, and this error message would prove very helpful in understanding.

end
end
end