test_sync_template_gen.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding:utf-8 -*-
  2. """
  3. datax-sync-template-gen 模板渲染 + JDBC URL 解析单测。
  4. 不连真 PG(query_columns_full 走 mock conn)。
  5. 脚本路径含连字符,用 importlib.util 动态加载为模块。
  6. """
  7. import importlib.util
  8. import os
  9. import sys
  10. from unittest.mock import MagicMock
  11. import pytest
  12. PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
  13. SCRIPT_PATH = os.path.join(PROJECT_ROOT, 'bin', 'datax-sync-template-gen.py')
  14. def _load_script():
  15. spec = importlib.util.spec_from_file_location('datax_sync_template_gen', SCRIPT_PATH)
  16. mod = importlib.util.module_from_spec(spec)
  17. sys.modules['datax_sync_template_gen'] = mod
  18. spec.loader.exec_module(mod)
  19. return mod
  20. GEN = _load_script()
  21. def test_parse_jdbc_url_with_port():
  22. host, port, db = GEN.parse_jdbc_url('jdbc:postgresql://10.0.0.1:5433/hobby_stocks')
  23. assert host == '10.0.0.1'
  24. assert port == 5433
  25. assert db == 'hobby_stocks'
  26. def test_parse_jdbc_url_default_port():
  27. host, port, db = GEN.parse_jdbc_url('jdbc:postgresql://pg.example.com/mydb')
  28. assert host == 'pg.example.com'
  29. assert port == 5432
  30. assert db == 'mydb'
  31. def test_parse_jdbc_url_invalid():
  32. with pytest.raises(ValueError, match='无法解析'):
  33. GEN.parse_jdbc_url('mysql://10.0.0.1:3306/foo')
  34. def test_render_template_includes_required_fields():
  35. columns = [('id', 'id'), ('name', '姓名'), ('create_time', '创建时间')]
  36. out = GEN.render_template(
  37. ds_ref='postgresql/prod-hobby',
  38. database='hobby_stocks',
  39. schema='public',
  40. table='users',
  41. columns=columns,
  42. pk='id',
  43. )
  44. assert 'dataSource = postgresql/prod-hobby' in out
  45. assert 'database = hobby_stocks' in out
  46. assert 'table = public.users' in out
  47. assert 'column = id,name,create_time' in out
  48. assert 'splitPk = id' in out
  49. assert "where = update_time >= '${start_date}' AND update_time < '${stop_date}'" in out
  50. assert 'path = /user/hive/warehouse/raw.db/users_TODO_d/dt=${dt}/' in out
  51. assert 'fileName = users_TODO_d' in out
  52. def test_query_columns_full_returns_full_metadata():
  53. conn = MagicMock()
  54. cur = conn.cursor.return_value
  55. cur.fetchall.return_value = [
  56. (1, 'id', 'id', 'bigint', 'PK'),
  57. (2, 'name', '名称', 'character varying', ''),
  58. ]
  59. rows = GEN.query_columns_full(conn, 'public', 'orders')
  60. assert rows == [
  61. (1, 'id', 'id', 'bigint', 'PK'),
  62. (2, 'name', '名称', 'character varying', ''),
  63. ]
  64. def test_render_schema_md_table_format():
  65. rows = [
  66. (1, 'id', 'id', 'bigint', 'PK'),
  67. (2, 'user_name', '用户名', 'character varying', ''),
  68. (3, 'create_time', None, 'timestamp without time zone', ''), # 无注释
  69. ]
  70. out = GEN.render_schema_md(rows)
  71. assert '| 序号 | 字段名 | 中文名 | 数据类型 | 主键标识 | 裁剪类型 |' in out
  72. assert '| 1 | `id` | id | bigint | PK | |' in out
  73. assert '| 2 | `user_name` | 用户名 | character varying | | |' in out
  74. assert '| 3 | `create_time` | | timestamp without time zone | | |' in out
  75. def test_render_template_empty_pk():
  76. out = GEN.render_template(
  77. ds_ref='postgresql/prod-hobby', database='db', schema='public',
  78. table='t', columns=[('a', '')], pk='',
  79. )
  80. assert 'splitPk = \n' in out