Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: 2.1

jobs:
run-tests:
docker:
- image: cimg/python:3.8.12
- image: circleci/postgres:latest
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

steps:
- checkout

- run:
name: Install dependencies 🔧
command: |
pip install -r requirements.txt -r requirements_test.txt

- run:
name: Run tests 🚀
command: |
pytest -v
environment:
SQLALCHEMY_DATABASE_URI_OPEN: "postgres://postgres:postgres@localhost:5432/postgres"
PYTHONUNBUFFERED: 1

workflows:
version: 2
test:
jobs:
- run-tests
11 changes: 11 additions & 0 deletions .docker/test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.8.12-bullseye

ENV PYTHONUNBUFFERED=1
ENV SQLALCHEMY_DATABASE_URI_OPEN='postgres://postgres:postgres@test_database:5432/postgres'

WORKDIR /src

COPY requirements.txt /src/
COPY requirements_test.txt /src/

RUN pip install -r requirements.txt -r requirements_test.txt
20 changes: 20 additions & 0 deletions .docker/test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "3"

services:
test_database:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres

test:
build:
context: ../..
dockerfile: .docker/test/Dockerfile
command: |
bash -c "coverage run -m pytest project/test/ && coveralls"
volumes:
- ../..:/src/
depends_on:
- test_database
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Tests

on: [push]

jobs:
run-tests:
runs-on: ubuntu-latest

steps:
- name: Checkout 🛎️
uses: actions/checkout@v2

- name: Build test container 🔧
run: |
make build-test

- name: Run tests 🚀
run: |
make test
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: build-test, test

DOCKER_COMPOSE_FILENAME := .docker/test/docker-compose.yml

build-test:
docker-compose -f $(DOCKER_COMPOSE_FILENAME) build

test:
docker-compose -f $(DOCKER_COMPOSE_FILENAME) up test
docker-compose -f $(DOCKER_COMPOSE_FILENAME) down
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ We work with modules, called qHAWAX, that capture data from gases, dust and envi

You can look for a more detailed documentation [here.](https://qaira.github.io/)

Feel welcome to join our [forum](https://unicef-if.discourse.group/c/projects/qaira/11) with UNICEF.
Feel welcome to join our [forum](https://unicef-if.discourse.group/c/projects/qaira/11) with UNICEF.

## Getting Started with installation

### Ubuntu, Mac & Windows
Expand All @@ -21,10 +21,10 @@ git clone https://github.com/qAIRa/qAIRaMapAPI-OpenSource.git

### Prerequisites for every OS
1. Having installed the postgreSQL driver.
MacOS:
MacOS:
```
brew install postgresql
```
brew install postgresql
```
If facing issues with brew installation: https://docs.brew.sh/Installation

Make sure to add the installed postgreSQL library to your PATH with the following
Expand Down Expand Up @@ -54,7 +54,7 @@ source venv/bin/activate
```
### Prerequisites Windows
Now you have to open CMD with administrator permissions
You must have an isolated environment by executing the following command:
You must have an isolated environment by executing the following command:

```
py -3 -m pip install virtualenv
Expand Down Expand Up @@ -111,6 +111,21 @@ If everything went well, the following should come out

```

## Tests

The tests use Docker to guarantee isolated reproducible builds and a Makefile to simplify the commands.

First, build the Docker containers
```
make build-test
```
You need to do this only the first time you want to run the tests, or if you ever change the Dockerfile (which shouldn't be necessary).

Then, to run the tests
```
make test
```

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand All @@ -137,14 +152,14 @@ Most API calls accessing a list of resources (e.g., users, issues, etc.). If you
Why am I getting a 500?
Server Mistake - Indicates that something went wrong on the server that prevent the server from fulfilling the request.

## Issues
## Issues

If you have found a bug in the project, you can file it here under the [“issues” tab](https://github.com/qAIRa/qAIRaMapAPI-OpenSource/issues). You can also request new features here. A set of templates for reporting issues and requesting features are provided to assist you (and us!).

## Pull Requests
## Pull Requests

If you have received a confirmation about your issue, you can file a pull request under the [“pull request” tab](https://github.com/qAIRa/qAIRaMapAPI-OpenSource/pulls), please use the PR [“template”](https://github.com/qAIRa/qAIRaMapAPI-OpenSource/blob/master/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md).
You can also request new features here.
If you have received a confirmation about your issue, you can file a pull request under the [“pull request” tab](https://github.com/qAIRa/qAIRaMapAPI-OpenSource/pulls), please use the PR [“template”](https://github.com/qAIRa/qAIRaMapAPI-OpenSource/blob/master/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md).
You can also request new features here.

## License
[GPLv2](https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
12 changes: 6 additions & 6 deletions project/main/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import json


def getCompanyTargetofJson(data):
data = checkVariable_helper(data, dict)
array = [
Expand Down Expand Up @@ -123,13 +120,16 @@ def checkVariable_helper(variable, type):
"""
helper function for checking the five types of variables: list, dictionary, string, integer, float

Parametres:
Parameters:
variable: variable to check
type: list / dict / str / int / float

"""
if isinstance(variable, type) is not True:
raise TypeError(f"Variable {str(variable)} should be {str(type)}")
# bool variables need a special check, isinstance(True, int) returns True
not_expecting_bool = isinstance(variable, bool) and type != bool

if not isinstance(variable, type) or not_expecting_bool:
raise TypeError(f"Variable {variable} should be {type}")
return variable


Expand Down
121 changes: 121 additions & 0 deletions project/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""
Data shared by all test cases
"""
import datetime

from project import db
from project.database.models import (
Company,
EcaNoise,
GasInca,
ProcessedMeasurement,
Qhawax,
QhawaxInstallationHistory,
)


now = datetime.datetime.now()
today_midnight = datetime.datetime(now.year, now.month, now.day)

company_data = {
"name": "qAIRa",
"email_group": "qairadrones.com",
"ruc": "20600763491",
"address": "Test Address",
"phone": "000000000",
"contact_person": "Test Person",
}
company = Company(**company_data)
db.session.add(company)
db.session.commit()

eca_noise = EcaNoise(
area_name="Special Protection Zone",
max_daytime_limit=50,
max_night_limit=40,
)
eca_noise2 = EcaNoise(
area_name="Residential Zone",
max_daytime_limit=60,
max_night_limit=50,
)
eca_noise3 = EcaNoise(area_name="Comercial Zone")
eca_noise4 = EcaNoise(area_name="Industry Zone")
db.session.add(eca_noise)
db.session.add(eca_noise2)
db.session.add(eca_noise3)
db.session.add(eca_noise4)
db.session.commit()

qhawax_data1 = {
"name": "qH004",
"qhawax_type": "STATIC",
"state": "ON",
"availability": "Available",
"main_aqi": -1.0,
"mode": "Stand By",
"on_loop": 0,
"main_inca": 50.0,
"first_time_loop": today_midnight,
}
qhawax_data2 = {
"name": "qH021",
"qhawax_type": "AEREAL",
"state": "OFF",
"availability": "Available2",
"main_aqi": -1.0,
"mode": "Customer",
"on_loop": 0,
"main_inca": 50.0,
"first_time_loop": today_midnight,
}
qhawax1 = Qhawax(**qhawax_data1)
qhawax2 = Qhawax(**qhawax_data2)
db.session.add(qhawax1)
db.session.add(qhawax2)
db.session.commit()

qhawax_installation_history_data = {
"lat": "-7.0000499",
"lon": "-70.9000000",
"installation_date_zone": today_midnight,
"end_date_zone": today_midnight,
"link_report": "Test",
"observations": "Test Obs",
"qhawax_id": qhawax1.id,
"district": "La Victoria",
"comercial_name": "Unit Test 1",
"address": "Test Address",
"company_id": company.id,
"eca_noise_id": eca_noise.id,
"connection_type": "Panel Solar",
"index_type": "Test Index",
"season": "Primavera",
"last_time_physically_turn_on_zone": today_midnight,
"person_in_charge": "Test Person",
"is_public": "no",
"last_registration_time_zone": today_midnight,
}
qhawax_installation_history = QhawaxInstallationHistory(
**qhawax_installation_history_data
)
db.session.add(qhawax_installation_history)
db.session.commit()

gas_inca_data = {
"timestamp_zone": today_midnight,
"qhawax_id": qhawax1.id,
"main_inca": 50.0,
}
gas_inca = GasInca(**gas_inca_data)
db.session.add(gas_inca)
db.session.commit()

processed_measurement_data = {
"timestamp": today_midnight,
"timestamp_zone": today_midnight,
"qhawax_id": qhawax1.id,
}
processed_measurement = ProcessedMeasurement(**processed_measurement_data)
db.session.add(processed_measurement)
db.session.commit()
Loading