Skip to content

API, Back End and Back End Tests

Nikki Babaii edited this page Apr 22, 2022 · 1 revision

Migrations

Treatments Schema

id is the treatment id

patient_id is the foreign id of the user who has been assigned treatment

title is the title of the treatment

notes are any notes belonging to a treatment.

start_date is the start date of the treatment

end_date is the end date of the treatment

start_time is the start time of the treatment

end_time is the end time of the treatment

is_completed is whether or not the treatment is completed by the patient, and is set to false

parent_id is used to assign any treatments to a parent treatment using the self reference trait of the Treatment model

Rules Schema

id is the id of rules

treatment_id is the foreign id that reference the id on the treatment table to which this rule belongs

freq is the frequency of the recurrence of a treatment, there are currently 4 types, yearly, monthly, weekly, and daily

interval is the gap needed in the frequency of the recurrence dates

max_num_of_occurrences is the count of the dates in the array of the occurrences of all dates

day_of_week is the day of the week, which is in json, but is cast as an array in the model to allow for all days, like Mo, Tu, We, and so on.

week_of_month is the number of the week when the dates should recur.

day_of_month is the date of the month when the dates should recur

month_of_year is the month of the year when the dates should recur

Note: This schema handles the rules needed to generate the dates for a recurring treatment using the rlanvin/php-rrule library that follows the RFC 5545 International Calendaring and Scheduling Core Object Specification. Please refer to rlanvin/php-rrule library’s RRule Creation wiki page for further explanations. Its corresponding values are provided in the comments within the schema

Models

Treatment

This is a model for the treatments schema in migrations and it has a factory trait (Treatment Factory), refresh database that allows you to use the test database as enabled in phpunit.xml for TreatmentTest in Feature and Unit Tests.

path

It allows to define the path in the model for the specific id of the treatment.

patient

This defines the treatment model relationship with the user model. Please see the Laravel’s ORM, Eloquent, to define relationships and their types.

rule

This defines the treatment model relationship with the rule model. A treatment has only one rule, or recurrence rule.

Rule

This is the model for the rules schema and we cast the day_of_week into arrays in the model. Rules also has a factory trait that allows you to create a dummy data.

treatments

This describes the relationship of the rules with the treatment. A rule belongs to a treatment.

Traits

Self-Reference Trait

Allows you to use the parent_id schema to assign a treatment as a parent to another treatment and use this relationship to call out a

$treatment->parent() or

$treatment->children()

Controllers

Treatment Controller

This defines the traditional methods for the controller that define the actions for the api resource for treatments.

Index

An authenticated user can get the access of the treatments assigned to them.

Store

It takes a post request in Json and an authenticated user can create treatments, when validated. The validation rules can be found in rules of Treatment Request, which is an extends the generic Request.

Show

It returns the response for a specific treatment model.

Destroy

The authenticated user’s treatment can be deleted.

Rules Controller

Rules Controller has 4 traditional methods defined for the controller namely, index, store, destroy and update

Index

You get the rule associated with a treatment.

Store

You can store a rule for a specific treatment. If the request payload is missing the end_date, we assign a max_num_of_occurrences or a count to the rules, to avoid an infinite occurrence of dates. This could be handled more formally.

Destroy

We delete a specific rule

Update

We update the specific rule

Request

Treatment Request

Allows the authorized users and validated the request as per the rules.

Service

Rules Service

This service parses the dates for a given rule model to construct this service, dependency is established using Laravel’s reflection.

Its corresponding test is RulesParsingTest in feature test to see how it works, and also the results of the dates recurrence.

Factory

Treatment Factory

This returns a dummy treatment for the treatment model

Rules Factory

We define a scope for the factory methods to generate 4 different types of rules, for each kind of recurrence, namely, yearly, monthly, weekly, and daily.

Tests

Feature Tests

Treatment Test

setup

Creates a fake authenticated user using Sanctum and creates a fake Treatment data using Treatment Factory

createTreatment

Allows the factory to create a Treatment for a given arguments

get_all_treatments

Allows you to create a treatment and get it through the treatment index route and asserts that there is a count of 1 in the response json and it contains the title of the treatment.

get_single_treatment

Allows you to test to get a specific treatment given an id, and asserts that the response json has the title of the treatment.

store_treatment

Allows you to make a temporary treatment and post it to the route treatments store and assert its creation, and assert that the response has the same values for the treatment made. It also tests that the database has the same values.

attributes_requires_while_storing_treatment

It allows you to test that the required parameters title and start date were passed in the treatments response and if not, it asserts the request is unprocessable.

delete_treatment

It tests to delete a specific treatment and asserts there is no content and the same is missing in the database.

update_treatment

It uses the route treatments update to patch or update data for a specific treatment and asserts if the request is ok. It also asserts If the database has these values.

while_updating_treatment_attributes_are_required

This tests whether the treatments update do not remove the required attributes and validates them and makes them unprocessable if the request doesn’t have one.

Rules Test

We test all the controller methods of the rules controller. The method names are self-explanatory for the tests it belongs to.

Rules Parsing Test

The method names in the tests are self-explanatory, namely, we tests to get all types of rules yearly, monthly, weekly, and daily. Note: It is faster to generate yearly recurrence than daily recurrence.

Treatment Completed Test

This tests allows you to check if you can update a treatment, specifically, the is_completed feature.

Unit Tests

Treatment Test

It_has_a_path

Allows you to test that the specific treatment created has a path

test_a_treatment_has_a_rule

Creates a yearly rule for a treatment id and checks the relationship exists.

If_treatment_is_deleted_then_rule_is_deleted

Allows you to test if a treatment is deleted then the rule associated with it is also deleted and is missing in the database, but not the other rules.

Rules Test

We test the relationship of the rule and the treatment

User Test

We test whether the user has many treatments.

API ROUTES

Api.php

It has the treatments routes in api resources. The full extended route list for treatments and rules could be accessed from the console command php artisan route:list with the controller methods that define the action.

The rules api resource has shallow that means that in order to delete a rule or update a rule, you do not need the treatments in your route, as you can only get a rule, if you have provided a treatment, so we don’t need to do it twice. This is also visible through the route list console command.