askrobots / dbbasic-examples
Public
Example DBBasic apps (an API, a blog and a microblog) and a template for new DBBasic modules.
danq9
Add official module template for creating DBBasic packages
17cf88a
1 year, 1 month ago
History
test_module.py · 3.8 KB · 148 lines
| 1 | """Tests for dbbasic-module""" |
| 2 | |
| 3 | import pytest |
| 4 | from pathlib import Path |
| 5 | import tempfile |
| 6 | import shutil |
| 7 | from dbbasic_module import Module |
| 8 | |
| 9 | |
| 10 | @pytest.fixture |
| 11 | def temp_data_dir(): |
| 12 | """Create temporary data directory for tests""" |
| 13 | temp_dir = tempfile.mkdtemp() |
| 14 | yield temp_dir |
| 15 | shutil.rmtree(temp_dir) |
| 16 | |
| 17 | |
| 18 | @pytest.fixture |
| 19 | def module(temp_data_dir): |
| 20 | """Create Module instance with temp data dir""" |
| 21 | return Module(data_dir=temp_data_dir) |
| 22 | |
| 23 | |
| 24 | def test_create(module): |
| 25 | """Test creating an item""" |
| 26 | item = module.create(name="Test", description="Test description") |
| 27 | |
| 28 | assert item is not None |
| 29 | assert item['name'] == "Test" |
| 30 | assert item['description'] == "Test description" |
| 31 | assert 'id' in item |
| 32 | assert 'created_at' in item |
| 33 | |
| 34 | |
| 35 | def test_create_requires_name(module): |
| 36 | """Test that name is required""" |
| 37 | with pytest.raises(ValueError): |
| 38 | module.create(description="No name") |
| 39 | |
| 40 | |
| 41 | def test_list(module): |
| 42 | """Test listing items""" |
| 43 | # Create some items |
| 44 | module.create(name="Item 1", description="First") |
| 45 | module.create(name="Item 2", description="Second") |
| 46 | |
| 47 | items = module.list() |
| 48 | assert len(items) == 2 |
| 49 | assert items[0]['name'] == "Item 1" |
| 50 | assert items[1]['name'] == "Item 2" |
| 51 | |
| 52 | |
| 53 | def test_get(module): |
| 54 | """Test getting item by ID""" |
| 55 | created = module.create(name="Test", description="Test") |
| 56 | item_id = created['id'] |
| 57 | |
| 58 | retrieved = module.get(item_id) |
| 59 | assert retrieved is not None |
| 60 | assert retrieved['id'] == item_id |
| 61 | assert retrieved['name'] == "Test" |
| 62 | |
| 63 | |
| 64 | def test_get_nonexistent(module): |
| 65 | """Test getting nonexistent item returns None""" |
| 66 | item = module.get(99999) |
| 67 | assert item is None |
| 68 | |
| 69 | |
| 70 | def test_update(module): |
| 71 | """Test updating an item""" |
| 72 | created = module.create(name="Original", description="Original desc") |
| 73 | item_id = created['id'] |
| 74 | |
| 75 | success = module.update(item_id, name="Updated") |
| 76 | assert success is True |
| 77 | |
| 78 | updated = module.get(item_id) |
| 79 | assert updated['name'] == "Updated" |
| 80 | assert updated['description'] == "Original desc" # Unchanged |
| 81 | |
| 82 | |
| 83 | def test_update_nonexistent(module): |
| 84 | """Test updating nonexistent item returns False""" |
| 85 | success = module.update(99999, name="Test") |
| 86 | assert success is False |
| 87 | |
| 88 | |
| 89 | def test_delete(module): |
| 90 | """Test deleting an item""" |
| 91 | created = module.create(name="To Delete", description="Will be deleted") |
| 92 | item_id = created['id'] |
| 93 | |
| 94 | success = module.delete(item_id) |
| 95 | assert success is True |
| 96 | |
| 97 | # Verify deleted |
| 98 | item = module.get(item_id) |
| 99 | assert item is None |
| 100 | |
| 101 | |
| 102 | def test_delete_nonexistent(module): |
| 103 | """Test deleting nonexistent item returns False""" |
| 104 | success = module.delete(99999) |
| 105 | assert success is False |
| 106 | |
| 107 | |
| 108 | def test_search(module): |
| 109 | """Test searching items""" |
| 110 | module.create(name="Alice", description="Engineer") |
| 111 | module.create(name="Bob", description="Designer") |
| 112 | module.create(name="Charlie", description="Engineer") |
| 113 | |
| 114 | # Search by description |
| 115 | results = module.search(description="Engineer") |
| 116 | assert len(results) == 2 |
| 117 | assert results[0]['name'] == "Alice" |
| 118 | assert results[1]['name'] == "Charlie" |
| 119 | |
| 120 | |
| 121 | def test_count(module): |
| 122 | """Test counting items""" |
| 123 | assert module.count() == 0 |
| 124 | |
| 125 | module.create(name="Item 1", description="First") |
| 126 | assert module.count() == 1 |
| 127 | |
| 128 | module.create(name="Item 2", description="Second") |
| 129 | assert module.count() == 2 |
| 130 | |
| 131 | |
| 132 | def test_list_with_limit(module): |
| 133 | """Test list with limit""" |
| 134 | for i in range(10): |
| 135 | module.create(name=f"Item {i}", description=f"Desc {i}") |
| 136 | |
| 137 | items = module.list(limit=5) |
| 138 | assert len(items) == 5 |
| 139 | |
| 140 | |
| 141 | def test_list_with_offset(module): |
| 142 | """Test list with offset""" |
| 143 | for i in range(10): |
| 144 | module.create(name=f"Item {i}", description=f"Desc {i}") |
| 145 | |
| 146 | items = module.list(limit=5, offset=5) |
| 147 | assert len(items) == 5 |
| 148 | assert items[0]['name'] == "Item 5" |