private static void ImportCsvToDatabase(ApplianceDbContext db, string partsFilePath) { var parts = CsvDataLoader.LoadPartsFromCsv(partsFilePath); db.AddRange(parts); db.SaveChanges(); }
protected static DbConnection CreateConnection(string path = null) { var dataLoader = new CsvDataLoader(path ?? DefaultTestDataUri); var cachingLoader = new CachingDataLoader(dataLoader); return(DbConnectionFactory.CreateTransient(cachingLoader)); }
public void CachingDataLoader_Recreate() { CsvDataLoader wrapped = new CsvDataLoader("C:\\path"); // Create a caching data loader CachingDataLoader original = new CachingDataLoader(wrapped); // Clone the caching data loader CachingDataLoader recreated = new CachingDataLoader(); ((IDataLoader)recreated).Argument = ((IDataLoader)original).Argument; // It should be exactly the same as the original Assert.AreEqual( original.WrappedDataLoader.GetType(), recreated.WrappedDataLoader.GetType()); recreated.WrappedDataLoader.Should().BeOfType <CsvDataLoader>(); // The wrapped data loader should be restored completely too CsvDataLoader recreatedWrapped = recreated.WrappedDataLoader as CsvDataLoader; Assert.IsNotNull(recreatedWrapped); Assert.AreEqual(wrapped.ContainerFolderPath, recreatedWrapped.ContainerFolderPath); }
public FullContext() { var loader = new CsvDataLoader(@".\CsvFiles"); var connection = DbConnectionFactory.CreateTransient(loader); var context = new AppDbContext(connection); context.Database.CreateIfNotExists(); _comments = context.Comments; }
public void LoadAll() { TextAsset l_TextAsset = Resources.Load("CsvTest") as TextAsset; CsvDataLoader.ConvertStringToData(m_TableSampleList, l_TextAsset, ""); TextAsset l_TextAssetBytes = Resources.Load("byteCsv") as TextAsset; CsvDataLoader.ConvertByteToData(m_byteTableSampleList, l_TextAssetBytes); }
public static TradeSharpConnectionPersistent InitializeTradeSharpConnection() { var path = GetCsvFilesFolder(); var loader = new CsvDataLoader(path); var connection = Effort.EntityConnectionFactory.CreateTransient("name=TradeSharpConnection", loader); var connectionPersistent = new TradeSharpConnectionPersistent(connection); DatabaseContext.InitializeFake(connectionPersistent); return(connectionPersistent); }
public FullContext() { var clockMock = new Mock <IClock>(); clockMock.Setup(c => c.Now()).Returns(25.December(2020).At(4, 20)); var loader = new CsvDataLoader(@".\CsvFiles"); var connection = DbConnectionFactory.CreateTransient(loader); _context = new AppDbContext(connection, clockMock.Object); _context.Database.CreateIfNotExists(); }
private static List <string[]> LoadCsvDataFromFile() { var fileName = SelectFile(); if (string.IsNullOrEmpty(fileName)) { return(null); } var data = CsvDataLoader.LoadCsv(fileName); return(data); }
public void CsvDataLoader_EmbeddedResource_NotExistingDirectory() { var loader = new CsvDataLoader("res://Effort.Test/Internal/NonExisting"); Exception expected = null; try { loader.CreateTableDataLoaderFactory(); } catch (Exception ex) { expected = ex; } expected.Should().BeOfType <ArgumentException>(); }
public static TestDB Create() { if (cachingDataLoader == null) { var csvDataLoader = new CsvDataLoader("res://WMIT.DataServices.Tests/Fixtures/Data/"); cachingDataLoader = new CachingDataLoader(csvDataLoader); } var connection = DbConnectionFactory.CreateTransient(cachingDataLoader); var db = new TestDB(connection); return(db); }
private static async Task UploadItemsFromCsvAsync(string filePath) { if (_currentConnection == null) { Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection."); return; } var records = CsvDataLoader.LoadDataFromCsv(filePath); foreach (var part in records) { var newItem = new ExternalItem { Id = part.PartNumber.ToString(), Content = new ExternalItemContent { // Need to set to null, service returns 400 // if @odata.type property is sent ODataType = null, Type = ExternalItemContentType.Text, Value = part.Description }, Acl = new List <Acl> { new Acl { AccessType = AccessType.Grant, Type = AclType.Everyone, Value = _tenantId, IdentitySource = "Azure Active Directory" } }, Properties = part.AsExternalItemProperties() }; try { Output.Write(Output.Info, $"Uploading part number {part.PartNumber}..."); await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem); Output.WriteLine(Output.Success, "DONE"); } catch (ServiceException serviceException) { Output.WriteLine(Output.Error, "FAILED"); Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.PartNumber}"); Output.WriteLine(Output.Error, serviceException.Message); } } }
public async Task CanCorrectlyImportCsvFilesAsync() { var csvFileInfo = new CsvFileInfo(new FileInfo(@"CSV\Resources\People.csv"), true); var mapping = new MappingRules <Person, int>(); mapping.AddMapping(0, p => p.Name); mapping.AddMapping(1, p => p.FriendsCount); mapping.AddMapping(2, p => p.DateOfBirth); mapping.AddMapping(3, p => p.Sex, value => Enum.Parse(typeof(Gender), value as string)); var dataLoader = new CsvDataLoader <Person>(csvFileInfo, mapping); var result = (await dataLoader.LoadDataAsync()).ToArray(); Assert.IsNotNull(result); Assert.IsTrue(result.Any()); }
public void CsvDataLoader_EmbeddedResource_NotExisting() { var loader = new CsvDataLoader("res://Effort.Test/Internal/Resources/"); var factory = loader.CreateTableDataLoaderFactory(); var tableLoader = factory.CreateTableDataLoader( new TableDescription( "DoesNotExist", new[] { new ColumnDescription("Id", typeof(int)), })); var data = tableLoader.GetData().ToList(); data.Should().HaveCount(0); }
public void CanCorrectlyImportCsvFilesWithoutHeaders() { var csvFileInfo = new CsvFileInfo(new FileInfo(@"CSV\Resources\People_NoHeaders.csv"), hasHeaders: false); var mapping = new MappingRules <Person, int>(); mapping.AddMapping(0, p => p.Name); mapping.AddMapping(1, p => p.FriendsCount); mapping.AddMapping(2, p => p.DateOfBirth); mapping.AddMapping(3, p => p.Sex, value => Enum.Parse(typeof(Gender), value as string)); var dataLoader = new CsvDataLoader <Person>(csvFileInfo, mapping); var result = dataLoader.LoadData().ToArray(); Assert.IsNotNull(result); Assert.IsTrue(result.Any()); }
private void InitialiseDatabase() { //id,name,genre //D53082F4 - 2AC4 - 437C - AC37 - 4240E7969A78,reader1,Drama //B807381C - 05F1 - 4F91 - A518 - 9AF155656849,reader2,SciFi //CAF36D09 - F5EF - 4D39 - 9047 - F141042AA452,reader3,Comedy //var path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); IDataLoader loader = new CsvDataLoader(@"C:\temp\"); DbConnection connection = Effort.DbConnectionFactory.CreateTransient(loader); db = new BookStoreContext(connection); //db.readers.Add(new Reader() { id = Guid.NewGuid(), genre="baubas", name="Jurgis" }); //db.SaveChanges(); readerDal = new ReaderDal(db); }
public void CsvDataLoader_EmbeddedResource() { var loader = new CsvDataLoader("res://Effort.Test/Internal/Resources/"); var factory = loader.CreateTableDataLoaderFactory(); var tableLoader = factory.CreateTableDataLoader( new TableDescription( "Foo", new[] { new ColumnDescription("Id", typeof(int)), new ColumnDescription("Value", typeof(string)) })); var data = tableLoader.GetData().Single(); data[0].Should().Be(1); data[1].Should().Be("Foo"); }
private static async Task UploadItemsFromCsvAsync(string filePath) { var records = CsvDataLoader.LoadDataFromCsv(filePath); foreach (var part in records) { var newItem = new ExternalItem { Id = part.RowID.ToString(), Content = new PayloadContent { Value = "<html>" + part.FileName + "</html>" }, Acl = new List <Acl> { new Acl { AccessType = "grant", Type = "everyone", Value = _tenantId } }, Properties = part }; try { Output.Write(Output.Info, $"Uploading part number {part.RowID}..."); await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem); Output.WriteLine(Output.Success, "DONE"); } catch (ServiceException serviceException) { Output.WriteLine(Output.Error, "FAILED"); Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.RowID}"); Output.WriteLine(Output.Error, serviceException.Message); } } }
//public override IRepoStrategy<T> CreateStrategy<T>() //{ // return new MemoryStrategy<T>(); //} public override HccDbContext CreateHccDbContext() { if (UseDbConnection) { var connString = WebAppSettings.HccEFConnectionString; return(new HccDbContext(connString)); } if (Connection == null) { var connString = WebAppSettings.HccEFConnectionString; var csvDataPath = Path.Combine(Environment.CurrentDirectory, "CsvData"); IDataLoader loader = null; if (InitFromCsv) { loader = new CsvDataLoader(csvDataPath); } Connection = EntityConnectionFactory.CreateTransient(connString, loader); } return(new HccDbContext(Connection, false)); }
private static async Task UpdateItemsFromDatabase(bool uploadModifiedOnly) { if (_currentConnection == null) { Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection."); return; } List <AppliancePart> partsToUpload = CsvDataLoader.LoadPartsFromCsv("ApplianceParts.csv"); List <AppliancePart> partsToDelete = new List <AppliancePart>(); var newUploadTime = DateTime.UtcNow; Output.WriteLine(Output.Info, $"Processing {partsToUpload.Count()} add/updates, {partsToDelete.Count()} deletes"); bool success = true; foreach (var part in partsToUpload) { var newItem = new ExternalItem { Id = part.Id.ToString(), Content = new ExternalItemContent { // Need to set to null, service returns 400 // if @odata.type property is sent ODataType = null, Type = ExternalItemContentType.Text, Value = part.Text }, Acl = new List <Acl> { new Acl { AccessType = AccessType.Grant, Type = AclType.Everyone, Value = _tenantId, IdentitySource = "Azure Active Directory" } }, Properties = part.AsExternalItemProperties() }; try { Output.Write(Output.Info, $"Uploading part number {part.Id}..."); await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem); Output.WriteLine(Output.Success, "DONE"); } catch (ServiceException serviceException) { success = false; Output.WriteLine(Output.Error, "FAILED"); Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.Id}"); Output.WriteLine(Output.Error, serviceException.Message); } } foreach (var part in partsToDelete) { try { Output.Write(Output.Info, $"Deleting part number {part.Id}..."); await _graphHelper.DeleteItem(_currentConnection.Id, part.Id.ToString()); Output.WriteLine(Output.Success, "DONE"); } catch (ServiceException serviceException) { if (serviceException.StatusCode.Equals(System.Net.HttpStatusCode.NotFound)) { Output.WriteLine(Output.Warning, "Not found"); } else { success = false; Output.WriteLine(Output.Error, "FAILED"); Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error deleting part {part.Id}"); Output.WriteLine(Output.Error, serviceException.Message); } } } // If no errors, update our last upload time if (success) { SaveLastUploadTime(newUploadTime); } }