Source code for pyleecan.Tests.GUI.DMatLib.test_save_load_matlib

# -*- coding: utf-8 -*-

import sys
from os import listdir, mkdir, remove
from os.path import abspath, basename, isdir, isfile, join
from shutil import copyfile, rmtree
from unittest import TestCase

from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QDialogButtonBox

from ....Functions.load import load
from ....GUI.Dialog.DMachineSetup.DMachineSetup import DMachineSetup
from ....Tests import DATA_DIR
from ....Tests import save_load_path as save_path


[docs]class test_save_load_matlib(TestCase): """Test that the widget DMachineSetup and DMatLib can save/load the MatLib (old and new) """
[docs] def setUp(self): """Run at the begining of every test to create the workspace """ self.work_path = join(save_path, "Material") # Delete old test if needed if isdir(self.work_path): rmtree(self.work_path) mkdir(self.work_path)
[docs] def teardown(self): """Delete the workspace at the end of the tests """ rmtree(self.work_path)
[docs] @classmethod def setUpClass(cls): """Start the app for the test""" print("\nStart Test Save/Load MatLib") cls.app = QtWidgets.QApplication(sys.argv)
[docs] @classmethod def tearDownClass(cls): """Exit the app after the test""" cls.app.quit()
[docs] def test_load_save_several_file(self): """Check that you can load/save several machine files """ # Copy the matlib mkdir(join(self.work_path, "Lamination")) mkdir(join(self.work_path, "Magnet")) copyfile( join(DATA_DIR, "Material", "Magnet1.json"), join(self.work_path, "Magnet", "Magnet1.json"), ) cop_path = join(self.work_path, "Copper1.json") copyfile(join(DATA_DIR, "Material", "Copper1.json"), cop_path) copyfile( join(DATA_DIR, "Material", "Insulator1.json"), join(self.work_path, "Insulator1.json"), ) lam_path = join(self.work_path, "Lamination", "M400-50A.json") copyfile(join(DATA_DIR, "Material", "M400-50A.json"), lam_path) # Check initial state nb_file = len( [ name for name in listdir(self.work_path) if isfile(join(self.work_path, name)) and name[-5:] == ".json" ] ) self.assertEqual(nb_file, 2) # Start the GUI self.widget = DMachineSetup( machine=None, machine_path=self.work_path, matlib_path=self.work_path ) # Check load of the matlib self.assertEqual(len(self.widget.matlib), 4) self.assertEqual( ["Copper1", "Insulator1", "M400-50A", "Magnet1"], [mat.name for mat in self.widget.matlib], ) self.assertEqual(self.widget.matlib[0].elec.rho, 1.73e-8) self.assertEqual(self.widget.matlib[0].HT.alpha, 0.00393) self.assertEqual( self.widget.matlib[0].path, join(self.work_path, "Copper1.json") ) self.assertEqual(self.widget.matlib[2].mag.mur_lin, 2500) self.assertEqual(self.widget.matlib[2].struct.rho, 7650) self.assertEqual(self.widget.matlib[2].struct.Ex, 215000000000) self.assertEqual( self.widget.matlib[2].path, join(self.work_path, "Lamination", "M400-50A.json"), ) # Change value of materials self.widget.matlib[0].elec.rho = 1.74e-8 self.widget.matlib[0].HT.alpha = 0.00555 self.widget.matlib[2].mag.mur_lin = 2501.2 self.widget.matlib[2].struct.rho = 76 # Save matlib for mat in self.widget.matlib: mat.save(mat.path) mat2 = load(mat.path) self.assertEqual(mat.as_dict(), mat2.as_dict())
[docs]def compare_file(file_path1, file_path2): # name1 = basename(file_path1) # name2 = basename(file_path2) # if name1 != name2: # return "Different file name: (" + name1 + ", " + name2 + ")" with open(file_path1) as file1: with open(file_path2) as file2: lines1 = file1.readlines() lines2 = file2.readlines() if len(lines1) != len(lines2): return ( "Different file length: (" + str(len(lines1)) + ", " + str(len(lines2)) + ")" ) for ii in range(len(lines1)): if lines1[ii] != lines2[ii]: return ( "Different line " + str(ii) + ":\n" + lines1[ii] + "\n" + lines2[ii] ) return None