|
1 | 1 | from filexdb import FileXdb |
2 | 2 |
|
3 | 3 |
|
4 | | -db = FileXdb("testDb", 'data') |
| 4 | +# Create an instance of Database |
| 5 | +db = FileXdb("testDb", "data/db") |
5 | 6 |
|
6 | | -data = {"name": "India", "capital": "New Delhi", "city": ["Kolkata", "Bangalore", "Mumbai", "Delhi"]} |
7 | | -data_list = [ |
8 | | - {"name": "Nepal", "capital": "Kathmandu", "city": []}, |
9 | | - {"name": "Russia", "capital": "Moscow"} |
10 | | -] |
| 7 | +# Create a Collection |
| 8 | +student_info = db.collection("student_info") |
11 | 9 |
|
12 | | -countries = db.collection("countries") |
13 | | -countries.insert(data) |
14 | | -countries.insert_all(data_list) |
15 | 10 |
|
| 11 | +def insert_single_document(): |
| 12 | + assert student_info.insert({"name": "Sam", "roll": "CSE/17/19", "dept": "CSE"}) |
| 13 | + assert student_info.insert({"name": "Bob", "roll": "EE/01/18", "dept": "EE", "skill": ["python", "c++"]}) |
| 14 | + assert student_info.insert({"name": "Rana", "dept": "CSE"}) |
| 15 | + |
| 16 | + |
| 17 | +def insert_multiple_document(): |
| 18 | + assert student_info.insert_all([ |
| 19 | + {"name": "Addy", "roll": "ME/57/19", "dept": "ME", "cgpa": 9.05}, |
| 20 | + {"name": "Roman", "roll": "ECE/80/13", "dept": "ECE", "skill": ["game design"], "spc": ["Blinder"]}, |
| 21 | + {"name": "Sam"} |
| 22 | + ] |
| 23 | + ) |
| 24 | + |
| 25 | + |
| 26 | +def find_document_1(): |
| 27 | + _query_1 = {"name": "Sam"} |
| 28 | + |
| 29 | + assert student_info.find() # Returns all Documents. |
| 30 | + assert student_info.find(query=_query_1) # Returns all Documents matches the ``_query``. |
| 31 | + assert student_info.find(query=_query_1, limit=(1, 3)) # Returns doc[1] to doc[2] matches the ``_query``. |
| 32 | + assert student_info.find(limit=(1, 10)) # Returns doc[1] to doc[9] of all Documents. |
| 33 | + |
| 34 | + |
| 35 | +def find_document_2(): |
| 36 | + _query_2 = {"name": "Sam", "roll": "CSE/17/19"} |
| 37 | + |
| 38 | + assert student_info.find() # Returns all Documents. |
| 39 | + assert student_info.find(query=_query_2) # Returns all Documents matches the ``_query``. |
| 40 | + assert student_info.find(query=_query_2, limit=(1, 3)) # Returns doc[1] to doc[2] matches the ``_query``. |
| 41 | + assert student_info.find(limit=(1, 10)) # Returns doc[1] to doc[9] of all Documents. |
| 42 | + |
| 43 | + |
| 44 | +def delete_document(): |
| 45 | + assert student_info.delete({"name": "Addy"}) |
| 46 | + assert student_info.delete({"name": "Sam", "roll": "CSE/17/19"}) |
| 47 | + assert student_info.delete({"name": "Roman", "roll": "ECE/80/13", "dept": "ECE"}) |
| 48 | + |
| 49 | + |
| 50 | +def update_document(): |
| 51 | + assert student_info.update({"passed": True, "mobile": 123456789}, {"name": "Bob"}) |
| 52 | + assert student_info.update({"name": "The Sam", "skill": ["C++", "Python"]}, {"name": "Sam"}) |
| 53 | + assert student_info.update({"dept": "Computer Science & Engineering"}, {"dept": "CSE"}) |
0 commit comments