Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Test NetworkTunnel model. Closes #94
Browse files Browse the repository at this point in the history
  • Loading branch information
umamaistempo committed Apr 3, 2017
1 parent 930fa61 commit 0e1b46d
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/network/model/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ defmodule Helix.Network.Model.Connection do

@primary_key false
@ecto_autogenerate {:connection_id, {PK, :pk_for, [:network_connection]}}
schema "links" do
schema "connections" do
field :connection_id, PK,
primary_key: true
field :tunnel_id, PK,
primary_key: true
field :tunnel_id, PK

field :connection_type, :string
end
Expand Down
4 changes: 3 additions & 1 deletion lib/network/model/link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ defmodule Helix.Network.Model.Link do
sequence: sequence
}

cast(%__MODULE__{}, params, [:source_id, :destination_id, :sequence])
%__MODULE__{}
|> cast(params, [:source_id, :destination_id, :sequence])
|> validate_required([:source_id, :destination_id, :sequence])
end
end
144 changes: 144 additions & 0 deletions test/network/model/tunnel_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
defmodule Helix.Network.Model.TunnelTest do

use ExUnit.Case, async: true

alias HELL.TestHelper.Random
alias Helix.Network.Model.Network
alias Helix.Network.Model.Tunnel

@moduletag :unit

describe "create/4" do
test "works without a bounce list" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
bounces = []

changeset = Tunnel.create(net, gateway, destination, bounces)

assert changeset.valid?
end

test "works with bounce list" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
bounces = [Random.pk(), Random.pk(), Random.pk()]

changeset = Tunnel.create(net, gateway, destination, bounces)

assert changeset.valid?
end

test "fails if node is repeated" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
repeated = Random.pk()
bounces = [Random.pk(), repeated, Random.pk(), repeated]

changeset = Tunnel.create(net, gateway, destination, bounces)

refute changeset.valid?
end

test "fails if gateway and destination are the same" do
net = %Network{}
gateway = Random.pk()
destination = gateway
bounces = [Random.pk(), Random.pk()]

changeset = Tunnel.create(net, gateway, destination, bounces)

refute changeset.valid?
end

test "fails if gateway or destination are on bounce list" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
bounces = [gateway, destination]

changeset = Tunnel.create(net, gateway, destination, bounces)

refute changeset.valid?
end

test "prepares link list" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
bounces = []

changeset = Tunnel.create(net, gateway, destination, bounces)
links = Ecto.Changeset.get_change(changeset, :links)

# gateway -> destination
assert 1 == Enum.count(links)
end

test "includes bounce nodes on link list" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
bounces = [Random.pk(), Random.pk(), Random.pk()]

changeset = Tunnel.create(net, gateway, destination, bounces)
links = Ecto.Changeset.get_change(changeset, :links)

# gateway -> bounce1;
# bounce1 -> bounce2;
# bounce2 -> bounce3;
# bounce3 -> destination
assert 4 == Enum.count(links)
end

test "generated links are valid" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
bounces = [Random.pk(), Random.pk(), Random.pk()]

changeset = Tunnel.create(net, gateway, destination, bounces)
links = Ecto.Changeset.get_change(changeset, :links)

Enum.all?(links, fn changeset -> assert changeset.valid? end)
end

test "all nodes from the tunnel are included as links" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
bounces = [Random.pk(), Random.pk(), Random.pk()]

changeset = Tunnel.create(net, gateway, destination, bounces)
links = Ecto.Changeset.get_change(changeset, :links)

expected_nodes = MapSet.new(bounces ++ [gateway, destination])
nodes = Enum.reduce(links, MapSet.new(), fn changeset, acc ->
acc
|> MapSet.put(Ecto.Changeset.get_field(changeset, :source_id))
|> MapSet.put(Ecto.Changeset.get_field(changeset, :destination_id))
end)

assert MapSet.equal?(expected_nodes, nodes)
end

test "nodes used on tunnel are ordered" do
net = %Network{}
gateway = Random.pk()
destination = Random.pk()
bounces = [Random.pk(), Random.pk(), Random.pk(), Random.pk()]

changeset = Tunnel.create(net, gateway, destination, bounces)
struct = Ecto.Changeset.apply_changes(changeset)
links = struct.links

links = Enum.map(links, &({&1.source_id, &1.sequence}))
expected = Enum.with_index([gateway| bounces])

assert :maps.from_list(expected) == :maps.from_list(links)
end
end
end

0 comments on commit 0e1b46d

Please sign in to comment.