Home > Mobile >  Testing a POST method unit test which inserts data to mongodb database
Testing a POST method unit test which inserts data to mongodb database

Time:11-12

I want to know how am I supposed to test my code and see whether it works properly. I want to make sure that it stores the received data to the database. Can you please tell me how am I supposed to do that? While I was searching the forum I found this post but I did not really understand what is going on. here is the code I want to test.

client = MongoClient(os.environ.get("MONGODB_URI"))
app.db = client.securify
app.secret_key = str(os.environ.get("APP_SECRET"))


@app.route("/", methods=["GET", "POST"])
def home():

    if request.method == "POST":
        ip_address = request.remote_addr
        entry_content = request.form.get("content")
        formatted_date = datetime.datetime.today().strftime("%Y-%m-%d/%H:%M")
        app.db.entries.insert({"content": entry_content, "date": formatted_date, "IP": ip_address})
      
    return render_template("home.html")

and here is the mock test I wrote:

import os
from unittest import TestCase




from app import app


class AppTest(TestCase):
    # executed prior to each test
    def setUp(self):
        # you can change your application configuration
        app.config['TESTING'] = True

        # you can recover a "test cient" of your defined application
        self.app = app.test_client()

    # then in your test method you can use self.app.[get, post, etc.] to make the request
    def test_home(self):
        url_path = '/'
        response = self.app.get(url_path)
        self.assertEqual(response.status_code, 200)
    def test_post(self):
        url_path = '/'
        response = self.app.post(url_path,data={"content": "this is a test"})
        self.assertEqual(response.status_code, 200)

The test_post gets stuck and after some seconds gives an error when reaches app.db.entries.insert({"content": entry_content, "date": formatted_date, "IP": ip_address}) part. Please tell me also how can I retrieve the saved data in order to make sure it is saved in the expected way

CodePudding user response:

This is what I do using NodeJS, not tested at all in python but the idea is the same.

First of all, find a in-memory DB, there are options like pymongo-inmemory or mongomock

Then in your code you have to do the connection according to you environment (production/development/whatever)

Something like this:

env = os.environ.get("ENV")
if env == "TESTING":
  # connect to mock db
elif env == "DEVELOMPENT":
  # for example if you want to test against a real DB but not the production one
  # then do the connection here
else:
  # connect to production DB

CodePudding user response:

I don't know if it is the proper way to do it but I found a solution. After creating a test client self.app = app.test_client() the db gets set to localhost:27017 so I changed it manually as follows and it worked:

self.app = app.test_client()
client = MongoClient(os.environ.get("MONGODB_URI"))
  • Related