Adding `skip()` and `only()` features to unittest

If you are like me, a python newbie just moved from web front-end world, you must have used Mocha before, the most commonly used test framework made by the genius TJ.

skip() and only() are the two most useful features for me among all the tools that Mocha provides. So when I started to write test cases in Python using Unittest, I felt so frustrated, because there’s nothing that can do the same as the skip() and only() did for me.

So I am going to teach you how to add these two features to unittest-testing in this article, but before we get there, I want to talk more about why these two features are important to your development.

Continue reading →

Using raw SQL with Flask-SQLAlchemy

This week I was using Flask-Migrate to do a database data migration. I created an empty reversion file and put my migrate script in it so that the migrate can be recorded just like the other reversion files.

The only thing special about writing a migration script is you need to use raw SQL to do the database manipulation instead of using ORM model object from Flask-SQLAlchemy. The reason is that maybe at the time your script is written, your model definition in your application reflects your actual database structure, but it may change over time, and once that happens, your script may not work correctly anymore.

This is one thing that I think is quite important when you are doing data migration with Flask-Migrate.

Continue reading →

Responsive Master-Detail Layout in React

I am using React with React-Router to make a SPA(Single Page Application) recently. In my setting page, I adopted the typical Master-Detail layout.

It’s quite easy if you only need to make it work on desktop devices, but for small devices, you need to put extra effort into it.

This article describes how I implement the Master-Detail Layout which works well on both desktop and mobile. If you just want to see the end result, check out the demo project.

Continue reading →

How to avoid preflight OPTION request in CROS request

When sending CROS Ajax request, sometimes you may found that before every request you make, there will be a OPTION request sent.

It’s quite annoying and slows down your data request since the actually request has to wait the OPTION request to finish.

According to the CORS strategy (highly recommend you read about it) You can’t just force the browser to stop sending OPTION request if it is necessary.

There’s two way you can work around it

  1. Make sure your request is a “Simple Request”
  2. Set Access-Control-Max-Age for the OPTION request

Continue reading →

Implementing responsive design in React

Two kinds of responsive design

There’s two type of responsive design:

One is just adjusting an element’s appearance in response to different media types, like hiding or showing some elements, change size, color or layout.

Another way will be using the diffent design or totally different component for different media types. For example, a complex table view with lots of fields for each row, for a large screen, just show a regular table, but when the screen shrinks, the table view will be replaced with a card list, within each card, all the fields will be rendered vertically.

Continue reading →