Skip to content

Commit

Permalink
Use basic global config
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Feb 28, 2024
1 parent bcc5363 commit 0dc5a76
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/polar/globals.ex
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
defmodule Polar.Globals do
alias Polar.Repo
alias Polar.Globals.Basic
alias Polar.Globals.Setting

@defaults %{
"basic" => Basic
}

def get(key) do
Setting
|> Repo.get_by(key: key)
|> case do
nil ->
module = Map.fetch!(@defaults, key)

struct(module, %{})

%Setting{value: value} ->
module = Map.fetch!(@defaults, key)

module.parse!(value)
end
end
end
24 changes: 24 additions & 0 deletions lib/polar/globals/basic.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Polar.Globals.Basic do
use Ecto.Schema
import Ecto.Changeset

@primary_key false

embedded_schema do
field :enable_registration, :boolean, default: true
field :versions_per_product, :integer, default: 1
end

def changeset(basic, attrs \\ %{}) do
basic
|> cast(attrs, [:enable_registration, :versions_per_product])
|> validate_required([:enable_registration, :versions_per_product])
|> validate_number(:versions_per_product, less_than_or_equal_to: 3)
end

def parse(value) do
%__MODULE__{}
|> changeset(value)
|> apply_action!(:insert)
end
end
1 change: 1 addition & 0 deletions lib/polar/globals/setting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ defmodule Polar.Globals.Setting do
setting
|> cast(attrs, [:key, :value])
|> validate_required([:key, :value])
|> validate_inclusion(:key, ["basic"])
end
end
27 changes: 27 additions & 0 deletions lib/polar/streams/version/manager.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
defmodule Polar.Streams.Version.Manager do
alias Polar.Repo
alias Polar.Globals
alias Polar.Streams.Version

import Ecto.Query, only: [from: 2]

def create(product, attrs) do
%Version{product_id: product.id}
|> Version.changeset(attrs)
|> Repo.insert()
|> case do
{:ok, version} = result ->
bot = Polar.Accounts.Automation.get_bot!()

basic_setting = Globals.get("basic")

from(
v in Version,
where:
v.product_id == ^version.product_id and
v.current_state == ^"active",
offset: ^basic_setting.versions_per_product,
order_by: [desc: :inserted_at]
)
|> Repo.all()
|> Enum.each(fn v ->
Eventful.Transit.perform(v, bot, "deactivate")
end)

result

error ->
error
end
end
end
4 changes: 4 additions & 0 deletions test/polar/streams/item/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ defmodule Polar.Streams.Item.ManagerTest do
setup do
user = user_fixture()

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, space} = Accounts.create_space(user, %{name: "test-item-increment"})

{:ok, credential} =
Expand Down
10 changes: 10 additions & 0 deletions test/polar/streams/product/manager_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
defmodule Polar.Streams.Product.ManagerTest do
use Polar.DataCase, async: true

alias Polar.Accounts
alias Polar.Streams
alias Polar.Streams.Product

import Polar.AccountsFixtures
import Polar.StreamsFixtures

setup do
password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

:ok
end

describe "filter" do
setup do
{:ok, %Product{} = without_active_versions} =
Expand Down
80 changes: 79 additions & 1 deletion test/polar/streams/version/manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ defmodule Polar.Streams.Version.ManagerTest do
use Polar.DataCase, async: true

alias Polar.Streams
alias Polar.Accounts

import Polar.AccountsFixtures

setup do
password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, product} =
Streams.create_product(%{
aliases: ["alpine/3.19", "alpine/3.19/default"],
Expand All @@ -24,7 +31,7 @@ defmodule Polar.Streams.Version.ManagerTest do
test "can successfully create new version", %{product: product} do
assert {:ok, _version} =
Streams.create_version(product, %{
serial: "20240209_13:00",
serial: "20240209-2",
items: [
%{
name: "lxd.tar.gz",
Expand All @@ -51,4 +58,75 @@ defmodule Polar.Streams.Version.ManagerTest do
})
end
end

describe "deactivate old version on create" do
setup %{product: product} do
{:ok, version} =
Streams.create_version(product, %{
serial: "20240209-2",
items: [
%{
name: "lxd.tar.gz",
file_type: "lxd.tar.gz",
hash: "35363f3d086271ed5402d61ab18ec03987bed51758c00079b8c9d372ff6d62dd",
size: 876,
is_metadata: true,
path: "images/alpine/edge/amd64/default/20240209_13:00/incus.tar.xz",
combined_hashes: [
%{
name: "combined_squashfs_sha256",
hash: "a9f02be498bf52b7bac7b5b1cfceb115878d257ad86a359a969e61fbd4bfe0bf"
}
]
},
%{
name: "root.squashfs",
file_type: "squashfs",
hash: "47cc4070da1bf17d8364c390…3603f4ed7e9e46582e690d2",
size: 2_982_800,
path: "images/alpine/edge/amd64/default/20240209_13:00/rootfs.tar.xz"
}
]
})

%{existing_version: version}
end

test "creating a new version deactivates old versions", %{
product: product,
existing_version: existing_version
} do
assert {:ok, _version} =
Streams.create_version(product, %{
serial: "20240209-3",
items: [
%{
name: "lxd.tar.gz",
file_type: "lxd.tar.gz",
hash: "35363f3d086271ed5402d61ab18ec03987bed51758c00079b8c9d372ff6d62aa",
size: 876,
is_metadata: true,
path: "images/alpine/edge/amd64/default/20240209_13:00/incus.tar.xz",
combined_hashes: [
%{
name: "combined_squashfs_sha256",
hash: "a9f02be498bf52b7bac7b5b1cfceb115878d257ad86a359a969e61fbd4bfe0aa"
}
]
},
%{
name: "root.squashfs",
file_type: "squashfs",
hash: "47cc4070da1bf17d8364c390…3603f4ed7e9e46582e690aa",
size: 2_982_800,
path: "images/alpine/edge/amd64/default/20240209_13:00/rootfs.tar.xz"
}
]
})

existing_version = Repo.reload(existing_version)

assert existing_version.current_state == "inactive"
end
end
end
5 changes: 5 additions & 0 deletions test/polar/streams/version/transitions_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
defmodule Polar.Streams.Version.TransitionsTest do
use Polar.DataCase, async: true

alias Polar.Accounts
alias Polar.Streams

import Polar.AccountsFixtures

setup do
user = user_fixture()

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, product} =
Streams.create_product(%{
aliases: ["alpine/3.19", "alpine/3.19/default"],
Expand Down
4 changes: 4 additions & 0 deletions test/polar_web/controllers/stream_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ defmodule PolarWeb.StreamControllerTest do
setup do
user = user_fixture()

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, space} = Accounts.create_space(user, %{name: "some-test-123"})

{:ok, credential} =
Expand Down
4 changes: 4 additions & 0 deletions test/polar_web/live/root_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ defmodule PolarWeb.RootLiveTest do
setup do
user = user_fixture()

password = Accounts.generate_automation_password()

_bot = bot_fixture(%{password: password})

{:ok, space} = Accounts.create_space(user, %{name: "some-test-123"})

{:ok, credential} =
Expand Down

0 comments on commit 0dc5a76

Please sign in to comment.