This commit is contained in:
Andros Fenollosa 2022-11-28 16:10:49 +01:00
parent a396a264e7
commit ba5778e21d
3 changed files with 94 additions and 19 deletions

View File

@ -203,6 +203,14 @@ fiable_db.find_one(id=1) # Default table
# {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}} # {"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 ### Other help functions
#### Get all data #### Get all data
@ -210,7 +218,7 @@ fiable_db.find_one(id=1) # Default table
Get all data from the database. Get all data from the database.
```python ```python
get_database() fiable_db.get_database()
``` ```
### Load file ### Load file
@ -218,15 +226,7 @@ get_database()
Load a file into the database. Load a file into the database.
```python ```python
load(filename) fiable_db.load(filename)
```
### Save file
Save the database to a file.
```python
save(filename, data)
``` ```
## Implementations in other languages ## Implementations in other languages

View File

@ -1,12 +1,14 @@
from os import path
import json
from typing import Dict, Tuple, Union, Sequence, TypedDict from typing import Dict, Tuple, Union, Sequence, TypedDict
import json
from os import path
# Variables # Variables
FILE = "fiabledb.json" FILE = "fiabledb.json"
database = {} database = []
# Type aliases # Type aliases
class TypeData(TypedDict): class TypeData(TypedDict):
id: int id: int
rev: int rev: int
@ -15,13 +17,20 @@ class TypeData(TypedDict):
Type_Data_List = Tuple[TypeData] Type_Data_List = Tuple[TypeData]
Type_Add_Data = Union[Dict, Sequence[Dict]] 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_Update_Return = Union[Tuple[Tuple[int, int, Dict]]]
Type_Delete_Return = Union[Tuple[Tuple[int, int, Dict]]] Type_Delete_Return = Union[Tuple[Tuple[int, int, Dict]]]
Type_Find_One_Return = TypeData Type_Find_One_Return = TypeData
Type_Find_All_Return = Tuple[Type_Find_One_Return] 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: def start(file_name: str = "") -> str:
"""Start the database """Start the database
@ -87,7 +96,7 @@ def get_database() -> Type_Data_List:
return database 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 """Add data to the database
Args: Args:
new_data (dict|list): The data to add 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: Returns:
dict[int, int, dict]|list[dict[int, int, dict]]: The data added dict[int, int, dict]|list[dict[int, int, dict]]: The data added
""" """
global database
if isinstance(new_data, dict): 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): 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: else:
raise TypeError("new_data must be a dict or list") raise TypeError("new_data must be a dict or list")
print("Function not implemented yet")
def update( def update(

62
test/test_add.py Normal file
View File

@ -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}},
]