Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
smogonet
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dominik Rosiek
smogonet
Commits
2af47614
Commit
2af47614
authored
Dec 26, 2016
by
Dominik Rosiek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unit tests for models
parent
f02c58ca
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
220 additions
and
18 deletions
+220
-18
.coverage
.coverage
+0
-1
test_models.py
test_models.py
+220
-17
No files found.
.coverage
deleted
100644 → 0
View file @
f02c58ca
This source diff could not be displayed because it is too large. You can
view the blob
instead.
test_models.py
View file @
2af47614
import
uuid
from
unittest.mock
import
MagicMock
,
patch
import
time
from
asynctest
import
CoroutineMock
from
models
import
Type
from
models
import
Type
,
Station
,
Measurement
from
tornado.testing
import
AsyncTestCase
,
gen_test
class
TypeTest
(
AsyncTestCase
):
def
setUp
(
self
):
Type
.
stored
=
{}
...
...
@@ -18,6 +21,9 @@ class TypeTest(AsyncTestCase):
self
.
type
=
Type
(
shortname
=
self
.
shortname
,
unit
=
self
.
unit
,
norm
=
self
.
norm
,
longname
=
self
.
longname
,
description
=
self
.
description
,
type_id
=
self
.
id
)
def
tearDown
(
self
):
Type
.
stored
=
{}
@
gen_test
def
test_init
(
self
):
self
.
assertEqual
(
self
.
type
.
id
,
self
.
id
)
...
...
@@ -55,19 +61,216 @@ class TypeTest(AsyncTestCase):
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
@
gen_test
def
test_save
(
self
):
with
patch
(
'models.DDBtypes'
)
as
mock_type
:
mock_type
.
return_value
.
add
=
CoroutineMock
()
self
.
type
.
save
()
mock_type
.
return_value
.
add
.
assert_called_once_with
(
pollution_type
=
self
.
type
)
@
gen_test
def
test_get_all
(
self
):
with
patch
(
'models.DDBtypes'
)
as
mock_type
:
mock_type
.
return_value
.
get_all
=
CoroutineMock
()
mock_type
.
return_value
.
get_all
.
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_all
()
expected
=
{
self
.
type
.
id
:
self
.
type
}
self
.
assertCountEqual
(
result
,
expected
)
self
.
assertCountEqual
(
Type
.
stored
,
expected
)
class
StationTest
(
AsyncTestCase
):
def
setUp
(
self
):
super
(
StationTest
,
self
)
.
setUp
()
Station
.
stored
=
{}
self
.
id
=
uuid
.
uuid4
()
self
.
api_id
=
1337
self
.
city
=
"test_city"
self
.
longitude
=
12345
self
.
latitude
=
54321
self
.
name
=
"test_name"
self
.
station
=
Station
(
station_id
=
self
.
id
,
api_station_id
=
self
.
api_id
,
city
=
self
.
city
,
longitude
=
self
.
longitude
,
latitude
=
self
.
latitude
,
name
=
self
.
name
)
def
tearDown
(
self
):
Station
.
stored
=
{}
@
gen_test
def
test_init
(
self
):
self
.
assertEqual
(
self
.
station
.
id
,
self
.
id
)
self
.
assertEqual
(
self
.
station
.
api_id
,
self
.
api_id
)
self
.
assertEqual
(
self
.
station
.
city
,
self
.
city
)
self
.
assertEqual
(
self
.
station
.
longitude
,
self
.
longitude
)
self
.
assertEqual
(
self
.
station
.
latitude
,
self
.
latitude
)
self
.
assertEqual
(
self
.
station
.
name
,
self
.
name
)
@
gen_test
def
test_get_from_storage
(
self
):
Station
.
stored
=
{
self
.
station
.
id
:
self
.
station
}
result
=
yield
Station
.
get
(
self
.
station
.
id
)
self
.
assertEqual
(
result
,
self
.
station
)
@
gen_test
def
test_get
(
self
):
with
patch
(
'models.DDBstations'
)
as
mock_types
:
mock_types
.
return_value
.
get
=
CoroutineMock
()
mock_types
.
return_value
.
get
.
return_value
=
{
'station_id'
:
self
.
id
,
'api_station_id'
:
self
.
api_id
,
'city'
:
self
.
city
,
'longitude'
:
self
.
longitude
,
'latitude'
:
self
.
latitude
,
'name'
:
self
.
name
}
result
=
yield
Station
.
get
(
self
.
station
.
id
)
self
.
assertEqual
(
result
.
id
,
self
.
id
)
self
.
assertEqual
(
result
.
api_id
,
self
.
api_id
)
self
.
assertEqual
(
result
.
city
,
self
.
city
)
self
.
assertEqual
(
result
.
longitude
,
self
.
longitude
)
self
.
assertEqual
(
result
.
latitude
,
self
.
latitude
)
self
.
assertEqual
(
result
.
name
,
self
.
name
)
@
gen_test
def
test_save
(
self
):
with
patch
(
'models.DDBstations'
)
as
mock_station
:
mock_station
.
return_value
.
add
=
CoroutineMock
()
self
.
station
.
save
()
mock_station
.
return_value
.
add
.
assert_called_once_with
(
station
=
self
.
station
)
@
gen_test
def
test_get_all
(
self
):
with
patch
(
'models.DDBstations'
)
as
mock_station
:
mock_station
.
return_value
.
get_all
=
CoroutineMock
()
mock_station
.
return_value
.
get_all
.
return_value
=
[{
'station_id'
:
self
.
id
,
'api_station_id'
:
self
.
api_id
,
'city'
:
self
.
city
,
'longitude'
:
self
.
longitude
,
'latitude'
:
self
.
latitude
,
'name'
:
self
.
name
}]
result
=
yield
Station
.
get_all
()
expected
=
{
self
.
station
.
id
:
self
.
station
}
self
.
assertCountEqual
(
result
,
expected
)
self
.
assertCountEqual
(
Station
.
stored
,
expected
)
class
MeasurementTest
(
AsyncTestCase
):
def
setUp
(
self
):
super
(
MeasurementTest
,
self
)
.
setUp
()
Measurement
.
stored
=
{}
self
.
id
=
uuid
.
uuid4
()
self
.
station
=
MagicMock
()
self
.
type
=
MagicMock
()
self
.
value
=
1337
self
.
time
=
time
.
time
()
self
.
meas
=
Measurement
(
measurement_id
=
self
.
id
,
station
=
self
.
station
,
pollution_type
=
self
.
type
,
value
=
self
.
value
,
time
=
self
.
time
)
def
tearDown
(
self
):
Measurement
.
stored
=
{}
@
gen_test
def
test_init
(
self
):
self
.
assertEqual
(
self
.
meas
.
id
,
self
.
id
)
self
.
assertEqual
(
self
.
meas
.
station
,
self
.
station
)
self
.
assertEqual
(
self
.
meas
.
type
,
self
.
type
)
self
.
assertEqual
(
self
.
meas
.
value
,
self
.
value
)
self
.
assertEqual
(
self
.
meas
.
time
,
self
.
time
)
@
gen_test
def
test_get_from_storage
(
self
):
Measurement
.
stored
=
{
self
.
meas
.
id
:
self
.
meas
}
result
=
yield
Measurement
.
get
(
self
.
meas
.
id
)
self
.
assertEqual
(
result
,
self
.
meas
)
@
gen_test
def
test_get
(
self
):
with
patch
(
'models.Station'
)
as
mock_station
:
mock_station
.
get
=
CoroutineMock
()
mock_station
.
get
.
return_value
=
self
.
station
with
patch
(
'models.Type'
)
as
mock_type
:
mock_type
.
get
=
CoroutineMock
()
mock_type
.
get
.
return_value
=
self
.
type
with
patch
(
'models.DDBmeasurements'
)
as
mock_types
:
mock_types
.
return_value
.
get
=
CoroutineMock
()
mock_types
.
return_value
.
get
.
return_value
=
{
'measurement_id'
:
self
.
id
,
'station_id'
:
self
.
station
.
id
,
'type_id'
:
self
.
type
.
id
,
'value'
:
self
.
value
,
'time'
:
self
.
time
,
}
result
=
yield
Measurement
.
get
(
self
.
station
.
id
)
mock_type
.
get
.
assert_called_once_with
(
self
.
type
.
id
)
mock_station
.
get
.
assert_called_once_with
(
self
.
station
.
id
)
self
.
assertEqual
(
result
.
id
,
self
.
id
)
self
.
assertEqual
(
result
.
station
,
self
.
station
)
self
.
assertEqual
(
result
.
type
,
self
.
type
)
self
.
assertEqual
(
result
.
value
,
self
.
value
)
self
.
assertEqual
(
result
.
time
,
self
.
time
)
@
gen_test
def
test_save
(
self
):
with
patch
(
'models.DDBmeasurements'
)
as
mock_measurements
:
with
patch
(
'models.DDBlastmeasurements'
)
as
mock_last
:
mock_measurements
.
return_value
.
add
=
CoroutineMock
()
mock_last
.
return_value
.
add
=
CoroutineMock
()
self
.
meas
.
save
()
mock_measurements
.
return_value
.
add
.
assert_called_once_with
(
measurement
=
self
.
meas
)
mock_last
.
return_value
.
add
.
assert_called_once_with
(
measurement
=
self
.
meas
)
@
gen_test
def
test_get_last
(
self
):
Measurement
.
stored
=
{
self
.
meas
.
id
:
self
.
meas
}
with
patch
(
'models.DDBlastmeasurements'
)
as
mock_last
:
mock_last
.
return_value
.
get
=
CoroutineMock
()
mock_last
.
return_value
.
get
.
return_value
=
{
'measurement_id'
:
self
.
meas
.
id
}
result
=
yield
Measurement
.
get_last
(
station
=
self
.
station
,
pollution_type
=
self
.
type
)
mock_last
.
return_value
.
get
.
assert_called_once_with
(
self
.
station
,
self
.
type
)
self
.
assertEqual
(
result
,
self
.
meas
)
@
gen_test
def
test_get_all_last
(
self
):
Measurement
.
stored
=
{
self
.
meas
.
id
:
self
.
meas
}
with
patch
(
'models.DDBlastmeasurements'
)
as
mock_last
:
mock_last
.
return_value
.
get_all
=
CoroutineMock
()
mock_last
.
return_value
.
get_all
.
return_value
=
[{
'measurement_id'
:
self
.
meas
.
id
}]
result
=
yield
Measurement
.
get_all_last
()
mock_last
.
return_value
.
get_all
.
assert_called_once_with
()
self
.
assertCountEqual
(
result
,
[
self
.
meas
])
def
test_str
(
self
):
expected
=
"{0} {1} {2} {3} {4}"
.
format
(
self
.
id
,
self
.
station
.
id
,
self
.
type
.
id
,
self
.
value
,
self
.
time
)
result
=
str
(
self
.
meas
)
self
.
assertEqual
(
result
,
expected
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment