Compare commits

...

8 Commits
0.1.1 ... main

Author SHA1 Message Date
Andros Fenollosa
d278077471 Add delete function 2022-12-14 09:48:24 +01:00
Andros Fenollosa
ea8807cd36 Update TODO 2022-12-13 20:23:26 +01:00
Andros Fenollosa
42fa9c9cf7 Finish update function 2022-12-13 20:19:34 +01:00
Andros Fenollosa
872ecbe4e1 Update TODO 2022-12-05 15:16:54 +01:00
Andros Fenollosa
ebf9b5bcb6 Update returns 2022-12-05 15:15:45 +01:00
Andros Fenollosa
3d09cb6ebf Update updates 2022-12-05 14:52:09 +01:00
Andros Fenollosa
7f00126742 Add test update 2022-12-05 14:02:22 +01:00
Andros Fenollosa
26f8caa3d2 Update test update 2022-11-30 12:57:16 +01:00
6 changed files with 501 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
dist/
build/
fiable_db.egg-info/
/fiabledb.json

View File

@ -109,14 +109,7 @@ You can be specific by using the `id`.
```python
fiable_db.delete(id=4)
# {"id": 4, "rev": 6, "table": "default", "data": None}
```
And you can delete by performing a search for their values:
```python
fiable_db.delete(data={"name": "Javier"})
# {"id": 4, "rev": 6, "table": "default", "data": None}
# {"id": 4, "rev": 6, "table": "default", "data": {}}
```
### Step 5: Find one

View File

@ -2,6 +2,7 @@ from typing import Dict, Tuple, Union, Sequence, TypedDict
from functools import reduce
import json
from os import path
from copy import deepcopy
# Variables
FILE = "fiabledb.json"
@ -19,8 +20,8 @@ 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_Update_Return = Union[Tuple[Tuple[int, int, Dict]]]
Type_Delete_Return = Union[Tuple[Tuple[int, int, Dict]]]
Type_Update_Return = Union[Tuple[Tuple[int, int, Dict]], None]
Type_Delete_Return = Union[Tuple[Tuple[int, int, Dict]], None]
Type_Find_One_Return = TypeData
Type_Find_All_Return = Tuple[Type_Find_One_Return]
@ -37,6 +38,7 @@ def get_next_id(table: str = "default") -> int:
return row["id"]
else:
return current_id
last_id = reduce(get_id, database[::-1], None)
# Return the next id, or 1 if there is no last id
return last_id + 1 if last_id else 1
@ -109,6 +111,31 @@ def get_database() -> Type_Data_List:
return database
def get_pos_by_id(id: int, table: str = "") -> int:
"""Get the position of the data by id
Args:
id (int): The id of the data
table (str, optional): The table to search in. Defaults to "".
Returns:
int: The position of the data
"""
global database
def get_key(
current_key: Union[TypeData, None], key_and_row: TypeData
) -> Union[int, None]:
"""Function to get the key of the data"""
if (
current_key is None
and key_and_row[1]["id"] == id
and key_and_row[1]["table"] == table
):
return key_and_row[0]
return current_key
return reduce(get_key, list(enumerate(database))[::-1], None)
def add(new_data: Type_Add_Data, table: str = "default") -> Type_Add_Return:
"""Add data to the database
Args:
@ -131,7 +158,7 @@ def add(new_data: Type_Add_Data, table: str = "default") -> Type_Add_Return:
def update(
id: int, new_data: dict, table: str = "", force: bool = False
id: int, new_data: dict, table: str = "default", force: bool = False
) -> Type_Update_Return:
"""Update data in the database
Args:
@ -140,12 +167,27 @@ def update(
table (str, optional): The table to update. Defaults to "".
force (bool, optional): Force the update. Defaults to False.
Returns:
dict[int, int, dict]: The data updated
dict[int, int, dict] or None: The data updated
"""
print("Function not implemented yet")
global database
# Get the key to update
key = get_pos_by_id(id, table)
if key is not None:
row = deepcopy(database[key])
my_new_data = deepcopy(new_data)
if force:
row["data"] = my_new_data
else:
row["data"].update(my_new_data)
new_rev = row["rev"] + 1
new_data_to_row = row["data"]
new_row = {"id": id, "rev": new_rev, "table": table, "data": new_data_to_row}
database.append(new_row)
return new_row
return None
def delete(id: int, data: dict, table: str = "") -> Type_Delete_Return:
def delete(id: int, table: str = "default") -> Type_Delete_Return:
"""Delete data from the database
Args:
id (int): The id of the data to delete
@ -154,7 +196,7 @@ def delete(id: int, data: dict, table: str = "") -> Type_Delete_Return:
Returns:
dict[int, int, dict]: The data deleted
"""
print("Function not implemented yet")
return update(id=id, new_data={}, table=table, force=True)
def find_one(

126
test/test_delete.py Normal file
View File

@ -0,0 +1,126 @@
from fiable_db import start, add, delete, get_database
def test_delete_simple():
"""Test delete in the default table."""
start()
add(
[
{"name": "Noelia", "age": 34, "height": 165},
{"name": "Juan", "age": 41, "height": 187},
{"name": "Valentina", "age": 12, "height": 142},
]
)
delete(1)
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Noelia", "age": 34, "height": 165},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Juan", "age": 41, "height": 187},
},
{
"id": 3,
"rev": 1,
"table": "default",
"data": {"name": "Valentina", "age": 12, "height": 142},
},
{"id": 1, "rev": 2, "table": "default", "data": {}},
]
def test_delete_multiple():
"""Test delete two rows"""
start()
add(
[
{"name": "Noelia", "age": 34, "height": 165},
{"name": "Juan", "age": 41, "height": 187},
{"name": "Valentina", "age": 12, "height": 142},
]
)
delete(2)
delete(3)
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Noelia", "age": 34, "height": 165},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Juan", "age": 41, "height": 187},
},
{
"id": 3,
"rev": 1,
"table": "default",
"data": {"name": "Valentina", "age": 12, "height": 142},
},
{"id": 2, "rev": 2, "table": "default", "data": {}},
{"id": 3, "rev": 2, "table": "default", "data": {}},
]
def test_delete_with_table():
"""Test delete two rows"""
start()
add(
[
{"name": "Noelia", "age": 34, "height": 165},
{"name": "Juan", "age": 41, "height": 187},
{"name": "Valentina", "age": 12, "height": 142},
],
table="people",
)
add(
[
{"name": "Mortadelo", "age": 22, "height": 184},
{"name": "Filemon", "age": 25, "height": 185},
],
)
delete(2, table="people")
delete(1)
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "people",
"data": {"name": "Noelia", "age": 34, "height": 165},
},
{
"id": 2,
"rev": 1,
"table": "people",
"data": {"name": "Juan", "age": 41, "height": 187},
},
{
"id": 3,
"rev": 1,
"table": "people",
"data": {"name": "Valentina", "age": 12, "height": 142},
},
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Mortadelo", "age": 22, "height": 184},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Filemon", "age": 25, "height": 185},
},
{"id": 2, "rev": 2, "table": "people", "data": {}},
{"id": 1, "rev": 2, "table": "default", "data": {}},
]

320
test/test_update.py Normal file
View File

@ -0,0 +1,320 @@
from fiable_db import start, add, update, get_database
def test_update_simple():
"""Test update in the default table."""
start()
add(
[
{"name": "Noelia", "age": 34, "height": 165},
{"name": "Juan", "age": 41, "height": 187},
{"name": "Valentina", "age": 12, "height": 142},
]
)
update(2, {"age": 99})
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Noelia", "age": 34, "height": 165},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Juan", "age": 41, "height": 187},
},
{
"id": 3,
"rev": 1,
"table": "default",
"data": {"name": "Valentina", "age": 12, "height": 142},
},
{
"id": 2,
"rev": 2,
"table": "default",
"data": {"name": "Juan", "age": 99, "height": 187},
},
]
update(2, {"name": "Cristo"})
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Noelia", "age": 34, "height": 165},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Juan", "age": 41, "height": 187},
},
{
"id": 3,
"rev": 1,
"table": "default",
"data": {"name": "Valentina", "age": 12, "height": 142},
},
{
"id": 2,
"rev": 2,
"table": "default",
"data": {"name": "Juan", "age": 99, "height": 187},
},
{
"id": 2,
"rev": 3,
"table": "default",
"data": {"name": "Cristo", "age": 99, "height": 187},
},
]
update(1, {"height": 150})
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Noelia", "age": 34, "height": 165},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Juan", "age": 41, "height": 187},
},
{
"id": 3,
"rev": 1,
"table": "default",
"data": {"name": "Valentina", "age": 12, "height": 142},
},
{
"id": 2,
"rev": 2,
"table": "default",
"data": {"name": "Juan", "age": 99, "height": 187},
},
{
"id": 2,
"rev": 3,
"table": "default",
"data": {"name": "Cristo", "age": 99, "height": 187},
},
{
"id": 1,
"rev": 2,
"table": "default",
"data": {"name": "Noelia", "age": 34, "height": 150},
},
]
def test_update_table():
"""Test update in a table."""
add({"name": "Antony", "age": 77, "height": 188}, table="users")
update(1, {"age": 99}, table="users")
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Noelia", "age": 34, "height": 165},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Juan", "age": 41, "height": 187},
},
{
"id": 3,
"rev": 1,
"table": "default",
"data": {"name": "Valentina", "age": 12, "height": 142},
},
{
"id": 2,
"rev": 2,
"table": "default",
"data": {"name": "Juan", "age": 99, "height": 187},
},
{
"id": 2,
"rev": 3,
"table": "default",
"data": {"name": "Cristo", "age": 99, "height": 187},
},
{
"id": 1,
"rev": 2,
"table": "default",
"data": {"name": "Noelia", "age": 34, "height": 150},
},
{
"id": 1,
"rev": 1,
"table": "users",
"data": {"name": "Antony", "age": 77, "height": 188},
},
{
"id": 1,
"rev": 2,
"table": "users",
"data": {"name": "Antony", "age": 99, "height": 188},
},
]
def test_update_table_not_exists():
"""Test update in a table that not exists."""
start()
add({"name": "Antony", "age": 77, "height": 188}, table="users")
update(1, {"name": "David"}, table="boo")
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "users",
"data": {"name": "Antony", "age": 77, "height": 188},
}
]
def test_update_id_not_exists():
"""Test update with an id that not exists."""
start()
add({"name": "Antony", "age": 77, "height": 188}, table="users")
update(2, {"name": "David"}, table="users")
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "users",
"data": {"name": "Antony", "age": 77, "height": 188},
}
]
def test_update_multiple_values_default():
"""Test update multiple values in the default table."""
start()
add({"name": "Antony", "age": 77, "height": 188})
add({"name": "Dolores", "age": 32})
update(1, {"name": "David", "age": 9})
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Antony", "age": 77, "height": 188},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Dolores", "age": 32},
},
{
"id": 1,
"rev": 2,
"table": "default",
"data": {"name": "David", "age": 9, "height": 188},
},
]
def test_update_multiple_values_table():
"""Test update multiple values in a table."""
start()
add({"name": "Antony", "age": 77, "height": 188}, table="users")
add({"name": "Dolores", "age": 32}, table="users")
update(1, {"name": "David", "age": 9}, table="users")
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "users",
"data": {"name": "Antony", "age": 77, "height": 188},
},
{
"id": 2,
"rev": 1,
"table": "users",
"data": {"name": "Dolores", "age": 32},
},
{
"id": 1,
"rev": 2,
"table": "users",
"data": {"name": "David", "age": 9, "height": 188},
},
]
def test_update_with_keys_not_exists():
"""Test update with keys that not exists."""
start()
add({"name": "Antony", "age": 77, "height": 188})
add({"name": "Dolores", "age": 32})
update(1, {"is_active": True, "eyes": "blue"})
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {
"name": "Antony",
"age": 77,
"height": 188,
},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Dolores", "age": 32},
},
{
"id": 1,
"rev": 2,
"table": "default",
"data": {
"name": "Antony",
"age": 77,
"height": 188,
"is_active": True,
"eyes": "blue",
},
},
]
def test_update_with_force():
"""Test update with force."""
start()
add({"name": "Antony", "age": 77, "height": 188})
add({"name": "Dolores", "age": 32})
update(1, {"name": "David", "age": 9}, force=True)
assert get_database() == [
{
"id": 1,
"rev": 1,
"table": "default",
"data": {"name": "Antony", "age": 77, "height": 188},
},
{
"id": 2,
"rev": 1,
"table": "default",
"data": {"name": "Dolores", "age": 32},
},
{
"id": 1,
"rev": 2,
"table": "default",
"data": {"name": "David", "age": 9},
},
]

View File

@ -1,5 +1,6 @@
* Tasks
** DONE Add table in add testing
** IN PROGRESS Test update
** TODO Function update
** TODO Test Find one
** TODO Function Find one
** TODO Test Find All
** TODO Function Find All