|
| 1 | +import pytest |
| 2 | +from taxonomy.models import Taxonomy |
| 3 | + |
| 4 | +pytestmark = pytest.mark.django_db |
| 5 | + |
| 6 | +def test_lookups_pattern_matching(): |
| 7 | + """Test basic lquery pattern matching with existing Taxonomy model.""" |
| 8 | + # Create some test data using the existing Taxonomy model |
| 9 | + Taxonomy.objects.create(path="tenant_a.departments.hr", name="HR Department") |
| 10 | + Taxonomy.objects.create(path="tenant_a.departments.finance", name="Finance Department") |
| 11 | + Taxonomy.objects.create(path="tenant_b.projects.alpha", name="Project Alpha") |
| 12 | + Taxonomy.objects.create(path="shared.public.docs", name="Public Documentation") |
| 13 | + |
| 14 | + # Test basic pattern matching |
| 15 | + hr_matches = Taxonomy.objects.filter(path__match="tenant_a.departments.*") |
| 16 | + assert hr_matches.count() == 2 |
| 17 | + |
| 18 | + # Test wildcard pattern |
| 19 | + tenant_a_matches = Taxonomy.objects.filter(path__match="tenant_a.*") |
| 20 | + assert tenant_a_matches.count() == 2 |
| 21 | + |
| 22 | +def test_lookups_contains(): |
| 23 | + """Test the key feature: contains lookup with array of lquery patterns.""" |
| 24 | + # Create test data |
| 25 | + Taxonomy.objects.create(path="tenant_a.departments.hr", name="HR") |
| 26 | + Taxonomy.objects.create(path="tenant_a.projects.alpha", name="Alpha Project") |
| 27 | + Taxonomy.objects.create(path="tenant_b.departments.eng", name="Engineering") |
| 28 | + Taxonomy.objects.create(path="shared.public.docs", name="Docs") |
| 29 | + |
| 30 | + # Test array of patterns with contains lookup |
| 31 | + patterns = [ |
| 32 | + "tenant_a.departments.*", # HR department |
| 33 | + "shared.public.*", # Public docs |
| 34 | + ] |
| 35 | + |
| 36 | + matching = Taxonomy.objects.filter( |
| 37 | + path__contains=patterns |
| 38 | + ) |
| 39 | + |
| 40 | + # Should match HR and public docs (2 items) |
| 41 | + assert matching.count() == 2 |
| 42 | + |
| 43 | + matched_names = set(item.name for item in matching) |
| 44 | + assert "HR" in matched_names |
| 45 | + assert "Docs" in matched_names |
| 46 | + |
| 47 | +def test_lookups_contains_with_single_value(): |
| 48 | + """Test the contains lookup with a single value.""" |
| 49 | + Taxonomy.objects.create(path="tenant_a.departments.hr", name="HR") |
| 50 | + Taxonomy.objects.create(path="tenant_a.projects.alpha", name="Alpha Project") |
| 51 | + Taxonomy.objects.create(path="tenant_b.departments.eng", name="Engineering") |
| 52 | + Taxonomy.objects.create(path="shared.public.docs", name="Docs") |
| 53 | + |
| 54 | + matching = Taxonomy.objects.filter(path__contains=["tenant_a.*"]) |
| 55 | + assert matching.count() == 2 |
| 56 | + matched_names = set(item.name for item in matching) |
| 57 | + assert "HR" in matched_names |
| 58 | + assert "Alpha Project" in matched_names |
| 59 | + |
| 60 | +def test_lookups_contains_invalid_value(): |
| 61 | + """Test the contains lookup with an invalid value.""" |
| 62 | + Taxonomy.objects.create(path="tenant_a.departments.hr", name="HR") |
| 63 | + Taxonomy.objects.create(path="tenant_a.projects.alpha", name="Alpha Project") |
| 64 | + Taxonomy.objects.create(path="tenant_b.departments.eng", name="Engineering") |
| 65 | + Taxonomy.objects.create(path="shared.public.docs", name="Docs") |
| 66 | + |
| 67 | + with pytest.raises(TypeError): |
| 68 | + Taxonomy.objects.filter(path__contains="tenant_a.*") |
| 69 | + |
| 70 | +def test_lookups_ancestors(): |
| 71 | + """Test the ancestors lookup.""" |
| 72 | + Taxonomy.objects.create(path="tenant_a", name="Tenant A") |
| 73 | + Taxonomy.objects.create(path="tenant_a.departments", name="Departments") |
| 74 | + Taxonomy.objects.create(path="tenant_a.departments.hr", name="HR") |
| 75 | + Taxonomy.objects.create(path="tenant_a.departments.alpha", name="Alpha Project") |
| 76 | + Taxonomy.objects.create(path="tenant_b.departments.eng", name="Engineering") |
| 77 | + Taxonomy.objects.create(path="shared.public.docs", name="Docs") |
| 78 | + |
| 79 | + matching = Taxonomy.objects.filter(path__ancestors="tenant_a.departments") |
| 80 | + assert matching.count() == 2 |
| 81 | + matched_names = set(item.name for item in matching) |
| 82 | + assert "Tenant A" in matched_names |
| 83 | + assert "Departments" in matched_names |
| 84 | + |
| 85 | +def test_lookups_descendants(): |
| 86 | + """Test the descendants lookup.""" |
| 87 | + Taxonomy.objects.create(path="tenant_a", name="Tenant A") |
| 88 | + Taxonomy.objects.create(path="tenant_a.departments", name="Departments") |
| 89 | + Taxonomy.objects.create(path="tenant_a.departments.hr", name="HR") |
| 90 | + Taxonomy.objects.create(path="tenant_a.departments.alpha", name="Alpha Project") |
| 91 | + Taxonomy.objects.create(path="tenant_b.departments.eng", name="Engineering") |
| 92 | + Taxonomy.objects.create(path="shared.public.docs", name="Docs") |
| 93 | + |
| 94 | + matching = Taxonomy.objects.filter(path__descendants="tenant_a.departments") |
| 95 | + |
| 96 | + assert matching.count() == 3 |
| 97 | + matched_names = set(item.name for item in matching) |
| 98 | + assert "HR" in matched_names |
| 99 | + assert "Alpha Project" in matched_names |
| 100 | + assert "Departments" in matched_names |
| 101 | + |
| 102 | +def test_lookups_exact(): |
| 103 | + """Test the exact lookup.""" |
| 104 | + Taxonomy.objects.create(path="tenant_a", name="Tenant A") |
| 105 | + Taxonomy.objects.create(path="tenant_a.departments", name="Departments") |
| 106 | + Taxonomy.objects.create(path="tenant_a.departments.hr", name="HR") |
| 107 | + Taxonomy.objects.create(path="tenant_a.departments.alpha", name="Alpha Project") |
| 108 | + Taxonomy.objects.create(path="tenant_b.departments.eng", name="Engineering") |
| 109 | + Taxonomy.objects.create(path="shared.public.docs", name="Docs") |
| 110 | + |
| 111 | + matching = Taxonomy.objects.filter(path__exact="tenant_a.departments.hr") |
| 112 | + assert matching.count() == 1 |
| 113 | + assert "HR" == matching.first().name |
0 commit comments