From bda7321c026a0ee9fb319851d9cda1f773fdfa75 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Mon, 15 May 2023 14:22:28 +0100 Subject: [PATCH] create test/httpoison_mock_test.exs to test "true" cond -> 100% cov #1 --- README.md | 2 +- lib/gcal.ex | 60 +++++++++++++++++------------------- test/gcal_test.exs | 16 ++++++++++ test/httpoison_mock_test.exs | 11 +++++++ 4 files changed, 57 insertions(+), 32 deletions(-) create mode 100644 test/httpoison_mock_test.exs diff --git a/README.md b/README.md index 2e902ed..881e104 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/lib/gcal.ex b/lib/gcal.ex index f3354d3..17597c3 100644 --- a/lib/gcal.ex +++ b/lib/gcal.ex @@ -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" @@ -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\"", @@ -106,7 +107,7 @@ defmodule Gcal do summary: "nelson@gmail.com", timeZone: "Europe/London" }} - + ``` """ def get_calendar_details(access_token, cal_name \\ "primary") do httpoison().get("#{@baseurl}/calendars/#{cal_name}", headers(access_token)) @@ -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, @@ -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}) @@ -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 diff --git a/test/gcal_test.exs b/test/gcal_test.exs index 2a0571c..92d7dd7 100644 --- a/test/gcal_test.exs +++ b/test/gcal_test.exs @@ -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 diff --git a/test/httpoison_mock_test.exs b/test/httpoison_mock_test.exs new file mode 100644 index 0000000..bf7b666 --- /dev/null +++ b/test/httpoison_mock_test.exs @@ -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