Commit f02c58ca authored by Dominik Rosiek's avatar Dominik Rosiek

first tests

parent bf8cd149
This source diff could not be displayed because it is too large. You can view the blob instead.
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""
This script is responsible for update measurement values.
"""
from uuid import UUID from uuid import UUID
import requests import requests
import time import time
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from functools import partial
from db import DDBstations, DDBtypes
from dateutil.parser import parse from dateutil.parser import parse
import models import models
from tornado.gen import coroutine from tornado.gen import coroutine
def get_station_by_id(station_id, station_data): def get_station_by_id(station_id, station_data):
"""
Returns station from dict of stations based on station_id
:param station_id (int):
:param station_data (dict):
:return (Station):
"""
station_id = str(UUID(int=int(station_id))) station_id = str(UUID(int=int(station_id)))
return station_data[station_id] return station_data[station_id]
# for station in station_data:
# if str(station['api_station_id']) == station_id:
# result = yield models.Station.get(station['station_id'])
# return result
def get_type_by_name(type_name, type_data): def get_type_by_name(type_name, type_data):
"""
Returns pollution type from dict of types based on type_name
:param type_name (str):
:param type_data (dict):
:return (Type):
"""
for type in type_data.values(): for type in type_data.values():
if type.shortname == type_name: if type.shortname == type_name:
return type return type
def get_timestamp_from_datetime(datetime_string): def get_timestamp_from_datetime(datetime_string):
"""
Parses datetime value and return timestamp
:param datetime_string (str):
:return (int):
"""
dt = parse(datetime_string) dt = parse(datetime_string)
return time.mktime(dt.timetuple()) return time.mktime(dt.timetuple())
@coroutine @coroutine
def main(): def main():
"""
Main function
:return (None):
"""
apiURL = "http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=1" apiURL = "http://powietrze.malopolska.pl/_powietrzeapi/api/dane?act=danemiasta&ci_id=1"
r = requests.get(apiURL) r = requests.get(apiURL)
...@@ -52,5 +79,4 @@ def main(): ...@@ -52,5 +79,4 @@ def main():
value=ready_measurement_value, time=ready_measurement_timestamp) value=ready_measurement_value, time=ready_measurement_timestamp)
yield measurement.save() yield measurement.save()
# ioloop = IOLoop.instance()
IOLoop.current().run_sync(main) IOLoop.current().run_sync(main)
from uuid import UUID from uuid import UUID
from functools import partial
import models import models
import csv import csv
......
requests requests==2.12.4
tornado == 4.4.2 tornado==4.4.2
botocore==0.65.0 botocore==0.65.0
tornado-botocore==0.1.6 tornado-botocore==0.1.6
nose==1.3.7
coverage==4.2
asynctest==0.9.0
\ No newline at end of file
from uuid import UUID from uuid import UUID
from functools import partial
import models import models
import csv import csv
......
import models import uuid
from unittest.mock import MagicMock, patch
station = models.Station(1, "Kraków", 12, 13, "Aleje")
station.save() from asynctest import CoroutineMock
station2 = models.Station.get(station.id) from models import Type
assert station.id == station2.id from tornado.testing import AsyncTestCase, gen_test
pollution_type = models.Type(shortname="pm2", unit="mg", norm=120, longname="long_pm2", description="description pm2") class TypeTest(AsyncTestCase):
pollution_type.save() def setUp(self):
pollution_type2 = models.Type.get(pollution_type.id) Type.stored = {}
assert pollution_type.id == pollution_type2.id super(TypeTest, self).setUp()
self.id = uuid.uuid4()
measurement = models.Measurement(station=station, pollution_type=pollution_type, value=13, time=124) self.shortname = "test_shortname"
measurement.save() self.unit = "test_unit"
measurement2 = models.Measurement.get(measurement.id) self.norm = 57
assert measurement.id == measurement2.id self.longname = "test_longname"
\ No newline at end of file self.description = "test_description"
self.type = Type(shortname=self.shortname, unit=self.unit, norm=self.norm, longname=self.longname,
description=self.description, type_id=self.id)
@gen_test
def test_init(self):
self.assertEqual(self.type.id, self.id)
self.assertEqual(self.type.shortname, self.shortname)
self.assertEqual(self.type.unit, self.unit)
self.assertEqual(self.type.norm, self.norm)
self.assertEqual(self.type.longname, self.longname)
self.assertEqual(self.type.description, self.description)
@gen_test
def test_get_from_storage(self):
Type.stored = {self.type.id: self.type}
result = yield Type.get(self.type.id)
self.assertEqual(result, self.type)
@gen_test
def test_get(self):
with patch('models.DDBtypes') as mock_types:
mock_types.return_value.get = CoroutineMock()
mock_types.return_value.get.return_value = {
'type_id': self.id,
'shortname': self.shortname,
'unit': self.unit,
'norm': self.norm,
'longname': self.longname,
'description': self.description
}
result = yield Type.get(self.type.id)
self.assertEqual(result.id, self.id)
self.assertEqual(result.shortname, self.shortname)
self.assertEqual(result.unit, self.unit)
self.assertEqual(result.norm, self.norm)
self.assertEqual(result.longname, self.longname)
self.assertEqual(result.description, self.description)
# import models
#
# station = models.Station(1, "Kraków", 12, 13, "Aleje")
# station.save()
# station2 = models.Station.get(station.id)
# assert station.id == station2.id
#
# pollution_type = models.Type(shortname="pm2", unit="mg", norm=120, longname="long_pm2", description="description pm2")
# pollution_type.save()
# pollution_type2 = models.Type.get(pollution_type.id)
# assert pollution_type.id == pollution_type2.id
#
# measurement = models.Measurement(station=station, pollution_type=pollution_type, value=13, time=124)
# measurement.save()
# measurement2 = models.Measurement.get(measurement.id)
# assert measurement.id == measurement2.id
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment