This is an automated email from the ASF dual-hosted git repository.

jan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 13501dc28c6d4039672ba3e02722a72e7b6c500d
Author: James Coglan <[email protected]>
AuthorDate: Fri Oct 3 15:27:30 2025 +0100

    Port the friend_docs fixture and one dependent test as an example from 
Python to Elixir
---
 test/elixir/test/config/search.elixir         |   3 +
 test/elixir/test/mango/06_basic_text_test.exs |  33 +++
 test/elixir/test/support/friend_docs.ex       | 286 ++++++++++++++++++++++++++
 test/elixir/test/support/mango_database.ex    |  13 +-
 4 files changed, 333 insertions(+), 2 deletions(-)

diff --git a/test/elixir/test/config/search.elixir 
b/test/elixir/test/config/search.elixir
index b49b8d762..a65b2f857 100644
--- a/test/elixir/test/config/search.elixir
+++ b/test/elixir/test/config/search.elixir
@@ -32,5 +32,8 @@
     "facet counts, empty",
     "facet ranges, empty",
     "facet ranges, non-empty"
+  ],
+  "ElemMatchTests": [
+    "elem match non object"
   ]
 }
diff --git a/test/elixir/test/mango/06_basic_text_test.exs 
b/test/elixir/test/mango/06_basic_text_test.exs
new file mode 100644
index 000000000..679875bfa
--- /dev/null
+++ b/test/elixir/test/mango/06_basic_text_test.exs
@@ -0,0 +1,33 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+defmodule ElemMatchTests do
+  use CouchTestCase
+
+  @db_name "basic-text-elem-match"
+
+  setup do
+    FriendDocs.setup(@db_name, "text")
+  end
+
+  test "elem match non object" do
+    q = %{"bestfriends" => %{"$elemMatch" => %{"$eq" => "Wolverine", "$eq" => 
"Cyclops"}}}
+    docs = MangoDatabase.find(@db_name, q)
+    assert length(docs) == 1
+    assert Enum.at(docs, 0)["bestfriends"] == ["Wolverine", "Cyclops"]
+
+    q = %{"results" => %{"$elemMatch" => %{"$gte" => 80, "$lt" => 85}}}
+    docs = MangoDatabase.find(@db_name, q)
+    assert length(docs) == 1
+    assert Enum.at(docs, 0)["results"] == [82, 85, 88]
+  end
+end
diff --git a/test/elixir/test/support/friend_docs.ex 
b/test/elixir/test/support/friend_docs.ex
new file mode 100644
index 000000000..289bc999d
--- /dev/null
+++ b/test/elixir/test/support/friend_docs.ex
@@ -0,0 +1,286 @@
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+defmodule FriendDocs do
+  @moduledoc """
+  Generated with http://www.json-generator.com/
+
+  With this pattern:
+
+  [
+    '{{repeat(15)}}',
+    {
+      _id: '{{index()}}',
+      name: {
+        first: '{{firstName()}}',
+        last: '{{surname()}}'
+      },
+      friends: [
+        '{{repeat(3)}}',
+        {
+          id: '{{index()}}',
+          name: {
+            first: '{{firstName()}}',
+            last: '{{surname()}}'
+          },
+          type: '{{random("personal", "work")}}'
+        }
+      ]
+    }
+  ]
+  """
+
+  @docs [
+    %{
+      "_id" => "54a43171d37ae5e81bff5ae0",
+      "user_id" => 0,
+      "name" => %{"first" => "Ochoa", "last" => "Fox"},
+      "friends" => [
+        %{
+          "id" => 0,
+          "name" => %{"first" => "Sherman", "last" => "Davidson"},
+          "type" => "personal",
+        },
+        %{
+          "id" => 1,
+          "name" => %{"first" => "Vargas", "last" => "Mendez"},
+          "type" => "personal",
+        },
+        %{"id" => 2, "name" => %{"first" => "Sheppard", "last" => "Cotton"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a43171958485dc32917c50",
+      "user_id" => 1,
+      "name" => %{"first" => "Sheppard", "last" => "Cotton"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Ochoa", "last" => "Fox"}, "type" 
=> "work"},
+        %{
+          "id" => 1,
+          "name" => %{"first" => "Vargas", "last" => "Mendez"},
+          "type" => "personal",
+        },
+        %{"id" => 2, "name" => %{"first" => "Kendra", "last" => "Burns"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a431711cf025ba74bea899",
+      "user_id" => 2,
+      "name" => %{"first" => "Hunter", "last" => "Wells"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Estes", "last" => "Fischer"}, 
"type" => "work"},
+        %{
+          "id" => 1,
+          "name" => %{"first" => "Farrell", "last" => "Maddox"},
+          "type" => "personal",
+        },
+        %{"id" => 2, "name" => %{"first" => "Kendra", "last" => "Burns"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a4317151a70a9881ac28a4",
+      "user_id" => 3,
+      "name" => %{"first" => "Millicent", "last" => "Guy"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Luella", "last" => "Mendoza"}, 
"type" => "work"},
+        %{
+          "id" => 1,
+          "name" => %{"first" => "Melanie", "last" => "Foster"},
+          "type" => "personal",
+        },
+        %{"id" => 2, "name" => %{"first" => "Hopkins", "last" => "Scott"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a43171d946b78703a0e076",
+      "user_id" => 4,
+      "name" => %{"first" => "Elisabeth", "last" => "Brady"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Sofia", "last" => "Workman"}, 
"type" => "work"},
+        %{"id" => 1, "name" => %{"first" => "Alisha", "last" => "Reilly"}, 
"type" => "work"},
+        %{"id" => 2, "name" => %{"first" => "Ochoa", "last" => "Burch"}, 
"type" => "personal"},
+      ],
+    },
+    %{
+      "_id" => "54a4317118abd7f1992464ee",
+      "user_id" => 5,
+      "name" => %{"first" => "Pollard", "last" => "French"},
+      "friends" => [
+        %{
+          "id" => 0,
+          "name" => %{"first" => "Hollie", "last" => "Juarez"},
+          "type" => "personal",
+        },
+        %{"id" => 1, "name" => %{"first" => "Nelda", "last" => "Newton"}, 
"type" => "personal"},
+        %{"id" => 2, "name" => %{"first" => "Yang", "last" => "Pace"}, "type" 
=> "personal"},
+      ],
+    },
+    %{
+      "_id" => "54a43171f139e63d6579121e",
+      "user_id" => 6,
+      "name" => %{"first" => "Acevedo", "last" => "Morales"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Payne", "last" => "Berry"}, 
"type" => "personal"},
+        %{
+          "id" => 1,
+          "name" => %{"first" => "Rene", "last" => "Valenzuela"},
+          "type" => "personal",
+        },
+        %{"id" => 2, "name" => %{"first" => "Dora", "last" => "Gallegos"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a431719783cef80876dde8",
+      "user_id" => 7,
+      "name" => %{"first" => "Cervantes", "last" => "Marquez"},
+      "friends" => [
+        %{
+          "id" => 0,
+          "name" => %{"first" => "Maxwell", "last" => "Norman"},
+          "type" => "personal",
+        },
+        %{"id" => 1, "name" => %{"first" => "Shields", "last" => "Bass"}, 
"type" => "personal"},
+        %{"id" => 2, "name" => %{"first" => "Luz", "last" => "Jacobson"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a43171ecc7540d1f7aceae",
+      "user_id" => 8,
+      "name" => %{"first" => "West", "last" => "Morrow"},
+      "friends" => [
+        %{
+          "id" => 0,
+          "name" => %{"first" => "Townsend", "last" => "Dixon"},
+          "type" => "personal",
+        },
+        %{
+          "id" => 1,
+          "name" => %{"first" => "Callahan", "last" => "Buck"},
+          "type" => "personal",
+        },
+        %{
+          "id" => 2,
+          "name" => %{"first" => "Rachel", "last" => "Fletcher"},
+          "type" => "personal",
+        },
+      ],
+    },
+    %{
+      "_id" => "54a4317113e831f4af041a0a",
+      "user_id" => 9,
+      "name" => %{"first" => "Cotton", "last" => "House"},
+      "friends" => [
+        %{
+          "id" => 0,
+          "name" => %{"first" => "Mckenzie", "last" => "Medina"},
+          "type" => "personal",
+        },
+        %{"id" => 1, "name" => %{"first" => "Cecilia", "last" => "Miles"}, 
"type" => "work"},
+        %{"id" => 2, "name" => %{"first" => "Guerra", "last" => "Cervantes"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a43171686eb1f48ebcbe01",
+      "user_id" => 10,
+      "name" => %{"first" => "Wright", "last" => "Rivas"},
+      "friends" => [
+        %{
+          "id" => 0,
+          "name" => %{"first" => "Campos", "last" => "Freeman"},
+          "type" => "personal",
+        },
+        %{
+          "id" => 1,
+          "name" => %{"first" => "Christian", "last" => "Ferguson"},
+          "type" => "personal",
+        },
+        %{"id" => 2, "name" => %{"first" => "Doreen", "last" => "Wilder"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a43171a4f3d5638c162f4f",
+      "user_id" => 11,
+      "name" => %{"first" => "Lorene", "last" => "Dorsey"},
+      "friends" => [
+        %{
+          "id" => 0,
+          "name" => %{"first" => "Gibbs", "last" => "Mccarty"},
+          "type" => "personal",
+        },
+        %{"id" => 1, "name" => %{"first" => "Neal", "last" => "Franklin"}, 
"type" => "work"},
+        %{"id" => 2, "name" => %{"first" => "Kristy", "last" => "Head"}, 
"type" => "personal"},
+      ],
+      "bestfriends" => ["Wolverine", "Cyclops"],
+    },
+    %{
+      "_id" => "54a431719faa420a5b4fbeb0",
+      "user_id" => 12,
+      "name" => %{"first" => "Juanita", "last" => "Cook"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Wilkins", "last" => "Chang"}, 
"type" => "work"},
+        %{"id" => 1, "name" => %{"first" => "Haney", "last" => "Rivera"}, 
"type" => "work"},
+        %{"id" => 2, "name" => %{"first" => "Lauren", "last" => "Manning"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "54a43171e65d35f9ee8c53c0",
+      "user_id" => 13,
+      "name" => %{"first" => "Levy", "last" => "Osborn"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Vinson", "last" => "Vargas"}, 
"type" => "work"},
+        %{"id" => 1, "name" => %{"first" => "Felicia", "last" => "Beach"}, 
"type" => "work"},
+        %{"id" => 2, "name" => %{"first" => "Nadine", "last" => "Kemp"}, 
"type" => "work"},
+      ],
+      "results" => [82, 85, 88],
+    },
+    %{
+      "_id" => "54a4317132f2c81561833259",
+      "user_id" => 14,
+      "name" => %{"first" => "Christina", "last" => "Raymond"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Herrera", "last" => "Walton"}, 
"type" => "work"},
+        %{"id" => 1, "name" => %{"first" => "Hahn", "last" => "Rutledge"}, 
"type" => "work"},
+        %{"id" => 2, "name" => %{"first" => "Stacie", "last" => "Harding"}, 
"type" => "work"},
+      ],
+    },
+    %{
+      "_id" => "589f32af493145f890e1b051",
+      "user_id" => 15,
+      "name" => %{"first" => "Tanisha", "last" => "Bowers"},
+      "friends" => [
+        %{"id" => 0, "name" => %{"first" => "Ochoa", "last" => "Pratt"}, 
"type" => "personal"},
+        %{"id" => 1, "name" => %{"first" => "Ochoa", "last" => "Romero"}, 
"type" => "personal"},
+        %{"id" => 2, "name" => %{"first" => "Ochoa", "last" => "Bowman"}, 
"type" => "work"},
+      ],
+    },
+  ]
+
+  def setup(db, index_type \\ "view") do
+    MangoDatabase.recreate(db)
+    MangoDatabase.save_docs(db, @docs)
+
+    case index_type do
+      "view" -> add_view_indexes(db)
+      "text" -> add_text_indexes(db)
+    end
+
+    :ok
+  end
+
+  defp add_view_indexes(db) do
+    # TODO: this function is not defined in the Python version of this module?
+  end
+
+  defp add_text_indexes(db) do
+    MangoDatabase.create_text_index(db)
+  end
+end
diff --git a/test/elixir/test/support/mango_database.ex 
b/test/elixir/test/support/mango_database.ex
index 80dcd338c..fbbd23d1c 100644
--- a/test/elixir/test/support/mango_database.ex
+++ b/test/elixir/test/support/mango_database.ex
@@ -11,6 +11,11 @@
 # the License.
 
 defmodule MangoDatabase do
+  def has_text_service() do
+    resp = Couch.get("/")
+    "search" in resp.body["features"]
+  end
+
   def recreate(db, opts \\ []) do
     resp = Couch.get("/#{db}")
     if resp.status_code == 200 do
@@ -39,7 +44,7 @@ defmodule MangoDatabase do
   end
 
   def create_index(db, fields, name) do
-    resp = Couch.post("/#{db}/_index", body: %{
+    Couch.post("/#{db}/_index", body: %{
       "index" => %{"fields" => fields},
       "name" => name,
       "ddoc" => name,
@@ -49,7 +54,11 @@ defmodule MangoDatabase do
   end
 
   def create_text_index(db) do
-    # TODO
+    Couch.post("/#{db}/_index", body: %{
+      "index" => %{},
+      "type" => "text",
+      "w" => 3
+    })
   end
 
   # TODO: port more options from src/mango/test/mango.py `def find(...)`

Reply via email to