|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | +import requests |
| 5 | +import logging |
| 6 | +from unittest.mock import patch, MagicMock |
| 7 | +from struct_module.main import FileItem, validate_configuration, create_structure |
| 8 | + |
| 9 | +# Mock the OpenAI API response |
| 10 | +@patch('struct_module.openai.Completion.create') |
| 11 | +def test_process_prompt(mock_openai_create): |
| 12 | + mock_openai_create.return_value = MagicMock(choices=[MagicMock(text='Generated content from prompt')]) |
| 13 | + |
| 14 | + file_item = FileItem({"name": "generated_file.txt", "prompt": "Write a short story about a dragon."}) |
| 15 | + file_item.process_prompt() |
| 16 | + |
| 17 | + assert file_item.content == 'Generated content from prompt' |
| 18 | + mock_openai_create.assert_called_once_with( |
| 19 | + engine="davinci", |
| 20 | + prompt="Write a short story about a dragon.", |
| 21 | + max_tokens=100 |
| 22 | + ) |
| 23 | + |
| 24 | +# Test for validate_configuration with prompt |
| 25 | +def test_validate_configuration_with_prompt(): |
| 26 | + valid_structure = [ |
| 27 | + { |
| 28 | + "story.txt": { |
| 29 | + "prompt": "Write a short story about a dragon." |
| 30 | + } |
| 31 | + } |
| 32 | + ] |
| 33 | + invalid_structure = [ |
| 34 | + { |
| 35 | + "story.txt": { |
| 36 | + "prompt": 12345 # Invalid type |
| 37 | + } |
| 38 | + } |
| 39 | + ] |
| 40 | + |
| 41 | + validate_configuration(valid_structure) |
| 42 | + |
| 43 | + with pytest.raises(ValueError): |
| 44 | + validate_configuration(invalid_structure) |
| 45 | + |
| 46 | +# Test for creating a file with prompt content |
| 47 | +@patch('struct_module.openai.Completion.create') |
| 48 | +def test_create_structure_with_prompt(mock_openai_create): |
| 49 | + mock_openai_create.return_value = MagicMock(choices=[MagicMock(text='Generated content from prompt')]) |
| 50 | + |
| 51 | + structure = [ |
| 52 | + { |
| 53 | + "story.txt": { |
| 54 | + "prompt": "Write a short story about a dragon." |
| 55 | + } |
| 56 | + } |
| 57 | + ] |
| 58 | + |
| 59 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 60 | + create_structure(tmpdirname, structure) |
| 61 | + |
| 62 | + story_path = os.path.join(tmpdirname, "story.txt") |
| 63 | + assert os.path.exists(story_path) |
| 64 | + with open(story_path, 'r') as f: |
| 65 | + assert f.read() == 'Generated content from prompt' |
| 66 | + |
| 67 | +# Test for dry run with prompt |
| 68 | +@patch('struct_module.openai.Completion.create') |
| 69 | +def test_dry_run_with_prompt(mock_openai_create, caplog): |
| 70 | + mock_openai_create.return_value = MagicMock(choices=[MagicMock(text='Generated content from prompt')]) |
| 71 | + |
| 72 | + structure = [ |
| 73 | + { |
| 74 | + "story.txt": { |
| 75 | + "prompt": "Write a short story about a dragon." |
| 76 | + } |
| 77 | + } |
| 78 | + ] |
| 79 | + |
| 80 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 81 | + with caplog.at_level(logging.INFO): |
| 82 | + create_structure(tmpdirname, structure, dry_run=True) |
| 83 | + |
| 84 | + assert not os.path.exists(os.path.join(tmpdirname, "story.txt")) |
| 85 | + assert any("[DRY RUN] Would create file:" in message for message in caplog.messages) |
| 86 | + assert any("Generated content from prompt" in message for message in caplog.messages) |
| 87 | + |
| 88 | +# Mocking requests.get for testing fetch_remote_content within create_structure with prompt |
| 89 | +@patch('struct_module.requests.get') |
| 90 | +@patch('struct_module.openai.Completion.create') |
| 91 | +def test_create_structure_with_remote_content_and_prompt(mock_openai_create, mock_get): |
| 92 | + mock_get.return_value.status_code = 200 |
| 93 | + mock_get.return_value.text = "Remote content" |
| 94 | + mock_openai_create.return_value = MagicMock(choices=[MagicMock(text='Generated content from prompt')]) |
| 95 | + |
| 96 | + structure = [ |
| 97 | + { |
| 98 | + "LICENSE": { |
| 99 | + "file": "https://example.com/mock" |
| 100 | + } |
| 101 | + }, |
| 102 | + { |
| 103 | + "story.txt": { |
| 104 | + "prompt": "Write a short story about a dragon." |
| 105 | + } |
| 106 | + } |
| 107 | + ] |
| 108 | + |
| 109 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 110 | + create_structure(tmpdirname, structure) |
| 111 | + |
| 112 | + license_path = os.path.join(tmpdirname, "LICENSE") |
| 113 | + story_path = os.path.join(tmpdirname, "story.txt") |
| 114 | + |
| 115 | + assert os.path.exists(license_path) |
| 116 | + assert os.path.exists(story_path) |
| 117 | + |
| 118 | + with open(license_path, 'r') as f: |
| 119 | + assert f.read() == "Remote content" |
| 120 | + |
| 121 | + with open(story_path, 'r') as f: |
| 122 | + assert f.read() == 'Generated content from prompt' |
0 commit comments