Browse Source

[ruby/hanami] Add fortunes test (#9686)

Petrik de Heus 5 months ago
parent
commit
864558a389

+ 1 - 0
frameworks/Ruby/hanami/Gemfile

@@ -7,6 +7,7 @@ gem "hanami-router", "~> 2.2"
 gem "hanami-controller", "~> 2.2"
 gem "hanami-controller", "~> 2.2"
 gem "hanami-db", "~> 2.2"
 gem "hanami-db", "~> 2.2"
 gem "hanami-validations", "~> 2.2"
 gem "hanami-validations", "~> 2.2"
+gem "hanami-view", "~> 2.2"
 
 
 gem "dry-types", "~> 1.0", ">= 1.6.1"
 gem "dry-types", "~> 1.0", ">= 1.6.1"
 gem "puma"
 gem "puma"

+ 10 - 0
frameworks/Ruby/hanami/Gemfile.lock

@@ -102,6 +102,13 @@ GEM
       dry-transformer (~> 1.0, < 2)
       dry-transformer (~> 1.0, < 2)
     hanami-validations (2.2.0)
     hanami-validations (2.2.0)
       dry-validation (>= 1.10, < 2)
       dry-validation (>= 1.10, < 2)
+    hanami-view (2.2.1)
+      dry-configurable (~> 1.0)
+      dry-core (~> 1.0)
+      dry-inflector (~> 1.0, < 2)
+      temple (~> 0.10.0, >= 0.10.2)
+      tilt (~> 2.3)
+      zeitwerk (~> 2.6)
     hansi (0.2.1)
     hansi (0.2.1)
     ice_nine (0.11.2)
     ice_nine (0.11.2)
     json (2.10.2)
     json (2.10.2)
@@ -146,6 +153,8 @@ GEM
     ruby2_keywords (0.0.5)
     ruby2_keywords (0.0.5)
     sequel (5.90.0)
     sequel (5.90.0)
       bigdecimal
       bigdecimal
+    temple (0.10.3)
+    tilt (2.6.0)
     transproc (1.1.1)
     transproc (1.1.1)
     zeitwerk (2.7.2)
     zeitwerk (2.7.2)
 
 
@@ -160,6 +169,7 @@ DEPENDENCIES
   hanami-db (~> 2.2)
   hanami-db (~> 2.2)
   hanami-router (~> 2.2)
   hanami-router (~> 2.2)
   hanami-validations (~> 2.2)
   hanami-validations (~> 2.2)
+  hanami-view (~> 2.2)
   pg
   pg
   puma
   puma
   rake
   rake

+ 12 - 0
frameworks/Ruby/hanami/app/actions/fortunes/index.rb

@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+module HelloWorld
+  module Actions
+    module Fortunes
+      class Index < HelloWorld::Action
+        def handle(*, response)
+        end
+      end
+    end
+  end
+end

+ 7 - 0
frameworks/Ruby/hanami/app/relations/fortunes.rb

@@ -0,0 +1,7 @@
+module HelloWorld
+  module Relations
+    class Fortunes < HelloWorld::DB::Relation
+      schema :Fortune, infer: true, as: :fortunes
+    end
+  end
+end

+ 11 - 0
frameworks/Ruby/hanami/app/repos/fortune_repo.rb

@@ -0,0 +1,11 @@
+module HelloWorld
+  module Repos
+    class FortuneRepo < HelloWorld::DB::Repo
+      def all
+        results = fortunes.to_a.map(&:to_h)
+        results << { id: 0, message: 'Additional fortune added at request time.' }
+        results.sort_by! { |fortune| fortune[:message] }
+      end
+    end
+  end
+end

+ 12 - 0
frameworks/Ruby/hanami/app/templates/fortunes/index.html.erb

@@ -0,0 +1,12 @@
+<table>
+  <tr>
+    <th>id</th>
+    <th>message</th>
+  </tr>
+  <% fortunes.each do |fortune| %>
+    <tr>
+      <td><%= fortune[:id] %></td>
+      <td><%= fortune[:message] %></td>
+    </tr>
+  <% end %>
+</table>

+ 2 - 4
frameworks/Ruby/hanami/app/templates/layouts/app.html.erb

@@ -1,9 +1,7 @@
 <!DOCTYPE html>
 <!DOCTYPE html>
-<html lang="en">
+<html>
   <head>
   <head>
-    <meta charset="UTF-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1.0">
-    <title>Bookshelf</title>
+    <title>Fortunes</title>
   </head>
   </head>
   <body>
   <body>
     <%= yield %>
     <%= yield %>

+ 14 - 0
frameworks/Ruby/hanami/app/views/fortunes/index.rb

@@ -0,0 +1,14 @@
+module HelloWorld
+  module Views
+    module Fortunes
+      class Index < HelloWorld::View
+
+        include Deps["repos.fortune_repo"]
+
+        expose :fortunes do
+          fortune_repo.all
+        end
+      end
+    end
+  end
+end

+ 1 - 0
frameworks/Ruby/hanami/benchmark_config.json

@@ -5,6 +5,7 @@
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
       "query_url": "/queries?queries=",
       "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
       "update_url": "/updates?queries=",
       "update_url": "/updates?queries=",
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
       "port": 8080,
       "port": 8080,

+ 3 - 2
frameworks/Ruby/hanami/config/routes.rb

@@ -2,10 +2,11 @@
 
 
 module HelloWorld
 module HelloWorld
   class Routes < Hanami::Routes
   class Routes < Hanami::Routes
-    get "/db", to: "db.index"
     get "/json", to: "json.index"
     get "/json", to: "json.index"
+    get "/db", to: "db.index"
+    get "/queries", to: "queries.index"
+    get "/fortunes", to: "fortunes.index"
     get "/updates", to: "updates.index"
     get "/updates", to: "updates.index"
     get "/plaintext", to: "plaintext.index"
     get "/plaintext", to: "plaintext.index"
-    get "/queries", to: "queries.index"
   end
   end
 end
 end