Test platform series (31) Writing environment configuration page (1)

Hi, this is Milo, a blogger who would like to share his test development techniques, interview tips and growing experiences with you!

Welcome everyone to pay attention to my public number: test development pit goods.

Write the environment management page

As mentioned in the previous section, use cases are not classified according to their environment, which is not very reasonable. After all, different environments may require different data, and it is not necessarily possible to write a completely generic use case. So today I’m going to start with this page and teach you how to write a full CRUD feature (with front-end + synth). The back end, the front end, if you’re not interested, just the back end. Because there will be a series of screenshots! That half hour is definitely gone, not to mention half dead.

Some people may not understand, the environment is not that several sets? Testing/pre-delivery/online etc. Yes, but companies call it different names, so in order to make a tool that is common and universal, we give them the ability to maintain their own environment.

CRUD/CURD is what

Those of you who go to Mai Mai often know that you often see words like CURD CRUD CRUD boy. To put it bluntly, curd means add, delete, change, check, and that’s what we’re going to be talking about today.

  • increase

    The company recently opened a new environment: FAT2, and our system has to be maintained, so we need to add a new data of FAT2 environment.

  • delete

    The company doesn’t use this environment anymore and wants to get rid of it so as not to confuse the public.

  • change

    Fat2 Environmental didn’t like it and decided to change it to Fat-beta.

  • check

    The company now has more than 100 kinds of environment, there are too many, we need to check whether fat77 environment exists.


Yes, a lot of developers do something close to that. I spent most of my time doing CRUD work. The above is detailed to our environmental management this time function points, if you feel difficult, patience to read (copy) you will not feel difficult.

A clear train of thought

First of all, we need to understand this requirement, in fact, the requirement is very simple, is to provide users with the ability to customize the environment name. So we don’t need to store information about these environments in the database. What is the basic information needed? Let me list it here:

Data table preparation

Core fields:

  • Name of the environment

For example, Fat UAT Pro (can I repeat it? That must not be repeated!

  • Environmental note

For example, fat is the test environment of whatever, in fact, you can not, but give you the option, in case you need to add some instructions

Basic fields:

  • Who is the founder
  • Who is the modifier
  • When was it created
  • When was it updated
  • Has it been deleted

Functional Requirements preparation

  • The environment can be obfuscated by name

  • Can edit, edit will update the editing time

  • You can add

  • You can delete

    The function of a split, is not particularly simple, in fact, the demand is not much, the back-end may be less than 1 hour to get it done, the front end is time-consuming.

Taking action

Define the model

Since the table does not have many fields and the data is not complex, we can define the Model first.

The content is not difficult, and getting familiar with SQLAlchemy is even easier. The purpose is to map a data table (environment table) class.

Tablename specifies the name of the database table corresponding to the current class. What follows is the definition of the field and the initialization of the class.

To generate the model

To generate tables automatically, we need to import the Model class in DAO /init.py.

Define the schema for Pydantic

The purpose of this step is very simple, is to do parameter verification, for example, some bad friends, he created the environment, directly give you an empty string or space string environment name, so we encounter such a person, we should despise him, before the data inserted into the database detection, and give him a heavy blow. If you are not familiar with Pydantic, please read the official pydantic document below or copy it.

An Environment form has three parameters: ID, name, and Remarks. These parameters are id, Environment name, and Remarks. Name can’t be empty, you can write the remarks, you can write it or not, you can pass the id, if you don’t pass it, it defaults to None.

Writing the Dao layer (the core of db interaction)

  • The new environment

Since the environment is also a set of contents in the configuration, it will be put together with global variables later, so we archive it in the config directory.

Let’s see what this new environment does.

First fixed collocation, this DDDD (understand all understand). Retrieve a session from the database, similar to db.cursor() and similar to getConnection in JDBC.

with Session() as session:
Copy the code

Select * from deleted_at=None (deleted_at=None); select * from deleted_at=None (deleted_at=None);

Otherwise, we insert the data into the database and commit it in session. If an exception occurs midway, we should log the log of the log, the return error of the return error. If nothing is wrong, return None, representing good luck and good luck.

  • Editing environment

The formula is the same, even the code is similar. But notice one difference: the update_model method is called. What does this method do?

As we know, to update a data, in addition to the fields that need to be changed, we also need to modify the two fields of update and update time. So we wrote a generic method that takes the data you pass in, changes it to a Query, and changes the update time and the update person.

Not_null means that only non-empty fields are updated. For example, if I had a data ABC and you passed None, I changed it to None. This takes a page from GORM and updates only non-NULL fields. Dist represents the target and source represents the data source.

  • Delete the environment

In fact, delete environment, we are doing soft delete, so it is still an update operation. We find the corresponding data by id, change its deleted_at to the current time, by the way change the update person (leave criminal evidence).

  • Query environment

Note that I’m using the name list_env, why not search? It’s up to you, but I’m actually combining list and search. As you can see, my name argument is a search action if it’s not None, otherwise it’s a list operation.

The basic condition for search is deleted_at == None, which means that no matter what, we have to make sure that the data is not deleted.

if name:
    search.append(Environment.name.ilike("% {} %".format(name)))
Copy the code

If name is passed, or is not “” or None, then we add a fuzzy query for name, equivalent to where name ilike XXXXX.

And then finally, automatic paging, because we have a lot of data, we can’t show it all, so we need to do paging. Page and size refer to the current page number and the number of pieces of data on a page.

Finally, we do reverse order by created_at, which means that the most recent creation comes first. Return a total, which is how many pieces of data were found in this query, so that the front end can display them.

Write the router

The router layer is very simple, through ApiRouter new routing, and put the list/insert/update/delete registered to the routing method. As you can see, basically there is very little code, which belongs to the core method of calling the DAO layer and returns HTTP response according to the return result.

Note that I’m not using restful apis to write interfaces, so udpate/ INSERT are post requests and others are GET requests. (No put delete)

Register the router

Finally, register the router you just wrote with app(pity).

Reveal the plot effect:

So, that’s the end of this episode.

The online demo is at http://47.112.32.195/

Front-end repository: github.com/wuranxu/pit…

Backend repository: github.com/wuranxu/pit…