In this blog, we will talk about how to add dredd hooks for testing the API of Event Image Sizes and Speaker Image Sizes in Open Event Server. The focus is on adding the factory class and dredd hooks of these APIs using factory-boy python library and Dredd API testing framework.
Factory Creation
For the Event and Speaker Image Sizes, we’ll make our factory classes EventImageSizeFactory and SpeakerImageSizeFactory as follows
Now, let’s try to understand this class.
In this class, we are writing the sample data two records of ImageSizes Model, these records corresponds to Event and Speaker Image Sizes.
- First of all, we inherit class factory.alchemy.SQLAlchemyModelFactory to build our sample data which for Image Sizes.
- Class Meta has model and sqlalchemy_session attributes. Model tells the factory class of to which model this factory class push the data to database and sqlalchemy_session is assigned with the current database session.
- Next, we add the attributes according to the model and Schema of Image Sizes.
Adding Dredd Hooks
For the ImageSizes, we’ll make our dredd hooks as follows
Now, let’s try to understand these tests.
In this tests, we check the API by matching the response after adding a record in these API to one which is present at API blueprint.
- First of all, we use decorator @hooks.before which means we first add a record in the database and then match the response we get from API say /v1/event-image-sizes with the response mentioned at Image Size > Event Image Size Details > Get Event Image Size Details in API blueprint.
- We create an instance of EventImageSizeFactory which is a record of model Image Sizes.
- This record is then returned as a response of API /v1/event-image-sizes and matches with the blueprint at Image Size > Event Image Size Details > Get Event Image Size Details
Similarly, we have added other dredd tests for PATCH method as well.
So, we saw how factory-boy python library and Dredd API testing framework helped us in testing the REST APIs on Open Event Server.
Resources
- Documentation | Dredd API testing framework: http://dredd.org/en/latest/
- Documentation | Factory-boy: http://factoryboy.readthedocs.io/en/latest/