Python falcon framework integration testing not working

I am pretty new to integration testing and falcon framework and having hard time to implement integration test for my apis. I have been trying to use sqllite to create a fixture data and use it instead of my database whenever i simulate a request to the actual endpoint. I’m using TestBase from falcon.testing and other dependencies from falcon again as it can be seen below.

import sqlite3
import json
import pytest
from resources.dashboards import StockStats
from urllib.parse import urlencode
from falcon.testing import TestBase


class FakeStockStats(StockStats):

    def __init__(self, name, conn):
        self.conn = conn
        super().__init__(name)

    def set_conn(self, conn):
        self.conn = conn

    def get_conn(self, connection):
        return self.conn


class TestStockStats(TestBase):
    def before(self):
        conn = sqlite3.connect('example.db')
        cursor = conn.cursor()
        cursor.execute('''
                CREATE TABLE IF NOT EXISTS stocks
                (date text, trans text, symbol text, qty real, price real)''')
        sample_data = [
            ('2020-01-01', 'BUY', 'IBM', 1000, 45.0),
            ('2020-01-01', 'SELL', 'GOOG', 40, 123.0),
        ]
        cursor.executemany('INSERT INTO stocks VALUES(?, ?, ?, ?, ?)', sample_data)

        self.conn = conn

        self.base_stats_test_1 = '/v{v}/test_stock_daily_stats'
        self.resource = FakeStockStats("Stock Test", self.conn)
        self.api.add_route(self.base_stats_test_1, self.resource)

    def after(self):
        del self.api

    def test_generate_report(self):
        resp = self.simulate_request('/v1.1/test_stock_daily_stats',
                                     headers={'x-id-header': '[email protected]'},
                                     query_string=urlencode({
                                         'stock_id': '098x990jlks2',
                                     }))

        data = json.loads(resp[0].decode('utf-8'))

        print("data is",data)

StockStats class is just taking some params from query and executes the query on the table stocks. I am trying to create the stocks table in sqllite database so that i wouldn’t make a connection to actual database. The data returns with an error saying

{'error': {'msg': 'Internal Server Error: An error occurred processing your request', 'code': '500 Internal Server Error'},

I know this error message is probably not helpful but i’m pretty new to falcon and testing. When i try to debug it, it seems like the StockStats is working properly and passing all the params and headers from simulate_request correct. However, i’m not sure if there is an error with sqllite connection that i’m trying to use. I was not able to find good resources with falcon testing framework, integration test and fixture data. It would be greatly appreciated if anyone knows good resources that i can take a look.

Source: Python Questions

LEAVE A COMMENT