Ejemplo n.º 1
0
        public void TestSyncFiles()
        {
            var existingFiles = new List<File>
            {
                new File { Name = "a001z160105.xml", Content = "a001z160105.xml content" }, 
                new File { Name = "b001z160105.xml", Content = "b001z160105.xml content" }
            };

            var newFiles = new List<CurrencyRates.NbpCurrencyRates.Services.Entities.File>
            {
                new CurrencyRates.NbpCurrencyRates.Services.Entities.File { Name = "a002z160106.xml", Content = "a002z160106.xml content" }, 
                new CurrencyRates.NbpCurrencyRates.Services.Entities.File { Name = "b002z160106.xml", Content = "b002z160106.xml content" }
            };

            var context = new Mock<Context>();
            var fileFetcher = new Mock<IFileFetcher>();
            var fileDbSet = DbSetMockBuilder.Build(existingFiles.AsQueryable());

            context.Setup(c => c.Files).Returns(fileDbSet.Object);
            fileFetcher.Setup(ff => ff.FetchAllFilesExcept(new[] { existingFiles[0].Name, existingFiles[1].Name })).Returns(newFiles);

            var synchronizer = new Synchronizer(context.Object, fileFetcher.Object);
            synchronizer.SyncFiles();

            fileDbSet.Verify(fds => fds.Add(It.Is<File>(f => f.Name == newFiles[0].Name && f.Content == newFiles[0].Content)));
            fileDbSet.Verify(fds => fds.Add(It.Is<File>(f => f.Name == newFiles[1].Name && f.Content == newFiles[1].Content)));

            context.Verify(c => c.SaveChanges());
        }
Ejemplo n.º 2
0
        public void TestSyncAll()
        {
            var existingFiles = new List<File>
            {
                new File { Name = "a002z160105.xml", Content = SampleXmlContent, Processed = false }
            };

            var currencies = DbSetMockBuilder.Build(Enumerable.Empty<Currency>().AsQueryable());
            var files = DbSetMockBuilder.Build(existingFiles.AsQueryable());
            var rates = DbSetMockBuilder.Build(Enumerable.Empty<Rate>().AsQueryable());
            var context = new Mock<Context>();
            var fileFetcher = new Mock<IFileFetcher>();

            context.Setup(c => c.Currencies).Returns(currencies.Object);
            context.Setup(c => c.Files).Returns(files.Object);
            context.Setup(c => c.Rates).Returns(rates.Object);

            var synchronizer = new Synchronizer(context.Object, fileFetcher.Object);
            synchronizer.SyncAll();

            context.Verify(c => c.SaveChanges(), Times.Exactly(2));
        }
Ejemplo n.º 3
0
        public void TestSyncRatesFromUnprocessedFiles()
        {
            var existingCurrencies = new List<Currency> { new Currency { Code = "USD", Name = "dolar amerykański" } };

            var existingFiles = new List<File>
            {
                new File { Name = "a001z160105.xml", Processed = true }, 
                new File { Name = "b001z160105.xml", Processed = true }, 
                new File { Name = "a002z160105.xml", Content = SampleXmlContent, Processed = false }, 
                new File { Name = "b002z160105.xml", Content = SampleXmlContent, Processed = false }
            };

            var currencies = DbSetMockBuilder.Build(existingCurrencies.AsQueryable());
            var files = DbSetMockBuilder.Build(existingFiles.AsQueryable());
            var rates = DbSetMockBuilder.Build(Enumerable.Empty<Rate>().AsQueryable());
            var context = new Mock<Context>();
            var fileFetcher = new Mock<IFileFetcher>();

            context.Setup(c => c.Currencies).Returns(currencies.Object);
            context.Setup(c => c.Files).Returns(files.Object);
            context.Setup(c => c.Rates).Returns(rates.Object);

            var synchronizer = new Synchronizer(context.Object, fileFetcher.Object);
            synchronizer.SyncRatesFromUnprocessedFiles();

            currencies.Verify(cs => cs.AddRange(It.Is<IEnumerable<Currency>>(c => c.Count() == 1)), Times.Exactly(2));
            rates.Verify(rs => rs.Add(It.IsAny<Rate>()), Times.Exactly(4));
            context.Verify(c => c.SaveChanges(), Times.Exactly(2));

            Assert.That(existingFiles.Count(f => !f.Processed), Is.EqualTo(0));
        }
Ejemplo n.º 4
0
 public Scheduler(Synchronizer synchronizer, int interval = 60000)
 {
     this.synchronizer = synchronizer;
     this.interval = interval;
     this.InitializeComponent();
 }