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

add models and unit test #16320

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
24 changes: 24 additions & 0 deletions app/models/pega_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

class PegaTable < ApplicationRecord
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we change the file name and model to just be "Pega" to match the format of other models? @cloudmagic80

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe PegaData for a bit more clarity since we are storing the response in there right?

# Validate presence of essential fields
validates :uuid, presence: true
validates :veteranfirstname, presence: true
validates :veteranlastname, presence: true
validates :response, presence: true

validate :validate_response_format

private

def validate_response_format
return if response.blank?

response_hash = JSON.parse(response)
unless response_hash['status'].present? && [200, 403, 500].include?(response_hash['status'].to_i)
errors.add(:response, 'must contain a valid HTTP status code (200, 403, 500)')
end
rescue JSON::ParserError
errors.add(:response, 'must be a valid JSON format')
end
end
21 changes: 21 additions & 0 deletions spec/factories/pega_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

FactoryBot.define do
factory :pega_table do
uuid { 'c47bec59-02c7-43e4-a0f7-acf287a32a97' }
veteranfirstname { 'John' }
veteranlastname { 'Doe' }

trait :with_valid_response do
response { '{"status": 200}' }
end

trait :with_invalid_response do
response { '{"status": 400}' }
end

trait :with_invalid_json_response do
response { 'invalid_json' }
end
end
end
30 changes: 30 additions & 0 deletions spec/models/pega_table_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PegaTable, type: :model do
describe 'validations' do
it { is_expected.to validate_presence_of(:uuid) }
it { is_expected.to validate_presence_of(:veteranfirstname) }
it { is_expected.to validate_presence_of(:veteranlastname) }
it { is_expected.to validate_presence_of(:response) }

context 'custom validations' do
it 'ensures response contains a valid HTTP status code' do
# Test case for valid status code (200)
pega_table = build(:pega_table, :with_valid_response)
expect(pega_table).to be_valid

# Test case for invalid status code (400)
pega_table = build(:pega_table, :with_invalid_response)
pega_table.valid?
expect(pega_table.errors[:response]).to include('must contain a valid HTTP status code (200, 403, 500)')

# Test case for invalid JSON format
pega_table = build(:pega_table, :with_invalid_json_response)
pega_table.valid?
expect(pega_table.errors[:response]).to include('must be a valid JSON format')
end
end
end
end
Loading