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

1215 add create table #142

Merged
merged 6 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions lib/smart_city/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ defmodule SmartCity.Event do
"""
defmacro data_standardization_end(), do: quote(do: "data:standardization:end")

@doc """
Signals that a table has been created
"""
defmacro table_created(), do: quote(do: "table:created")

@doc """
Defines a user organization relationship.
"""
Expand Down
87 changes: 87 additions & 0 deletions lib/smart_city/event_log.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
defmodule SmartCity.EventLog do
require Logger

alias SmartCity.Helpers

@moduledoc """
Struct defining an event log event.

```javascript
const EventLog = {
"title": "",
"timestamp", "",
"source": "",
"description": "",
"dataset_id": "",
"ingestion_id": ""
}
```
"""
@type not_required(type) :: type | nil

@type t :: %SmartCity.EventLog{
title: String.t(),
timestamp: String.t(),
source: String.t(),
description: String.t(),
dataset_id: String.t(),
ingestion_id: not_required(String.t())
}

@derive Jason.Encoder
defstruct title: nil,
timestamp: nil,
source: nil,
description: nil,
dataset_id: nil,
ingestion_id: nil

use Accessible

@doc """
Returns a new `SmartCity.EventLog`.
Can be created from `Map` with string or atom keys.
Raises an `ArgumentError` when passed invalid input

## Parameters

- msg: Map with string or atom keys that defines the event log metadata

Required Keys:
- title
- timestamp
- source
- description
- dataset_id
"""

@spec new(map()) :: SmartCity.EventLog.t()
def new(%{"title" => _} = msg) do
msg
|> Helpers.to_atom_keys()
|> new()
end

@spec new(map()) :: SmartCity.EventLog.t()
def new(
%{
title: _title,
timestamp: _timestamp,
source: _source,
description: _description,
dataset_id: _dataset_id
} = msg
) do
create(msg)
end

def new(msg) do
raise ArgumentError, "Invalid event log metadata: #{inspect(msg)}"
end

defp create(%__MODULE__{} = struct) do
struct |> Map.from_struct() |> create()
end

defp create(map), do: struct(%__MODULE__{}, map)
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def project do
[
app: :smart_city,
version: "5.4.2",
version: "5.4.3",
elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand All @@ -30,7 +30,7 @@
{:jason, "~> 1.1"},
{:mime, "~> 1.3"},
{:mix_test_watch, "~> 0.9", only: :dev, runtime: false},
{:placebo, "~> 1.2", only: [:dev, :test, :integration]},

Check warning on line 33 in mix.exs

View workflow job for this annotation

GitHub Actions / Verify Application

Mix.Local.path_for/1 is deprecated. Use Mix.path_for/1 instead

Check warning on line 33 in mix.exs

View workflow job for this annotation

GitHub Actions / Publish Hex Version

Mix.Local.path_for/1 is deprecated. Use Mix.path_for/1 instead
{:timex, "~> 3.6"}
]
end
Expand Down
102 changes: 102 additions & 0 deletions test/smart_city/event_log_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
defmodule SmartCity.EventLogTest do
use ExUnit.Case
import Checkov

alias SmartCity.EventLog

setup do
message = %{
"title" => "MyTitle",
"timestamp" => "myTimestamp",
"source" => "mySource",
"description" => "myDescription",
"dataset_id" => "myDatasetId",
"ingestion_id" => "myIngestionId"
}

{:ok, message: message}
end

describe "new/1" do
test "returns EventLog struct" do
actual =
EventLog.new(%{
title: "MyTitle",
timestamp: "myTimestamp",
source: "mySource",
description: "myDescription",
dataset_id: "myDatasetId",
ingestion_id: "myIngestionId"
})

assert actual.title == "MyTitle"
assert actual.timestamp == "myTimestamp"
assert actual.source == "mySource"
assert actual.description == "myDescription"
assert actual.dataset_id == "myDatasetId"
assert actual.ingestion_id == "myIngestionId"
end

test "a struct with additional fields is cleaned" do
actual =
EventLog.new(%{
title: "MyTitle",
timestamp: "myTimestamp",
source: "mySource",
description: "myDescription",
dataset_id: "myDatasetId",
ingestion_id: "myIngestionId",
is_a_good_struct: "no"
})

assert actual.title == "MyTitle"
assert actual.timestamp == "myTimestamp"
assert actual.source == "mySource"
assert actual.description == "myDescription"
assert actual.dataset_id == "myDatasetId"
assert actual.ingestion_id == "myIngestionId"
assert not Map.has_key?(actual, :is_a_good_struct)
end

data_test "field #{field} has a default value of #{inspect(default)}" do
actual =
EventLog.new(%{
title: "",
timestamp: "",
source: "",
description: "",
dataset_id: ""
})

assert Map.get(actual, field) == default

where(
field: [:title, :timestamp, :source, :description, :dataset_id],
default: ["", "", "", "", ""]
)
end

test "returns Ingestion struct when given string keys", %{message: msg} do
actual = EventLog.new(msg)
assert actual.title == "MyTitle"
assert actual.timestamp == "myTimestamp"
assert actual.source == "mySource"
assert actual.description == "myDescription"
assert actual.dataset_id == "myDatasetId"
assert actual.ingestion_id == "myIngestionId"
end

data_test "throws error when creating EventLog struct without required field: #{field}", %{message: msg} do
assert_raise ArgumentError, fn -> EventLog.new(msg |> Map.delete(field)) end

where(field: ["title", "timestamp", "source", "description", "dataset_id"])
end
end

describe "struct" do
test "can be encoded to JSON", %{message: message} do
json = Jason.encode!(message)
assert is_binary(json)
end
end
end
Loading