1
- import psycopg2
1
+ import logging
2
2
import os
3
- import ace
3
+ import psycopg2
4
4
import pytest
5
-
5
+ import tempfile
6
+ import ace
7
+ from camp .generators .lib .campch import yang_to_sql
6
8
from .util import ADMS_DIR , adm_files , run_camp
7
- from camp .generators .create_sql import yang_to_sql
9
+
10
+ LOGGER = logging .getLogger (__name__ )
8
11
9
12
10
13
@pytest .fixture (scope = "session" , autouse = True )
@@ -27,10 +30,9 @@ def setup():
27
30
cursor = conn .cursor ()
28
31
29
32
# reusable objects that the tests will need
30
- yield cursor ,
33
+ yield conn ,
31
34
32
35
# teardown: close connections
33
- cursor .close ()
34
36
conn .close ()
35
37
36
38
@@ -41,22 +43,31 @@ def test_adms(setup, adm):
41
43
Resulting sql files will be placed in ADMS_DIR/amp-sql/Agent_Scripts and executed in the anms library.
42
44
"""
43
45
44
- if adm == 'ietf-amm.yang' : # doesn't have unique enum
45
- pytest .xfail ("ADM with known issue" )
46
-
47
- cursor = setup [0 ]
46
+ conn = setup [0 ]
48
47
49
48
# input file full filepath
50
49
filepath = os .path .join (ADMS_DIR , adm )
51
50
51
+ # output to temporary
52
+ adm_set = ace .AdmSet ()
53
+ norm_name = adm_set .load_from_file (filepath ).norm_name
54
+ filename = f"{ yang_to_sql (norm_name )} .sql"
55
+ LOGGER .info ('Expecting SQL source %s' , filename )
56
+
57
+ outdir = tempfile .TemporaryDirectory ()
58
+
52
59
# run camp
53
- exitcode = run_camp (filepath , ADMS_DIR , only_sql = True , only_ch = False )
60
+ exitcode = run_camp (filepath , outdir . name , only_sql = True , only_ch = False )
54
61
assert 0 == exitcode
55
62
56
- # execute sql
57
- adm_set = ace .AdmSet ()
58
- norm_name = yang_to_sql (adm_set .load_from_file (filepath ).norm_name )
59
- sql_file = os .path .join (ADMS_DIR , "amp-sql" , "Agent_Scripts" , 'adm_{name}.sql' .format (name = norm_name ))
60
- with open (sql_file , "r" ) as f :
61
- cursor .execute (f .read ())
62
- cursor .execute ("rollback" )
63
+ # verify the generated source executes
64
+ file_path = os .path .join (outdir .name , filename )
65
+ with open (file_path , "r" ) as srcfile :
66
+ script = srcfile .read ()
67
+ LOGGER .info ('Generated script:\n %s' , script )
68
+
69
+ try :
70
+ with conn .cursor () as curs :
71
+ curs .execute (script )
72
+ finally :
73
+ conn .rollback ()
0 commit comments