def test_simple() -> None: db = DataBase() assert db.num_tables() == 0 db.create_table('Students', [DBField('ID', int), DBField('First', str), DBField('Last', str), DBField('Birthday', dt.datetime) ], 'ID') assert db.num_tables() == 1 students = db.get_table('Students') for i in range(50): students.insert_record(dict( ID=1_000_000 + i, First=f'John{i}', Last=f'Doe{i}', Birthday=dt.datetime(2000, 2, 1) + dt.timedelta(days=i) )) students.delete_record(1_000_001) students.delete_records([SelectionCriteria('ID', '=', 1_000_020)]) students.delete_records([SelectionCriteria('ID', '<', 1_000_003)]) students.delete_records([SelectionCriteria('ID', '>', 1_000_033)]) students.delete_records([ SelectionCriteria('ID', '>', 1_000_020), SelectionCriteria('ID', '<', 1_000_023) ]) students.update_record(1_000_009, dict(First='Jane', Last='Doe')) assert students.count() == 28 results = students.query_table([SelectionCriteria('First', '=', 'Jane')]) assert len(results) == 1 assert results[0]['First'] == 'Jane' with pytest.raises(ValueError): # record already exists students.insert_record(dict( ID=1_000_010, First='John', Last='Doe', Birthday=dt.datetime(2000, 2, 1) + dt.timedelta(days=i) )) with pytest.raises(ValueError): students.delete_record(key=1_000_000)
def get_table(self, table_name: str) -> DBTable: if table_name in self.my_tables.keys(): return self.my_tables[table_name] if os.path.isdir(f"{DB_ROOT}/{table_name}"): with open(f"{DB_ROOT}/{table_name}/{table_name}.json") as tables: table_data = json.load(tables) new_table = DBTable(table_data["name"], [DBField(list(item.keys())[0], list(item.values())[0]) for item in table_data["fields"]], table_data["key_field_name"]) self.my_tables[table_name] = new_table return new_table raise NameError
def __reload_backup(self): with open(os.path.join(DB_ROOT, "DB.json"), "r") as backup_file: tables = json.load(backup_file) for name, table in tables.items(): if not self.__tables.get(name): self.__tables[name] = DBTable(name, [DBField(field[0], field[1]) for field in table["fields"]], table["key_field_name"]) self.__tables[name].num_rows = table["num_rows"] self.__tables[name].indexes = table["indexes"]
def convert_to_DBFields(fields): db_fields_list = [] for k, v in fields.items(): db_fields_list.append(DBField(k, types[v])) return db_fields_list
import datetime as dt import time from functools import partial from pathlib import Path from typing import Generator import pytest from db import DataBase from db_api import DBField, SelectionCriteria, DB_ROOT, DBTable DB_BACKUP_ROOT = DB_ROOT.parent / (DB_ROOT.name + '_backup') STUDENT_FIELDS = [ DBField('ID', int), DBField('First', str), DBField('Last', str), DBField('Birthday', dt.datetime) ] def delete_files(folder: Path): for path in Path(folder).iterdir(): if path.is_dir(): # No coverage when folder is empty delete_files(path) path.rmdir() else: path.unlink() # No coverage when folder is empty def get_folder_size(folder: Path) -> int: return sum(f.stat().st_size for f in folder.glob('**/*') if f.is_file())
import datetime as dt import time from functools import partial from pathlib import Path from typing import Generator import shutil import pytest from db import DataBase from db_api import DBField, SelectionCriteria, DB_ROOT, DBTable DB_BACKUP_ROOT = DB_ROOT.parent / (DB_ROOT.name + '_backup') STUDENT_FIELDS = [DBField('ID', int), DBField('First', str), DBField('Last', str), DBField('Birthday', dt.datetime)] def delete_files(folder: Path): for path in Path(folder).iterdir(): if path.is_dir(): # No coverage when folder is empty delete_files(path) path.rmdir() else: path.unlink() # No coverage when folder is empty def get_folder_size(folder: Path) -> int: return sum(f.stat().st_size for f in folder.glob('**/*') if f.is_file()) db_size = partial(get_folder_size, DB_ROOT)
def get_tables_names(self) -> List[Any]: dictionary = read_tables() return list(dictionary.keys()) def query_multiple_tables( self, tables: List[str], fields_and_values_list: List[List[SelectionCriteria]], fields_to_join_by: List[str]) -> List[Dict[str, Any]]: raise NotImplementedError db = DataBase() db.create_table( 'library', [DBField('name', str), DBField('books', list), DBField('address', str)], 'name') db.create_table('book', [ DBField('id', int), DBField('name', str), DBField('writer', str), DBField('pages', int) ], 'id') print(db.num_tables()) print(db.get_table('library')) print(db.get_tables_names()) library = db.get_table('library') library.insert_record({ 'name': 'MMNN', 'books': ['kkk', 'jjj'],
import datetime as dt import time from functools import partial from pathlib import Path from typing import Generator import pytest from db import DataBase from db_api import DBField, SelectionCriteria, DB_ROOT, DBTable DB_BACKUP_ROOT = DB_ROOT.parent / (DB_ROOT.name + '_backup') # STUDENT_FIELDS = [DBField('ID', int), DBField('First', str), # DBField('Last', str), DBField('Birthday', dt.datetime)] STUDENT_FIELDS = [DBField('ID', int), DBField('First', str), DBField('Last', str)] def delete_files(folder: Path): for path in Path(folder).iterdir(): if path.is_dir(): # No coverage when folder is empty delete_files(path) path.rmdir() else: path.unlink() # No coverage when folder is empty def get_folder_size(folder: Path) -> int: return sum(f.stat().st_size for f in folder.glob('**/*') if f.is_file())
import datetime as dt import time from functools import partial from pathlib import Path from typing import Generator import pytest from db import DataBase from db_api import DBField, SelectionCriteria, DB_ROOT, DBTable DB_BACKUP_ROOT = DB_ROOT.parent / (DB_ROOT.name + '_backup') # STUDENT_FIELDS = [DBField('ID', int), DBField('First', str), # DBField('Last', str), DBField('Birthday', dt.datetime)] STUDENT_FIELDS = [ DBField('ID', int), DBField('First', str), DBField('Last', str) ] def delete_files(folder: Path): for path in Path(folder).iterdir(): if path.is_dir(): # No coverage when folder is empty delete_files(path) path.rmdir() else: path.unlink() # No coverage when folder is empty def get_folder_size(folder: Path) -> int:
import pytest from db import DataBase from db_api import DBField, SelectionCriteria, DB_ROOT, DBTable STUDENT_FIELDS = [ DBField('ID', int), DBField('First', str), DBField('Last', str), DBField('Birthday', str) ] db = DataBase() def create_students_table(db: DataBase, num_students: int = 0) -> DBTable: table = db.create_table('Students_index', STUDENT_FIELDS, 'ID') for i in range(num_students): add_student(table, i) return table def add_student(table: DBTable, index: int, **kwargs) -> None: info = dict(ID=1_000_000 + index, First=f'John{index}', Last=f'Doe{index}', Birthday="23/11/2000") info.update(**kwargs) table.insert_record(info)
self.my_tables.pop(table_name) def get_tables_names(self) -> List[Any]: return os.listdir(DB_ROOT) def query_multiple_tables( self, tables: List[str], fields_and_values_list: List[List[SelectionCriteria]], fields_to_join_by: List[str]) -> List[Dict[str, Any]]: raise NotImplementedError my_data_base = DataBase() student = my_data_base.create_table( "student", [DBField("id", int), DBField("name", str), DBField("age", int)], "id") student.insert_record({"id": 1, "name": "sss", "age": 78}) student.insert_record({"id": 6, "name": "kjh"}) student.insert_record({"id": 2, "age": 4}) student.insert_record({"id": 10, "name": "d"}) student.delete_record(1) student.insert_record({"id": 18, "name": "d"})