Skip to content

Commit

Permalink
Unit test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
prakass1 committed Feb 1, 2021
1 parent 90811f7 commit d1f4d1a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
11 changes: 6 additions & 5 deletions blog/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from flask import Flask
from common import db, cache
from os import environ
from dotenv import load_dotenv, find_dotenv
from instance.config import app_config
from flask_wtf.csrf import CSRFProtect
from flask_mail import Mail

load_dotenv(find_dotenv())
load_dotenv(find_dotenv(raise_error_if_not_found=True))

from instance.config import app_config
from common import db, cache

blog_header = environ.get("blog_header")
blog_subheader = environ.get("blog_subheader")
Expand Down Expand Up @@ -56,10 +57,10 @@ def create_app(config_name):
# Clear cache
cache.clear()

# create ab
# create db
db.create_all()

from common.models import users_model
# from common.models import users_model

# Register blueprints
app.register_blueprint(api_controller.api_bp)
Expand Down
3 changes: 1 addition & 2 deletions blog/posts/service/posts_blog_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ def get_posts_html_resp(serialized_obj, post_len, new_limit):
"</h2></a>" + "<p class='post-meta'>Posted by " + "<a href='#'>" + \
post_obj["author"] + "</a> on " + post_obj["posted_date"] + "</p></div><hr>"
cache.set(str(post_len), str_concat, timeout=50)
return str_concat
return str_concat
#if post_len > limit:
# # Add load more content
# str_concat += "<div class='clearfix'>" + "<button class='btn btn-primary float-right' id='load_more'>Older Posts &rarr;</button>" + \
# "<input type='hidden' name='prev_limit' id='prev_limit' value=" + "'" + str(limit) + "'" + "/>"
#return str_concat

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ astroid==2.4.2
autoenv==1.0.0
autopep8==1.5.4
beautifulsoup4==4.9.3
blinker==1.4
bs4==0.0.1
certifi==2020.12.5
chardet==4.0.0
Expand All @@ -11,6 +12,7 @@ colorama==0.4.4
Flask==1.1.2
Flask-Caching==1.9.0
Flask-Login==0.5.0
Flask-Mail==0.9.1
Flask-RESTful==0.3.8
Flask-SQLAlchemy==2.4.4
Flask-WTF==0.14.3
Expand Down
Empty file added tests/__init__.py
Empty file.
Empty file added tests/blog/__init__.py
Empty file.
Empty file added tests/blog/unit/__init__.py
Empty file.
63 changes: 63 additions & 0 deletions tests/blog/unit/test_blog_post_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from os import environ
import datetime
import pytest
from flask import url_for
from common.models import posts_model
from dotenv import load_dotenv, find_dotenv
from blog import create_app
from blog.posts.service import posts_blog_service


@pytest.fixture(scope='session', autouse=True)
def load_env():
load_dotenv(find_dotenv())

@pytest.fixture(scope="module")
def test_client():
app_config = environ.get("APP_CONFIG")
test_app = create_app(app_config)
with test_app.test_client() as testing_client:
yield testing_client

@pytest.fixture(scope="module")
def test_context():
app_config = environ.get("APP_CONFIG")
test_app = create_app(app_config)
with test_app.app_context() as testing_context:
yield testing_context

def test_serialize(test_client):
post_db_obj = posts_model.Posts(content="test content",
posted_date = datetime.datetime.now(),
title = "test title",
author = "test author"
)
serialized_obj = posts_blog_service.serialize(post_db_obj)
test_stub = {
"content": "test content",
"posted_date": datetime.datetime.now().strftime('%B %d, %Y'),
"title": "test title",
"author": "test author"
}

assert test_stub == serialized_obj

def test_get_posts_html_resp(test_context):
test_stub_items = list()
test_stub = {
"content": "test content",
"posted_date": datetime.datetime.now().strftime('%B %d, %Y'),
"title": "test title",
"author": "test author"
}
test_stub_items.append(test_stub)
print(test_stub)
test_html_str = "<a href=" + "/post/" + test_stub_items[0]["title"] + ">" + "<h2 class='post-title'>" + test_stub_items[0]["title"] + \
"</h2></a>" + "<p class='post-meta'>Posted by " + "<a href='#'>" + \
test_stub_items[0]["author"] + "</a> on " + test_stub_items[0]["posted_date"] + "</p></div><hr>"
html_resp = posts_blog_service.get_posts_html_resp(test_stub_items, 10, None)
print(test_html_str)
print(html_resp)

assert test_html_str == html_resp

0 comments on commit d1f4d1a

Please sign in to comment.