Эх сурвалжийг харах

fix elixir update test (#8226)

* fix elixir update test

* refactor to new function random_id
Chris Garvis 2 жил өмнө
parent
commit
98982e4bed

+ 1 - 1
frameworks/Elixir/phoenix/lib/hello/application.ex

@@ -6,7 +6,7 @@ defmodule Hello.Application do
   def start(_type, _args) do
     children = [
       Hello.Repo,
-      Hello.Cache,
+      {Hello.Cache, []},
       HelloWeb.Endpoint
     ]
 

+ 7 - 0
frameworks/Elixir/phoenix/lib/hello/models/world.ex

@@ -1,8 +1,15 @@
 defmodule Hello.Models.World do
   use Ecto.Schema
 
+  import Ecto.Changeset
+
   @derive {Jason.Encoder, only: [:id, :randomnumber]}
   schema "world" do
     field :randomnumber, :integer
   end
+
+  def changeset(world, params \\ %{}) do
+    world
+    |> cast(params, [:randomnumber])
+  end
 end

+ 5 - 12
frameworks/Elixir/phoenix/lib/hello_web/controllers/error_html.ex

@@ -1,17 +1,10 @@
 defmodule HelloWeb.ErrorHTML do
   use HelloWeb, :html
 
-  def render("404.html", _assigns) do
-    "Page not found - 404"
-  end
-
-  def render("500.html", _assigns) do
-    "Server internal error - 500"
-  end
-
-  # In case no render clause matches or no
-  # template is found, let's render it as 500
-  def template_not_found(_template, assigns) do
-    render("500.html", assigns)
+  # The default is to render a plain text page based on
+  # the template name. For example, "404.html" becomes
+  # "Not Found".
+  def render(template, _assigns) do
+    Phoenix.Controller.status_message_from_template(template)
   end
 end

+ 40 - 77
frameworks/Elixir/phoenix/lib/hello_web/controllers/page_controller.ex

@@ -6,50 +6,33 @@ defmodule HelloWeb.PageController do
   alias Hello.Repo
   alias Hello.Cache
 
-  @json "application/json"
-  @plain "text/plain"
   @random_max 10_000
-  @all_ids 1..10_000
 
   def index(conn, _params) do
-    resp = Jason.encode!(%{"TE Benchmarks\n" => "Started"})
-
-    conn
-    |> put_resp_content_type(@json, nil)
-    |> send_resp(200, resp)
+    json(conn, %{"TE Benchmarks\n" => "Started"})
   end
 
   # avoid namespace collision
   def _json(conn, _params) do
-    resp = Jason.encode!(%{"message" => "Hello, world!"})
-
-    conn
-    |> put_resp_content_type(@json, nil)
-    |> send_resp(200, resp)
+    json(conn, %{message: "Hello, World!"})
   end
 
   def db(conn, _params) do
-    resp =
-      Repo.get(World, :rand.uniform(@random_max))
-      |> Jason.encode!()
+    world = Repo.get(World, random_id())
 
-    conn
-    |> put_resp_content_type(@json, nil)
-    |> send_resp(200, resp)
+    json(conn, world)
   end
 
   def queries(conn, params) do
     :rand.seed(:exsp)
 
-    resp =
-      1..@random_max
-      |> Enum.take_random(size(params["queries"]))
-      |> parallel(fn idx -> Repo.get(World, idx) end)
-      |> Jason.encode_to_iodata!()
+    worlds =
+      Stream.repeatedly(&random_id/0)
+      |> Stream.uniq()
+      |> Stream.map(fn idx -> Repo.get(World, idx) end)
+      |> Enum.take(size(params["queries"]))
 
-    conn
-    |> put_resp_content_type(@json, nil)
-    |> send_resp(200, resp)
+    json(conn, worlds)
   end
 
   def fortunes(conn, _params) do
@@ -68,48 +51,38 @@ defmodule HelloWeb.PageController do
   def updates(conn, params) do
     :rand.seed(:exsp)
 
-    count = size(params["queries"])
-
     worlds =
-      1..@random_max
-      |> Enum.take_random(count)
-      |> parallel(fn idx -> Repo.get(World, idx) end)
-      |> Enum.map(fn world ->
-        %{id: world.id, randomnumber: random_but(world.randomnumber)}
-      end)
-
-    {^count, result} =
-      Repo.insert_all(
-        World,
-        worlds,
-        on_conflict: :replace_all,
-        conflict_target: [:id],
-        returning: true
-      )
-
-    conn
-    |> put_resp_content_type(@json, nil)
-    |> send_resp(200, Jason.encode_to_iodata!(result))
+      Stream.repeatedly(&random_id/0)
+      |> Stream.uniq()
+      |> Stream.map(fn idx -> Repo.get(World, idx) end)
+      |> Stream.map(fn world -> %{id: world.id, randomnumber: :rand.uniform(@random_max)} end)
+      |> Enum.take(size(params["queries"]))
+
+    Repo.insert_all(
+      World,
+      worlds,
+      on_conflict: {:replace_all_except, [:id]},
+      conflict_target: [:id],
+      returning: false
+    )
+
+    json(conn, worlds)
   end
 
   def plaintext(conn, _params) do
-    conn
-    |> put_resp_content_type(@plain, nil)
-    |> send_resp(200, "Hello, world!")
+    text(conn, "Hello, World!")
   end
 
   def cached(conn, params) do
     :rand.seed(:exsp)
 
-    resp =
-      @all_ids
-      |> Enum.take_random(size(params["count"]))
-      |> parallel(&get_cached_world/1)
-      |> Jason.encode_to_iodata!()
+    worlds =
+      Stream.repeatedly(&random_id/0)
+      |> Stream.uniq()
+      |> Stream.map(&get_cached_world/1)
+      |> Enum.take(size(params["count"]))
 
-    conn
-    |> put_resp_content_type(@json, nil)
-    |> send_resp(200, resp)
+    json(conn, worlds)
   end
 
   defp get_cached_world(idx) do
@@ -118,35 +91,25 @@ defmodule HelloWeb.PageController do
         world = Repo.get(World, idx)
         :ok = Cache.put(idx, world)
         world
+
       world ->
         world
     end
   end
 
-
-  defp random_but(not_this_value) do
-    case :rand.uniform(@random_max) do
-      new_value when new_value == not_this_value ->
-        random_but(not_this_value)
-
-      new_value ->
-        new_value
-    end
-  end
-
-  defp parallel(collection, func) do
-    collection
-    |> Enum.map(&Task.async(fn -> func.(&1) end))
-    |> Task.await_many()
+  defp random_id() do
+    :rand.uniform(@random_max)
   end
 
   defp size(nil), do: 1
+  defp size(""), do: 1
 
-  defp size(queries) do
+  defp size(queries) when is_bitstring(queries) do
     case Integer.parse(queries) do
-      {x, ""} when x in 1..500 -> x
-      {x, ""} when x > 500 -> 500
+      {count, _} -> max(1, min(500, count))
       _ -> 1
     end
   end
+
+  defp size(_), do: 1
 end

+ 1 - 1
frameworks/Elixir/phoenix/mix.exs

@@ -29,7 +29,7 @@ defmodule Hello.Mixfile do
   # Type `mix help deps` for examples and options
   defp deps do
     [
-      {:bandit, ">= 0.6.2"},
+      {:bandit, "~> 0.6.2"},
       {:gettext, "~> 0.20"},
       {:ecto_sql, "~> 3.7"},
       {:jason, "~> 1.2"},

+ 1 - 1
frameworks/Elixir/phoenix/phoenix-bandit.dockerfile

@@ -1,4 +1,4 @@
-ARG ELIXIR="1.14.4"
+ARG ELIXIR="1.14.5"
 ARG ERLANG="26.0"
 ARG ALPINE="3.17.3"
 

+ 1 - 1
frameworks/Elixir/phoenix/phoenix.dockerfile

@@ -1,4 +1,4 @@
-ARG ELIXIR="1.14.4"
+ARG ELIXIR="1.14.5"
 ARG ERLANG="26.0"
 ARG ALPINE="3.17.3"