Json
Warning
The to_json
method has been deprecated and will be removed
in a future releases. Use to_dict instead
Generating dynamic json object based on model. This is one of the most powerful feature of using models
Generating json based on model
Let's imagine that we have a user object, and we need to get this object with random values each time
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"username": "some random username",
"email": "some random email"
}
So let's create a model which will generate us json above
Note
Id should be dynamic value. Usually when we write auto tests, we use dynamic values. Just like our API can't always accept static values, and vice versa they must be unique each time
import uuid
from models_manager import Model, Field
from models_manager.utils import random_string
class User(Model):
id = Field(default=uuid.uuid4, json='id')
username = Field(default=random_string, json='username')
email = Field(default=random_string, json='email')
Let's see what's going on here
import uuid
from models_manager.utils import random_string
callable(uuid.uuid4) # True
callable(random_string) # True
callable(uuid.uuid4()) # False
callable(random_string()) # False
In this case, uuid4, random_string are callable objects. When generating json, these objects will be called and return values will be obtained from them
Let's try to use our user model and generate random json
import uuid
from models_manager import Model, Field
from models_manager.utils import random_string
class User(Model):
id = Field(default=uuid.uuid4, json='id')
username = Field(default=random_string, json='username')
email = Field(default=random_string, json='email')
User.manager.to_json
{
'id': '992ae8a2-ab6d-4d16-a7fd-e009bdcbff4c',
'username': 'P8aNC414bqfSZEyUVpgZZQt',
'email': 'UDxG3bKhvlLuM8n1LFCtfbZqwlKihogkL6BM8gfVIVk28'
}
User.manager.to_json
{
'id': 'b3a02f98-7691-4a30-a42d-a1a3c600b584',
'username': 'XUzl4QLP5mENis0tadeqacSSHf0vwaySeGQgV7S64R2M',
'email': 'h1OnsjE5s1UXwTeEpuB5GbPPLF8'
}
Now we get random json values every time we call the to_json
method
Generating negative json based on model
Let's now imagine that we need to generate a negative json to test for an error in our API
Note
To generate negative json, we need to describe the fields in our model in more detail. This is necessary to more accurately generate negative json
Now we will extend our model and add new field attributes to it.
Imagine that the business logic of our product requests:
id
not nullusername
should be up to 100 symbols, might be nullemail
should be up to 70 symbols, not null
Let's describe it in our model
import uuid
from models_manager import Model, Field
from models_manager.utils import random_string
class User(Model):
id = Field(default=uuid.uuid4, json='id', null=False)
username = Field(default=random_string, json='username', max_length=100, null=True)
email = Field(default=random_string, json='email', max_length=70, null=False)
Now let's try to generate negative json based on the model
import uuid
from models_manager import Model, Field
from models_manager.utils import random_string
class User(Model):
id = Field(default=uuid.uuid4, json='id', null=False)
username = Field(default=random_string, json='username', max_length=100, null=True)
email = Field(default=random_string, json='email', max_length=70, null=False)
User.manager.to_negative_json()
{
'id': None,
'username': 'DV3CTqGsrXy95cXtCYniEE0BARnvmZTru0jhRFjEg7c4HZEsDHqo8TxkJbIh5FalDWNV2Cknu4OoeQ9CWyymfYvPUp9sXCvgbm13Eq1opfsVjiwbeIhTZo4gOyIbXDtbpg6vMHwuY1N',
'email': None
}
Based on the business logic we described in the model, we generated a negative json that we can use in our test to test the negative scenario for the endpoint API
Overriding negative json provider
You can make your own negative json generator. For that you have to override default negative json provider class. More info here