alphaolomi / alphaolomi/python-sqlite3

Update code and tests

Open
#2 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

To write tests for the given code, you can use the `unittest` module in Python. Here's an example set of tests for the provided code:

```python
import sqlite3
import unittest
from unittest.mock import patch
from io import StringIO
from contextlib import redirect_stdout
from sqlite3 import IntegrityError

class TestSQLiteCRUD(unittest.TestCase):

def setUp(self):
self.db = sqlite3.connect(':memory:')
self.db.row_factory = sqlite3.Row
self.db.execute('create table test ( t1 text, i1 int )')

def tearDown(self):
self.db.close()

def test_insert_retrieve(self):
insert(self.db, dict(t1='test_insert', i1=42))
result = dict(retrieve(self.db, 'test_insert'))
self.assertEqual(result, {'t1': 'test_insert', 'i1': 42})

def test_update(self):
insert(self.db, dict(t1='test_update', i1=42))
update(self.db, dict(t1='test_update', i1=99))
result = dict(retrieve(self.db, 'test_update'))
self.assertEqual(result, {'t1': 'test_update', 'i1': 99})

def test_delete(self):
insert(self.db, dict(t1='test_delete', i1=42))
delete(self.db, 'test_delete')
result = retrieve(self.db, 'test_delete')
self.assertIsNone(result)

def test_disp_rows(self):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
insert(self.db, dict(t1='disp_row_test', i1=42))
disp_rows(self.db)
output = mock_stdout.getvalue().strip()
self.assertIn('disp_row_test', output)
self.assertIn('42', output)

def test_main_integration(self):
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
main_output = StringIO()
with redirect_stdout(main_output):
main()
main_output = main_output.getvalue().strip()

self.assertIn('Create table test', main_output)
self.assertIn('Create rows', main_output)
self.assertIn('Retrieve rows', main_output)
self.assertIn('Update rows', main_output)
self.assertIn('Delete rows', main_output)

if __name__ == '__main__':
unittest.main()
```

This test suite covers the basic functionality of the CRUD operations and ensures that the output is as expected.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.