aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.UnitTesting/CodeGeneration_TST.cs
blob: b5240bd5fd6023fc662f27dd3d9ad554a6906289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tango.DAL.Remote.DB;
using Tango.DAL.Remote.ObservableEntities;
using Tango.Settings;
using System.Linq;
using System.Collections;
using Tango.CodeGeneration;
using System.Data.Entity;
using System.Collections.Generic;
using System.IO;
using System.Data.Entity.Design.PluralizationServices;

namespace Tango.UnitTesting
{
    [TestClass]
    [TestCategory("Code Generation")]
    public class CodeGeneration_TST
    {
        [TestMethod]
        public void Generate_DAL_Observable_Entities()
        {
            String tempPath = Helper.GetTempFolderPath();

            foreach (var table in typeof(RemoteDB).GetProperties().Where(x => typeof(IEnumerable).IsAssignableFrom(x.PropertyType)))
            {
                EntityCodeFile codeFile = new EntityCodeFile(Singularize(ObservableEntity<Object>.DalNameToStandardName(table.Name)))
                {
                    TableName = Singularize(table.Name)
                };

                foreach (var field in table.PropertyType.GenericTypeArguments.First().GetProperties().Skip(4))
                {
                    EntityCodeFileField codeField = new EntityCodeFileField();
                    codeField.FieldName = field.Name;
                    codeField.Name = ObservableEntity<Object>.DalNameToStandardName(field.Name);
                    codeField.Description = field.Name.Replace("_", " ").ToLower();


                    if (field.PropertyType.IsGenericType)
                    {
                        codeField.Type = String.Format("ObservableCollection<{0}>", Singularize(ObservableEntity<Object>.DalNameToStandardName(field.PropertyType.GenericTypeArguments.Single().Name)));
                        codeField.Construct = true;
                    }
                    else
                    {
                        if (field.PropertyType.IsClass && field.PropertyType != typeof(String))
                        {
                            codeField.Type = Singularize(ObservableEntity<Object>.DalNameToStandardName(field.PropertyType.Name));
                            codeField.Construct = true;
                        }
                        else
                        {
                            codeField.Type = field.PropertyType.Name;
                        }
                    }
                    codeFile.Fields.Add(codeField);
                }

                String code = codeFile.GenerateCode();

                File.WriteAllText(Path.Combine(tempPath, codeFile.Name + ".cs"), code);
            }

            Helper.ShowInExplorer(tempPath);
        }

        [TestMethod]
        public void Load_Observable_Machines()
        {
            using (RemoteDB db = new RemoteDB(SettingsManager.Default.DataBase.SQLServerAddress, false))
            {
                List<Machine> machines = db.MACHINES.ToList().Select(x => new Machine(x)).ToList();

                foreach (var machine in machines)
                {

                }
            }
        }

        private String Singularize(String text)
        {
            var serv = PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
            return serv.Singularize(text);
        }
    }
}