This is the 11th day of my participation in the August More Text Challenge.

Hey, it’s Milo. Company three, please! Beg attention test development pit goods!

review

In the last post, we have almost finished dealing with the test report. In fact, the blogger has been thinking about how to optimize the use case writing/running, because the previous writing is too hot. Use cases still cannot be completely attached to the project, and they have to have their own way to survive.

I decided to split the project and the use case on the page, but still keep it as a tree. We don’t have a good UI presentation yet, and we’ll have to work on it over the weekend if we do, so we’ll work on the database related data constructors first.


Why are we doing this?

As we know, in the interface test, we often use the data of the database for assertion operations or query data. Some students may not go to the DB for verification. Let’s leave this controversy aside, but I support the data verification in the DB.

So to operate DB, you have to have some db connection information. I politely decline to support Oracle because I have never used it.

Select a common relational database: Mysql+Postgresql. Not only do we have SQL, we also support Redis.

This section

  • Design the core information stored in the data table
  • The database has been configuredAdd and deletefunction

Design the database configuration table

Start by giving it a name: pity_database_info, so you can see ata glance that it is the database configuration related table.

  • id

  • env

  • Sql_type (database type, 0: mysql 1:postgres)

  • Name (name of the database configuration, for example, order table)

  • host

  • port

  • username

  • password

  • database

  • create_user

  • update_user

  • created_at

  • updated_at

  • deleted_at

    Basically, we can connect to our database through host:port/database@username:password. If we can connect to the database, we are afraid that we can not operate the database?

Write DbConfigDao. Py

First of all, we can write the query function, according to the env environment,name alias,database database name three dimensions to query, as for the follow-up need to host, to be determined, to add is also very convenient.

Note that whether we switch from Flask to FastApi, or whether we switch from synchronous to asynchronous, it does take a process. But don’t panic, for us asynchrony is an accelerated process.

The key is to master the writing method of async/await. After removing these two keywords, have you found that asynchronous and synchronous codes are basically the same? So let’s get over the fact that asynchrony is definitely the future of Python.

Query is our query condition. If name exists, we use it as our query condition. Simple explanation: if name

In Python, if the object after an if is not a bool is automatically escaped, including while.

For example, the familiar while 1: can keep looping because 1 is escaped to True and the effect is the same as while True:, which is relatively simple, but there are some pitfalls.

After selecting the query criteria for the object, we call the session.execute method to find the data and return it.

If we encounter an exception, we throw it directly to the Router.

Write new/delete/modify functions

The important thing to note is that we use async with session.begin() and continue within the block.

I haven’t fully researched it yet, but I know it needs to be used this way. After the begin method ends, the database changes will be automatically committed, but not sure if this is the case.

Write the Router

Here I encapsulate the specific Response so that I don’t have to keep writing dict(code=0, MSG =”success”) returns.

Test the

This is not the end of the story. At present, we can only manage the database configuration, but not use it.

The next article will show you how to execute and return SQL online and use it in case data constructors. It’s going to be long, and I need to do some of the front-end stuff.


That’s all for today. Thank you for watching. If you’re interested, you can join me. I will also give a working version as soon as possible.