Skip to content

Commit

Permalink
Validate yaml (#75)
Browse files Browse the repository at this point in the history
* add tests to validate DAG yaml

* up airflow to 2.8.1

---------

Co-authored-by: vitor <[email protected]>
  • Loading branch information
vitorbellini and vitor authored Feb 16, 2024
1 parent 6327996 commit 70a45b3
Show file tree
Hide file tree
Showing 22 changed files with 294 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI Tests

on:
push:
branches: [ main ]
branches: [ "*" ]
pull_request:
branches: [ main ]

Expand Down
12 changes: 12 additions & 0 deletions .vscode_edit/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"yaml.schemas": {
// XXX para usar um schema do repo oficial https://github.com/gestaogovbr/Ro-dou:
// "https://raw.githubusercontent.com/gestaogovbr/Ro-dou/main/schemas/ro-dou.json":
// XXX para usar um schema de arquivo local:
"./schemas/ro-dou.json": [
// XXX edit to your DAGs yaml directory
"dag_confs/**/*.yml",
"dag_confs/**/*.yaml"
],
},
}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM apache/airflow:2.7.3-python3.10
FROM apache/airflow:2.8.1-python3.10

USER root

# Copy Ro-dou core files from the host Docker context
COPY src /opt/airflow/dags/ro_dou
COPY src /opt/airflow/dags/ro_dou_src

RUN chown -R airflow /opt/airflow

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ houve algum resultado encontrado na API da Imprensa Nacional para os termos e
demais parâmetros deste clipping. Se a tarefa chamada **"send_report"** estiver
na cor verde significa que houve resultado e que o email foi enviado.

Para visualizar o email acesse o endereço http://localhost:5000/. Este é um
Para visualizar o email acesse o endereço http://localhost:5001/. Este é um
serviço que simula uma caixa de email (servidor SMTP) para fins de
experimentação. **_Voilà!_**. O arquivo de configuração deste Clipping está na
pasta `dag_confs/`. Confira [aqui](https://github.com/gestaogovbr/Ro-dou/blob/main/dag_confs/all_parameters_example.yaml) no Github.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ dag:
- governo aberto
- lei de acesso à informação
field: TUDO
is_exact_search: On
ignore_signature_match: On
force_rematch: On
is_exact_search: True
ignore_signature_match: True
force_rematch: True
date: MES
dou_sections:
- SECAO_1
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ dag:
report:
emails:
- [email protected]
attach_csv: On
attach_csv: True
subject: "[String] com caracteres especiais deve estar entre aspas"
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ dag:
report:
emails:
- [email protected]
attach_csv: On
attach_csv: True
subject: "Exemplo de termos na Variável"

3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
- ./src:/opt/airflow/dags/ro_dou_src # for development purpose
- ./dag_confs:/opt/airflow/dags/ro_dou/dag_confs
- ./tests:/opt/airflow/tests # for test purpose
- ./schemas:/opt/airflow/schemas # for test purpose
depends_on:
postgres:
condition: service_healthy
Expand Down Expand Up @@ -118,7 +119,7 @@
image: rnwood/smtp4dev:v3
restart: always
ports:
- '5000:80'
- '5001:80'
- '25:25' # Change the number before : to the port the SMTP server should be accessible on
- '143:143' # Change the number before : to the port the IMAP server should be accessible on
volumes:
Expand Down
202 changes: 202 additions & 0 deletions schemas/ro-dou.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"dag": {
"type": "object",
"description": "Instanciação da DAG",
"properties": {
"id": {
"type": "string",
"description": "Nome único da DAG"
},
"description": {
"type": "string",
"description": "Descrição da DAG"
},
"tags": {
"type": "array",
"description": "Lista de tags para filtragem da DAG no Airflow",
"items": {
"type": "string"
}
},
"owner": {
"type": "array",
"description": "Lista de owners para filtragem da DAG no Airflow",
"items": {
"type": "string"
}
},
"schedule": {
"type": "string",
"description": "Expressão cron válida ou uma das seguintes strings: @once, @continuous, @hourly, @daily, @weekly, @monthly, @quarterly, @yearly.",
"pattern": "^(\\S+\\s+){4}\\S+$|^@(once|continuous|hourly|daily|weekly|monthly|quarterly|yearly)$"
},
"search": {
"type": "object",
"description": "Seção para definição da busca no Diário",
"properties": {
"sources": {
"type": "array",
"description": "description",
"items": {
"type": "string",
"enum": ["QD", "DOU"]
}
},
"territory_id": {
"type": "integer",
"description": "Id do território no Querido Diário - QD"
},
"terms": {
"oneOf": [
{
"type": "array",
"description": "Lista de termos de busca no Diário",
"items": {
"type": "string"
}
},
{
"type": "object",
"description": "description",
"properties": {
"from_airflow_variable": {
"type": "string",
"description": "description"
},
"from_db_select": {
"type": "object",
"description": "description",
"properties": {
"sql": {
"type": "string",
"description": "description"
},
"conn_id": {
"type": "string",
"description": "description"
}
}
}
}
}
]
},
"field": {
"type": "string",
"description": "description",
"enum": ["TUDO", "TITULO", "CONTEUDO"]
},
"is_exact_search": {
"type": "boolean",
"description": "description"
},
"ignore_signature_match": {
"type": "boolean",
"description": "description"
},
"force_rematch": {
"type": "boolean",
"description": "description"
},
"date": {
"type": "string",
"description": "description",
"enum": [
"DIA",
"SEMANA",
"MES",
"ANO"
]
},
"dou_sections": {
"type": "array",
"description": "description",
"items": {
"type": "string",
"enum": [
"SECAO_1",
"SECAO_2",
"SECAO_3",
"EDICAO_EXTRA",
"EDICAO_EXTRA_1A",
"EDICAO_EXTRA_1B",
"EDICAO_EXTRA_1D",
"EDICAO_EXTRA_2A",
"EDICAO_EXTRA_2B",
"EDICAO_EXTRA_2D",
"EDICAO_EXTRA_3A",
"EDICAO_EXTRA_3B",
"EDICAO_EXTRA_3D",
"EDICAO_SUPLEMENTAR",
"TODOS"
]
}
}
},
"required": ["terms"],
"additionalProperties": false
},
"doc_md": {
"type": "string",
"description": "description"
},
"report": {
"type": "object",
"description": "Aceita: `slack`, `discord`, `emails`, `attach_csv`, `subject`, `skip_null`",
"properties": {
"slack": {
"type": "object",
"description": "description",
"properties": {
"webhook": {
"type": "string",
"description": "description",
"format": "uri-reference"
}
}
},
"discord": {
"type": "object",
"description": "description",
"properties": {
"webhook": {
"type": "string",
"description": "description",
"format": "uri-reference"
}
}
},
"emails": {
"type": "array",
"description": "description",
"items": {
"type": "string",
"format": "email"
}
},
"attach_csv": {
"type": "boolean",
"description": "description"
},
"subject": {
"type": "string",
"description": "description"
},
"skip_null": {
"type": "boolean",
"description": "description"
}
},
"additionalProperties": false
}
},
"required": ["id", "description", "search", "report"],
"additionalProperties": false
}
},
"required": ["dag"],
"additionalProperties": false
}
5 changes: 4 additions & 1 deletion tests-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ pytest-mock==3.10.0
unidecode==1.2.0
xlrd==1.2.0
ijson==3.0.4
openpyxl==3.0.7
openpyxl==3.0.7
jsonschema==4.21.1
PyYAML==6.0.1
requests==2.31.0
7 changes: 4 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import pytest
from typing import Tuple

from dags.ro_dou.dou_dag_generator import (DouDigestDagGenerator,
from dags.ro_dou_src.dou_dag_generator import (DouDigestDagGenerator,
SearchResult)
from dags.ro_dou.parsers import YAMLParser
from dags.ro_dou.searchers import DOUSearcher
from dags.ro_dou_src.parsers import YAMLParser
from dags.ro_dou_src.searchers import DOUSearcher

TEST_AIRFLOW_HOME = '/opt/airflow'

Expand Down Expand Up @@ -42,6 +42,7 @@ def dag_gen() -> DouDigestDagGenerator:
@pytest.fixture()
def yaml_parser()-> YAMLParser:
filepath = os.path.join(DouDigestDagGenerator().YAMLS_DIR,
"examples_and_tests",
'basic_example.yaml')
return YAMLParser(filepath=filepath)

Expand Down
4 changes: 2 additions & 2 deletions tests/dag_generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import pandas as pd
import pytest
from dags.ro_dou.dou_dag_generator import merge_results
from dags.ro_dou.notification.email_sender import EmailSender, repack_match
from dags.ro_dou_src.dou_dag_generator import merge_results
from dags.ro_dou_src.notification.email_sender import EmailSender, repack_match


def test_repack_match(report_example):
Expand Down
16 changes: 8 additions & 8 deletions tests/discord_sender_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import namedtuple

import pytest
from dags.ro_dou.notification.discord_sender import DiscordSender, requests
from dags.ro_dou_src.notification.discord_sender import DiscordSender, requests
from pytest_mock import MockerFixture

WEBHOOK = 'https://some-url.com/xxx'
Expand All @@ -13,7 +13,7 @@ def mocked_specs():

def test_send_discord_data(session_mocker: MockerFixture, mocked_specs):
session_mocker.patch(
'dags.ro_dou.notification.discord_sender.requests.post')
'dags.ro_dou_src.notification.discord_sender.requests.post')

sender = DiscordSender(mocked_specs)
sender.send_data(
Expand All @@ -31,7 +31,7 @@ def test_send_discord_data(session_mocker: MockerFixture, mocked_specs):

def test_send_text_to_discord(session_mocker: MockerFixture, mocked_specs):
session_mocker.patch(
'dags.ro_dou.notification.discord_sender.requests.post')
'dags.ro_dou_src.notification.discord_sender.requests.post')

sender = DiscordSender(mocked_specs)
sender.send_text('string')
Expand All @@ -46,7 +46,7 @@ def test_send_text_to_discord(session_mocker: MockerFixture, mocked_specs):

def test_send_embeds_to_discord(session_mocker: MockerFixture, mocked_specs):
session_mocker.patch(
'dags.ro_dou.notification.discord_sender.requests.post')
'dags.ro_dou_src.notification.discord_sender.requests.post')
sender = DiscordSender(mocked_specs)
items = [
{
Expand Down Expand Up @@ -127,9 +127,9 @@ def _send_report(specs):
def test_send_report_to_discord__texts(session_mocker: MockerFixture,
mocked_specs):
session_mocker.patch(
'dags.ro_dou.notification.discord_sender.DiscordSender.send_text')
'dags.ro_dou_src.notification.discord_sender.DiscordSender.send_text')
session_mocker.patch(
'dags.ro_dou.notification.discord_sender.DiscordSender.send_embeds')
'dags.ro_dou_src.notification.discord_sender.DiscordSender.send_embeds')

_send_report(mocked_specs)

Expand All @@ -147,9 +147,9 @@ def test_send_report_to_discord__texts(session_mocker: MockerFixture,
def test_send_report_to_discord__embeds(session_mocker: MockerFixture,
mocked_specs):
session_mocker.patch(
'dags.ro_dou.notification.discord_sender.DiscordSender.send_text')
'dags.ro_dou_src.notification.discord_sender.DiscordSender.send_text')
session_mocker.patch(
'dags.ro_dou.notification.discord_sender.DiscordSender.send_embeds')
'dags.ro_dou_src.notification.discord_sender.DiscordSender.send_embeds')

_send_report(mocked_specs)

Expand Down
Loading

0 comments on commit 70a45b3

Please sign in to comment.