19from gams
import GamsWorkspace, GamsSet
91def same_gdx_dump(ws, gdxfile, expected_result):
94 os.path.join(ws.system_directory,
"gdxdump"),
95 os.path.join(ws.working_directory, gdxfile),
98 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE
100 result, error = p.communicate()
102 raise Exception(error)
103 result = result.decode()
104 return re.sub(
r"\s",
"", result.lower()) == re.sub(
105 r"\s",
"", expected_result.lower()
113 f
"GamsDatabase '{prefix}' should have five symbols: i, j, ij, a, aa."
117 if db[
"ii"].name !=
"i":
118 raise Exception(f
"{prefix}: We should get set 'i' when asking for alias 'ii'.")
119 if db[
"jj"].name !=
"j":
120 raise Exception(f
"{prefix}: We should get set 'j' when asking for alias 'jj'.")
121 if db[
"iijj"].name !=
"ij":
123 f
"{prefix}: We should get set 'ij' when asking for alias 'iijj'."
127 if not db.check_domains():
128 raise Exception(f
"{prefix}: Check domains should be true.")
129 if not isinstance(db[
"aa"].domains[0], GamsSet):
130 raise Exception(f
"{prefix}: domains[0] of 'aa' should be a set.")
131 if db[
"aa"].domains[0].name !=
"i":
132 raise Exception(f
"{prefix}: domains[0] of 'aa' should be 'i'.")
134 db[
"ii"].delete_record(
"i1")
135 if db.check_domains():
137 f
"{prefix}: check_domains() should be False after removal of 'i1'."
139 db[
"ii"].add_record(
"i1")
140 if not db.check_domains():
142 f
"{prefix}: check_domains() should be True after adding 'i1' again."
146if __name__ ==
"__main__":
147 sys_dir = sys.argv[1]
if len(sys.argv) > 1
else None
148 ws = GamsWorkspace(system_directory=sys_dir)
152 job = ws.add_job_from_string(GAMS_DATA)
155 job.out_db.export(
"outdb.gdx")
157 raise Exception(
"Unexpected result of gdxdump 'outdb.gdx'.")
160 db = ws.add_database(source_database=job.out_db)
164 raise Exception(
"Unexpected result of gdxdump 'db.gdx'.")
165 db2 = ws.add_database()
166 ii = db2.add_set_dc(db[
"ii"].name, [
"*"], db[
"ii"].text)
167 db[
"ii"].copy_symbol(ii)
170 aa = db2.add_parameter_dc(db[
"aa"].name, [ii], db[
"aa"].text)
171 aa_orig.copy_symbol(aa)
172 db2.export(
"db2.gdx")
174 raise Exception(
"Unexpected result of gdxdump 'db2.gdx'.")
178 if aa_orig.domains[0].name !=
"i":
179 raise Exception(
"The domain set should be the original set.")
180 if aa_orig.domains_as_strings[0] !=
"ii":
181 raise Exception(
"The domain as string should be the alias name.")
def check_alias_logic(prefix, db)
def same_gdx_dump(ws, gdxfile, expected_result)