diff --git a/README.md b/README.md index 907a7a0..f50f0f7 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,14 @@ fiable_db.find_one(id=1) # Default table # {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}} ``` +### Step 9: Save changes + +Save the database to a file. + +```python +fiable_db.save(filename, data) +``` + ### Other help functions #### Get all data @@ -210,7 +218,7 @@ fiable_db.find_one(id=1) # Default table Get all data from the database. ```python -get_database() +fiable_db.get_database() ``` ### Load file @@ -218,15 +226,7 @@ get_database() Load a file into the database. ```python -load(filename) -``` - -### Save file - -Save the database to a file. - -```python -save(filename, data) +fiable_db.load(filename) ``` ## Implementations in other languages diff --git a/fiable_db.py b/fiable_db.py index 7bc2508..a3ad9b8 100644 --- a/fiable_db.py +++ b/fiable_db.py @@ -1,12 +1,14 @@ -from os import path -import json from typing import Dict, Tuple, Union, Sequence, TypedDict +import json +from os import path # Variables FILE = "fiabledb.json" -database = {} +database = [] # Type aliases + + class TypeData(TypedDict): id: int rev: int @@ -15,13 +17,20 @@ class TypeData(TypedDict): Type_Data_List = Tuple[TypeData] Type_Add_Data = Union[Dict, Sequence[Dict]] -Type_Add_Return = Union[Tuple[int, int, Dict], Tuple[Tuple[int, int, Dict]], None] +Type_Add_Return = Union[Tuple[int, int, Dict], + Tuple[Tuple[int, int, Dict]], None] Type_Update_Return = Union[Tuple[Tuple[int, int, Dict]]] - Type_Delete_Return = Union[Tuple[Tuple[int, int, Dict]]] Type_Find_One_Return = TypeData Type_Find_All_Return = Tuple[Type_Find_One_Return] +# Functions + +def get_next_id(table: str = "default") -> int: + """Get the next id for a table""" + global database + return database[-1]["id"] + 1 if database else 1 + def start(file_name: str = "") -> str: """Start the database @@ -87,7 +96,7 @@ def get_database() -> Type_Data_List: return database -def add(new_data: Type_Add_Data, table: str = "") -> Type_Add_Return: +def add(new_data: Type_Add_Data, table: str = "default") -> Type_Add_Return: """Add data to the database Args: new_data (dict|list): The data to add @@ -95,13 +104,17 @@ def add(new_data: Type_Add_Data, table: str = "") -> Type_Add_Return: Returns: dict[int, int, dict]|list[dict[int, int, dict]]: The data added """ + global database if isinstance(new_data, dict): - return _add(new_data, table) + new_row = {"id": get_next_id(table), "rev": 1, "data": new_data} + database.append(new_row) + return new_row elif isinstance(new_data, list): - return [_add(entry, table) for entry in new_data] + for row in new_data: + new_row = {"id": get_next_id(table), "rev": 1, "data": row} + database.append(new_row) else: raise TypeError("new_data must be a dict or list") - print("Function not implemented yet") def update( diff --git a/test/test_add.py b/test/test_add.py new file mode 100644 index 0000000..46c1117 --- /dev/null +++ b/test/test_add.py @@ -0,0 +1,62 @@ +from fiable_db import add, get_database + + +def test_add_one(): + """Add one item to the database.""" + add({"name": "John", "age": 42}) + assert get_database() == [{"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}] + + +def test_add_two(): + """Add two items to the database.""" + add({"name": "John", "age": 12}) + add({"name": "Jane", "age": 34}) + print(get_database()) + assert get_database() == [ + {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, + ] + + +def test_add_list(): + """Add a list of items to the database.""" + add( + [ + {"name": "John", "age": 12}, + {"name": "Jane", "age": 34}, + ] + ) + assert get_database() == [ + {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, + {"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, + {"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, + ] + + +def test_add_list_with_one(): + """Add a list with one item to the database.""" + add([{"name": "John", "age": 42}]) + assert get_database() == [ + {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, + {"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, + {"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, + {"id": 6, "rev": 1, "data": {"name": "John", "age": 42}}, + ] + + +def test_add_empty(): + """Add an empty list to the database.""" + add([]) + assert get_database() == [ + {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, + {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, + {"id": 3, "rev": 1, "data": {"name": "Jane", "age": 34}}, + {"id": 4, "rev": 1, "data": {"name": "John", "age": 12}}, + {"id": 5, "rev": 1, "data": {"name": "Jane", "age": 34}}, + {"id": 6, "rev": 1, "data": {"name": "John", "age": 42}}, + ]