Skip to content

Commit 3dbbb38

Browse files
author
juliocastillo
committed
fix: Fix functional testing
1 parent 13006b9 commit 3dbbb38

File tree

2 files changed

+185
-185
lines changed

2 files changed

+185
-185
lines changed
Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,92 @@
1-
from crud import CRUDStrategy
2-
3-
4-
class ProductCRUD(CRUDStrategy):
5-
"""
6-
A CRUD strategy class for product documents in a MongoDB collection.
7-
8-
This class inherits from CRUDStrategy and implements specific CRUD operations
9-
for managing product documents in the 'products' collection of MongoDB.
10-
11-
Methods:
12-
create(data): Inserts a new product document into the collection.
13-
read(query): Retrieves a product document from the collection based on a query.
14-
update(query, data): Updates a product document in the collection.
15-
delete(query): Deletes a product document from the collection.
16-
"""
17-
def __init__(self):
18-
"""Initializes the ProductCRUD class for the 'products' collection."""
19-
super().__init__("products")
20-
21-
def create(self, data):
22-
"""
23-
Inserts a new product document into the collection.
24-
25-
Args:
26-
data (dict): The product data to insert.
27-
28-
Returns:
29-
InsertOneResult: The result object which includes the ID of the newly inserted document.
30-
"""
31-
return self.collection.insert_one(data)
32-
33-
def read(self, query):
34-
"""
35-
Retrieves a product document from the collection based on a query.
36-
37-
Args:
38-
query (dict): The query to select the document.
39-
40-
Returns:
41-
dict: The first document found matching the query. None if no document matches.
42-
"""
43-
return self.collection.find_one(query)
44-
45-
def update(self, query, data):
46-
"""
47-
Updates a product document in the collection.
48-
49-
Args:
50-
query (dict): The query to select the document for update.
51-
data (dict): The data to update in the selected document.
52-
53-
Returns:
54-
UpdateResult: The result object of the update operation.
55-
"""
56-
return self.collection.update_one(query, {"$set": data})
57-
58-
def delete(self, query):
59-
"""
60-
Deletes a product document from the collection.
61-
62-
Args:
63-
query (dict): The query to select the document for deletion.
64-
65-
Returns:
66-
DeleteResult: The result object of the delete operation.
67-
"""
68-
return self.collection.delete_one(query)
69-
70-
71-
if __name__ == "__main__":
72-
# Example usage of the ProductCRUD class
73-
product_crud = ProductCRUD()
74-
75-
# Creating a product
76-
product_id = product_crud.create(
77-
{"product_id": "123", "name": "Example Product", "price": 29.99}
78-
)
79-
80-
# Reading the newly created product
81-
product = product_crud.read({"_id": product_id})
82-
print(product)
83-
84-
# Updating the product's price
85-
product_crud.update({"_id": product_id}, {"price": 99.99})
86-
product = product_crud.read({"_id": product_id})
87-
print(product)
88-
89-
# Deleting the product
90-
product_crud.delete({"_id": product_id})
91-
product = product_crud.read({"_id": product_id})
92-
print(product)
1+
from crud import CRUDStrategy
2+
3+
4+
class ProductCRUD(CRUDStrategy):
5+
"""
6+
A CRUD strategy class for product documents in a MongoDB collection.
7+
8+
This class inherits from CRUDStrategy and implements specific CRUD operations
9+
for managing product documents in the 'products' collection of MongoDB.
10+
11+
Methods:
12+
create(data): Inserts a new product document into the collection.
13+
read(query): Retrieves a product document from the collection based on a query.
14+
update(query, data): Updates a product document in the collection.
15+
delete(query): Deletes a product document from the collection.
16+
"""
17+
def __init__(self):
18+
"""Initializes the ProductCRUD class for the 'products' collection."""
19+
super().__init__("products")
20+
21+
def create(self, data):
22+
"""
23+
Inserts a new product document into the collection.
24+
25+
Args:
26+
data (dict): The product data to insert.
27+
28+
Returns:
29+
InsertOneResult: The result object which includes the ID of the newly inserted document.
30+
"""
31+
return self.collection.insert_one(data)
32+
33+
def read(self, query):
34+
"""
35+
Retrieves a product document from the collection based on a query.
36+
37+
Args:
38+
query (dict): The query to select the document.
39+
40+
Returns:
41+
dict: The first document found matching the query. None if no document matches.
42+
"""
43+
return self.collection.find_one(query)
44+
45+
def update(self, query, data):
46+
"""
47+
Updates a product document in the collection.
48+
49+
Args:
50+
query (dict): The query to select the document for update.
51+
data (dict): The data to update in the selected document.
52+
53+
Returns:
54+
UpdateResult: The result object of the update operation.
55+
"""
56+
return self.collection.update_one(query, {"$set": data})
57+
58+
def delete(self, query):
59+
"""
60+
Deletes a product document from the collection.
61+
62+
Args:
63+
query (dict): The query to select the document for deletion.
64+
65+
Returns:
66+
DeleteResult: The result object of the delete operation.
67+
"""
68+
return self.collection.delete_one(query)
69+
70+
71+
if __name__ == "__main__":
72+
# Example usage of the ProductCRUD class
73+
product_crud = ProductCRUD()
74+
75+
# Creating a product
76+
product_id = product_crud.create(
77+
{"product_id": "123", "name": "Example Product", "price": 29.99}
78+
).inserted_id
79+
80+
# Reading the newly created product
81+
product = product_crud.read({"_id": product_id})
82+
print(product)
83+
84+
# Updating the product's price
85+
product_crud.update({"_id": product_id}, {"price": 99.99})
86+
product = product_crud.read({"_id": product_id})
87+
print(product)
88+
89+
# Deleting the product
90+
product_crud.delete({"_id": product_id})
91+
product = product_crud.read({"_id": product_id})
92+
print(product)

02_CRUD_Strategy/user_mongo_crud.py

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
1-
from crud import CRUDStrategy
2-
3-
4-
class UserCRUD(CRUDStrategy):
5-
"""
6-
Implements CRUD operations specifically for user documents in a MongoDB collection.
7-
8-
This class inherits from CRUDStrategy and provides concrete implementations of the
9-
create, read, update, and delete operations for managing user documents within the
10-
'users' collection of MongoDB.
11-
12-
Methods:
13-
create(data): Inserts a new user document into the collection.
14-
read(query): Retrieves a user document from the collection based on a query.
15-
update(query, data): Updates a user document in the collection.
16-
delete(query): Deletes a user document from the collection.
17-
"""
18-
def __init__(self):
19-
"""Initializes the UserCRUD class to work with the 'users' collection."""
20-
super().__init__("users")
21-
22-
def create(self, data):
23-
"""
24-
Inserts a new user document into the collection.
25-
26-
Args:
27-
data (dict): The user data to insert.
28-
29-
Returns:
30-
InsertOneResult: The result object which includes the ID of the newly inserted document.
31-
"""
32-
return self.collection.insert_one(data)
33-
34-
def read(self, query):
35-
"""
36-
Retrieves a user document from the collection based on a query.
37-
38-
Args:
39-
query (dict): The query to select the document.
40-
41-
Returns:
42-
dict: The first document found matching the query. None if no document matches.
43-
"""
44-
return self.collection.find_one(query)
45-
46-
def update(self, query, data):
47-
"""
48-
Updates a user document in the collection.
49-
50-
Args:
51-
query (dict): The query to select the document for update.
52-
data (dict): The data to update in the selected document.
53-
54-
Returns:
55-
UpdateResult: The result object of the update operation.
56-
"""
57-
return self.collection.update_one(query, {"$set": data})
58-
59-
def delete(self, query):
60-
"""
61-
Deletes a user document from the collection.
62-
63-
Args:
64-
query (dict): The query to select the document for deletion.
65-
66-
Returns:
67-
DeleteResult: The result object of the delete operation.
68-
"""
69-
return self.collection.delete_one(query)
70-
71-
72-
if __name__ == "__main__":
73-
# Using the class directly
74-
user_crud = UserCRUD()
75-
76-
# Creating a user
77-
user_id = user_crud.create(
78-
{"username": "jonhdoe", "name": "John Doe", "email": "[email protected]"}
79-
)
80-
81-
# Reading the newly created user
82-
user = user_crud.read({"_id": user_id})
83-
print(user)
84-
85-
# Updating the user's name
86-
user_crud.update({"_id": user_id}, {"name": "John Dollar"})
87-
user = user_crud.read({"_id": user_id})
88-
print(user)
89-
90-
# Deleting the user
91-
user_crud.delete({"_id": user_id})
92-
user = user_crud.read({"_id": user_id})
93-
print(user)
1+
from crud import CRUDStrategy
2+
3+
4+
class UserCRUD(CRUDStrategy):
5+
"""
6+
Implements CRUD operations specifically for user documents in a MongoDB collection.
7+
8+
This class inherits from CRUDStrategy and provides concrete implementations of the
9+
create, read, update, and delete operations for managing user documents within the
10+
'users' collection of MongoDB.
11+
12+
Methods:
13+
create(data): Inserts a new user document into the collection.
14+
read(query): Retrieves a user document from the collection based on a query.
15+
update(query, data): Updates a user document in the collection.
16+
delete(query): Deletes a user document from the collection.
17+
"""
18+
def __init__(self):
19+
"""Initializes the UserCRUD class to work with the 'users' collection."""
20+
super().__init__("users")
21+
22+
def create(self, data):
23+
"""
24+
Inserts a new user document into the collection.
25+
26+
Args:
27+
data (dict): The user data to insert.
28+
29+
Returns:
30+
InsertOneResult: The result object which includes the ID of the newly inserted document.
31+
"""
32+
return self.collection.insert_one(data)
33+
34+
def read(self, query):
35+
"""
36+
Retrieves a user document from the collection based on a query.
37+
38+
Args:
39+
query (dict): The query to select the document.
40+
41+
Returns:
42+
dict: The first document found matching the query. None if no document matches.
43+
"""
44+
return self.collection.find_one(query)
45+
46+
def update(self, query, data):
47+
"""
48+
Updates a user document in the collection.
49+
50+
Args:
51+
query (dict): The query to select the document for update.
52+
data (dict): The data to update in the selected document.
53+
54+
Returns:
55+
UpdateResult: The result object of the update operation.
56+
"""
57+
return self.collection.update_one(query, {"$set": data})
58+
59+
def delete(self, query):
60+
"""
61+
Deletes a user document from the collection.
62+
63+
Args:
64+
query (dict): The query to select the document for deletion.
65+
66+
Returns:
67+
DeleteResult: The result object of the delete operation.
68+
"""
69+
return self.collection.delete_one(query)
70+
71+
72+
if __name__ == "__main__":
73+
# Using the class directly
74+
user_crud = UserCRUD()
75+
76+
# Creating a user
77+
user_id = user_crud.create(
78+
{"username": "jonhdoe", "name": "John Doe", "email": "[email protected]"}
79+
).inserted_id
80+
81+
# Reading the newly created user
82+
user = user_crud.read({"_id": user_id})
83+
print(user)
84+
85+
# Updating the user's name
86+
user_crud.update({"_id": user_id}, {"name": "John Dollar"})
87+
user = user_crud.read({"_id": user_id})
88+
print(user)
89+
90+
# Deleting the user
91+
user_crud.delete({"_id": user_id})
92+
user = user_crud.read({"_id": user_id})
93+
print(user)

0 commit comments

Comments
 (0)