Skip to content

Commit

Permalink
create test/httpoison_mock_test.exs to test "true" cond -> 100% cov #1
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed May 15, 2023
1 parent e24aad6 commit bda7321
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Add `gcal` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:gcal, "~> 1.0.2"}
{:gcal, "~> 1.0.3"}
]
end
```
Expand Down
60 changes: 29 additions & 31 deletions lib/gcal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule Gcal do
import Gcal.HTTPoison

@moduledoc """
`Gcal` helps you interact with your `Google` Calendar via the API.
`Gcal` lets you interact with your `Google` Calendar via the API.
View your calendar events, modify event details and create new events.
"""

@baseurl "https://www.googleapis.com/calendar/v3"
Expand Down Expand Up @@ -97,7 +98,7 @@ defmodule Gcal do
`calendar`: (optional) the string name of the calendar; defaults to "primary"
Sample response:
```elixir
{:ok, %{
conferenceProperties: %{"allowedConferenceSolutionTypes" => ["hangoutsMeet"]},
etag: "\"oftesUJ77GfcrwCPCmctnI90Qzs\"",
Expand All @@ -106,7 +107,7 @@ defmodule Gcal do
summary: "[email protected]",
timeZone: "Europe/London"
}}
```
"""
def get_calendar_details(access_token, cal_name \\ "primary") do
httpoison().get("#{@baseurl}/calendars/#{cal_name}", headers(access_token))
Expand Down Expand Up @@ -242,6 +243,8 @@ defmodule Gcal do
`event_details`: the details of the event to be created
`cal_name`(optional): the calendar to create the event in
Sample `event_details`:
```elixir
%{
"title" => title,
"date" => date,
Expand All @@ -250,41 +253,19 @@ defmodule Gcal do
"all_day" => all_day,
"hoursFromUTC" => hoursFromUTC
}
```
"""
# Create new event to the primary calendar.
def create_event(access_token, event, cal_name \\ "primary") do
e = Useful.atomize_map_keys(event)
def create_event(access_token, event_details, cal_name \\ "primary") do
e = Useful.atomize_map_keys(event_details)
# Get primary calendar
{:ok, primary_cal} = get_calendar_details(access_token, cal_name)

# Setting `start` and `stop` according to the `all-day` boolean,
# If `all-day` is set to true, we should return the date instead of the datetime,
# as per https://developers.google.com/calendar/api/v3/reference/events/insert.
start =
case e.all_day do
true ->
%{date: e.date}

false ->
%{
dateTime:
Timex.parse!("#{e.date} #{e.start} #{e.hoursFromUTC}", "{YYYY}-{0M}-{D} {h24}:{m} {Z}")
|> Timex.format!("{RFC3339}")
}
end

stop =
case e.all_day do
true ->
%{date: e.date}

false ->
%{
dateTime:
Timex.parse!("#{e.date} #{e.stop} #{e.hoursFromUTC}", "{YYYY}-{0M}-{D} {h24}:{m} {Z}")
|> Timex.format!("{RFC3339}")
}
end
start = all_day_or_datetime(e, e.start)
stop = all_day_or_datetime(e, e.stop)

# Post new event
body = Jason.encode!(%{summary: e.title, start: start, end: stop})
Expand All @@ -297,7 +278,24 @@ defmodule Gcal do
|> parse_body_response()
end

# TODO: split the `all_day/2` function out from `create_event/3` ...
@doc """
`all_day_or_datetime/2` is a helper function
that formats the date (in the case of an all_day event)
or datetime in the format that the Google Calendar API accepts.
"""
def all_day_or_datetime(e, time) do
case e.all_day do
true ->
%{date: e.date}

false ->
%{
dateTime:
Timex.parse!("#{e.date} #{time} #{e.hoursFromUTC}", "{YYYY}-{0M}-{D} {h24}:{m} {Z}")
|> Timex.format!("{RFC3339}")
}
end
end

# Parse JSON body response
defp parse_body_response({:ok, response}) do
Expand Down
16 changes: 16 additions & 0 deletions test/gcal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ defmodule GcalTest do
# dbg(event)
assert event.summary == event_detail.title
end

test "create_event/3 all_day=true" do
access_token = System.get_env("TOKEN")
event_detail = %{
all_day: true,
date: "2023-05-15",
hoursFromUTC: "+0100",
start: "16:00",
stop: "18:00",
title: "My Awesome Event"
}

{:ok, event} = Gcal.create_event(access_token, event_detail, "primary")
# dbg(event)
assert event.summary == event_detail.title
end
end
11 changes: 11 additions & 0 deletions test/httpoison_mock_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule GcalHTTPoisonMockTest do
use ExUnit.Case

# This test only exists because we are using a "cond" in post/3
# for future-proofing the function.
test "Gcal.HTTPoisonMock.post returns empty map" do
{:ok, data} = Gcal.HTTPoisonMock.post("any", "body", "headers")
dbg(data)
assert data == %{body: "{}"}
end
end

0 comments on commit bda7321

Please sign in to comment.