ExtensionesTest_ewmante.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using sage.ew.ewbase;
6 using System.Reflection;
7 using Microsoft.VisualStudio.TestTools.UnitTesting;
8 using sage.ew.interficies;
9 
10 namespace Sage.ES.S50.UnitTest
11 {
12  static partial class ExtensionesTest
13  {
14  #region PE-93059 : Extensiones para tipo ewMante
15 
20  public static void __Test_Constructores(this ewMante toObjeto)
21  {
22  //Comprobar número de constructors
23  ConstructorInfo[] p = toObjeto.GetType().GetConstructors();
24  if (p.Length < 2)
25  Assert.Inconclusive("El mantenimiento debe tener como mínimo dos constructores (con y sin código)");
26 
27  //TODO Comprobar els valors de les propietats privades de la clase ja que es defineixen al constructor?
28  }
29 
37  public static void __Test_EwMante_New(this IMante toObjeto, string tcCodigo, bool tlDeleteOnFinish = true, RequiredValues toRequiredValues = null)
38  {
39  Type loType = toObjeto.GetType();
40  IMante target = (IMante)Activator.CreateInstance(loType);
41  IMante loadedTarget = (IMante)Activator.CreateInstance(loType);
42  //dynamic target = loType.GetConstructor(new Type[] { }).Invoke(new object[] { });
43  //dynamic loadedTarget = loType.GetConstructor(new Type[] { }).Invoke(new object[] { });
44 
45  bool llResult = false;
46  string newNombre = "Registro para " + tcCodigo;
47 
48  //Primera prova : Comrpobem que el nou codi no existexi a la BD
49  target._Codigo = tcCodigo;
50  Assert.AreEqual<bool>(false, target._Existe_Registro(), "No se ha puede hacer el test _New puesto que el código " + tcCodigo + " ya existe en la base de datos");
51 
52  //Segona prova: guardar sense definir _Nombre
53  llResult = target._Save();
54  Assert.AreEqual<bool>(false, llResult, "Se ha guardado el registro con código " + tcCodigo + " sin definir su nombre");
55  //Nota: Tal i com estan els manteniments, el fet de navegar a un codi inexistent genera el registre a la BD sense nom, per lo que aquesta validació falla.
56 
57  //Tercera prova: guardar manteniment amb _Codigo i _Nombre
58  target._Nombre = newNombre;
59  toRequiredValues?.FillRequiredValues(target);
60  llResult = target._Save();
61  Assert.AreEqual<bool>(true, llResult, "No se ha guardado el nuevo registro con código " + tcCodigo + ". _Error_Message:" + target._Error_Message);
62  //Carreguem el nou registre en un nou objecte
63  loadedTarget._Codigo = tcCodigo;
64  loadedTarget._Load();
65  //Comprobem codi i nom
66  Assert.AreEqual<bool>(true, loadedTarget._Existe_Registro(), "No se ha encontrado el nuevo registro '" + loadedTarget._Codigo + "' en la base de datos");
67  Assert.AreEqual<string>(tcCodigo, loadedTarget._Codigo, "El campo _Codigo no coincide (" + loadedTarget._Codigo + " - " + tcCodigo + ").");
68  Assert.AreEqual<string>(newNombre, loadedTarget._Nombre, "El campo _Nombre no coincide (" + loadedTarget._Nombre + " - " + newNombre + ").");
69 
70  if (tlDeleteOnFinish)
71  {
72  //Quarta prova : Esborrem el registre generat y comprobem que no existeixi
73  target._Delete();
74  Assert.AreEqual<bool>(false, loadedTarget._Existe_Registro(), "No se ha borrado el registro '" + loadedTarget._Codigo + "' de la base de datos. _Error_Message:" + target._Error_Message);
75  }
76  }
77 
85  public static void __Test_EwMante_Load(this IMante toObjeto, string tcCodigo, string tcCodigoNew = "", RequiredValues toRequiredValues = null)
86  {
87  Type loType = toObjeto.GetType();
88  IMante target = (IMante)Activator.CreateInstance(loType);
89 
90  bool llDelete = false;
91  if (string.IsNullOrWhiteSpace(tcCodigo))
92  {
93  Assert.AreNotEqual<string>(String.Empty, tcCodigoNew, "Load generando registro: No se ha pasado el código para generar un nuevo registro");
94  target._Codigo = tcCodigo;
95  Assert.AreEqual<bool>(false, target._Existe_Registro(), "Load generando registro: Se ha encontrado el registro '" + tcCodigo + "' en la base de datos");
96  toObjeto.__Test_EwMante_New(tcCodigoNew, tlDeleteOnFinish: false, toRequiredValues: toRequiredValues);
97  tcCodigo = tcCodigoNew;
98  llDelete = true;
99  }
100 
101  //Existeix el codi?
102  target._Codigo = tcCodigo;
103  Assert.AreEqual<bool>(true, target._Existe_Registro(), "No se ha encontrado el registro '" + tcCodigo + "' en la base de datos");
104 
105  //Carreguem dades
106  target._Load();
107  //Comprobem _Codigo y _Nombre
108  Assert.AreEqual<string>(tcCodigo, target._Codigo, "El campo _Codigo no coincide (" + tcCodigo + ").");
109  Assert.AreNotEqual<string>(String.Empty, target._Nombre, "El campo _Nombre esta vacío (" + tcCodigo + ")");
110 
111  if (llDelete)
112  {
113  IMante loadedTarget = (IMante)Activator.CreateInstance(loType);
114  //Carreguem el nou registre en un nou objecte
115  loadedTarget._Codigo = tcCodigo;
116  loadedTarget._Load();
117  //Esborrem el registre generat y comprobem que no existeixi
118  target._Delete();
119  Assert.AreEqual<bool>(false, loadedTarget._Existe_Registro(), "Load generando registro: No se ha borrado el registro '" + loadedTarget._Codigo + "' de la base de datos. _Error_Message:" + loadedTarget._Error_Message);
120  }
121  }
122 
130  public static void __Test_EwMante_Save(this IMante toObjeto, string tcCodigo, string tcCodigoNew = "", RequiredValues toRequiredValues = null)
131  {
132  Type loType = toObjeto.GetType();
133  IMante target = (IMante)Activator.CreateInstance(loType);
134  IMante loadedTarget = (IMante)Activator.CreateInstance(loType);
135 
136  bool llDelete = false;
137  if (string.IsNullOrWhiteSpace(tcCodigo))
138  {
139  Assert.AreNotEqual<string>(String.Empty, tcCodigoNew, "Save generando registro: No se ha pasado el código para generar un nuevo registro");
140  target._Codigo = tcCodigo;
141  Assert.AreEqual<bool>(false, target._Existe_Registro(), "Save generando registro: Se ha encontrado el registro '" + tcCodigo + "' en la base de datos");
142  toObjeto.__Test_EwMante_New(tcCodigoNew, tlDeleteOnFinish: false, toRequiredValues: toRequiredValues);
143  tcCodigo = tcCodigoNew;
144  llDelete = true;
145  }
146 
147  string lcNombreOriginal = string.Empty;
148  string lcNombreModificado = string.Empty;
149 
150  //Carreguem el nou registre en un nou objecte
151  target._Codigo = tcCodigo;
152  //Comprobem que el registre existeixi
153  Assert.AreEqual<bool>(true, target._Existe_Registro(), "No se ha encontrado el registro '" + tcCodigo + "' en la base de datos");
154  target._Load();
155  //El manteniment no pot tindre el nom en blanc
156  Assert.AreNotEqual<string>(String.Empty, target._Nombre, "El campo _Nombre del registro esta vacío (" + tcCodigo + ")");
157  //Modifiquem el nom i guardem
158  lcNombreOriginal = target._Nombre;
159  //Si s'ha quedat algun registre amb error sense modificar al seu estat original el modifiquem com estaria inicialment
160  if (lcNombreOriginal.StartsWith("(TEST)"))
161  lcNombreModificado = lcNombreOriginal.Substring(6);
162  else
163  lcNombreModificado = "(TEST)" + lcNombreOriginal;
164  target._Nombre = lcNombreModificado;
165  target._Save();
166 
167  //Comprobem el _Nombre amb un nou objecte
168  loadedTarget._Codigo = tcCodigo;
169  loadedTarget._Load();
170 
171  //Task 105067 Excepciones por substring fuera de rango => es un ínidice base 0
172  if (loadedTarget._Nombre.Length < lcNombreModificado.Length)
173  lcNombreModificado = loadedTarget._Nombre; //La modificación excede el campo nombre => Recorto
174 
175  Assert.AreNotEqual<string>(String.Empty, loadedTarget._Nombre, "El campo _Nombre del registro modificado esta vacío (" + tcCodigo + ")");
176 
177  Assert.AreEqual<string>((lcNombreModificado.Length > loadedTarget._Nombre.Length) ?
178  lcNombreModificado.Substring(6, loadedTarget._Nombre.Length) :
179  lcNombreModificado, loadedTarget._Nombre, "El campo _Nombre del registro modificado no coincide (" + tcCodigo + ").");
180 
181  //Deixem el manteniment com al principi
182  // Hacer un Load para detectar el cambio en el nombre
183  target._Load();
184  target._Nombre = lcNombreOriginal;
185  target._Save();
186 
187  //Tornem a comprobar-ho
188  //Volvemos a crear objeto loaderTarget ya que sino, no vuelve a cargar los datos (debido al cacheo que realiza internamente (JBuitrago)
189  loadedTarget = null;
190  loadedTarget = (IMante)Activator.CreateInstance(loType);
191  loadedTarget._Codigo = tcCodigo;
192  loadedTarget._Load();
193  Assert.AreNotEqual<string>(String.Empty, loadedTarget._Nombre, "El campo _Nombre del registro modificado esta vacío (" + tcCodigo + ")");
194  Assert.AreEqual<string>((lcNombreOriginal.Length > loadedTarget._Nombre.Length) ?
195  lcNombreOriginal.Substring(0, loadedTarget._Nombre.Length) :
196  lcNombreOriginal, loadedTarget._Nombre, "El campo _Nombre del registro modificado no coincide (" + tcCodigo + ").");
197 
198  if (llDelete)
199  {
200  IMante loadedTarget2 = (IMante)Activator.CreateInstance(loType);
201  //Carreguem el nou registre en un nou objecte
202  loadedTarget2._Codigo = tcCodigo;
203  loadedTarget2._Load();
204  //Esborrem el registre generat y comprobem que no existeixi
205  target._Delete();
206  Assert.AreEqual<bool>(false, loadedTarget2._Existe_Registro(), "Load generando registro: No se ha borrado el registro '" + loadedTarget2._Codigo + "' de la base de datos. _Error_Message:" + loadedTarget2._Error_Message);
207  }
208  }
209 
218  public static void __Test_EwMante_Delete(this IMante toObjeto, string tcCodigo, string tcCodigoNew = "", RequiredValues toRequiredValues = null)
219  {
220  Type loType = toObjeto.GetType();
221  IMante target = (IMante)Activator.CreateInstance(loType);
222  IMante loadedTarget = (IMante)Activator.CreateInstance(loType);
223 
224  bool llDelete = false;
225  if (string.IsNullOrWhiteSpace(tcCodigo))
226  {
227  Assert.AreNotEqual<string>(String.Empty, tcCodigoNew, "Delete generando registro: No se ha pasado el código para generar un nuevo registro");
228  target._Codigo = tcCodigo;
229  Assert.AreEqual<bool>(false, target._Existe_Registro(), "Delete generando registro: Se ha encontrado el registro '" + tcCodigo + "' en la base de datos");
230  toObjeto.__Test_EwMante_New(tcCodigoNew, tlDeleteOnFinish: false, toRequiredValues: toRequiredValues);
231  tcCodigo = tcCodigoNew;
232  llDelete = true;
233  }
234 
235  //Primera prova : Comrpobem que el nou codi no existexi a la BD
236  target._Codigo = tcCodigo;
237  if (!target._Existe_Registro())
238  {
239  //El registro no existe. Lo creamos nuevo y luego lo borramos
240  target._Codigo = tcCodigo;
241  target._Nombre = "Registro temporal";
242  toRequiredValues?.FillRequiredValues(target);
243  target._Save();
244  //Comrpobem que el nou registre s'hagi creat a la BD
245  Assert.AreEqual<bool>(true, target._Existe_Registro(), "No se ha puede hacer el test _Delete puesto que no se ha podido crear el registro con código " + tcCodigo);
246  }
247  else
248  {
249  //Nou objecte per tornar a crear el registre despres d'esborrarlo
250  loadedTarget._Codigo = target._Codigo;
251  loadedTarget._Load();
252  }
253 
254  //Borrem el registre
255  if (target._Delete())
256  {
257  Assert.AreEqual<bool>(false, target._Existe_Registro(), "No se ha borrado el registro con código " + tcCodigo + ". _Error_Message:" + target._Error_Message);
258  //Regenerem el registre si cal
259  if (!string.IsNullOrWhiteSpace(loadedTarget._Codigo))
260  {
261  loadedTarget._Save();
262  Assert.AreEqual<bool>(true, loadedTarget._Existe_Registro(), "No se ha podido regenerar el registro con código " + tcCodigo + ". _Error_Message:" + target._Error_Message);
263  }
264  }
265  else
266  {
267  //No s'ha pogut esborrar. Comprobem que s'hagi definit el perque a la propietat _Error_Message
268  Assert.AreNotEqual<string>(String.Empty, target._Error_Message, "No se ha borrado el registro con código " + tcCodigo + " ni se ha informado el porque en _Error_Message");
269  }
270 
271  if (llDelete)
272  {
273  IMante loadedTarget2 = (IMante)Activator.CreateInstance(loType);
274  //Carreguem el nou registre en un nou objecte
275  loadedTarget2._Codigo = tcCodigo;
276  loadedTarget2._Load();
277  //Esborrem el registre generat y comprobem que no existeixi
278  target._Delete();
279  Assert.AreEqual<bool>(false, loadedTarget2._Existe_Registro(), "Load generando registro: No se ha borrado el registro '" + loadedTarget2._Codigo + "' de la base de datos. _Error_Message:" + loadedTarget2._Error_Message);
280  }
281  }
282 
289  public static void __Test_EwMante_New_Load_Save_Delete(this IMante toObjeto, string tcNewCodigo, string tcRandomCodigo, RequiredValues toRequiredValues = null)
290  {
291  //New
292  toObjeto.__Test_EwMante_New(tcNewCodigo,true,toRequiredValues);
293  //Load
294  toObjeto.__Test_EwMante_Load(tcRandomCodigo, tcNewCodigo, toRequiredValues);
295  //Save
296  toObjeto.__Test_EwMante_Save(tcRandomCodigo, tcNewCodigo, toRequiredValues);
297  //Delete
298  toObjeto.__Test_EwMante_Delete(tcNewCodigo, "", toRequiredValues);
299  toObjeto.__Test_EwMante_Delete(tcRandomCodigo, tcNewCodigo, toRequiredValues);
300  }
301 
302  #endregion PE-93059 : Extensiones para tipo ewMante
303  }
304 }
string _Nombre
Campo de la tabla que representa el "Nombre" del mantenimiento. Por defecto siempre es NOMBRE ...
Definition: IMantes.cs:29
bool _Existe_Registro()
Nos indica si existe el registro en la Base de Datos
Clase de negocio base para mantenimientos
Definition: clsEwBase.cs:1643
Nueva interficie madre de IMantes y de la que colgara ewMante. Propiedades: _Codigo, _Nombre, _Error_Message. Metodos: _New, Save, _Delete, _Load, _Existe_Registro, _Clonar
Definition: IMantes.cs:17
string _Codigo
Campo de la tabla que representa el "Código" del mantenimiento. Por defecto siempre es CODIGO ...
Definition: IMantes.cs:24
bool _Delete()
Elimina el registro actual
string _Error_Message
Campo donde se informará de los errores en mantenimientos.
Definition: IMantes.cs:34
bool _Save()
Guarda el registro actual en la tabla asociada
void _Load()
Carga el registro actual de la tabla asociada