/// <summary>
 /// Gets the name for specific locale
 /// </summary>
 /// <param name="culture">The culture</param>
 /// <returns>Return the Country if exists, or null</returns>
 public string GetCountry(CultureInfo culture)
 {
     // no need to call GetOrLoadCompetitor() since already called before populating dictionary
     return(Countries.ContainsKey(culture)
         ? Countries[culture]
         : null);
 }
Exemplo n.º 2
0
        private void Load(string path, string property)
        {
            var pi  = typeof(Statistics).GetProperty(property);
            var doc = new CsvDocument();

            doc.Load(path);
            foreach (var item in doc.Items)
            {
                var countryName = item[0];
                if (!Countries.ContainsKey(countryName))
                {
                    Countries[countryName] = new Country(countryName);
                }
                var country = Countries[countryName];
                for (int i = 1; i < doc.Headers.Length; i++)
                {
                    int year = int.Parse(doc.Headers[i]);
                    if (!country.StatisticsByYear.ContainsKey(year))
                    {
                        country.StatisticsByYear[year] = new Statistics(year);
                    }
                    double value;
                    if (double.TryParse(item[i], out value))
                    {
                        var statistics = country.StatisticsByYear[year];
                        pi.SetValue(statistics, value, null);
                    }
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Gets the name for specific locale
 /// </summary>
 /// <param name="culture">The culture</param>
 /// <returns>Return the Country if exists, or null</returns>
 public string GetCountry(CultureInfo culture)
 {
     return(Countries.ContainsKey(culture)
         ? Countries[culture]
         : null);
 }
Exemplo n.º 4
0
        public async Task TestGenerateInsertScript_DifferentColumnTypes_DefaultFormats()
        {
            // Arrange
            const string tableName = "dbo.Test01";

            tableToDrop = tableName;
            string createTableScript =
                $"create table {tableName} (" +
                "Id int identity(1,1) not null, " +
                "SomeUniqueId uniqueidentifier not null, " +
                "SomeGuid varchar(64) not null, " +
                "Country varchar(128) not null, " +
                "RegionName nvarchar(128) not null, " +
                "City nvarchar(128) not null, " +
                "AddressLine1 varchar(256) not null, " +
                "AddressLine2 varchar(256) not null, " +
                "LatitudeLongitude varchar(32) not null, " +
                "SomeTextColumn varchar(128) not null, " +
                "PersonFullName varchar(16) not null, " +
                "Email varchar(64) not null, " +
                "Telephone varchar(16) not null, " +
                "PassportNo varchar(16) not null, " +
                "PostalCode varchar(12) not null, " +
                "WholeValue int not null, " +
                "DecimalValue decimal not null, " +
                "FloatValue float not null, " +
                "BoolValue bit not null, " +
                "DateTimeValue datetime not null, " +
                ") on [primary];";
            InsertScriptGenerationOptions generationOptions = new InsertScriptGenerationOptions {
                RowCount = 10
            };
            IDataGenStrategy strategy = CreateSqlServerDataGenStrategy();
            // Act
            TableInfo tableInfo = await strategy.GetTableInfoAsync(createTableScript);

            string insertScript = await strategy.GenerateInsertScriptAsync(tableInfo, generationOptions);

            // Assert
            Log(createTableScript);
            Log(insertScript);
            Assert.IsTrue(tableInfo.Columns.Count > 0);
            string createInsertScript = createTableScript + Environment.NewLine + insertScript;

            ExecuteNonQuery(createInsertScript);

            using (SqlCommand cmd = new SqlCommand($"select * from {tableName}", Connection))
            {
                cmd.CommandType = CommandType.Text;
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    int col                  = 0;
                    int colId                = col++;
                    int colGuid              = col++;
                    int colGuidString        = col++;
                    int colCountry           = col++;
                    int colRegion            = col++;
                    int colCity              = col++;
                    int colAddressLine1      = col++;
                    int colAddressLine2      = col++;
                    int colLatitudeLongitude = col++;
                    int colRandomText        = col++;
                    int colFullName          = col++;
                    int colEmail             = col++;
                    int colPhone             = col++;
                    int colPassport          = col++;
                    int colPostalCode        = col++;
                    int colInt               = col++;
                    int colDecimal           = col++;
                    int colFloat             = col++;
                    int colBoolean           = col++;
                    int colDateTime          = col++;

                    Assert.AreEqual(col, reader.FieldCount);

                    Assert.AreEqual("int", reader.GetDataTypeName(colId));
                    Assert.AreEqual("uniqueidentifier", reader.GetDataTypeName(colGuid));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colGuidString));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colCountry));
                    Assert.AreEqual("nvarchar", reader.GetDataTypeName(colRegion));
                    Assert.AreEqual("nvarchar", reader.GetDataTypeName(colCity));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colAddressLine1));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colAddressLine2));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colLatitudeLongitude));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colRandomText));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colFullName));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colEmail));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colPhone));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colPassport));
                    Assert.AreEqual("varchar", reader.GetDataTypeName(colPostalCode));
                    Assert.AreEqual("int", reader.GetDataTypeName(colInt));
                    Assert.AreEqual("decimal", reader.GetDataTypeName(colDecimal));
                    Assert.AreEqual("float", reader.GetDataTypeName(colFloat));
                    Assert.AreEqual("bit", reader.GetDataTypeName(colBoolean));
                    Assert.AreEqual("datetime", reader.GetDataTypeName(colDateTime));
                    int rowCount = 0;
                    while (reader.Read())
                    {
                        for (int i = 0; i < col; i++)
                        {
                            Assert.IsFalse(reader.IsDBNull(i));
                        }

                        rowCount++;
                        Assert.AreEqual(rowCount, reader.GetInt32(colId));

                        Guid guid = reader.GetGuid(colGuid);
                        Assert.AreNotEqual(Guid.Empty, guid);

                        string guidString     = reader.GetString(colGuidString);
                        Guid   guidFromString = Guid.Parse(guidString);
                        Assert.AreNotEqual(Guid.Empty, guidFromString);
                        Assert.AreNotEqual(guid, guidFromString);

                        string country = reader.GetString(colCountry);
                        Assert.IsTrue(Countries.ContainsKey(country), $"Country not found: {country}");

                        string region = reader.GetString(colRegion);
                        Assert.IsTrue(Regions.ContainsKey(region), $"Region not found: {region}. (Country: {country})");

                        string city = reader.GetString(colCity);
                        Assert.IsTrue(Cities.ContainsKey(city), $"City not found: {city}. (Country: {country}, Region: {region})");

                        string addressLine1 = reader.GetString(colAddressLine1);
                        Assert.IsFalse(string.IsNullOrWhiteSpace(addressLine1));

                        string addressLine2 = reader.GetString(colAddressLine2);
                        Assert.IsFalse(string.IsNullOrWhiteSpace(addressLine2));

                        string latitudeLongitude = reader.GetString(colLatitudeLongitude);
                        Assert.IsFalse(string.IsNullOrWhiteSpace(latitudeLongitude));

                        string randomText = reader.GetString(colRandomText);
                        Assert.IsFalse(string.IsNullOrWhiteSpace(randomText));

                        string fullName = reader.GetString(colFullName);
                        Assert.AreEqual(1, fullName.Count(c => c == ' '), $"Generated Full name: {fullName}");

                        string email = reader.GetString(colEmail);
                        Assert.IsTrue(IsValidEmail(email), $"Generated Email: {email}");

                        string phone = reader.GetString(colPhone);
                        Regex  regex = new Regex(@"[\d\s]+");
                        Assert.IsTrue(regex.IsMatch(phone), $"Generated Phone: {phone}");

                        string passport = reader.GetString(colPassport);
                        regex = new Regex(@"[\d\s]+");
                        Assert.IsTrue(regex.IsMatch(passport), $"Generated Passport: {passport}");

                        string postalCode = reader.GetString(colPostalCode);
                        regex = new Regex(@"[\d]+");
                        Assert.IsTrue(regex.IsMatch(postalCode), $"Generated Postal code: {postalCode}");

                        int intValue = reader.GetInt32(colInt);
                        Assert.IsTrue(intValue >= 0);

                        decimal decimalNumber = reader.GetDecimal(colDecimal);
                        Assert.IsTrue(decimalNumber >= 0);

                        double floatNumber = reader.GetDouble(colFloat);
                        Assert.IsTrue(floatNumber >= 0);

                        bool boolValue = reader.GetBoolean(colBoolean);
                        Assert.IsTrue(boolValue || !boolValue);

                        DateTime dateTime = reader.GetDateTime(colDateTime);
                        Assert.IsTrue(dateTime > DateTime.Now.AddYears(-10));
                        Assert.IsTrue(dateTime < DateTime.Now.AddYears(10));
                    }
                    Assert.AreEqual(generationOptions.RowCount, rowCount);
                }
            }
        }
        public static AreaInfo AreInfoFromGeoJson(string geoJson)
        {
            try
            {
                if (string.IsNullOrEmpty(geoJson))
                {
                    return(null);
                }

                var fc = JsonConvert.DeserializeObject <GeoJSON.Net.Feature.FeatureCollection>(geoJson);
                var ai = new AreaInfo();
                try
                {
                    ai.dataSource = "osm"; ai.sourceServer = new Uri(photonUrl).Host.Split('.')[1];
                }
                catch { }

                if (fc != null && fc.Features.Count > 0)
                {
                    var fe = fc.Features[0];
                    if (fe.Geometry is Point)
                    {
                        var p = fe.Geometry as Point;
                        ai.pointLat = p.Coordinates.Latitude;
                        ai.pointLng = p.Coordinates.Longitude;
                    }
                    if (fe.Properties.ContainsKey("name"))
                    {
                        ai.locality = fe.Properties["name"].ToString();
                    }
                    if (fe.Properties.ContainsKey("street"))
                    {
                        ai.route = fe.Properties["street"].ToString();
                    }
                    if (fe.Properties.ContainsKey("postcode"))
                    {
                        ai.postalCode = fe.Properties["postcode"].ToString();
                    }
                    if (fe.Properties.ContainsKey("city"))
                    {
                        ai.adminArea3 = fe.Properties["city"].ToString();
                    }
                    if (fe.Properties.ContainsKey("state"))
                    {
                        ai.adminArea1 = fe.Properties["state"].ToString();
                    }
                    if (fe.Properties.ContainsKey("country"))
                    {
                        ai.countryName = fe.Properties["country"].ToString();
                    }

                    if (Countries.ContainsKey(ai.countryName))
                    {
                        ai.countryCode = Countries[ai.countryName];
                    }
                    else
                    {
                        ai.ToString();
                    }

                    string cArea = string.IsNullOrEmpty(ai.adminArea3) ? string.IsNullOrEmpty(ai.adminArea2) ? (string.IsNullOrEmpty(ai.adminArea1) ? ai.countryName : ai.adminArea1) : ai.adminArea2 : ai.adminArea3;

                    if (!string.IsNullOrEmpty(ai.locality))
                    {
                        ai.ToString();
                    }
                    if (!string.IsNullOrEmpty(ai.route))
                    {
                        ai.ToString();
                    }
                    ai.toponymName = string.IsNullOrEmpty(ai.locality) ? (string.IsNullOrEmpty(ai.route) ? cArea : ai.route) : ai.locality;
                    if (string.IsNullOrEmpty(ai.locality) || !string.IsNullOrEmpty(string.Concat(ai.route, ai.postalCode, ai.adminArea3)))
                    {
                        ai.toponymName = cArea;
                    }

                    if (fe.Properties.ContainsKey("extent"))
                    {
                        try
                        {
                            var ex = fe.Properties["extent"] as ICollection;
                            var e  = ex.GetEnumerator();
                            if (e.MoveNext())
                            {
                                ai.boxWest = sys.parseDouble(e.Current.ToString().Replace(",", "."));
                                if (e.MoveNext())
                                {
                                    ai.boxNorth = sys.parseDouble(e.Current.ToString().Replace(",", "."));
                                    if (e.MoveNext())
                                    {
                                        ai.boxEast = sys.parseDouble(e.Current.ToString().Replace(",", "."));
                                        if (e.MoveNext())
                                        {
                                            ai.boxSouth = sys.parseDouble(e.Current.ToString().Replace(",", "."));
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            xLog.Error(ex);
                        }
                    }
                    ai.ToString();

                    return(ai);
                }
                else
                {
                    geoJson.ToString();
                }
            }
            catch (Exception ex)
            {
                xLog.Error(ex);
            }
            return(null);
        }