From 95c6d1a7fabdf178bd9aa2f17925a5c4010325ea Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 20 Nov 2022 22:15:20 +0100 Subject: [PATCH] Add test start --- README.md | 30 +++++++++++++++--------------- test/example.json | 5 +++++ test/test_start.py | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 test/example.json create mode 100644 test/test_start.py diff --git a/README.md b/README.md index 20c7a28..3f3261c 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ fiable_db.add( ] ) # [ -# {"id": 2, "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}}, +# {"id": 2, "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}}, # ] ``` @@ -77,28 +77,28 @@ Update a key: ```python fiable_db.update(4, {"age": 21}) -# {"id": 4, "rev": 2, "data": {{"name": "Valentina", "age": 21, "height": 172}} +# {"id": 4, "rev": 2, "data": {"name": "Valentina", "age": 21, "height": 172}} ``` If the key does not exist, it will be added: ```python fiable_db.update(4, {"is_active": True}) -# {"id": 4, "rev": 3, "data": {{"name": "Valentina", "age": 21, "height": 172, "is_active": True}} +# {"id": 4, "rev": 3, "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}} ``` To delete a key you only have to give it a value `None`. ```python fiable_db.update(4, {"height": None}) -# {"id": 4, "rev": 4, "data": {{"name": "Valentina", "age": 21, "is_active": True}} +# {"id": 4, "rev": 4, "data": {"name": "Valentina", "age": 21, "is_active": True}} ``` To overwrite the dictionary, use the `force=True`: ```python fiable_db.update(4, {"name": "Javier", "email": "foo@example.com"}, force=True) -# {"id": 4, "rev": 5, "data": {{"name": "Javier", "email": "foo@example.com"}} +# {"id": 4, "rev": 5, "data": {"name": "Javier", "email": "foo@example.com"}} ``` ### Step 4: Delete @@ -123,21 +123,21 @@ Search by id. ```python fiable_db.find_one(id=2) -# {"id": 2, "rev": 1, "data": {{"name": "Noelia", "age": 34, "height": 165}} +# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}} ``` Search by value. It will give you the first match. ```python fiable_db.find_one(data={"name": "Noelia"}) -# {"id": 2, "rev": 1, "data": {{"name": "Noelia", "age": 34, "height": 165}} +# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}} ``` Search by several values. ```python fiable_db.find_one(data={"name": "Noelia", "age": 34}) -# {"id": 2, "rev": 1, "data": {{"name": "Noelia", "age": 34, "height": 165}} +# {"id": 2, "rev": 1, "data": {"name": "Noelia", "age": 34, "height": 165}} ``` If there are no results it will return a None. @@ -153,8 +153,8 @@ fiable_db.find_one(data={"name": "Noelia", "is_active": False}) ```python fiable_db.find_all(data={"age": 41}) # [ -# {"id": 1, "rev": 1, "data": {{"name": "Miguel", "age": 41, "height": 189}}, -# {"id": 3, "rev": 1, "data": {{"name": "Juan", "age": 41, "height": 187}}, +# {"id": 1, "rev": 1, "data": {"name": "Miguel", "age": 41, "height": 189}}, +# {"id": 3, "rev": 1, "data": {"name": "Juan", "age": 41, "height": 187}}, # ] ``` @@ -173,17 +173,17 @@ Example: Previous version to be deleted. ```python fiable_db.find_one(id=4, rev=3) -# {"id": 4, "rev": 3, "data": {{"name": "Valentina", "age": 21, "height": 172, "is_active": True}} +# {"id": 4, "rev": 3, "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}} ``` For convenience, you can use negative numbers. `-1` will be the previous state, `-2` is 2 states back, etc. ```python fiable_db.find_one(id=4, rev=-1) -# {"id": 4, "rev": 3, "data": {{"name": "Valentina", "age": 21, "height": 172, "is_active": True}} +# {"id": 4, "rev": 3, "data": {"name": "Valentina", "age": 21, "height": 172, "is_active": True}} fiable_db.find_one(id=4, rev=-2) -# {"id": 4, "rev": 2, "data": {{"name": "Valentina", "age": 21, "height": 172}} +# {"id": 4, "rev": 2, "data": {"name": "Valentina", "age": 21, "height": 172}} ``` ### Step 8: Working with tables or collections. diff --git a/test/example.json b/test/example.json new file mode 100644 index 0000000..b2eaaf8 --- /dev/null +++ b/test/example.json @@ -0,0 +1,5 @@ +[ + {"id": 2, "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}} +] diff --git a/test/test_start.py b/test/test_start.py new file mode 100644 index 0000000..2438761 --- /dev/null +++ b/test/test_start.py @@ -0,0 +1,40 @@ +from fiable_db import start, data + +def test_create_new_file(): + """Create a new file with a different name""" + filename = 'test.json' + start(filename) + assert os.path.isfile(filename) + # Remove the file + os.remove(filename) + + +def test_create_default_file(): + """Create a new file with the default name""" + filename = 'fiabledb.json' + start() + assert os.path.isfile(filename) + # Remove the file + os.remove(filename) + +def test_read_file_default(): + """Read the default file""" + os.copyfile('test/example.json', 'test/fiabledb.json') + start() + assert data == [ + {"id": 2, "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}}, + ] + os.remove('test/fiabledb.json') + + +def test_read_file_custom_name(): + """Read a file with a custom name""" + filename = 'example.json' + start(filename) + assert data == [ + {"id": 2, "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}}, + ]