Skip to content

11. Unit Tests

Chris Nurse edited this page Aug 9, 2022 · 3 revisions

This section provides basic information about the unit tests and the data used during testing.

Unit Test Folder

/test/unit folder contains a collection of Jest test specifications to test the command line interface, data creation, and the (coming soon...) script execution engine.

Test Data Model

/datamodel folder contains the JSON files that make up a data model. At the moment we call the main file datamodel.json, but the CLI allows you to use any filename as the root of the data model.

File Purpose
datamodel.json Defines the demonstration data model, as a JSON object. In here, you will find descriptions of the data objects (organisation, client, etc.)
iot.json Include this data model to populate your database with IoT devices and related data
base.json Includes all of the base data model components include the base entity, snippets and generators
base_entity.json Contains the base entity from which all other entities should be based (use - "inhert": "Entity" on your entity definitions)
base_generators.json Contains a basic set of generators for various data such as people's names, places, times and dates
base_snippets.json Contains pre-defined fields which can be used to rapidly build entities

NPM scripts

  • Open the project with Visual Studio code.
  • Look at the NPM scripts in package.json
  • test : Run the unit tests
  • build : Compile the typescript
  • start_cli : Test the command line function
  • up : Use docker-compose to build the demo containers (you must run this before you can run the tests)
  • down : Stop the docker containers
  • remove : Stop and remove the docker containers and related images

Review the Unit Tests

It's really cool to be able to create complete test database systems from the command line. But the Crudio Github repository provides you with the full code and unit tests. You can now understand how you may completely customise the test data, and even integrate Crudio with a different database, other than Postgres, my writing your own CrudioDataWrapper, to persist the data model to your own database type.

Maybe you want to become a contributor? Please reach out and participate.

You can see how simple Crudio is to use, by looking at the unit tests in test/unit.

Here is what the key unit tests do:

  • Test flatted - This test proves that we can save JSON data which may contain circular references. JSON.stringify doesn't work for this purpose, but flatted is perfect!
  • Ensure that Crudio has the ability to create unique values for fields. This is important if you want everyone to have a unique email address that you might use to login later.
  • Load the data model - This loads the datamodel/datamodel.json file which describes a demo data model, and it uses it to create a large in-memory JSON object, and then it tests the object contains the right data.
  • Save and load - This ensures that we can save and load the JSON data. We might want to do this in order to work with a snapshot of data, whereby our data looks the same every time we load it. If we just run Crudio every time we get new random data.
  • Populate database - This is the super power. Crudio creates a database schema in Postgres called crudio, creates all the tables, loads the test data in to the database, then adds all of the foreign keys that connect the data together.

When the tests have run, open the Hasura Console, go to the DATA tab, and track the tables and relationships, and you're ready to go with GraphQL.

NOTE: The unit tests create the crudio_test schema to keep the data model isolated from your own crudio schema, which is what the CLI will use by default.