Initial upload: curl-converter-cli with CI/CD workflow
This commit is contained in:
158
tests/test_generators.py
Normal file
158
tests/test_generators.py
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
"""Tests for code generators."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from curlconverter.parser import parse_curl
|
||||||
|
from curlconverter.generators import generate_code, get_supported_languages
|
||||||
|
|
||||||
|
|
||||||
|
class TestPythonGenerator:
|
||||||
|
"""Tests for Python code generator."""
|
||||||
|
|
||||||
|
def test_basic_get_request(self):
|
||||||
|
"""Test generating Python code for GET request."""
|
||||||
|
curl = "curl https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "python")
|
||||||
|
|
||||||
|
assert "import requests" in code
|
||||||
|
assert "https://api.example.com/users" in code
|
||||||
|
assert "requests.get" in code
|
||||||
|
|
||||||
|
def test_post_with_json(self):
|
||||||
|
"""Test generating Python code for POST with JSON."""
|
||||||
|
curl = 'curl -X POST -H "Content-Type: application/json" -d \'{"name":"test"}\' https://api.example.com/users'
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "python")
|
||||||
|
|
||||||
|
assert "requests.post" in code
|
||||||
|
assert "json=data" in code or "json=data" in code
|
||||||
|
|
||||||
|
def test_basic_auth(self):
|
||||||
|
"""Test generating Python code with basic auth."""
|
||||||
|
curl = "curl -u user:pass https://api.example.com"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "python")
|
||||||
|
|
||||||
|
assert "Authorization" in code or "requests" in code
|
||||||
|
|
||||||
|
def test_headers(self):
|
||||||
|
"""Test generating Python code with headers."""
|
||||||
|
curl = 'curl -H "Authorization: Bearer token" https://api.example.com'
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "python")
|
||||||
|
|
||||||
|
assert "headers" in code
|
||||||
|
|
||||||
|
|
||||||
|
class TestJavaScriptGenerator:
|
||||||
|
"""Tests for JavaScript code generator."""
|
||||||
|
|
||||||
|
def test_basic_get_request(self):
|
||||||
|
"""Test generating JavaScript code for GET request."""
|
||||||
|
curl = "curl https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "javascript")
|
||||||
|
|
||||||
|
assert "fetch" in code
|
||||||
|
assert "https://api.example.com/users" in code
|
||||||
|
assert "GET" in code
|
||||||
|
|
||||||
|
def test_post_with_data(self):
|
||||||
|
"""Test generating JavaScript code for POST."""
|
||||||
|
curl = "curl -X POST -d 'name=test' https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "javascript")
|
||||||
|
|
||||||
|
assert "POST" in code
|
||||||
|
assert "body" in code
|
||||||
|
|
||||||
|
|
||||||
|
class TestGoGenerator:
|
||||||
|
"""Tests for Go code generator."""
|
||||||
|
|
||||||
|
def test_basic_get_request(self):
|
||||||
|
"""Test generating Go code for GET request."""
|
||||||
|
curl = "curl https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "go")
|
||||||
|
|
||||||
|
assert "package main" in code
|
||||||
|
assert "net/http" in code
|
||||||
|
assert "https://api.example.com/users" in code
|
||||||
|
|
||||||
|
def test_post_request(self):
|
||||||
|
"""Test generating Go code for POST."""
|
||||||
|
curl = "curl -X POST -d 'name=test' https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "go")
|
||||||
|
|
||||||
|
assert "POST" in code
|
||||||
|
assert "bytes.NewBuffer" in code
|
||||||
|
|
||||||
|
|
||||||
|
class TestRubyGenerator:
|
||||||
|
"""Tests for Ruby code generator."""
|
||||||
|
|
||||||
|
def test_basic_get_request(self):
|
||||||
|
"""Test generating Ruby code for GET request."""
|
||||||
|
curl = "curl https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "ruby")
|
||||||
|
|
||||||
|
assert "require 'net/http'" in code
|
||||||
|
assert "Net::HTTP::Get" in code
|
||||||
|
assert "https://api.example.com/users" in code
|
||||||
|
|
||||||
|
def test_post_request(self):
|
||||||
|
"""Test generating Ruby code for POST."""
|
||||||
|
curl = "curl -X POST -d 'name=test' https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "ruby")
|
||||||
|
|
||||||
|
assert "Net::HTTP::Post" in code
|
||||||
|
|
||||||
|
|
||||||
|
class TestPHPGenerator:
|
||||||
|
"""Tests for PHP code generator."""
|
||||||
|
|
||||||
|
def test_basic_get_request(self):
|
||||||
|
"""Test generating PHP code for GET request."""
|
||||||
|
curl = "curl https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "php")
|
||||||
|
|
||||||
|
assert "<?php" in code
|
||||||
|
assert "curl_init" in code
|
||||||
|
assert "https://api.example.com/users" in code
|
||||||
|
|
||||||
|
def test_post_request(self):
|
||||||
|
"""Test generating PHP code for POST."""
|
||||||
|
curl = "curl -X POST -d 'name=test' https://api.example.com/users"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
code = generate_code(parsed, "php")
|
||||||
|
|
||||||
|
assert "CURLOPT_POST" in code or "CUSTOMREQUEST" in code
|
||||||
|
|
||||||
|
|
||||||
|
class TestSupportedLanguages:
|
||||||
|
"""Tests for supported languages."""
|
||||||
|
|
||||||
|
def test_get_supported_languages(self):
|
||||||
|
"""Test getting list of supported languages."""
|
||||||
|
languages = get_supported_languages()
|
||||||
|
|
||||||
|
assert "python" in languages
|
||||||
|
assert "javascript" in languages
|
||||||
|
assert "go" in languages
|
||||||
|
assert "ruby" in languages
|
||||||
|
assert "php" in languages
|
||||||
|
|
||||||
|
def test_unsupported_language_raises_error(self):
|
||||||
|
"""Test that unsupported language raises ValueError."""
|
||||||
|
curl = "curl https://example.com"
|
||||||
|
parsed = parse_curl(curl)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError) as exc_info:
|
||||||
|
generate_code(parsed, "unsupported_lang")
|
||||||
|
|
||||||
|
assert "Unsupported language" in str(exc_info.value)
|
||||||
Reference in New Issue
Block a user