Skip to content

Commit

Permalink
MR21: Add TDVT unit tests for tds mangling
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewrites committed Dec 5, 2023
1 parent 6adf741 commit 80f30ac
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tdvt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Run TDVT unit tests
run: |
cd tdvt/test
python tdvt_test.py -v CommandLineTest ConfigTest DiffTest PrintConfigurationsTest ResultsTest ResultsExceptionTest TestCreatorTest
python tdvt_test.py -v CommandLineTest ConfigTest DiffTest PrintConfigurationsTest ResultsTest ResultsExceptionTest TestCreatorTest MangleTest
10 changes: 10 additions & 0 deletions tdvt/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [2.9.5] - 2023-08-28
- Add regex/tds mangling tests.

## [2.9.4] - 2023-08-09
- Add more `dateadd` tests.

## [2.9.3] - 2023-07-26
- PR 1151 from the monolith, add missing metadata argument.

## [2.7.6]. - 2023-10-11
- Use quoting for column references in the expression tests.

Expand Down
56 changes: 33 additions & 23 deletions tdvt/tdvt/setup_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,32 +245,42 @@ def update_tds_files(name, connection_password_name):
def mangle_tds(file_path, connection_password_name):
print('Modifying ' + file_path)
try:
r1 = re.compile('(^\s*<named-connection .*? name=\').*?(\'>)')
r2 = re.compile('(^\s*<.*relation connection=\').*?(\' .*>)')
r3 = re.compile('(^\s*<connection .*?)(\s*/>)')

f = open(file_path, 'r')
new_tds = ''
for line in f:
new_line = line.rstrip()
m1 = r1.match(line)
if m1:
new_line = m1.group(1) + 'leaf' + m1.group(2)

m2 = r2.match(line)
if m2:
new_line = m2.group(1) + 'leaf' + m2.group(2)

m3 = r3.match(line)
if m3 and not 'tdvtconnection=\'' in line.lower():
new_line = m3.group(1) + ' tdvtconnection=\'' + connection_password_name + '\' ' + m3.group(2)

new_tds += new_line + '\n'

f.close()
with open(file_path, 'r') as f:
new_tds = updated_tds_as_str(f, connection_password_name)
f = open(file_path, 'w')
f.write(new_tds)
f.close()
except IOError as e:
print(e)
return

def get_tds_new_line(rmatch: Optional[re.Match[str]], mid_str: str, connection_password_name=None):
new_line = ''
new_line = rmatch.group(1) + mid_str + connection_password_name + '\' ' + rmatch.group(2)

return new_line


def updated_tds_as_str(f, connection_name) -> str:
r1 = re.compile('(^\s*<named-connection .*? name=\').*?(\'>)')
r2 = re.compile('(^\s*<.*relation connection=\').*?(\' .*>)')
r3 = re.compile('(^\s*<connection .*?).*?(\' .*>)')

new_tds = ''

for line in f:
new_line = line.rstrip()
m1 = r1.match(line)
if m1:
new_line = m1.group(1) + 'leaf' + m1.group(2)

m2 = r2.match(line)
if m2:
new_line = m2.group(1) + 'leaf' + m2.group(2)

m3 = r3.match(line)
if m3 and not 'tdvtconnection=\'' in line.lower():
new_line = m3.group(1) + 'tdvtconnection=\'' + connection_name + m3.group(2)

new_tds += new_line + '\n'
return new_tds
25 changes: 25 additions & 0 deletions tdvt/test/tdvt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io
import logging
import re
import shutil
import subprocess
import unittest
Expand All @@ -34,6 +35,7 @@
from tdvt.tabquery import *

from tdvt.config_gen.test_creator import TestCreator
from tdvt.setup_env import updated_tds_as_str


class DiffTest(unittest.TestCase):
Expand Down Expand Up @@ -1152,6 +1154,29 @@ def test_map_user_cols_to_test_cols_missing_t0(self):
'zzz': ['k', 's0', 's1', 'z']}
)

class MangleTest(unittest.TestCase):

with open('tool_test/tds/cast_calcs.tde.tds', 'r') as f:
new_lines = updated_tds_as_str(f, 'postgres_connection')

def test_mangletds_tdvtconnection(self):
self.assertIn(
"<connection tdvtconnection='postgres_connection' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2014' username='test' />",
self.new_lines
)

def test_mangletds_relation_connection(self):
self.assertIn(
"<relation connection='leaf' name='Calcs' table='[dbo].[Calcs]' type='table' />",
self.new_lines
)

def test_mangletds_named_connection(self):
self.assertIn(
"<named-connection caption='mssql2014' name='leaf'>",
self.new_lines
)


ROOT_DIRECTORY = pkg_resources.resource_filename(__name__, '')
TEST_DIRECTORY = pkg_resources.resource_filename(__name__, 'tool_test')
Expand Down

0 comments on commit 80f30ac

Please sign in to comment.