Search code examples
pythonmongodbpymongocrud

Says 'AnimalShelter' object has no attritbute 'create' but everything is there?


I am trying to test my AnimalShelter.py code in Jupyter Notebook but I keep getting the error that the 'create' attribute is not there. Did I miss something? I have it defined in the code but no matter what I change it still says it is not there.

AnimalShelter.py code:

import pymongo

from pymongo import MongoClient
from bson.objectid import ObjectId

class AnimalShelter(object):
    """ CRUD operations for Animal collection in MongoDB """

    def __init__(self, username, password):
        #Initializing the MongoClient. This helps to access the MongoDB databases and collections.
        self.client = MongoClient('mongodb://%s:%s@localhost:45344' % (username, password))
        #where xxxx is your unique port number
        self.database = self.client['AAC']
    
#Complete this create method to implement the C in CRUD.
    def create(self, data):
        if data is not None:
            insert = self.database.animals.insert(data) #data should be dictionary
        
        else:
            raise Exception("Nothing to save, because data parameter is empty")
            
#Create method to implement the R in CRUD.
    def read(self, searchData):
        if searchData:
            data = self.database.animals.find(searchData, {"_id": False})
        
        else:
            data = self.database.animals.find({}, {"_id": False})
        return data

#Create method to implement U in CRUD.
    def update(self, searchData, updateData):
        if searchData is not None:
            result = self.database.animals.update_many(searchData, {"$set": updateData})
        else:
            return "{}"
        return result.raw_result

#Create method to implement D in CRUD.
    def delete(self, deleteData):
        if deleteData is not None:
            result = self.database.animals.delete_many(deleteData)
        
        else:
            return "{}"
        return result.raw_result

The test code I am trying to use to see that CRUD is functioning properly:

from AnimalShelter import AnimalShelter

data = {}
query = {}

test_class = AnimalShelter()

#test each function in the AnimalShelter class
response = test_class.create(data)
assert response

response = test_class.read(data)
assert response

response = test_class.update(data)
assert response

response = test_class.delete(data)
assert response

I am at a loss. I am new to Jupyter Notebook with the whole testing thing and I figured this was the simplest way to test the attributes before making a different test for specific data from the python code but either way I still get that the create attribute doesn't exist!


Solution

  • I created a slightly altered version of your code for easy testing and got the error that no username and password were being passed to the AnimalShelter constructor.

    class AnimalShelter(object):
        """ CRUD operations for Animal collection in MongoDB """
    
        def __init__(self, username, password):
            print("constructor")
        
    #Complete this create method to implement the C in CRUD.
        def create(self, data):
            print("create")
                
    #Create method to implement the R in CRUD.
        def read(self, searchData):
            print("read")
    
    #Create method to implement U in CRUD.
        def update(self, searchData, updateData):
            print("update")
    
    #Create method to implement D in CRUD.
        def delete(self, deleteData):
            print("delete")
    
    test_class = AnimalShelter("user", "pass")
    data = {}
    
    test_class.create(data)
    test_class.read(data)
    test_class.update(data, {})
    test_class.delete(data)
    

    Could your error be because you didn't pass these variables when constructing the test_class object? Upon passing "user" and "pass", I got the expected output:

    constructor
    create
    read
    update
    delete