Skip to main content
Tweeted twitter.com/StackSoftEng/status/1075677384983134208
fixed code flaw
Source Link
Doc Brown
  • 220.7k
  • 35
  • 411
  • 625

i am having a simple app example here:

say i have this piece of code which handles requests from user to get a list of books stored in a database.

from .handlers import all_books
from flask import jsonify

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    books = all_books(user_name=user_name)
    return jsonify(books), 200
 

and in handlers.py i have :

def all_books(user_name):
        db = get_db('books')
        books = []
        for book in db.books.find({'read_by':user_name}):
            books.append(book)
        return books

but while writing unit tests i realised if i use get_db() inside all_books() it would be harder to unit test the method. so i thought this would be the good way.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    db = get_db('books')
    collection = db.books
    all_books(collection=collection,user_name=user_name)

def all_books(collection,user_name):
        books = []
        for book in collection.find({'read_by':user_name}):
            books.append(book)
        return books

i want to know what is the good design to use? have all code doing one thing at one place like the first example or the second example is good.

To me first one seems more clear as it has all related logic at one place. but its easier to pass a fake collection in second case to unit test it.

i am having a simple app example here:

say i have this piece of code which handles requests from user to get a list of books stored in a database.

from .handlers import all_books
from flask import jsonify

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    books = all_books(user_name=user_name)
    return jsonify(books), 200
 

and in handlers.py i have :

def all_books(user_name):
        db = get_db('books')
        books = []
        for book in db.books.find({'read_by':user_name}):
            books.append(book)
        return books

but while writing unit tests i realised if i use get_db() inside all_books() it would be harder to unit test the method. so i thought this would be the good way.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    db = get_db('books')
    collection = db.books
    all_books(collection=collection)

def all_books(collection):
        books = []
        for book in collection.find({'read_by':user_name}):
            books.append(book)
        return books

i want to know what is the good design to use? have all code doing one thing at one place like the first example or the second example is good.

To me first one seems more clear as it has all related logic at one place. but its easier to pass a fake collection in second case to unit test it.

i am having a simple app example here:

say i have this piece of code which handles requests from user to get a list of books stored in a database.

from .handlers import all_books
from flask import jsonify

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    books = all_books(user_name=user_name)
    return jsonify(books), 200
 

and in handlers.py i have :

def all_books(user_name):
        db = get_db('books')
        books = []
        for book in db.books.find({'read_by':user_name}):
            books.append(book)
        return books

but while writing unit tests i realised if i use get_db() inside all_books() it would be harder to unit test the method. so i thought this would be the good way.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    db = get_db('books')
    collection = db.books
    all_books(collection=collection,user_name=user_name)

def all_books(collection,user_name):
        books = []
        for book in collection.find({'read_by':user_name}):
            books.append(book)
        return books

i want to know what is the good design to use? have all code doing one thing at one place like the first example or the second example is good.

To me first one seems more clear as it has all related logic at one place. but its easier to pass a fake collection in second case to unit test it.

added 82 characters in body
Source Link
anekix
  • 203
  • 1
  • 7

i am having a simple app example here:

say i have this piece of code which handles requests from user to get a list of books stored in a database.

from .handlers import all_books
from flask import jsonify

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    books = all_books(user_name=user_name)
    return jsonify(books), 200
 

and in handlers.py i have :

def all_books(user_name):
        db = get_db('books')
        books = []
        for book in db.books.find({'read_by':user_name}):
            books.append(book)
        return books

but while writing unit tests i realised if i use get_db() inside all_books() it would be harder to unit test the method. so i thought this would be the good way.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    db = get_db('books')
    collection = db.books
    all_books(collection=collection)

def all_books(collection):
        books = []
        for book in collection.find({'read_by':user_name}):
            books.append(book)
        return books

i want to know what is the good design to use? have all code doing one thing at one place like the first example or the second example is good.

To me first one seems more clear as it has all related logic at one place. but its easier to pass a fake collection in second case to unit test it.

i am having a simple app example here:

say i have this piece of code which handles requests from user to get a list of books stored in a database.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    all_books(user_name=user_name)

and in handlers.py i have :

def all_books(user_name):
        db = get_db('books')
        books = []
        for book in db.books.find():
            books.append(book)
        return books

but while writing unit tests i realised if i use get_db() inside all_books() it would be harder to unit test the method. so i thought this would be the good way.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    db = get_db('books')
    collection = db.books
    all_books(collection=collection)

def all_books(collection):
        books = []
        for book in collection.find():
            books.append(book)
        return books

i want to know what is the good design to use? have all code doing one thing at one place like the first example or the second example is good.

To me first one seems more clear as it has all related logic at one place. but its easier to pass a fake collection in second case to unit test it.

i am having a simple app example here:

say i have this piece of code which handles requests from user to get a list of books stored in a database.

from .handlers import all_books
from flask import jsonify

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    books = all_books(user_name=user_name)
    return jsonify(books), 200
 

and in handlers.py i have :

def all_books(user_name):
        db = get_db('books')
        books = []
        for book in db.books.find({'read_by':user_name}):
            books.append(book)
        return books

but while writing unit tests i realised if i use get_db() inside all_books() it would be harder to unit test the method. so i thought this would be the good way.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    db = get_db('books')
    collection = db.books
    all_books(collection=collection)

def all_books(collection):
        books = []
        for book in collection.find({'read_by':user_name}):
            books.append(book)
        return books

i want to know what is the good design to use? have all code doing one thing at one place like the first example or the second example is good.

To me first one seems more clear as it has all related logic at one place. but its easier to pass a fake collection in second case to unit test it.

Source Link
anekix
  • 203
  • 1
  • 7

refactoring function to have a robust design

i am having a simple app example here:

say i have this piece of code which handles requests from user to get a list of books stored in a database.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    all_books(user_name=user_name)

and in handlers.py i have :

def all_books(user_name):
        db = get_db('books')
        books = []
        for book in db.books.find():
            books.append(book)
        return books

but while writing unit tests i realised if i use get_db() inside all_books() it would be harder to unit test the method. so i thought this would be the good way.

from .handlers import all_books

@apps.route('/show/all', methods=['GET'])
@jwt_required
def show_books():
    user_name = get_jwt_identity()['user_name']
    db = get_db('books')
    collection = db.books
    all_books(collection=collection)

def all_books(collection):
        books = []
        for book in collection.find():
            books.append(book)
        return books

i want to know what is the good design to use? have all code doing one thing at one place like the first example or the second example is good.

To me first one seems more clear as it has all related logic at one place. but its easier to pass a fake collection in second case to unit test it.