Child pages
  • Туториал по написанию теста API

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Эта страница документации не завершена.

Table of Contents

Старт

Для работы API тестов нужно заполнить 2 конфигурационные переменные:

...

Опционально можете заполнить $conf{API_TEST_URL} - URL для тестов. По умолчанию это значение localhost.

Модуль

За пример взят модуль Portal.
Расположение Abills/modules/Portal

...

  • articles_list        - GET /portal/articles
  • articles_info       - GET /portal/articles/:id/
  • article_add         - POST /portal/articles
  • articles_update - PUT /portal/articles/:id/
  • article_delete   - DELETE /portal/articles/:id/

И так далее.

Ядро

За пример взят микромодуль Companies.

...

  • companies                 - GET /companies
  • company                     - GET /companies/:id/
  • company_add           - POST /companies
  • companies_change - PUT /companies/:id/
  • company_delete      - DELETE /companies/:id/

Написание Api.t

будет дополняться

Создание реквеста и json-schema

И в каждом эндпоинте создаём файлы:

...

Про json-schema почитать тут.

Create

Code Block
languagejs
titlePortal/t/schemas/admin/articles_add/request.json
collapsetrue
{
   "body": {
      "addressFlat": "4",
      "archive": 1,
      "buildId": 8,
      "content": "<p>Новая Open Source версия биллинга!</p>\n\n\n\n<p>Полный список новинок, исправлений и улучшений биллинга к новому релизу!</p>",
      "date": "2023-01-27",
      "districtId": 8,
      "domainId": 0,
      "endDate": "",
      "gid": 0,
      "importance": 0,
      "name": "Releases",
      "onMainPage": 0,
      "permalink": "releases-abills-095-blackout",
      "picture": "https://demo.abills.net.ua:9443/images/attach/portal/13863233.jpg",
      "portalMenuId": 8,
      "shortDescription": "Встречайте новый релиз 2023",
      "stName": "",
      "status": 1,
      "streetId": 0,
      "tagName": "",
      "tags": 0,
      "title": "Релиз ABillS 0.95 Blackout"
   },
   "method": "POST",
   "name": "ADMIN_PORTAL_ARTICLES_ADD",
   "path": "portal/articles/"
}


Code Block
languagejs
titlePortal/t/schemas/admin/articles_add/schema.json
collapsetrue
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "affected": {
      "type": "integer"
    },
    "insertId": {
      "type": "integer"
    },
    "total": {
      "type": "integer"
    }
  },
  "required": ["affected", "insertId", "total"]
}

Read

Code Block
languagejs
titlePortal/t/schemas/admin/articles_add/request.json
collapsetrue
{
   "method": "GET",
   "name": "ADMIN_PORTAL_ARTICLES_LIST",
   "path": "portal/articles/"
}
 


Code Block
languagejs
titlePortal/t/schemas/admin/articles_add/schema.json
collapsetrue
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "list": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "archive": {
            "type": "integer"
          },
          "addressFlat": {
            "type": "string"
          },
          "buildId": {
            "type": "number"
          },
          "content": {
            "type": "string"
          },
          "date": {
            "type": "string"
          },
          "deeplink": {
            "type": "integer"
          },
          "districtId": {
            "type": "string"
          },
          "domainId": {
            "type": "integer"
          },
          "endDate": {
            "type": "string"
          },
          "etimestamp": {
            "type": "integer"
          },
          "gid": {
            "type": "integer"
          },
          "importance": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "onMainPage": {
            "type": "integer"
          },
          "permalink": {
            "type": "string"
          },
          "picture": {
            "type": "string"
          },
          "portalMenuId": {
            "type": "integer"
          },
          "shortDescription": {
            "type": "string"
          },
          "stName": {
            "type": "string"
          },
          "status": {
            "type": "integer"
          },
          "streetId": {
            "type": "integer"
          },
          "tagName": {
            "type": "string"
          },
          "tags": {
            "type": "integer"
          },
          "title": {
            "type": "string"
          },
          "url": {
            "type": "string"
          },
          "utimestamp": {
            "type": "integer"
          }
        },
        "required": [
          "id",
          "archive",
          "addressFlat",
          "buildId",
          "content",
          "date",
          "deeplink",
          "districtId",
          "domainId",
          "endDate",
          "etimestamp",
          "gid",
          "importance",
          "name",
          "onMainPage",
          "permalink",
          "picture",
          "portalMenuId",
          "shortDescription",
          "stName",
          "status",
          "streetId",
          "tagName",
          "tags",
          "title",
          "url",
          "utimestamp"
        ]
      }
    },
    "total": {
      "type": "number"
    }
  },
  "required": [
    "list",
    "total"
  ]
}


Update

Code Block
languagejs
titlePortal/t/schemas/admin/articles_add/request.json
collapsetrue
{
   "body": {
      "addressFlat": "4",
      "archive": 1,
      "buildId": 8,
      "content": "<p>Новая Open Source версия биллинга!</p>\n\n\n\n<p>Полный список новинок, исправлений и улучшений биллинга к новому релизу!</p>",
      "date": "2023-01-27",
      "districtId": 8,
      "domainId": 0,
      "endDate": "",
      "gid": 0,
      "importance": 0,
      "name": "Releases",
      "onMainPage": 0,
      "permalink": "releases-abills-095-blackout",
      "picture": "https://demo.abills.net.ua:9443/images/attach/portal/13863233.jpg",
      "portalMenuId": 8,
      "shortDescription": "Встречайте новый релиз 2023",
      "stName": "",
      "status": 1,
      "streetId": 0,
      "tagName": "",
      "tags": 0,
      "title": "Релиз ABillS 0.95 Blackout"
   },
   "method": "PUT",
   "name": "ADMIN_PORTAL_ARTICLE_UPDATE",
   "path": "portal/articles/:id/"
}
  


Code Block
languagejs
titlePortal/t/schemas/admin/articles_add/schema.json
collapsetrue
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "affected": {
      "type": "integer"
    },
    "total": {
      "type": "integer"
    }
  },
  "required": ["affected", "total"]
}

Delete

Code Block
languagejs
titlePortal/t/schemas/admin/articles_add/request.json
collapsetrue
{
   "method": "DELETE",
   "name": "ADMIN_PORTAL_ARTICLE_DELETE",
   "path": "portal/articles/:id/"
}
  


Code Block
languagejs
titlePortal/t/schemas/admin/articles_add/schema.json
collapsetrue
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "result": {
      "type": "string"
    }
  },
  "required": [
    "result"
  ]
}

Запуск теста

Для запуска теста на ваш модуль - запускайте созданный вами Api.t.

Практики

  1. Для генерации request.json используйте ChatGPT - это сэкономит вам время.
    Отправляйте ему OpenAPI вашего пути, и он сгенерирует нужный вам schema.json.