Matt Chaput avatar Matt Chaput committed 789264f

Fixed obnoxious message when you try to use NUMERIC(Decimal).
Added unit test.

Comments (0)

Files changed (2)

src/whoosh/fields.py

         # Raise an error if the user tries to use a type other than int or
         # float
         if numtype is Decimal:
-            raise TypeError("To store Decimal instances, set type to int use "
-                            "the decimal_places argument")
+            numtype = int
+            if not decimal_places:
+                raise TypeError("To store Decimal instances, you must set the "
+                                "decimal_places argument")
         elif numtype not in (int, float):
             raise TypeError("Can't use %r as a type, use int or float"
                             % numtype)

tests/test_searching.py

         assert r[0]["name"] == u("close")
 
 
+def test_find_decimals():
+    from decimal import Decimal
+
+    schema = fields.Schema(name=fields.KEYWORD(stored=True),
+                           num=fields.NUMERIC(Decimal, decimal_places=5))
+    ix = RamStorage().create_index(schema)
+
+    with ix.writer() as w:
+        w.add_document(name=u("alfa"), num=Decimal("1.5"))
+        w.add_document(name=u("bravo"), num=Decimal("2.1"))
+        w.add_document(name=u("charlie"), num=Decimal("5.3"))
+        w.add_document(name=u("delta"), num=Decimal(3))
+        w.add_document(name=u("echo"), num=Decimal("3.00001"))
+        w.add_document(name=u("foxtrot"), num=Decimal("3"))
+
+    qp = qparser.QueryParser("name", ix.schema)
+    q = qp.parse("num:3.0")
+    print("q=", repr(q))
+    assert isinstance(q, query.Term)
+
+    with ix.searcher() as s:
+        r = s.search(q)
+        names = " ".join(sorted(hit["name"] for hit in r))
+        assert names == "delta foxtrot"
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.