test_data_show.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import os
  2. import pytest
  3. from flexmeasures.cli.tests.utils import (
  4. check_command_ran_without_error,
  5. get_click_commands,
  6. )
  7. from flexmeasures.tests.utils import get_test_sensor
  8. @pytest.mark.skip_github
  9. def test_list_accounts(app, fresh_db, setup_accounts_fresh_db):
  10. from flexmeasures.cli.data_show import list_accounts
  11. runner = app.test_cli_runner()
  12. result = runner.invoke(list_accounts)
  13. assert "All accounts on this" in result.output
  14. for account in setup_accounts_fresh_db.values():
  15. assert account.name in result.output
  16. check_command_ran_without_error(result)
  17. @pytest.mark.skip_github
  18. def test_list_roles(app, fresh_db, setup_roles_users_fresh_db):
  19. from flexmeasures.cli.data_show import list_roles
  20. runner = app.test_cli_runner()
  21. result = runner.invoke(list_roles)
  22. assert "Account roles" in result.output
  23. assert "User roles" in result.output
  24. for role in ("account-admin", "Supplier", "Dummy"):
  25. assert role in result.output
  26. check_command_ran_without_error(result)
  27. @pytest.mark.skip_github
  28. def test_list_asset_types(app, fresh_db, setup_generic_asset_types_fresh_db):
  29. from flexmeasures.cli.data_show import list_asset_types
  30. runner = app.test_cli_runner()
  31. result = runner.invoke(list_asset_types)
  32. for asset_type in setup_generic_asset_types_fresh_db.values():
  33. assert asset_type.name in result.output
  34. check_command_ran_without_error(result)
  35. @pytest.mark.skip_github
  36. def test_list_sources(app, fresh_db, setup_sources_fresh_db):
  37. from flexmeasures.cli.data_show import list_data_sources
  38. runner = app.test_cli_runner()
  39. result = runner.invoke(list_data_sources)
  40. for source in setup_sources_fresh_db.values():
  41. assert source.name in result.output
  42. check_command_ran_without_error(result)
  43. @pytest.mark.skip_github
  44. def test_show_accounts(app, fresh_db, setup_accounts_fresh_db):
  45. from flexmeasures.cli.data_show import show_account
  46. fresh_db.session.flush() # get IDs in DB
  47. runner = app.test_cli_runner()
  48. result = runner.invoke(
  49. show_account, ["--id", setup_accounts_fresh_db["Prosumer"].id]
  50. )
  51. assert "Account Test Prosumer Account" in result.output
  52. assert "No users in account" in result.output
  53. check_command_ran_without_error(result)
  54. @pytest.mark.skip_github
  55. def test_show_asset(app, fresh_db, setup_generic_assets_fresh_db):
  56. from flexmeasures.cli.data_show import show_generic_asset
  57. fresh_db.session.flush() # get IDs in DB
  58. runner = app.test_cli_runner()
  59. result = runner.invoke(
  60. show_generic_asset,
  61. ["--id", setup_generic_assets_fresh_db["test_wind_turbine"].id],
  62. )
  63. assert "Asset Test wind turbine" in result.output
  64. assert "No sensors in asset" in result.output
  65. assert result.exit_code == 1 # command raises a click.Abort Exception
  66. @pytest.mark.skip_github
  67. def test_plot_beliefs(app, fresh_db, setup_beliefs_fresh_db):
  68. from flexmeasures.cli.data_show import plot_beliefs
  69. sensor = get_test_sensor(fresh_db)
  70. runner = app.test_cli_runner()
  71. result = runner.invoke(
  72. plot_beliefs,
  73. [
  74. "--sensor",
  75. sensor.id,
  76. "--start",
  77. "2021-03-28T16:00+01",
  78. "--duration",
  79. "PT1H",
  80. ],
  81. )
  82. assert "Beliefs for Sensor 'epex_da'" in result.output
  83. assert "Data spans an hour" in result.output
  84. check_command_ran_without_error(result)
  85. def test_cli_help(app):
  86. """Test that showing help does not throw an error."""
  87. from flexmeasures.cli import data_show
  88. runner = app.test_cli_runner()
  89. for cmd in get_click_commands(data_show):
  90. result = runner.invoke(cmd, ["--help"])
  91. assert "Usage" in result.output
  92. check_command_ran_without_error(result)
  93. @pytest.mark.skip_github
  94. @pytest.mark.parametrize("_format", ["png", "svg"])
  95. def test_export_chart(app, fresh_db, setup_beliefs_fresh_db, _format):
  96. from flexmeasures.cli.data_show import chart
  97. sensor = get_test_sensor(fresh_db)
  98. sensor_id = sensor.id
  99. runner = app.test_cli_runner()
  100. # run test in an isolated file system
  101. with runner.isolated_filesystem():
  102. result = runner.invoke(
  103. chart,
  104. [
  105. "--sensor",
  106. sensor_id,
  107. "--start",
  108. "2021-03-28T15:00+01",
  109. "--end",
  110. "2021-03-29T16:00+01",
  111. "--filename",
  112. f"chart-$entity_type-$id.{_format}",
  113. ],
  114. )
  115. check_command_ran_without_error(result)
  116. assert os.path.exists(f"chart-sensor-{sensor_id}.{_format}")
  117. assert (
  118. os.path.getsize(f"chart-sensor-{sensor_id}.{_format}") > 100
  119. ) # bytes: non empty file