Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeGuzmanSierra committed Jul 25, 2024
1 parent 0d1f4ba commit 1ca3deb
Show file tree
Hide file tree
Showing 8 changed files with 932 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/bas/bot/create_work_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def body
}
end

def properties
def properties # rubocop:disable Metrics/AbcSize
{
"Responsible domain": select(process_options[:domain]),
"Github Issue id": rich_text(read_response.data["issue"]["id"].to_s),
Expand All @@ -133,7 +133,7 @@ def work_item_type
end

def tag
return write_options[:tag] if process_response[:success][:notion_wi].nil?
return write_options[:tag] if process_response[:success].nil? || process_response[:success][:notion_wi].nil?

UPDATE_REQUEST
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bas/bot/verify_issue_existance_in_notion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def notion_wi_id(result)
def tag
issue = process_response[:success]

return write_options[:tag] if issue[:notion_wi].nil?
return write_options[:tag] if issue.nil? || issue[:notion_wi].nil?

issue[:notion_wi].eql?(NOT_FOUND) ? "CreateWorkItemRequest" : "UpdateWorkItemRequest"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/bas/utils/notion/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

module Utils
module Notion
##
# This module is a Notion utility for formating standard notion types to
# filter or create.
#
module Types
def multi_select(name)
{ "multi_select" => [{ "name" => name }] }
Expand Down
182 changes: 182 additions & 0 deletions spec/bas/bot/create_work_item_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# frozen_string_literal: true

require "bas/bot/create_work_item"

RSpec.describe Bot::CreateWorkItem do
before do
connection = {
host: "localhost",
port: 5432,
dbname: "bas",
user: "postgres",
password: "postgres"
}

config = {
read_options: {
connection:,
db_table: "github_issues",
tag: "CreateWorkItemRequest"
},
process_options: {
database_id: "database id",
secret: "notion secret",
domain: "domain",
status: "Backlog",
work_item_type: "project",
project: "project_id"
},
write_options: {
connection:,
db_table: "github_issues",
tag: "CreateWorkItem"
}
}

@bot = described_class.new(config)
end

describe "attributes and arguments" do
it { expect(described_class).to respond_to(:new).with(1).arguments }

it { expect(@bot).to respond_to(:execute).with(0).arguments }
it { expect(@bot).to respond_to(:read).with(0).arguments }
it { expect(@bot).to respond_to(:process).with(0).arguments }
it { expect(@bot).to respond_to(:write).with(0).arguments }

it { expect(@bot).to respond_to(:read_options) }
it { expect(@bot).to respond_to(:process_options) }
it { expect(@bot).to respond_to(:write_options) }
end

describe ".read" do
let(:pg_conn) { instance_double(PG::Connection) }
let(:issue_result) do
"{\"issue\":\
{\"id\": \"12345\", \"body\": \"simple description\",\
\"state\": \"open\", \"title\": \"Issue\", \"labels\": [],\
\"html_url\":\"https://github.com/repo/issues\", \"assignees\": [],\
\"created_at\":\"2024-07-24 20:13:18 UTC\",\
\"updated_at\":\"2024-07-24 20:36:57 UTC\"}, \"notion_wi\": \"not found\"\
}"
end

let(:formatted_issue) do
{ "issue" => {
"id" => "12345",
"body" => "simple description",
"state" => "open",
"title" => "Issue",
"labels" => [],
"html_url" => "https://github.com/repo/issues",
"assignees" => [],
"created_at" => "2024-07-24 20:13:18 UTC",
"updated_at" => "2024-07-24 20:36:57 UTC"
}, "notion_wi" => "not found" }
end

before do
@pg_result = double

allow(PG::Connection).to receive(:new).and_return(pg_conn)
allow(pg_conn).to receive(:exec_params).and_return(@pg_result)
allow(@pg_result).to receive(:values).and_return([[1, issue_result, "date"]])
end

it "read the notification from the postgres database" do
read = @bot.read

expect(read).to be_a Read::Types::Response
expect(read.data).to be_a Hash
expect(read.data).to_not be_nil
expect(read.data).to eq(formatted_issue)
end
end

describe ".process" do
let(:work_item) { { "id" => "123456789" } }

let(:issue) do
{
"id" => "12345",
"body" => "simple description",
"state" => "open",
"title" => "Issue",
"labels" => [],
"html_url" => "https://github.com/repo/issues",
"assignees" => [],
"created_at" => "2024-07-24 20:13:18 UTC",
"updated_at" => "2024-07-24 20:36:57 UTC"
}
end

let(:issue_request) { { "issue" => issue } }

let(:error_response) { { "object" => "error", "status" => 404, "message" => "not found" } }

let(:response) { double("http_response") }

before do
@bot.read_response = Read::Types::Response.new(1, issue_request, "date")

allow(HTTParty).to receive(:send).and_return(response)
end

it "creates a work item on notion and returns its notion id" do
allow(response).to receive(:code).and_return(200)
allow(response).to receive(:[]).and_return("123456789")

processed = @bot.process

expect(processed).to eq({ success: { issue:, notion_wi: "123456789" } })
end

it "returns an error hash with the error message" do
allow(response).to receive(:code).and_return(404)
allow(response).to receive(:parsed_response).and_return(error_response)

processed = @bot.process

expect(processed).to eq({ error: { message: error_response, status_code: 404 } })
end
end

describe ".write" do
let(:pg_conn) { instance_double(PG::Connection) }

let(:issue) do
{
"id" => "12345",
"body" => "simple description",
"state" => "open",
"title" => "Issue",
"labels" => [],
"html_url" => "https://github.com/repo/issues",
"assignees" => [],
"created_at" => "2024-07-24 20:13:18 UTC",
"updated_at" => "2024-07-24 20:36:57 UTC"
}
end

let(:error_response) { { "object" => "error", "status" => 404, "message" => "not found" } }

before do
pg_result = instance_double(PG::Result)

allow(PG::Connection).to receive(:new).and_return(pg_conn)
allow(pg_conn).to receive(:exec_params).and_return(pg_result)
end

it "save the process success response with 'UpdateWorkItemRequest' tag when it has a notion wi" do
@bot.process_response = { success: { issue:, notion_wi: "123456789" } }

expect(@bot.write).to_not be_nil
end

it "save the process fail response in a postgres table" do
@bot.process_response = { error: { message: error_response, status_code: 404 } }

expect(@bot.write).to_not be_nil
end
end
end
Loading

0 comments on commit 1ca3deb

Please sign in to comment.