Skip to content

Commit

Permalink
message tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vswamidass-sfdc committed Sep 9, 2024
1 parent 3c36c67 commit 0a97ec9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/base_messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def show; end
def create
@message = Message.new(message_params)
@message.chat_id = @chat.id
@message.user_id = @current_user.id
@message.user_id = current_user.id
@message.from = :user

respond_to do |format|
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def edit; end
def create
@message = Message.new(message_params)
@message.chat_id = @chat.id
@message.user_id = @current_user.id
@message.user_id = current_user.id
@message.from = :user

respond_to do |format|
Expand Down
1 change: 1 addition & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Message < ApplicationRecord
belongs_to :user
after_create :enqueue_generate_message_response_job
validates :from, presence: true
validates :content, presence: true

enum from: { user: 0, assistant: 1 }
enum status: { ready: 0, generating: 1 }
Expand Down
2 changes: 1 addition & 1 deletion spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

it 'is not valid without content' do
message = Message.new(content: nil, chat:, user:, from: :user)
expect(message).to be_valid
expect(message).not_to be_valid
end

it 'is not valid without a chat' do
Expand Down
42 changes: 42 additions & 0 deletions spec/views/api/mesage_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'rails_helper'

RSpec.describe 'Messages API', type: :request do
let(:user) { User.create!(email: '[email protected]', password: 'Password1!') }
let(:library) { Library.create!(name: 'Test Library', user:) }
let(:assistant) { Assistant.create!(name: 'Test Assistant', user:, libraries: '1,2', input: 'Sample input', instructions: 'Sample instructions', output: 'Sample output') }
let(:chat) { Chat.create!(first_message: 'Hello', assistant:, user:) }
let(:valid_attributes) { { content: 'This is a test message', user_id: user.id } }
let(:invalid_attributes) { { content: '', user_id: nil } }

before do
# Assuming you have some authentication mechanism
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
end

describe 'POST /api/v1/chats/:chat_id/messages' do
context 'with valid parameters' do
it 'creates a new Message' do
expect do
post "/api/v1/chats/#{chat.id}/messages", params: { message: valid_attributes }
end.to change(Message, :count).by(1)
expect(response).to have_http_status(:created)
expect(response.content_type).to match(a_string_including('application/json'))
json = JSON.parse(response.body)
expect(json).to include(
'content' => 'This is a test message',
'user_id' => user.id
)
end
end

context 'with invalid parameters' do
it 'does not create a new Message' do
expect do
post "/api/v1/chats/#{chat.id}/messages", params: { message: invalid_attributes }
end.to change(Message, :count).by(0)
expect(response.content_type).to match(a_string_including('application/json'))
expect(response).to have_http_status(:unprocessable_entity) # Adjust based on error handling
end
end
end
end

0 comments on commit 0a97ec9

Please sign in to comment.