|
|
@@ -171,3 +171,73 @@ def test_render_template_empty_pk():
|
|
|
table='t', columns=[('a', '')], pk='',
|
|
|
)
|
|
|
assert 'splitPk = \n' in out
|
|
|
+
|
|
|
+
|
|
|
+def _patch_main_dependencies(monkeypatch):
|
|
|
+ """共享 mock:让 main() 不连真 PG / 真 datasource。"""
|
|
|
+ fake_ds = MagicMock()
|
|
|
+ fake_ds.parse.return_value = {
|
|
|
+ GEN.DS_POSTGRE_SQL_JDBC_URL: 'jdbc:postgresql://10.0.0.1:5432/mydb',
|
|
|
+ 'username': 'u',
|
|
|
+ 'password': 'p',
|
|
|
+ }
|
|
|
+ monkeypatch.setattr(GEN, 'resolve_datasource', lambda ref: fake_ds)
|
|
|
+
|
|
|
+ fake_conn = MagicMock()
|
|
|
+ fake_cur = fake_conn.cursor.return_value
|
|
|
+ fake_cur.fetchall.return_value = [
|
|
|
+ (1, 'id', 'id', 'bigint', 'PK'),
|
|
|
+ (2, 'name', '名称', 'character varying', ''),
|
|
|
+ ]
|
|
|
+ fake_pg8000 = MagicMock()
|
|
|
+ fake_pg8000.dbapi.connect.return_value = fake_conn
|
|
|
+ monkeypatch.setitem(sys.modules, 'pg8000', fake_pg8000)
|
|
|
+ monkeypatch.setitem(sys.modules, 'pg8000.dbapi', fake_pg8000.dbapi)
|
|
|
+
|
|
|
+
|
|
|
+def test_main_stdout_only_when_no_o(monkeypatch, capsys):
|
|
|
+ _patch_main_dependencies(monkeypatch)
|
|
|
+ monkeypatch.setattr(sys, 'argv', [
|
|
|
+ 'datax-sync-template-gen.py',
|
|
|
+ '-ds', 'postgresql/prod-hobby',
|
|
|
+ '-t', 'public.users',
|
|
|
+ ])
|
|
|
+ GEN.main()
|
|
|
+ captured = capsys.readouterr()
|
|
|
+ assert '| 序号 | 字段名 |' in captured.out
|
|
|
+ assert '[reader]' in captured.out
|
|
|
+ assert '已写入' not in captured.err
|
|
|
+
|
|
|
+
|
|
|
+def test_main_stdout_and_disk_when_o_with_dir(monkeypatch, capsys, tmp_path):
|
|
|
+ _patch_main_dependencies(monkeypatch)
|
|
|
+ out_dir = tmp_path / 'out'
|
|
|
+ monkeypatch.setattr(sys, 'argv', [
|
|
|
+ 'datax-sync-template-gen.py',
|
|
|
+ '-ds', 'postgresql/prod-hobby',
|
|
|
+ '-t', 'public.users',
|
|
|
+ '-o', str(out_dir),
|
|
|
+ ])
|
|
|
+ GEN.main()
|
|
|
+ captured = capsys.readouterr()
|
|
|
+ assert '| 序号 | 字段名 |' in captured.out
|
|
|
+ assert '[reader]' in captured.out
|
|
|
+ assert '已写入' in captured.err
|
|
|
+ assert (out_dir / 'users.md').exists()
|
|
|
+ assert (out_dir / 'users.ini').exists()
|
|
|
+
|
|
|
+
|
|
|
+def test_main_stdout_and_disk_when_o_no_value(monkeypatch, capsys, tmp_path):
|
|
|
+ _patch_main_dependencies(monkeypatch)
|
|
|
+ monkeypatch.setattr(GEN, 'WORKSPACE_DEFAULT', str(tmp_path / 'workspace'))
|
|
|
+ monkeypatch.setattr(sys, 'argv', [
|
|
|
+ 'datax-sync-template-gen.py',
|
|
|
+ '-ds', 'postgresql/prod-hobby',
|
|
|
+ '-t', 'public.users',
|
|
|
+ '-o',
|
|
|
+ ])
|
|
|
+ GEN.main()
|
|
|
+ captured = capsys.readouterr()
|
|
|
+ assert '| 序号 | 字段名 |' in captured.out
|
|
|
+ assert '[reader]' in captured.out
|
|
|
+ assert '已写入' in captured.err
|