Skip to content

Instantly share code, notes, and snippets.

View agusrichard's full-sized avatar
🏠
Working from home

Agus Richard Lubis agusrichard

🏠
Working from home
View GitHub Profile
@agusrichard
agusrichard / main.py
Last active July 18, 2021 05:36
Hello World API
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get('/hello')
def hello():
return 'Hello World'
if __name__ == '__main__':
type tweetHandlerSuite struct {
// we need this to use the suite functionalities from testify
suite.Suite
// the mocked version of the usecase
usecase *mocks.TweetUsecase
// the functionalities we need to test
handler TweetHandler
// testing server to be used the handler
testingServer *httptest.Server
}
type tweetHandler struct {
tweetUsecase usecases.TweetUsecase
}
type TweetHandler interface {
CreateTweet(ctx *gin.Context) *entities.AppResult
// ... another methods
}
func InitializeTweetHandler(usecase usecases.TweetUsecase) TweetHandler {
type tweetUsecase struct {
tweetRepository repositories.TweetRepository
}
type TweetUsecase interface {
CreateTweet(tweet *entities.Tweet) error
// ... other methods
}
func InitializeTweetUsecase(repository repositories.TweetRepository) TweetUsecase {
type tweetUsecaseSuite struct {
// we need this to use the suite functionalities from testify
suite.Suite
// the generated mocked version of our repository
repository *mocks.TweetRepository
// the functionalities we want to test
usecase TweetUsecase
}
func (suite *tweetUsecaseSuite) SetupTest() {
// TweetRepository is an autogenerated mock type for the TweetRepository type
type TweetRepository struct {
mock.Mock
}
// CreateTweet provides a mock function with given fields: tweet
func (_m *TweetRepository) CreateTweet(tweet *entities.Tweet) error {
ret := _m.Called(tweet)
var r0 error
type tweetRepository struct {
db *sqlx.DB
}
type TweetRepository interface {
CreateTweet(tweet *entities.Tweet) error
// ... another methods for tweet repository
}
func InitializeTweetRepository(db *sqlx.DB) TweetRepository {
type tweetRepositorySuite struct {
// we need this to use the suite functionalities from testify
suite.Suite
// the funcionalities we need to test
repository TweetRepository
// some helper function to clean-up any used tables
cleanupExecutor utils.TruncateTableExecutor
}
func (suite *tweetRepositorySuite) SetupSuite() {
<html>
<body style="margin: 0; padding: 0; box-sizing: border-box; font-family: Arial, Helvetica, sans-serif;">
<div style="width: 100%; background: #efefef; border-radius: 10px; padding: 10px;">
<div style="margin: 0 auto; width: 90%; text-align: center;">
<h1 style="background-color: rgba(0, 53, 102, 1); padding: 5px 10px; border-radius: 5px; color: white;">{{ body.title }}</h1>
<div style="margin: 30px auto; background: white; width: 40%; border-radius: 10px; padding: 50px; text-align: center;">
<h3 style="margin-bottom: 100px; font-size: 24px;">{{ body.name }}!</h3>
<p style="margin-bottom: 30px;">Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi, doloremque.</p>
<a style="display: block; margin: 0 auto; border: none; background-color: rgba(255, 214, 10, 1); color: white; width: 200px; line-height: 24px; padding: 10px; font-size: 24px; border-radius: 10px; cursor: pointer; text-decoration: none;"
href="https://fastapi.tiangolo.com/"
@todo_blueprint.route('/update/<int:todo_id>', methods=['POST'])
@token_required
def update_todo(todo_id):
try:
user_data = session['user_data']
client = TodoClient()
result_update = client.update_todo(todo_id, request.form['title'], request.form['description'])
result_get = client.get_todo(todo_id, user_data.get('ID'))
data = json.loads(result_get.data)
return render_template('todo/item.html', todo=data)