-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
172 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,34 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
from pieces.commands import create_asset, delete_asset,startup | ||
from pieces.commands import commands_functions | ||
from pieces.api.assets import get_asset_by_id | ||
from pieces.assets import AssetsCommands, AssetsCommandsApi | ||
from pieces.settings import Settings | ||
from pieces_os_client.models.asset import Asset | ||
|
||
class TestAssetFunctions(unittest.TestCase): | ||
class TestCreateDeleteFunctions(unittest.TestCase): | ||
|
||
@patch('builtins.input', side_effect=['y', 'y']) | ||
@patch('pyperclip.paste', return_value='print("Hello, World!")') | ||
def test_create_and_delete_asset(self, mock_paste, mock_input): | ||
startup() | ||
Settings.startup() | ||
# Call create_asset and store the returned asset | ||
create_asset() | ||
AssetsCommands.create_asset() | ||
|
||
# Ensure that it is correctly assigned to the current asset | ||
new_asset=list(commands_functions.current_asset)[0] | ||
new_asset=AssetsCommands.current_asset | ||
|
||
|
||
asset_object = get_asset_by_id(new_asset) | ||
asset_object = AssetsCommandsApi.get_asset_snapshot(new_asset) | ||
|
||
get_asset_by_id(new_asset) # Checks that the asset was created with no errors | ||
self.assertIsInstance(asset_object,Asset) | ||
|
||
# Call delete_asset | ||
delete_asset() | ||
AssetsCommands.delete_asset() | ||
|
||
# Check that the asset was deleted | ||
try: | ||
get_asset_by_id(new_asset) | ||
except Exception: | ||
AssetsCommandsApi.get_asset_snapshot(new_asset) | ||
except: # Asset not found! | ||
self.assertTrue(True) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
from pieces.assets import AssetsCommands,AssetsCommandsApi | ||
from pieces.settings import Settings | ||
|
||
class TestEditCommand(unittest.TestCase): | ||
@patch('builtins.input', side_effect='y') | ||
@patch('pyperclip.paste', return_value='print("Hello, World!")') | ||
def test_edit_command(self,mock_paste,mock_buildins): | ||
Settings.startup() | ||
|
||
NAME = "TEST" | ||
CLASSIFICATION = "java" | ||
|
||
AssetsCommands.create_asset() # create new asset to test on | ||
|
||
AssetsCommands.edit_asset(name = NAME, classification=CLASSIFICATION) # change some asset meta data | ||
|
||
|
||
asset = AssetsCommandsApi.update_asset_snapshot(AssetsCommands.current_asset) | ||
|
||
self.assertEqual(asset.name,NAME) | ||
self.assertEqual(asset.formats.iterable[0].classification.specific.value.lower(),CLASSIFICATION.lower()) | ||
|
||
AssetsCommands.delete_asset() # Delete the created asset | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
from pieces.assets import AssetsCommands,AssetsCommandsApi | ||
from pieces.settings import Settings | ||
import sys | ||
from io import StringIO | ||
import random,os | ||
import json | ||
from pieces.utils import sanitize_filename | ||
|
||
class TestOpenSaveCommand(unittest.TestCase): | ||
def test_open_command(self,ITEM_INDEX=None): | ||
Settings.startup() | ||
stdout = sys.stdout | ||
sys.stdout = StringIO() | ||
|
||
|
||
assets_length = len(AssetsCommandsApi.get_assets_snapshot()) | ||
|
||
|
||
sys.stdout = StringIO() | ||
if not ITEM_INDEX: | ||
ITEM_INDEX = random.randint(1,assets_length) | ||
|
||
# Act | ||
AssetsCommands.open_asset(ITEM_INDEX = ITEM_INDEX) | ||
|
||
result_open = sys.stdout.getvalue() | ||
|
||
sys.stdout = stdout # Reset sys.stdout to its original state | ||
|
||
result_list = result_open.strip().split('\n') | ||
|
||
name = result_list[-6].removeprefix('Name: ') | ||
created_readable = result_list[-5].removeprefix('Created: ') | ||
updated_readable = result_list[-4].removeprefix('Updated: ') | ||
type = result_list[-3].removeprefix('Type: ') | ||
language = result_list[-2].removeprefix('Language: ') | ||
code_snippet_path = result_list[-1].removeprefix('Code: ') | ||
|
||
|
||
with open(Settings.extensions_dir) as f: | ||
language_extension_mapping = json.load(f) | ||
self.assertTrue(os.path.exists(code_snippet_path)) # assert that the code snippet file exists | ||
self.assertEqual(os.path.splitext(code_snippet_path)[-1], language_extension_mapping[language]) # assert that the file extension matches the language | ||
self.assertEqual(os.path.splitext(os.path.basename(code_snippet_path))[0], sanitize_filename(name)) | ||
|
||
return code_snippet_path # Return the code path to be tested for the save command | ||
|
||
@patch('builtins.input', side_effect=['y','y']) | ||
@patch('pyperclip.paste', return_value='print("Hello, World!")') | ||
def test_save_command(self, mock_paste,mock_buildins): | ||
Settings.startup() | ||
TEXT = "TEST SNIPPET CODE" | ||
# Call create_asset to create a new asset to test on | ||
AssetsCommands.create_asset() # Create a hello world asset | ||
|
||
AssetsCommandsApi.assets_snapshot = {AssetsCommands.current_asset:None} # Update the asset cache | ||
|
||
code_snippet_path = self.test_open_command(ITEM_INDEX=1) # Open the created asset | ||
|
||
with open(code_snippet_path,'w') as f: | ||
f.write(TEXT) | ||
|
||
AssetsCommands.update_asset() # Run the save | ||
|
||
|
||
code = AssetsCommandsApi.update_asset_snapshot(AssetsCommands.current_asset).formats.iterable[0].fragment.string.raw | ||
|
||
self.assertEqual(code, TEXT) # Check if the code was saved | ||
|
||
AssetsCommands.delete_asset() # Delete the asset | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.