Update test start
This commit is contained in:
parent
95c6d1a7fa
commit
a396a264e7
28
README.md
28
README.md
@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
|
Python version: >=3.8
|
||||||
|
|
||||||
```python
|
```python
|
||||||
pip3 install --user fiable_db
|
pip3 install --user fiable_db
|
||||||
```
|
```
|
||||||
@ -201,6 +203,32 @@ 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}}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Other help functions
|
||||||
|
|
||||||
|
#### Get all data
|
||||||
|
|
||||||
|
Get all data from the database.
|
||||||
|
|
||||||
|
```python
|
||||||
|
get_database()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Load file
|
||||||
|
|
||||||
|
Load a file into the database.
|
||||||
|
|
||||||
|
```python
|
||||||
|
load(filename)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Save file
|
||||||
|
|
||||||
|
Save the database to a file.
|
||||||
|
|
||||||
|
```python
|
||||||
|
save(filename, data)
|
||||||
|
```
|
||||||
|
|
||||||
## Implementations in other languages
|
## Implementations in other languages
|
||||||
|
|
||||||
- [Clojure](https://github.com/Toni-zgz/db_inmutable).
|
- [Clojure](https://github.com/Toni-zgz/db_inmutable).
|
||||||
|
91
fiable_db.py
91
fiable_db.py
@ -1,43 +1,93 @@
|
|||||||
|
from os import path
|
||||||
|
import json
|
||||||
|
from typing import Dict, Tuple, Union, Sequence, TypedDict
|
||||||
|
|
||||||
|
# Variables
|
||||||
FILE = "fiabledb.json"
|
FILE = "fiabledb.json"
|
||||||
data = {}
|
database = {}
|
||||||
|
|
||||||
|
# Type aliases
|
||||||
|
class TypeData(TypedDict):
|
||||||
|
id: int
|
||||||
|
rev: int
|
||||||
|
data: dict
|
||||||
|
|
||||||
|
|
||||||
def start(filename: str = "") -> str:
|
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_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]
|
||||||
|
|
||||||
|
|
||||||
|
def start(file_name: str = "") -> str:
|
||||||
"""Start the database
|
"""Start the database
|
||||||
Args:
|
Args:
|
||||||
file (str, optional): The file to use. Defaults to FILE.
|
file (str, optional): The file to use. Defaults to FILE.
|
||||||
Returns:
|
Returns:
|
||||||
str: The file used
|
str: The file used
|
||||||
"""
|
"""
|
||||||
file_name = filename if filename else FILE
|
global database
|
||||||
print("Function not implemented yet")
|
my_file_name = file_name if file_name else FILE
|
||||||
return file_name
|
if path.exists(my_file_name):
|
||||||
|
# Load the database
|
||||||
|
load(my_file_name)
|
||||||
|
else:
|
||||||
|
# Create the database
|
||||||
|
save(my_file_name, database)
|
||||||
|
return my_file_name
|
||||||
|
|
||||||
|
|
||||||
def save(filename: str = "", data: list[str, list[int, int, dict]] = {}) -> bool:
|
def save(file_name: str = "", data: TypeData = {}) -> bool:
|
||||||
"""Save the database
|
"""Save the database
|
||||||
Args:
|
Args:
|
||||||
filename (str, optional): The file to save to. Defaults to "".
|
file_name (str, optional): The file to save to. Defaults to "".
|
||||||
data (list[str, list[int, int, dict]], optional): The data to save. Defaults to {}.
|
data (list[str, list[int, int, dict]], optional): The data to save. Defaults to {}.
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if the data was saved, False otherwise
|
bool: True if the data was saved, False otherwise
|
||||||
"""
|
"""
|
||||||
print("Function not implemented yet")
|
global database
|
||||||
|
my_file_name = file_name if file_name else FILE
|
||||||
|
with open(my_file_name, "w") as f:
|
||||||
|
database = json.dump({}, f)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def load(filename: str = "") -> list[str, list[int, int, dict]]:
|
def load(file_name: Union[str, None] = None) -> bool:
|
||||||
"""Load the database
|
"""Load the database
|
||||||
Args:
|
Args:
|
||||||
filename (str, optional): The file to load from. Defaults to "".
|
file_name (str, optional): The file to load from. Defaults to "".
|
||||||
Returns:
|
Returns:
|
||||||
list[str, list[int, int, dict]]: The data loaded
|
Bool - The data loaded
|
||||||
"""
|
"""
|
||||||
print("Function not implemented yet")
|
global database
|
||||||
|
my_file_name = file_name if file_name else FILE
|
||||||
|
is_exists = path.exists(my_file_name)
|
||||||
|
if is_exists:
|
||||||
|
with open(my_file_name, "r") as f:
|
||||||
|
text = f.read()
|
||||||
|
if text != "":
|
||||||
|
database = json.loads(text)
|
||||||
|
else:
|
||||||
|
database = []
|
||||||
|
else:
|
||||||
|
raise FileNotFoundError("File not found")
|
||||||
|
return is_exists
|
||||||
|
|
||||||
|
|
||||||
def add(
|
def get_database() -> Type_Data_List:
|
||||||
new_data: dict | list, table: str = ""
|
"""Get the data
|
||||||
) -> dict[int, int, dict] | list[dict[int, int, dict]]:
|
Returns:
|
||||||
|
list[dict[int, int, dict]]: The data
|
||||||
|
"""
|
||||||
|
global database
|
||||||
|
return database
|
||||||
|
|
||||||
|
|
||||||
|
def add(new_data: Type_Add_Data, table: str = "") -> 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
|
||||||
@ -55,9 +105,8 @@ def add(
|
|||||||
|
|
||||||
|
|
||||||
def update(
|
def update(
|
||||||
id: int,
|
id: int, new_data: dict, table: str = "", force: bool = False
|
||||||
new_data: dict, table: str = "", force: bool = False
|
) -> Type_Update_Return:
|
||||||
) -> dict[int, int, dict]:
|
|
||||||
"""Update data in the database
|
"""Update data in the database
|
||||||
Args:
|
Args:
|
||||||
id (int): The id of the data to update.
|
id (int): The id of the data to update.
|
||||||
@ -70,7 +119,7 @@ def update(
|
|||||||
print("Function not implemented yet")
|
print("Function not implemented yet")
|
||||||
|
|
||||||
|
|
||||||
def delete(id: int, data: dict, table: str = "") -> dict[int, int, dict]:
|
def delete(id: int, data: dict, table: str = "") -> Type_Delete_Return:
|
||||||
"""Delete data from the database
|
"""Delete data from the database
|
||||||
Args:
|
Args:
|
||||||
id (int): The id of the data to delete
|
id (int): The id of the data to delete
|
||||||
@ -84,7 +133,7 @@ def delete(id: int, data: dict, table: str = "") -> dict[int, int, dict]:
|
|||||||
|
|
||||||
def find_one(
|
def find_one(
|
||||||
id: int = 0, data: dict = {}, table: str = "", rev: int = 0
|
id: int = 0, data: dict = {}, table: str = "", rev: int = 0
|
||||||
) -> dict[int, int, dict]:
|
) -> Type_Find_One_Return:
|
||||||
"""Find one data in the database
|
"""Find one data in the database
|
||||||
Args:
|
Args:
|
||||||
id (int, optional): The id of the data to find. Defaults to 0.
|
id (int, optional): The id of the data to find. Defaults to 0.
|
||||||
@ -97,7 +146,7 @@ def find_one(
|
|||||||
print("Function not implemented yet")
|
print("Function not implemented yet")
|
||||||
|
|
||||||
|
|
||||||
def find_all(data: dict = {}, table: str = "") -> list[dict[int, int, dict]]:
|
def find_all(data: dict = {}, table: str = "") -> Type_Find_All_Return:
|
||||||
"""Find all data in the database
|
"""Find all data in the database
|
||||||
Args:
|
Args:
|
||||||
data (dict, optional): Filter the data to find. Defaults to {}.
|
data (dict, optional): Filter the data to find. Defaults to {}.
|
||||||
|
0
test/__init__.py
Normal file
0
test/__init__.py
Normal file
@ -1,5 +1,29 @@
|
|||||||
[
|
[
|
||||||
{"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}},
|
{
|
||||||
{"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}},
|
"id": 2,
|
||||||
{"id": 4, "rev": 1, "data": {"name": "Valentina", "age": 12, "height": 142}}
|
"rev": 1,
|
||||||
|
"data": {
|
||||||
|
"name": "Noelia",
|
||||||
|
"age": 34,
|
||||||
|
"height": 165
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 3,
|
||||||
|
"rev": 1,
|
||||||
|
"data": {
|
||||||
|
"name": "Juan",
|
||||||
|
"age": 41,
|
||||||
|
"height": 187
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 4,
|
||||||
|
"rev": 1,
|
||||||
|
"data": {
|
||||||
|
"name": "Valentina",
|
||||||
|
"age": 12,
|
||||||
|
"height": 142
|
||||||
|
}
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
# content of test_sample.py
|
|
||||||
def inc(x):
|
|
||||||
return x + 1
|
|
||||||
|
|
||||||
|
|
||||||
def test_answer():
|
|
||||||
assert inc(3) == 5
|
|
@ -1,40 +1,48 @@
|
|||||||
from fiable_db import start, data
|
import os
|
||||||
|
import shutil
|
||||||
|
from fiable_db import start, get_database
|
||||||
|
|
||||||
|
|
||||||
def test_create_new_file():
|
def test_create_new_file():
|
||||||
"""Create a new file with a different name"""
|
"""Create a new file with a different name"""
|
||||||
filename = 'test.json'
|
filename = "test.json"
|
||||||
start(filename)
|
start(filename)
|
||||||
assert os.path.isfile(filename)
|
assert os.path.isfile(filename), "The file does not exist"
|
||||||
# Remove the file
|
# Remove the file
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
|
||||||
|
|
||||||
def test_create_default_file():
|
def test_create_default_file():
|
||||||
"""Create a new file with the default name"""
|
"""Create a new file with the default name"""
|
||||||
filename = 'fiabledb.json'
|
filename = "fiabledb.json"
|
||||||
start()
|
start()
|
||||||
assert os.path.isfile(filename)
|
assert os.path.isfile(filename), "The file does not exist"
|
||||||
# Remove the file
|
# Remove the file
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
|
||||||
|
|
||||||
def test_read_file_default():
|
def test_read_file_default():
|
||||||
"""Read the default file"""
|
"""Read the default file"""
|
||||||
os.copyfile('test/example.json', 'test/fiabledb.json')
|
input_file = "test/example.json"
|
||||||
|
output_file = "fiabledb.json"
|
||||||
|
shutil.copy(input_file, output_file)
|
||||||
start()
|
start()
|
||||||
|
data = get_database()
|
||||||
assert data == [
|
assert data == [
|
||||||
{"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}},
|
{"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}},
|
||||||
{"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}},
|
{"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}},
|
||||||
{"id": 4, "rev": 1, "data": {"name": "Valentina", "age": 12, "height": 142}},
|
{"id": 4, "rev": 1, "data": {"name": "Valentina", "age": 12, "height": 142}},
|
||||||
]
|
], "The data is not the same: " + str(data)
|
||||||
os.remove('test/fiabledb.json')
|
os.remove(output_file)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_read_file_custom_name():
|
def test_read_file_custom_name():
|
||||||
"""Read a file with a custom name"""
|
"""Read a file with a custom name"""
|
||||||
filename = 'example.json'
|
filename = "test/example.json"
|
||||||
start(filename)
|
start(filename)
|
||||||
|
data = get_database()
|
||||||
assert data == [
|
assert data == [
|
||||||
{"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}},
|
{"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}},
|
||||||
{"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}},
|
{"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}},
|
||||||
{"id": 4, "rev": 1, "data": {"name": "Valentina", "age": 12, "height": 142}},
|
{"id": 4, "rev": 1, "data": {"name": "Valentina", "age": 12, "height": 142}},
|
||||||
]
|
], "The data is not the same: " + str(data)
|
||||||
|
Loading…
Reference in New Issue
Block a user