Skip to content

Commit 47f361b

Browse files
committed
Fixed multiple insertion error
1 parent 75f658f commit 47f361b

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

filexdb/collection.py

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,26 @@ def __init__(self, col_name: str, file_handler: FileIO) -> None:
1919
self._file_handler = file_handler
2020

2121
# Get the data of existing Database or empty database.
22-
self._database = Document(self._file_handler.read(), False)
22+
self._database = self._get_database()
2323

24-
self._cursor: int = 0
24+
# Initiating Collecting
25+
self._collection = self._get_collection()
2526

26-
# Initiate a default Collection.
27-
# Check the Collection is already exists or no.
28-
if self._col_name in self._database.keys():
27+
# Cursor
28+
self._cursor: int = 0
2929

30-
# Get the existing Collection
31-
self._collection: JsonArray = self._database[self._col_name]
3230

33-
else:
34-
# Create new Collection
35-
self._database[self._col_name] = JsonArray([])
36-
self._collection: JsonArray = self._database[self._col_name]
3731

3832
def insert(self, document: Mapping) -> str:
3933
"""
4034
Inserts a single Document into the Database.
4135
4236
Document should be JSON Object.
4337
44-
:param document: Document to insert into database
45-
:return: None
38+
:param document: Document to insert into the database.
39+
:return: Document ID.
4640
"""
41+
4742
# Make sure the document implements the ``Mapping`` interface
4843
if not isinstance(document, Mapping):
4944
raise ValueError('Document is not a Dictionary')
@@ -52,10 +47,14 @@ def insert(self, document: Mapping) -> str:
5247
if "_id_" in document.keys():
5348
raise KeyError(f"You are not allowed to modify key `_id_`")
5449

50+
51+
# getting Database
52+
_database = self._get_database()
53+
5554
# Create a Document
5655
_document = Document(document)
5756

58-
# id of Document
57+
# ID of Document
5958
_doc_id: str = _document.id
6059

6160
# check Document is already exist or not
@@ -65,11 +64,11 @@ def insert(self, document: Mapping) -> str:
6564
self._collection.append(_document)
6665

6766
# Add modified Collection to Database
68-
self._database[self._col_name] = self._collection
67+
_database[self._col_name] = self._collection
6968

7069
# print(self._database)
7170
# Write current state of Database into the Database-file
72-
self._file_handler.write(self._database)
71+
self._file_handler.write(_database)
7372

7473
return _doc_id
7574
else:
@@ -312,6 +311,38 @@ def drop(self) -> int:
312311

313312

314313
# ----------------------------------------------------------------#
314+
def _get_database(self) -> Document:
315+
"""
316+
Getting Database
317+
318+
:return: Database
319+
"""
320+
# Get the data of existing Database or empty database.
321+
database = Document(self._file_handler.read(), False)
322+
323+
return database
324+
325+
def _get_collection(self) -> JsonArray:
326+
"""
327+
Getting Collection
328+
329+
:return: Collection
330+
"""
331+
# Initiate a default Collection.
332+
# Check the Collection is already exists or no.
333+
if self._col_name in self._database.keys():
334+
335+
# Get the existing Collection
336+
_collection: JsonArray = self._database[self._col_name]
337+
338+
else:
339+
# Create new Collection
340+
self._database[self._col_name] = JsonArray([])
341+
_collection: JsonArray = self._database[self._col_name]
342+
343+
return _collection
344+
345+
315346
def _reset_cursor(self) -> None:
316347
"""
317348
Reset Cursor Pointer to 0th index

test/test_collection.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@
66

77
# Create a Collection
88
student_info = db.collection("student_info")
9+
student_2 = db.collection("student_2")
910

1011

1112
def test_insert():
1213
assert student_info.insert({"name": "Sam", "roll": "CSE/17/19", "dept": "CSE"})
1314
assert student_info.insert({"name": "Bob", "roll": "EE/01/18", "dept": "EE", "skill": ["python", "c++"]})
1415
assert student_info.insert({"name": "Rana", "dept": "CSE"})
16+
assert student_2.insert({"name": "Rana", "dept": "CSE"})
1517

1618

1719
def test_insert_all():
1820
assert student_info.insert_all([
1921
{"name": "Addy", "roll": "ME/57/19", "dept": "ME", "cgpa": 9.05},
2022
{"name": "Roman", "roll": "ECE/80/13", "dept": "ECE", "skill": ["game design"], "spc": ["Blinder"]},
2123
{"name": "Sam"}
22-
]
23-
)
24+
])
25+
assert student_2.insert_all([
26+
{"name": "Addy", "roll": "CSE/57/19", "dept": "CSE", "cgpa": 9.05},
27+
{"name": "Roman", "roll": "CSE/80/13", "dept": "CSE", "skill": ["game design"], "spc": ["Blinder"]},
28+
{"name": "Sam", "dept": "CSE"}
29+
])
2430

2531

2632
def test_find():
@@ -36,7 +42,7 @@ def test_find():
3642

3743
assert student_info.find() # Returns all Documents.
3844
assert student_info.find(query=_query_2) # Returns all Documents matches the ``_query``.
39-
assert student_info.find(query=_query_2, limit=(0, 30)) # Returns doc[1] to doc[2] matches the ``_query``.
45+
assert student_info.find(query=_query_2, limit=(0, 30)) # Returns doc[1] to doc[2] matches the ``_query``.
4046
assert student_info.find(limit=(1, 10)) # Returns doc[1] to doc[9] of all Documents.
4147

4248

@@ -64,7 +70,17 @@ def test_delete():
6470
assert student_info.delete({"name": "Roman", "roll": "ECE/80/13", "dept": "ECE"})
6571

6672

67-
def update_document():
73+
def test_update_document():
6874
assert student_info.update({"passed": True, "mobile": 123456789}, {"name": "Bob"})
6975
assert student_info.update({"name": "The Sam", "skill": ["C++", "Python"]}, {"name": "Sam"})
7076
assert student_info.update({"dept": "Computer Science & Engineering"}, {"dept": "CSE"})
77+
78+
79+
def test_rename():
80+
student_2.rename("student_info_cse")
81+
82+
83+
def test_drop():
84+
student_info.drop()
85+
86+

test/test_database.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
db_2 = FileXdb("NewDb", "test_data/db")
77

88
# Create a Collection
9-
student_info = db.collection("student_info")
10-
player_info = db.collection("student_info")
9+
student_info = db_2.collection("student_info")
10+
player_info = db_2.collection("student_info")
1111

1212
student_info.insert_all([
1313
{"name": "Addy", "roll": "ME/57/19", "dept": "ME", "cgpa": 9.05},
@@ -23,12 +23,12 @@ def test_show_collections():
2323

2424

2525
def test_show():
26-
assert db.show()
27-
db_2.show()
26+
db._show()
27+
db_2._show()
2828

2929
# prettify json object
30-
assert db.show().prettify()
31-
assert db_2.show().prettify()
30+
assert db._show().prettify()
31+
assert db_2._show().prettify()
3232

3333

3434
def test_export():

0 commit comments

Comments
 (0)