Skip to content

Commit

Permalink
Run "black" code formatter on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed May 17, 2021
1 parent f0937fd commit 2634c79
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 288 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ virtualenv-dev: setup-virtualenv
lint: virtualenv-dev
$(flakehell) lint data_generator query_timer
$(flakehell) lint batch_size_automator float_simulator tictrack
#$(flakehell) lint tests
$(flakehell) lint tests

format: virtualenv-dev
$(black) data_generator query_timer
$(black) batch_size_automator float_simulator tictrack
$(black) tests

test: virtualenv-dev
$(pytest) -vvv tests
Expand Down
37 changes: 26 additions & 11 deletions tests/test_crate_db_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from tests.test_models import test_model, test_model2


@mock.patch.object(client, 'connect', autospec=True)
@mock.patch.object(client, "connect", autospec=True)
def test_close_connection(mock_connect):
"""
This function tests if the .close() functions of the self.conn and self.cursor objects is called
Expand All @@ -25,14 +25,16 @@ def test_close_connection(mock_connect):
mock_connect.return_value = conn
conn.cursor.return_value = cursor
db_writer = CrateDbWriter("localhost:4200", "crate", "password", test_model)
mock_connect.assert_called_with("localhost:4200", username="crate", password="password")
mock_connect.assert_called_with(
"localhost:4200", username="crate", password="password"
)
# Test Case 1:
db_writer.close_connection()
conn.close.assert_called()
cursor.close.assert_called()


@mock.patch.object(client, 'connect', autospec=True)
@mock.patch.object(client, "connect", autospec=True)
def test_prepare_database1(mock_connect):
"""
This function tests if the .prepare_database() functions of the db_writer uses the correct values when
Expand All @@ -58,7 +60,9 @@ def test_prepare_database1(mock_connect):
mock_connect.return_value = conn
conn.cursor.return_value = cursor
db_writer = CrateDbWriter("localhost:4200", "crate2", "password2", test_model)
mock_connect.assert_called_with("localhost:4200", username="crate2", password="password2")
mock_connect.assert_called_with(
"localhost:4200", username="crate2", password="password2"
)
# Test Case 1:
db_writer.prepare_database()
stmt = cursor.execute.call_args.args[0]
Expand All @@ -72,7 +76,7 @@ def test_prepare_database1(mock_connect):
assert "number_of_replicas = 1" in stmt


@mock.patch.object(client, 'connect', autospec=True)
@mock.patch.object(client, "connect", autospec=True)
def test_prepare_database2(mock_connect):
"""
This function tests if the .prepare_database() functions of the db_writer uses the correct values when
Expand All @@ -97,7 +101,9 @@ def test_prepare_database2(mock_connect):
cursor = mock.Mock()
mock_connect.return_value = conn
conn.cursor.return_value = cursor
db_writer = CrateDbWriter("localhost:4200", "crate3", "password3", test_model2, "table_name", 3, 0, "day")
db_writer = CrateDbWriter(
"localhost:4200", "crate3", "password3", test_model2, "table_name", 3, 0, "day"
)
db_writer.prepare_database()
# Test Case 1:
stmt = cursor.execute.call_args.args[0]
Expand All @@ -111,7 +117,7 @@ def test_prepare_database2(mock_connect):
assert "number_of_replicas = 0" in stmt


@mock.patch.object(client, 'connect', autospec=True)
@mock.patch.object(client, "connect", autospec=True)
def test_insert_stmt(mock_connect):
"""
This function tests if the .insert_stmt() functions of CrateDbWriter uses the correct table name and arguments
Expand All @@ -134,15 +140,24 @@ def test_insert_stmt(mock_connect):
conn.cursor.return_value = cursor
db_writer = CrateDbWriter("localhost:4200", "crate2", "password2", test_model)
# Test Case 1:
db_writer.insert_stmt([1586327807000], [{"plant": 1, "line": 1, "sensor_id": 1, "value": 6.7, "button_press": False}])
db_writer.insert_stmt(
[1586327807000],
[{"plant": 1, "line": 1, "sensor_id": 1, "value": 6.7, "button_press": False}],
)
call_arguments = cursor.execute.call_args.args
stmt = call_arguments[0]
values = call_arguments[1]
assert stmt == "INSERT INTO temperature (ts, payload) (SELECT col1, col2 FROM UNNEST(?,?))"
assert values == ([1586327807000], [{"plant": 1, "line": 1, "sensor_id": 1, "value": 6.7, "button_press": False}])
assert (
stmt
== "INSERT INTO temperature (ts, payload) (SELECT col1, col2 FROM UNNEST(?,?))"
)
assert values == (
[1586327807000],
[{"plant": 1, "line": 1, "sensor_id": 1, "value": 6.7, "button_press": False}],
)


@mock.patch.object(client, 'connect', autospec=True)
@mock.patch.object(client, "connect", autospec=True)
def test_execute_query(mock_connect):
"""
This function tests if the .execute_query() functions of CrateDbWriter uses the correct query
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dg_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def test_config_constructor_no_env_set():
assert config.stat_delta == 30

assert config.host == "localhost"
assert config.username == None
assert config.password == None
assert config.username is None
assert config.password is None
assert config.db_name == ""
assert config.table_name == ""
assert config.partition == "week"
Expand Down
11 changes: 9 additions & 2 deletions tests/test_dg_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
@mock.patch("data_generator.__main__.MsSQLDbWriter", autospec=True)
@mock.patch("data_generator.__main__.PostgresDbWriter", autospec=True)
@mock.patch("data_generator.__main__.TimeStreamWriter", autospec=True)
def test_get_db_writer(mock_timestream, mock_postgres, mock_mssql, mock_mongo, mock_influx, mock_timescale, mock_crate):
def test_get_db_writer(
mock_timestream,
mock_postgres,
mock_mssql,
mock_mongo,
mock_influx,
mock_timescale,
mock_crate,
):
dg.config.database = 0
dg.get_db_writer()
mock_crate.assert_called_once()
Expand Down Expand Up @@ -242,4 +250,3 @@ def test_stop_process():
dg.stop_queue.put(1)
assert dg.stop_process()
dg.stop_queue.get() # resetting the stop queue

9 changes: 7 additions & 2 deletions tests/test_edge.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import pytest
import numpy
from data_generator.edge import Edge, BoolSensor
from tests.test_models import metrics_model_float1_bool1, metrics_model_string, \
tag_model_plant100_line5_sensorId, bool_model, tag_model_list
from tests.test_models import (
metrics_model_float1_bool1,
metrics_model_string,
tag_model_plant100_line5_sensorId,
bool_model,
tag_model_list,
)


def test_init_sensors():
Expand Down
16 changes: 12 additions & 4 deletions tests/test_influx_db_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def test_prepare_database_bucket_exists(mock_client):
db_writer = InfluxDbWriter("localhost", "token1", "org1", test_model)
mock_client.assert_called_with("localhost", token="token1")
bucket_list = DotMap()
bucket_list.buckets = [Bucket(name="", retention_rules=[]), Bucket(name="temperature", retention_rules=[])]
bucket_list.buckets = [
Bucket(name="", retention_rules=[]),
Bucket(name="temperature", retention_rules=[]),
]
buckets_api.find_buckets.return_value = bucket_list
# Test Case 1:
db_writer.prepare_database()
Expand Down Expand Up @@ -88,7 +91,10 @@ def test_prepare_database_bucket_not_exists(mock_client):
db_writer = InfluxDbWriter("localhost", "token2", "org2", test_model)
mock_client.assert_called_with("localhost", token="token2")
bucket_list = DotMap()
bucket_list.buckets = [Bucket(name="x", retention_rules=[]), Bucket(name="y", retention_rules=[])]
bucket_list.buckets = [
Bucket(name="x", retention_rules=[]),
Bucket(name="y", retention_rules=[]),
]
buckets_api.find_buckets.return_value = bucket_list
# Test Case 1:
db_writer.prepare_database()
Expand Down Expand Up @@ -120,8 +126,10 @@ def test_insert_stmt(mock_client):
client.write_api.return_value = write_api
db_writer = InfluxDbWriter("localhost", "token", "org", test_model)
# Test Case 1:
db_writer.insert_stmt([1586327807000],
[{"plant": 2, "line": 2, "sensor_id": 2, "value": 6.7, "button_press": False}])
db_writer.insert_stmt(
[1586327807000],
[{"plant": 2, "line": 2, "sensor_id": 2, "value": 6.7, "button_press": False}],
)
call_arguments = write_api.write.call_args[1]
org = call_arguments["org"]
data = call_arguments["record"]
Expand Down
Loading

0 comments on commit 2634c79

Please sign in to comment.