Пример #1
0
        public AccountRepository_Test()
        {
            var dbLogger = new Mock <ILogger <ChilindoContext> >();
            // Given
            // https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory
            var options = new DbContextOptionsBuilder <ChilindoContext>()
                          .UseInMemoryDatabase(databaseName: "Chilindo")
                          .Options;

            using (var context = new ChilindoContext(options, dbLogger.Object))
            {
                context.Database.EnsureDeleted();

                context.Accounts.Add(new Account
                {
                    AccountNumber = 1234,
                    IsActive      = true,
                    Balances      = new List <AccountBalance>
                    {
                        new AccountBalance {
                            AccountNumber = 1234, Currency = "THB", Balance = 5000
                        },
                        new AccountBalance {
                            AccountNumber = 1234, Currency = "USD", Balance = 250
                        }
                    }
                });

                context.Accounts.Add(new Account
                {
                    AccountNumber = 3456,
                    IsActive      = true,
                    Balances      = new List <AccountBalance>
                    {
                        new AccountBalance {
                            AccountNumber = 3456, Currency = "THB", Balance = 22500
                        }
                    }
                });

                context.Accounts.Add(new Account
                {
                    AccountNumber = 7890,
                    IsActive      = true,
                    Balances      = new List <AccountBalance>
                    {
                        new AccountBalance {
                            AccountNumber = 7890, Currency = "USD", Balance = 750
                        }
                    }
                });

                context.SaveChanges();
            }
            var starWarsContext = new ChilindoContext(options, dbLogger.Object);
            var repoLogger      = new Mock <ILogger <AccountRepository> >();

            _accountRepository = new AccountRepository(starWarsContext, repoLogger.Object);
        }
Пример #2
0
        public static void EnsureSeedData(this ChilindoContext db)
        {
            if (db.Accounts == null)
            {
                return;
            }

            db._logger.LogInformation("Seeding database");

            //Account 1
            var account1 = new Account {
                AccountNumber = 1234, IsActive = true
            };

            account1.Balances = new List <AccountBalance>
            {
                new AccountBalance
                {
                    AccountNumber = 1234,
                    Currency      = "THB",
                    Balance       = 15000
                },
                new AccountBalance
                {
                    AccountNumber = 1234,
                    Currency      = "USD",
                    Balance       = 250
                },
            };

            //Account 2
            var account2 = new Account {
                AccountNumber = 3456, IsActive = true
            };

            account2.Balances = new List <AccountBalance>
            {
                new AccountBalance
                {
                    AccountNumber = 3456,
                    Currency      = "THB",
                    Balance       = 25000
                },
                new AccountBalance
                {
                    AccountNumber = 3456,
                    Currency      = "USD",
                    Balance       = 0
                },
            };

            //Account 3
            var account3 = new Account {
                AccountNumber = 7890, IsActive = true
            };

            account3.Balances = new List <AccountBalance>
            {
                new AccountBalance
                {
                    AccountNumber = 7890,
                    Currency      = "THB",
                    Balance       = 0
                },
                new AccountBalance
                {
                    AccountNumber = 7890,
                    Currency      = "USD",
                    Balance       = 500
                },
            };

            var accounts = new List <Account> {
                account1, account2, account3
            };

            if (!db.Accounts.Any())
            {
                db._logger.LogInformation("Seeding accounts");
                db.Accounts.AddRange(accounts);
                db.SaveChanges();
            }
        }