Skip to content

Commit

Permalink
add search()
Browse files Browse the repository at this point in the history
  • Loading branch information
bboonstra committed Oct 23, 2024
1 parent b890c5d commit 0b9d300
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.0] - 2024-10-22

### Added

- Database versioning is now synced with the project version
- Databases on old versions will now automatically migrate to the version of Effortless
- New configuration applications will now fail to apply if their required fields or max_size do not match the current database
- Added search() to filter for a single value

### Changed

- EffortlessConfig now takes keyword parameters instead of a dictioniary
- Use from_dict if you need the old functionality

### Docs

- TODO

## [1.2.0.1] - 2024-10-22

### Added
Expand Down
92 changes: 84 additions & 8 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def test_nested_field_query(self):
)


class TestAdvancedSearch(unittest.TestCase):
class TestAdvancedFilter(unittest.TestCase):
def setUp(self):
self.db = EffortlessDB()
self.db.wipe()
Expand Down Expand Up @@ -514,15 +514,11 @@ def test_between_dates_with_unix_timestamps(self):
def test_between_dates_with_invalid_unix_timestamps(self):
# Test with negative Unix timestamp
with self.assertRaises(ValueError):
self.db.filter(
Field("registration_date").between_dates(-1, time.time())
)
self.db.filter(Field("registration_date").between_dates(-1, time.time()))

# Test with Unix timestamp that's too large
with self.assertRaises(ValueError):
self.db.filter(
Field("registration_date").between_dates(2**63, time.time())
)
self.db.filter(Field("registration_date").between_dates(2**63, time.time()))

# Test with invalid Unix timestamp (string that's not a valid float)
with self.assertRaises(ValueError):
Expand All @@ -538,7 +534,7 @@ def test_between_dates_with_future_unix_timestamps(self):
self.assertEqual(len(result), 0) # Should be no matches


class TestAdvancedSearchErrors(unittest.TestCase):
class TestAdvancedFilterErrors(unittest.TestCase):
def setUp(self):
self.db = EffortlessDB()
self.db.wipe()
Expand Down Expand Up @@ -922,5 +918,85 @@ def test_is_type_no_matches(self):
self.assertEqual(len(result), 0)


class TestSearch(unittest.TestCase):
def setUp(self):
self.db = EffortlessDB()
self.db.wipe()
self.db.add(
{
"id": 1,
"name": "Alice",
"age": 30,
"skills": ["Python", "JavaScript"],
"address": {"city": "New York", "country": "USA"},
}
)
self.db.add(
{
"id": 2,
"name": "Bob",
"age": 25,
"skills": ["Java", "C++"],
"address": {"city": "London", "country": "UK"},
}
)
self.db.add(
{
"id": 3,
"name": "Charlie",
"age": 35,
"skills": ["Python", "Ruby"],
"address": {"city": "San Francisco", "country": "USA"},
}
)

def test_search_single_result(self):
result = self.db.search(Field("name").equals("Alice"))
self.assertIsNotNone(result)
self.assertEqual(result["name"], "Alice") # type: ignore

def test_search_no_result(self):
result = self.db.search(Field("name").equals("David"))
self.assertIsNone(result)

def test_search_multiple_results(self):
with self.assertRaises(ValueError):
self.db.search(Field("age").greater_than(20))

def test_search_complex_query(self):
result = self.db.search(
Field("age").greater_than(30) & Field("address.country").equals("USA")
)
self.assertIsNotNone(result)
self.assertEqual(result["name"], "Charlie") # type: ignore

def test_search_nested_field(self):
result = self.db.search(Field("address.city").equals("London"))
self.assertIsNotNone(result)
self.assertEqual(result["name"], "Bob") # type: ignore

def test_search_with_list_field(self):
result = self.db.search(Field("skills").contains("Java"))
self.assertIsNotNone(result)
self.assertEqual(result["name"], "Bob") # type: ignore

def test_search_with_numeric_comparison(self):
result = self.db.search(Field("age").less_than(26))
self.assertIsNotNone(result)
self.assertEqual(result["name"], "Bob") # type: ignore

def test_search_with_regex(self):
result = self.db.search(Field("name").matches_regex(r"^A.*"))
self.assertIsNotNone(result)
self.assertEqual(result["name"], "Alice") # type: ignore

def test_search_with_fuzzy_match(self):
result = self.db.search(Field("name").fuzzy_match("Allice", threshold=0.8))
self.assertIsNotNone(result)
self.assertEqual(result["name"], "Alice") # type: ignore

self.assertEqual(result["name"], "Alice") # type: ignore


if __name__ == "__main__":
unittest.main()

0 comments on commit 0b9d300

Please sign in to comment.