// Метод для очистки таблиц БД public void ClearData(string connectionstring) { try { //Подключаемся к БД WeatherDBEntity weatherDB = new WeatherDBEntity(connectionstring); // Очистка таблиц БД foreach (var item in weatherDB.City1Entity) weatherDB.City1Entity.Remove(item); foreach (var item in weatherDB.City2Entity) weatherDB.City2Entity.Remove(item); foreach (var item in weatherDB.City3Entity) weatherDB.City3Entity.Remove(item); // Сохраняем состояние БД weatherDB.SaveChanges(); weatherDB.Dispose(); } catch (Exception exception) { // Делаем запись в лог файл Logger.WriteLog(System.DateTime.Now.ToString() + " " + exception.TargetSite.ToString() + " " + exception.Message); } }
// Получение погодных данных для города с порядковым номером 2 // за дату date public List<City2Entity> GetCity2Weather(WeatherService wservice, string date) { List<City2Entity> queryResult = new List<City2Entity>(); try { // Подключаемся к СУБД WeatherDBEntity weatherDB = new WeatherDBEntity(wservice.ConnectionString); // Запрос погодных данных за дату date queryResult = (from item in weatherDB.City2Entity.ToList<City2Entity>().AsParallel() where item.Date.Contains(date) orderby item.Date descending select item).ToList<City2Entity>(); weatherDB.Dispose(); } catch (Exception exception) { // Делаем запись в лог файл Logger.WriteLog(System.DateTime.Now.ToString() + " " + exception.TargetSite.ToString() + " " + exception.Message); } return queryResult; }
public void DBReadWriteTest_1() { // Подключаемся к БД EntityConnectionStringBuilder ConnectionString = new EntityConnectionStringBuilder(); ConnectionString.ProviderConnectionString = @"data source=../../Test_Data/DBReadWriteTest_1/testDB.sqlite"; ConnectionString.Provider = @"System.Data.SQLite"; ConnectionString.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl"; WeatherDBEntity testDB = new WeatherDBEntity(ConnectionString.ToString()); // Очистка таблиц БД foreach (var item in testDB.City1Entity) testDB.City1Entity.Remove(item); foreach (var item in testDB.City2Entity) testDB.City2Entity.Remove(item); foreach (var item in testDB.City3Entity) testDB.City3Entity.Remove(item); // Сохраняем состояние БД testDB.SaveChanges(); // Создаем записи в таблицы БД // Запись в таблицу 1-ого города City1Entity City1Item = new City1Entity(); City1Item.Date = "29.06.2014"; City1Item.Temperature = "24"; City1Item.Pressure = "748"; City1Item.Wetness = "28"; City1Item.Wind = "с/з 10"; City1Item.Clouds = "Облачно"; testDB.City1Entity.Add(City1Item); // Запись в таблицу 2-ого города City2Entity City2Item = new City2Entity(); City2Item.Date = "30.07.2015"; City2Item.Temperature = "26"; City2Item.Pressure = "749"; City2Item.Wetness = "30"; City2Item.Wind = "с/в 3"; City2Item.Clouds = "Ясно"; testDB.City2Entity.Add(City2Item); // Запись в таблицу 3-ого города City3Entity City3Item = new City3Entity(); City3Item.Date = "05.03.2014"; City3Item.Temperature = "30"; City3Item.Pressure = "750"; City3Item.Wetness = "20"; City3Item.Wind = "ю/в 5"; City3Item.Clouds = "Пасмурно"; testDB.City3Entity.Add(City3Item); // Сохраняем состояние БД testDB.SaveChanges(); // Читаем записи из таблиц БД и сравниваем с ожидаемым результатом Assert.AreEqual<City1Entity>(City1Item, testDB.City1Entity.ToList<City1Entity>()[0]); Assert.AreEqual<City2Entity>(City2Item, testDB.City2Entity.ToList<City2Entity>()[0]); Assert.AreEqual<City3Entity>(City3Item, testDB.City3Entity.ToList<City3Entity>()[0]); }
// Метод для сохранения погодных данных в 1-ую таблицу БД public void City1Save(string connectionstring, Dictionary<string, string> weatherData) { try { // Соединение с БД WeatherDBEntity weatherDB = new WeatherDBEntity(connectionstring); // 1-ый город City1Entity City1Item = new City1Entity(); City1Item.Date = weatherData["Date"]; City1Item.Temperature = weatherData["Temperature"]; City1Item.Pressure = weatherData["Pressure"]; City1Item.Wetness = weatherData["Wetness"]; City1Item.Wind = weatherData["Wind"]; City1Item.Clouds = weatherData["Clouds"]; // Добавляем запись в БД if ((from item in weatherDB.City1Entity.ToList<City1Entity>() where item.Date == City1Item.Date select item).Count<City1Entity>() == 0) { weatherDB.City1Entity.Add(City1Item); weatherDB.SaveChanges(); } weatherDB.Dispose(); } catch (Exception exception) { // Делаем запись в лог файл Logger.WriteLog(System.DateTime.Now.ToString() + " " + exception.TargetSite.ToString() + " " + exception.Message); } }
public void DBReadWriteTest_2() { // Подключаемся к БД EntityConnectionStringBuilder ConnectionString = new EntityConnectionStringBuilder(); ConnectionString.ProviderConnectionString = @"data source=../../Test_Data/DBReadWriteTest_2/testDB.sqlite"; ConnectionString.Provider = @"System.Data.SQLite"; ConnectionString.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl"; WeatherDBEntity testDB = new WeatherDBEntity(ConnectionString.ToString()); // Очистка таблиц БД foreach (var item in testDB.City1Entity) testDB.City1Entity.Remove(item); foreach (var item in testDB.City2Entity) testDB.City2Entity.Remove(item); foreach (var item in testDB.City3Entity) testDB.City3Entity.Remove(item); // Сохраняем состояние БД testDB.SaveChanges(); // Создаем записи в таблицы БД // Запись в таблицу 1-ого города City1Entity City1Item = new City1Entity(); City1Item.Date = "29.06.2014"; City1Item.Temperature = "24"; City1Item.Pressure = "748"; City1Item.Wetness = "28"; City1Item.Wind = "с/з 10"; City1Item.Clouds = "Облачно"; testDB.City1Entity.Add(City1Item); // Запись в таблицу 2-ого города City2Entity City2Item = new City2Entity(); City2Item.Date = "30.07.2015"; City2Item.Temperature = "26"; City2Item.Pressure = "749"; City2Item.Wetness = "30"; City2Item.Wind = "с/в 3"; City2Item.Clouds = "Ясно"; testDB.City2Entity.Add(City2Item); // Запись в таблицу 3-ого города City3Entity City3Item = new City3Entity(); City3Item.Date = "05.03.2014"; City3Item.Temperature = "30"; City3Item.Pressure = "750"; City3Item.Wetness = "20"; City3Item.Wind = "ю/в 5"; City3Item.Clouds = "Пасмурно"; testDB.City3Entity.Add(City3Item); // Сохраняем записи в БД testDB.SaveChanges(); // Текущее число записей в таблицах БД int[] beforeCleanActual = new int[3] { testDB.City1Entity.Count<City1Entity>(), testDB.City2Entity.Count<City2Entity>(), testDB.City3Entity.Count<City3Entity>() }; int[] beforeCleanExpected = new int[3] { 1, 1, 1}; // Очистка таблиц БД foreach (var item in testDB.City1Entity) testDB.City1Entity.Remove(item); foreach (var item in testDB.City2Entity) testDB.City2Entity.Remove(item); foreach (var item in testDB.City3Entity) testDB.City3Entity.Remove(item); // Сохраняем состояние БД testDB.SaveChanges(); // Текущее число записей в таблицах БД int[] afterCleanActual = new int[3] { testDB.City1Entity.Count<City1Entity>(), testDB.City2Entity.Count<City2Entity>(), testDB.City3Entity.Count<City3Entity>() }; int[] afterCleanExpected = new int[3] { 0, 0, 0}; // Читаем количество записей в таблицах БД // и сравниваем с ожидаемым результатом CollectionAssert.AreEqual(beforeCleanExpected, beforeCleanActual); CollectionAssert.AreEqual(afterCleanExpected, afterCleanActual); }
public void SaveWeatherToDBTest_1() { //Создаем подключение к БД и чистим таблицы БД EntityConnectionStringBuilder ConnectionString = new EntityConnectionStringBuilder(); ConnectionString.ProviderConnectionString = @"data source=../../Test_Data/SaveWeatherToDBTest_1/testDB.sqlite"; ConnectionString.Provider = @"System.Data.SQLite"; ConnectionString.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl"; LittleLogger logger = new LittleLogger("../../Test_Data/SaveWeatherToDBTest_1/log.txt"); WeatherWriter wWriter = new WeatherWriter(logger); wWriter.ClearData(ConnectionString.ToString()); // Формируем погодные данные для записи в таблицы БД WeatherService wService = new WeatherService(logger); wService.WeatherData.Add("Date", "Tue, 17 Jun 2014 12:29 pm MSK"); wService.WeatherData.Add("Temperature", "17C"); wService.WeatherData.Add("Pressure", "982.05millibars"); wService.WeatherData.Add("Wetness", "52%"); wService.WeatherData.Add("Wind", "Direction: 210deegres. | 25.75km/h"); wService.WeatherData.Add("Clouds", "Mostly Cloudy"); // Сохраняем погодные данные в таблицы БД wWriter.City1Save(ConnectionString.ToString(), wService.WeatherData); wWriter.City2Save(ConnectionString.ToString(), wService.WeatherData); wWriter.City3Save(ConnectionString.ToString(), wService.WeatherData); // Создаем подключение к БД и извлекаем данные из таблиц WeatherDBEntity WeatherDB = new WeatherDBEntity(ConnectionString.ToString()); // Данные из 1-ой таблицы WeatherService wService_t1 = new WeatherService(logger); wService_t1.WeatherData.Add("Date", WeatherDB.City1Entity.ToList<City1Entity>()[0].Date); wService_t1.WeatherData.Add("Temperature", WeatherDB.City1Entity.ToList<City1Entity>()[0].Temperature); wService_t1.WeatherData.Add("Pressure", WeatherDB.City1Entity.ToList<City1Entity>()[0].Pressure); wService_t1.WeatherData.Add("Wetness", WeatherDB.City1Entity.ToList<City1Entity>()[0].Wetness); wService_t1.WeatherData.Add("Wind", WeatherDB.City1Entity.ToList<City1Entity>()[0].Wind); wService_t1.WeatherData.Add("Clouds", WeatherDB.City1Entity.ToList<City1Entity>()[0].Clouds); // Данные из 2-ой таблицы WeatherService wService_t2 = new WeatherService(logger); wService_t2.WeatherData.Add("Date", WeatherDB.City2Entity.ToList<City2Entity>()[0].Date); wService_t2.WeatherData.Add("Temperature", WeatherDB.City2Entity.ToList<City2Entity>()[0].Temperature); wService_t2.WeatherData.Add("Pressure", WeatherDB.City2Entity.ToList<City2Entity>()[0].Pressure); wService_t2.WeatherData.Add("Wetness", WeatherDB.City2Entity.ToList<City2Entity>()[0].Wetness); wService_t2.WeatherData.Add("Wind", WeatherDB.City2Entity.ToList<City2Entity>()[0].Wind); wService_t2.WeatherData.Add("Clouds", WeatherDB.City2Entity.ToList<City2Entity>()[0].Clouds); // Данные из 3-ой таблицы WeatherService wService_t3 = new WeatherService(logger); wService_t3.WeatherData.Add("Date", WeatherDB.City3Entity.ToList<City3Entity>()[0].Date); wService_t3.WeatherData.Add("Temperature", WeatherDB.City3Entity.ToList<City3Entity>()[0].Temperature); wService_t3.WeatherData.Add("Pressure", WeatherDB.City3Entity.ToList<City3Entity>()[0].Pressure); wService_t3.WeatherData.Add("Wetness", WeatherDB.City3Entity.ToList<City3Entity>()[0].Wetness); wService_t3.WeatherData.Add("Wind", WeatherDB.City3Entity.ToList<City3Entity>()[0].Wind); wService_t3.WeatherData.Add("Clouds", WeatherDB.City3Entity.ToList<City3Entity>()[0].Clouds); // Сравниваем ожидаемые и фактические значения CollectionAssert.AreEqual(wService.WeatherData, wService_t1.WeatherData); CollectionAssert.AreEqual(wService.WeatherData, wService_t2.WeatherData); CollectionAssert.AreEqual(wService.WeatherData, wService_t3.WeatherData); }