livebook/test/support/factory.ex

63 lines
1.4 KiB
Elixir
Raw Normal View History

defmodule Livebook.Factory do
@moduledoc false
def build(:user) do
%Livebook.Users.User{
id: Livebook.Utils.random_id(),
name: "Jose Valim",
hex_color: Livebook.EctoTypes.HexColor.random()
}
end
def build(:fly_metadata) do
%Livebook.Hubs.Metadata{
id: "fly-foo-bar-baz",
name: "My Personal Hub",
color: "#FF00FF",
provider: build(:fly)
}
end
def build(:fly) do
%Livebook.Hubs.Fly{
id: "fly-foo-bar-baz",
hub_name: "My Personal Hub",
hub_color: "#FF00FF",
access_token: Livebook.Utils.random_cookie(),
organization_id: Livebook.Utils.random_id(),
organization_type: "PERSONAL",
organization_name: "Foo",
application_id: "foo-bar-baz"
}
end
def build(:env_var) do
%Livebook.Settings.EnvVar{
2022-09-17 07:37:01 +08:00
name: "BAR",
value: "foo"
}
end
def build(factory_name, attrs \\ %{}) do
factory_name |> build() |> struct!(attrs)
end
def params_for(factory_name, attrs \\ %{}) do
factory_name |> build() |> struct!(attrs) |> Map.from_struct()
end
def insert_hub(factory_name, attrs \\ %{}) do
factory_name
|> build(attrs)
|> Livebook.Hubs.save_hub()
end
def insert_env_var(factory_name, attrs \\ %{}) do
env_var = build(factory_name, attrs)
attributes = env_var |> Map.from_struct() |> Map.to_list()
2022-09-17 07:37:01 +08:00
Livebook.Storage.current().insert(:env_vars, env_var.name, attributes)
env_var
end
end