test_weaviate.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import pytest
  2. from utils.weaviate_db import WeaviateEngine
  3. # Test case 1: Empty conditions list
  4. def test_build_filter_empty_conditions():
  5. engine = WeaviateEngine("test_collection", None)
  6. result = engine.build_filter([])
  7. assert result is None
  8. # Test case 2: Single condition
  9. def test_build_filter_single_condition():
  10. engine = WeaviateEngine("test_collection", None)
  11. conditions = [{"key": "name", "match": "John"}]
  12. result = engine.build_filter(conditions)
  13. expected = {
  14. "operator": "And",
  15. "operands": [
  16. {
  17. "path": ["metadata.name"],
  18. "operator": "Equal",
  19. "valueString": "John"
  20. }
  21. ]
  22. }
  23. assert result == expected
  24. # Test case 3: Multiple conditions
  25. def test_build_filter_multiple_conditions():
  26. engine = WeaviateEngine("test_collection", None)
  27. conditions = [
  28. {"key": "name", "match": "John"},
  29. {"key": "age", "match": 30}
  30. ]
  31. result = engine.build_filter(conditions)
  32. expected = {
  33. "operator": "And",
  34. "operands": [
  35. {
  36. "path": ["metadata.name"],
  37. "operator": "Equal",
  38. "valueString": "John"
  39. },
  40. {
  41. "path": ["metadata.age"],
  42. "operator": "Equal",
  43. "valueString": "30"
  44. }
  45. ]
  46. }
  47. assert result == expected
  48. # Test case 4: Missing key or match value
  49. def test_build_filter_missing_key_or_match():
  50. engine = WeaviateEngine("test_collection", None)
  51. conditions = [{"key": "name"}, {"match": "John"}]
  52. result = engine.build_filter(conditions)
  53. assert result is None