Creating a full-fledged application with all the features you've requested would require a significant amount of code and might be beyond the scope of this platform. However, I can provide a basic skeleton of how you might structure such an application using Python and SQLite, a lightweight disk-based database, that doesn't require a separate server process.
import sqlite3
from sqlite3 import Error
def create_connection():
conn = None;
try:
conn = sqlite3.connect(':memory:') # creates a database in RAM
print(sqlite3.version)
except Error as e:
print(e)
return conn
def create_table(conn):
try:
sql = """ CREATE TABLE Data (
id integer PRIMARY KEY,
data text NOT NULL
); """
c = conn.cursor()
c.execute(sql)
except Error as e:
print(e)
def insert_data(conn, data):
try:
sql = ''' INSERT INTO Data(data) VALUES(?) '''
cur = conn.cursor()
cur.execute(sql, data)
return cur.lastrowid
except Error as e:
print(e)
def view_data(conn):
try:
cur = conn.cursor()
cur.execute("SELECT * FROM Data")
rows = cur.fetchall()
for row in rows:
print(row)
except Error as e:
print(e)
def search_data(conn, data):
try:
cur = conn.cursor()
cur.execute("SELECT * FROM Data WHERE data=?", (data,))
rows = cur.fetchall()
for row in rows:
print(row)
except Error as e:
print(e)
def main():
conn = create_connection()
with conn:
create_table(conn)
insert_data(conn, ('Test data',))
view_data(conn)
search_data(conn, ('Test data',))
if __name__ == '__main__':
main()
This script will create a connection to an SQLite database, create a table, insert some data, view the data, and search the data.
For testing, you can use Python's built-in unittest module. Here's a simple example of a test case for this application:
import unittest
import sqlite3
from sqlite3 import Error
class TestDatabase(unittest.TestCase):
def setUp(self):
self.conn = sqlite3.connect(':memory:')
self.cur = self.conn.cursor()
def tearDown(self):
self.conn.close()
def test_create_table(self):
try:
sql = """ CREATE TABLE Data (
id integer PRIMARY KEY,
data text NOT NULL
); """
self.cur.execute(sql)
except Error as e:
self.fail("Creating table failed!")
def test_insert_data(self):
try:
sql = ''' INSERT INTO Data(data) VALUES(?) '''
self.cur.execute(sql, ('Test data',))
except Error as e:
self.fail("Inserting data failed!")
if __name__ == '__main__':
unittest.main()
This will test the creation of the table and the insertion of data. You can add more tests as needed.