How to write tests

Jam.py is using Mocha/Chai for front-end unit tests and pytest for dataset integration testing. The examples are in tests folder.

First, start with forking Jam.py-v7.

Next, clone your fork:

...\> git clone https://github.com/YourGitHubName/jam-py-v7.git
...\> cd jam-py-v7/tests/project

To add a new test for the front-end, add JavaScript file into project/js folder. Let’s say we want to test user table with CRUD, using one field called username.

The project folder has the below structure:

├── admin.sqlite
├── css
│   └── project.css
├── index.html
├── js
│   ├── test_dataset.js
│   ├── test_details.js
│   ├── test_edit_lock.js
│   ├── test_fields.js
│   ├── test.js
│   └── test_locale.js
├── langs.sqlite
├── server.py
├── templates.html
├── test.html
├── test.sqlite
└── wsgi.py

Start the project as usual:

...\> ./server.py

Add table Users with a field name Username on builder, ie. visiting:

127.0.0.1:8080/builder.html

Add to index.html new file:

<script src="js/test_users.js"></script>

The index.html might look like below:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Jam.py tests</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/6.1.4/mocha.css">
    </head>
    <body>
        <div id="mocha"></div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/11.7.2/mocha.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/chai@4.3.4/chai.js"></script>
        <script>mocha.setup('bdd')</script>
        <script src="jam/js/jquery.js"></script>
        <script src="jam/js/bs5/bootstrap.bundle.js"></script>
        <script src="jam/js/zebra_datepicker.js"></script>
        <script src="jam/js/jquery.maskedinput.js"></script>
        <script type="module" src="jam/js/jam.js"></script>


        <script src="js/test_dataset.js"></script>
        <script src="js/test_details.js"></script>
        <script src="js/test_fields.js"></script>
        <script src="js/test_edit_lock.js"></script>
        <script src="js/test_locale.js"></script>
        <script src="js/test_users.js"></script>
        <script>
            $(document).ready(function(){
                task.load(function() {
                    mocha.run();
                });
            });
        </script>
    </body>
</html>

Create the file js/test_users.js with the tests and visit the application on:

127.0.0.1:8080/index.html

All unit tests will run and display the results. The database tests.sqlite will be updated with a new user. The DELETED field within the table will be set to 1 for new row, if the table was created with Soft delete option. If not, the new record will be deleted.

If all good to go, create a Github pull request with the changes.