Add test save

This commit is contained in:
Andros Fenollosa 2022-11-28 16:50:21 +01:00
parent ba5778e21d
commit 677704cdf9
4 changed files with 66 additions and 9 deletions

View File

@ -208,7 +208,7 @@ fiable_db.find_one(id=1) # Default table
Save the database to a file. Save the database to a file.
```python ```python
fiable_db.save(filename, data) fiable_db.save()
``` ```
### Other help functions ### Other help functions
@ -229,6 +229,14 @@ Load a file into the database.
fiable_db.load(filename) fiable_db.load(filename)
``` ```
### Save file
Save a file into the database.
```python
fiable_db.save(filename)
```
## Implementations in other languages ## Implementations in other languages
- [Clojure](https://github.com/Toni-zgz/db_inmutable). - [Clojure](https://github.com/Toni-zgz/db_inmutable).

View File

@ -17,8 +17,7 @@ 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], Type_Add_Return = Union[Tuple[int, int, Dict], Tuple[Tuple[int, int, Dict]], None]
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
@ -26,6 +25,7 @@ Type_Find_All_Return = Tuple[Type_Find_One_Return]
# Functions # Functions
def get_next_id(table: str = "default") -> int: def get_next_id(table: str = "default") -> int:
"""Get the next id for a table""" """Get the next id for a table"""
global database global database
@ -39,18 +39,20 @@ def start(file_name: str = "") -> str:
Returns: Returns:
str: The file used str: The file used
""" """
global FILE
global database global database
my_file_name = file_name if file_name else FILE my_file_name = file_name if file_name != "" else FILE
if path.exists(my_file_name): if path.exists(my_file_name):
# Load the database # Load the database
load(my_file_name) load(my_file_name)
else: else:
# Create the database # Create the database
save(my_file_name, database) database = []
save(my_file_name)
return my_file_name return my_file_name
def save(file_name: str = "", data: TypeData = {}) -> bool: def save(file_name: str = "") -> bool:
"""Save the database """Save the database
Args: Args:
file_name (str, optional): The file to save to. Defaults to "". file_name (str, optional): The file to save to. Defaults to "".
@ -58,10 +60,11 @@ def save(file_name: str = "", data: TypeData = {}) -> bool:
Returns: Returns:
bool: True if the data was saved, False otherwise bool: True if the data was saved, False otherwise
""" """
global FILE
global database global database
my_file_name = file_name if file_name else FILE my_file_name = file_name if file_name != "" else FILE
with open(my_file_name, "w") as f: with open(my_file_name, "w") as f:
database = json.dump({}, f) json.dump(database, f)
return True return True

View File

@ -11,7 +11,6 @@ def test_add_two():
"""Add two items to the database.""" """Add two items to the database."""
add({"name": "John", "age": 12}) add({"name": "John", "age": 12})
add({"name": "Jane", "age": 34}) add({"name": "Jane", "age": 34})
print(get_database())
assert get_database() == [ assert get_database() == [
{"id": 1, "rev": 1, "data": {"name": "John", "age": 42}}, {"id": 1, "rev": 1, "data": {"name": "John", "age": 42}},
{"id": 2, "rev": 1, "data": {"name": "John", "age": 12}}, {"id": 2, "rev": 1, "data": {"name": "John", "age": 12}},

47
test/test_save.py Normal file
View File

@ -0,0 +1,47 @@
import os
from fiable_db import start, add, save, get_database, load
filename = "fiabledb.json"
def delete_file():
"""Delete the database file."""
if os.path.exists(filename):
os.remove(filename)
def test_save_empty():
"""Test that save() works when the database is empty."""
delete_file()
start()
save()
global database
database = []
load()
assert get_database() == []
def test_save_one():
"""Test that save() works when the database has one entry."""
delete_file()
start()
add({"name": "John", "age": 30})
save()
global database
database = []
load()
assert get_database() == [{"id": 1, "rev": 1, "data": {"name": "John", "age": 30}}]
def test_save_two():
"""Test that save() works when the database has two entries."""
delete_file()
start()
add({"name": "John", "age": 30})
add({"name": "Jane", "age": 28})
save()
global database
database = []
load()
assert get_database() == [
{"id": 1, "rev": 1, "data": {"name": "John", "age": 30}},
{"id": 2, "rev": 1, "data": {"name": "Jane", "age": 28}},
]