|
| 1 | +import logging |
| 2 | +import unittest |
| 3 | +import config |
| 4 | +import contentstack |
| 5 | +import pytest |
| 6 | + |
| 7 | +API_KEY = config.APIKEY |
| 8 | +DELIVERY_TOKEN = config.DELIVERYTOKEN |
| 9 | +ENVIRONMENT = config.ENVIRONMENT |
| 10 | +HOST = config.HOST |
| 11 | + |
| 12 | +class TestTaxonomyAPI(unittest.TestCase): |
| 13 | + def setUp(self): |
| 14 | + self.stack = contentstack.Stack(API_KEY, DELIVERY_TOKEN, ENVIRONMENT, host=HOST) |
| 15 | + |
| 16 | + def test_01_taxonomy_complex_query(self): |
| 17 | + """Test complex taxonomy query combining multiple filters""" |
| 18 | + taxonomy = self.stack.taxonomy() |
| 19 | + result = taxonomy.and_( |
| 20 | + {"taxonomies.category": {"$in": ["test"]}}, |
| 21 | + {"taxonomies.test1": {"$exists": True}} |
| 22 | + ).or_( |
| 23 | + {"taxonomies.status": {"$in": ["active"]}}, |
| 24 | + {"taxonomies.priority": {"$in": ["high"]}} |
| 25 | + ).find({'limit': 10}) |
| 26 | + if result is not None: |
| 27 | + self.assertIn('entries', result) |
| 28 | + |
| 29 | + def test_02_taxonomy_in_query(self): |
| 30 | + """Test taxonomy query with $in filter""" |
| 31 | + taxonomy = self.stack.taxonomy() |
| 32 | + result = taxonomy.in_("taxonomies.category", ["category1", "category2"]).find() |
| 33 | + if result is not None: |
| 34 | + self.assertIn('entries', result) |
| 35 | + |
| 36 | + def test_03_taxonomy_exists_query(self): |
| 37 | + """Test taxonomy query with $exists filter""" |
| 38 | + taxonomy = self.stack.taxonomy() |
| 39 | + result = taxonomy.exists("taxonomies.test1").find() |
| 40 | + if result is not None: |
| 41 | + self.assertIn('entries', result) |
| 42 | + |
| 43 | + def test_04_taxonomy_or_query(self): |
| 44 | + """Test taxonomy query with $or filter""" |
| 45 | + taxonomy = self.stack.taxonomy() |
| 46 | + result = taxonomy.or_( |
| 47 | + {"taxonomies.category": {"$in": ["category1"]}}, |
| 48 | + {"taxonomies.test1": {"$exists": True}} |
| 49 | + ).find() |
| 50 | + if result is not None: |
| 51 | + self.assertIn('entries', result) |
| 52 | + |
| 53 | + def test_05_taxonomy_and_query(self): |
| 54 | + """Test taxonomy query with $and filter""" |
| 55 | + taxonomy = self.stack.taxonomy() |
| 56 | + result = taxonomy.and_( |
| 57 | + {"taxonomies.category": {"$in": ["category1"]}}, |
| 58 | + {"taxonomies.test1": {"$exists": True}} |
| 59 | + ).find() |
| 60 | + if result is not None: |
| 61 | + self.assertIn('entries', result) |
| 62 | + |
| 63 | + def test_06_taxonomy_equal_and_below(self): |
| 64 | + """Test taxonomy query with $eq_below filter""" |
| 65 | + taxonomy = self.stack.taxonomy() |
| 66 | + result = taxonomy.equal_and_below("taxonomies.color", "blue", levels=1).find() |
| 67 | + if result is not None: |
| 68 | + self.assertIn('entries', result) |
| 69 | + |
| 70 | + def test_07_taxonomy_below(self): |
| 71 | + """Test taxonomy query with $below filter""" |
| 72 | + taxonomy = self.stack.taxonomy() |
| 73 | + result = taxonomy.below("taxonomies.hierarchy", "parent_uid", levels=2).find() |
| 74 | + if result is not None: |
| 75 | + self.assertIn('entries', result) |
| 76 | + |
| 77 | + def test_08_taxonomy_equal_and_above(self): |
| 78 | + """Test taxonomy query with $eq_above filter""" |
| 79 | + taxonomy = self.stack.taxonomy() |
| 80 | + result = taxonomy.equal_and_above("taxonomies.hierarchy", "child_uid", levels=3).find() |
| 81 | + if result is not None: |
| 82 | + self.assertIn('entries', result) |
| 83 | + |
| 84 | + def test_09_taxonomy_above(self): |
| 85 | + """Test taxonomy query with $above filter""" |
| 86 | + taxonomy = self.stack.taxonomy() |
| 87 | + result = taxonomy.above("taxonomies.hierarchy", "child_uid", levels=2).find() |
| 88 | + if result is not None: |
| 89 | + self.assertIn('entries', result) |
| 90 | + |
| 91 | + def test_10_taxonomy_find_with_params(self): |
| 92 | + """Test taxonomy find with additional parameters""" |
| 93 | + taxonomy = self.stack.taxonomy() |
| 94 | + result = taxonomy.in_("taxonomies.category", ["test"]).find({'limit': 5}) |
| 95 | + if result is not None: |
| 96 | + self.assertIn('entries', result) |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + unittest.main() |
0 commit comments