Skip to content

Commit

Permalink
WIP:TST
Browse files Browse the repository at this point in the history
  • Loading branch information
ashdriod committed Nov 12, 2023
1 parent 4f5638d commit 9571946
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion test/test_main_app_actions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import os
from unittest.mock import patch, MagicMock
import json
from unittest import mock
from unittest.mock import patch, mock_open, Mock, MagicMock

import pytest
import dtoolcore

from dtool_lookup_gui.models.settings import settings as app_settings



@pytest.mark.asyncio
async def test_app_id(app):
assert app.get_application_id() == 'de.uni-freiburg.dtool-lookup-gui'
Expand Down Expand Up @@ -42,3 +47,63 @@ async def test_do_reset_config(app, settings):
# 'dtool-config-changed' signal.
# Assert that the 'dtool-config-changed' signal has been emitted
mock_emit.assert_called_once_with('dtool-config-changed')


@pytest.mark.asyncio
async def test_do_import_config(app, settings):
"""Test do_import_config action."""
mock_config = {'key1': 'value1', 'key2': 'value2'}
# Define the mock config file path
config_file_path = '/path/to/mock/config.json'

with patch('builtins.open', mock_open(read_data=json.dumps(mock_config))) as mock_file, \
patch('json.load', return_value=mock_config), \
patch('dtoolcore.utils.write_config_value_to_file') as mock_write_config, \
patch.object(app, 'emit') as mock_emit:

# Create a mock for the GLib.Variant object
mock_value = mock.Mock()
mock_value.get_string.return_value = config_file_path

# Perform the import config action
app.do_import_config('import-config', mock_value)

# Assert that the config file is opened correctly with the specified path
mock_file.assert_any_call(config_file_path, 'r')

# Assert that the config values are written to file
for key, value in mock_config.items():
mock_write_config.assert_any_call(key, value)

# Assert that the 'dtool-config-changed' signal has been emitted
mock_emit.assert_called_once_with('dtool-config-changed')


@pytest.mark.asyncio
async def test_do_export_config(app, settings):
"""Test do_export_config action."""
mock_config = {'key1': 'value1', 'key2': 'value2'}
# Define the mock config file path
config_file_path = '/path/to/mock/config.json'

# Setup mock open
mock_file_handle = mock_open()
with patch('builtins.open', mock_file_handle) as mock_file, \
patch('dtoolcore.utils._get_config_dict_from_file', return_value=mock_config), \
patch.object(app, 'emit') as mock_emit:

# Create a mock for the GLib.Variant object
mock_value = mock.Mock()
mock_value.get_string.return_value = config_file_path

# Perform the export config action
app.do_export_config('export-config', mock_value)

# Assert that the config file is opened correctly with the specified path
mock_file.assert_called_once_with(config_file_path, 'w')

# Concatenate all the calls to write and compare with expected JSON
written_data = ''.join(call_arg.args[0] for call_arg in mock_file_handle().write.call_args_list)
expected_file_content = json.dumps(mock_config, indent=4)
assert written_data == expected_file_content

0 comments on commit 9571946

Please sign in to comment.