def test_init_ShouldInitializePydalConnectionWithMongoConnectionInside(self): pydalConnection = InstanceFactory.createPydalConnectionInstance() assert pydalConnection is not None assert pydalConnection._connection_string == InstanceFactory.mongoConnection() assert pydalConnection._mongo_connection is not None
def test_db_ShouldCreatePydalDbWithMongoDbDatabaseInside(self): pydalDb = InstanceFactory.createPydalDbInstance() assert pydalDb is not None assert pydalDb.db_name == InstanceFactory.mongoDbName() assert pydalDb.mongodb is not None assert pydalDb.mongodb.name == InstanceFactory.mongoDbName()
def test_collection_ShouldCreatePydalCollectionWithMongoDbCollectionInside(self): pydalCollection = InstanceFactory.createPydalCollectionInstance() assert pydalCollection is not None assert pydalCollection.collection_name == InstanceFactory.mongoCollectionName() assert pydalCollection._mongo_collection is not None assert pydalCollection._mongo_collection.name == InstanceFactory.mongoCollectionName() assert pydalCollection._mongo_collection.full_name == InstanceFactory.mongoDbName() + "." + InstanceFactory.mongoCollectionName()
def test_isAvailable_ShouldCheckIfDocumentIsAvailable(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() pydalCollectionInstance.writer.add(employee) assert pydalCollectionInstance.reader.is_available({"name": employee.Name}) == True assert pydalCollectionInstance.reader.is_available({"_id": ObjectId(employee._id)}) == True assert pydalCollectionInstance.reader.is_available({"_id": ObjectId()}) == False
def test_findOne_ShouldFindOneDocumentBasedOnQuery(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() pydalCollectionInstance.writer.add(employee) foundedEmployee = pydalCollectionInstance.reader.find_one({"_id": ObjectId(employee._id)}) assert foundedEmployee.Name == employee.Name assert foundedEmployee._id == employee._id
def test_count_ShouldGetCountOfDocumentBasedOnQuery(self): employee = Employee.createSampleInstance() employee1 = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() pydalCollectionInstance.writer.add(employee) pydalCollectionInstance.writer.add(employee1) assert pydalCollectionInstance.reader.count({"name": employee.Name}) > 0 assert pydalCollectionInstance.reader.count({"name": employee1.Name}) > 0
def test_removeByCondition_ShouldRemoveDocumentsInMongoDbIfTheConditionIsEstablishedForThem(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() mongoDeveloperCollection = pydalCollectionInstance._mongo_collection pydalCollectionInstance.writer.add(employee) assert mongoDeveloperCollection.find({"_id": ObjectId(employee._id)}).count() > 0 pydalCollectionInstance.writer.remove_by_condition({"_id": ObjectId(employee._id)}) assert mongoDeveloperCollection.find({"_id": ObjectId(employee._id)}).count() == 0
def test_removeById_ShouldRemoveDocumentInMongoDbByItsId(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() mongoDeveloperCollection = pydalCollectionInstance._mongo_collection pydalCollectionInstance.writer.add(employee) assert mongoDeveloperCollection.find({"_id": ObjectId(employee._id)}).count() > 0 pydalCollectionInstance.writer.remove_by_id(employee._id) assert mongoDeveloperCollection.find({"_id": ObjectId(employee._id)}).count() == 0
def test_add_ShouldCreateDocumentInMondoDb(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() pydalCollectionInstance.writer.add(employee) assert hasattr(employee, "_id") assert employee._id is not None assert employee._id != "" assert type(employee._id) == str
def test_findMany_ShouldFindManyDocumentsBasedOnQuery(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() pydalCollectionInstance.writer.add(employee) employee._id = str(ObjectId()) pydalCollectionInstance.writer.add(employee) foundedEmployees = pydalCollectionInstance.reader.find_many({"name": employee.Name}) assert len(foundedEmployees) > 0 assert foundedEmployees[0].Name == employee.Name
def test_edit_ShouldUpdateAvailableDocumentInMongoDb(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() assert not hasattr(employee, "_id") pydalCollectionInstance.writer.add(employee) assert hasattr(employee, "_id") createdEmployeeId = employee._id employee.Name = employee.Name + " - Edited" pydalCollectionInstance.writer.edit(employee) editedEmployeeId = employee._id assert createdEmployeeId == editedEmployeeId
def test_save_ShouldUpdateCreatedDocument(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() assert not hasattr(employee, "_id") pydalCollectionInstance.writer.save(employee) assert hasattr(employee, "_id") createdEmployeeId = employee._id employee.Name = employee.Name + " - Edited" pydalCollectionInstance.writer.save(employee) editedEmployeeId = employee._id assert createdEmployeeId == editedEmployeeId
def test_removeAll_ShouldRemoveAllDocumentsInCollection(self): employee = Employee.createSampleInstance() employee1 = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() mongoDeveloperCollection = pydalCollectionInstance._mongo_collection pydalCollectionInstance.writer.add(employee) pydalCollectionInstance.writer.add(employee1) assert mongoDeveloperCollection.find({"_id": ObjectId(employee._id)}).count() > 0 assert mongoDeveloperCollection.find({"_id": ObjectId(employee1._id)}).count() > 0 pydalCollectionInstance.writer.remove_all() assert mongoDeveloperCollection.find({"_id": ObjectId(employee._id)}).count() == 0 assert mongoDeveloperCollection.find({"_id": ObjectId(employee1._id)}).count() == 0
def test_editByCondition_ShouldExecuteSecondQueryOnElementsOfDocumentsWhichAreUnderFirstQueryCondition(self): employee = Employee.createSampleInstance() pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() mongoDeveloperCollection = pydalCollectionInstance._mongo_collection pydalCollectionInstance.writer.add(employee) addedEmployee = mongoDeveloperCollection.find_one({"_id": ObjectId(employee._id)}) assert len(addedEmployee["address"]) == 2 pydalCollectionInstance.writer.edit_by_condition( {"_id": ObjectId(employee._id)}, {"$pull": {"address": {"street": "Farmanieh"}}} ) editedEmployee = mongoDeveloperCollection.find_one({"_id": ObjectId(employee._id)}) assert len(editedEmployee["address"]) == 1
def test_Reader_ShouldReturnPydalReadCommandWithMongoCollectionInside(self): pydalCollectionInstance = InstanceFactory.createPydalCollectionInstance() self._checkCorrectionOfPydalCollectionReaderOrWriterInitialization(pydalCollectionInstance.reader, pydalCollectionInstance)
def setUpClass(cls): cls.capped_collection = InstanceFactory.createPydalCappedCollectionInstance()