Exemplo n.º 1
0
        public async Task TestCase1(int hid, string currency, string user, short doctype)
        {
            var context = this.fixture.GetCurrentDataContext();

            // 0. Prepare the context for current home
            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
            }
            else if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
            }
            else if (hid == DataSetupUtility.Home3ID)
            {
                fixture.InitHome3TestData(context);
            }
            else if (hid == DataSetupUtility.Home4ID)
            {
                fixture.InitHome4TestData(context);
            }
            else if (hid == DataSetupUtility.Home5ID)
            {
                fixture.InitHome5TestData(context);
            }
            var account = context.FinanceAccount.Where(p => p.HomeID == hid && p.Status != (Byte)FinanceAccountStatus.Closed).FirstOrDefault();
            var cc      = context.FinanceControlCenter.Where(p => p.HomeID == hid).FirstOrDefault();

            // 1. Create first DP docs.
            var control   = new FinanceDocumentsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(user);
            var httpctx   = UnitTestUtility.GetDefaultHttpContext(provider, userclaim);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };

            // 1a. Prepare data
            var dpcontext = new FinanceADPDocumentCreateContext();

            dpcontext.DocumentInfo = new FinanceDocument()
            {
                HomeID   = hid,
                DocType  = doctype,
                TranCurr = currency,
                Desp     = "Test 1"
            };
            var item = new FinanceDocumentItem()
            {
                DocumentHeader = dpcontext.DocumentInfo,
                ItemID         = 1,
                Desp           = "Item 1.1",
                TranType       = doctype == FinanceDocumentType.DocType_AdvancePayment
                    ? FinanceTransactionType.TranType_AdvancePaymentOut
                    : FinanceTransactionType.TranType_AdvanceReceiveIn,
                TranAmount      = 1200,
                AccountID       = account.ID,
                ControlCenterID = cc.ID,
            };

            dpcontext.DocumentInfo.Items.Add(item);
            dpcontext.AccountInfo = new FinanceAccount()
            {
                HomeID     = hid,
                Name       = "Account_8" + ".1",
                CategoryID = doctype == FinanceDocumentType.DocType_AdvancePayment
                        ? FinanceAccountCategory.AccountCategory_AdvancePayment
                        : FinanceAccountCategory.AccountCategory_AdvanceReceive,
                Owner  = user,
                Status = (Byte)FinanceAccountStatus.Normal,
            };
            var startdate = DateTime.Today;
            var enddate   = DateTime.Today.AddMonths(6);

            dpcontext.AccountInfo.ExtraDP = new FinanceAccountExtraDP()
            {
                StartDate  = startdate,
                EndDate    = enddate,
                RepeatType = RepeatFrequency.Month,
                Comment    = "Test",
            };
            var rsts = CommonUtility.WorkoutRepeatedDatesWithAmount(new RepeatDatesWithAmountCalculationInput
            {
                StartDate   = startdate,
                EndDate     = enddate,
                TotalAmount = item.TranAmount,
                RepeatType  = RepeatFrequency.Month,
                Desp        = item.Desp,
            });
            var tmpdocid = 1;

            foreach (var rst in rsts)
            {
                var tmpdoc = new FinanceTmpDPDocument
                {
                    DocumentID      = tmpdocid++,
                    TranAmount      = rst.TranAmount,
                    TransactionDate = rst.TranDate,
                    HomeID          = hid,
                    TransactionType = 5,
                    ControlCenterID = item.ControlCenterID,
                    OrderID         = item.OrderID,
                    Description     = item.Desp
                };

                dpcontext.AccountInfo.ExtraDP.DPTmpDocs.Add(tmpdoc);
            }
            var resp = await control.PostDPDocument(dpcontext);

            var doc = Assert.IsType <CreatedODataResult <FinanceDocument> >(resp).Entity;

            documentsCreated.Add(doc.ID);
            Assert.True(doc.Items.Count == 2);

            // Now check in the databse
            foreach (var docitem in doc.Items)
            {
                if (docitem.AccountID != account.ID)
                {
                    accountsCreated.Add(docitem.AccountID);

                    var acnt = context.FinanceAccount.Find(docitem.AccountID);
                    Assert.NotNull(acnt);
                    if (doctype == FinanceDocumentType.DocType_AdvancePayment)
                    {
                        Assert.True(acnt.CategoryID == FinanceAccountCategory.AccountCategory_AdvancePayment);
                    }
                    if (doctype == FinanceDocumentType.DocType_AdvanceReceive)
                    {
                        Assert.True(acnt.CategoryID == FinanceAccountCategory.AccountCategory_AdvanceReceive);
                    }
                    var acntExtraDP = context.FinanceAccountExtraDP.Find(docitem.AccountID);
                    Assert.NotNull(acntExtraDP);
                    Assert.True(acntExtraDP.RefenceDocumentID == doc.ID);

                    var tmpdocs = context.FinanceTmpDPDocument.Where(p => p.AccountID == docitem.AccountID).OrderBy(p => p.TransactionDate).ToList();
                    Assert.True(rsts.Count == tmpdocs.Count);

                    foreach (var rst in rsts)
                    {
                        DateTime dat  = rst.TranDate;
                        var      tdoc = tmpdocs.Find(p => p.TransactionDate.Date == dat);
                        Assert.NotNull(tdoc);
                        Assert.True(rst.TranAmount == tdoc.TranAmount);
                        Assert.True(tdoc.AccountID == acntExtraDP.AccountID);
                    }
                }
            }

            // Last, clear all created objects
            CleanupCreatedEntries();

            await context.DisposeAsync();
        }
Exemplo n.º 2
0
        public async Task TestController(int hid, string user)
        {
            var context   = this.fixture.GetCurrentDataContext();
            var control   = new LearnObjectsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(user);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = userclaim
                }
            };

            // 0. Initialize data
            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
            }
            else if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
            }
            var existamt = context.LearnObjects.Where(p => p.HomeID == hid).Count();
            var ctgid    = context.LearnCategories.Where(p => p.HomeID == hid || p.HomeID == null).FirstOrDefault();
            var objamt   = (from homemem in context.HomeMembers
                            join lobjs in context.LearnObjects
                            on new { homemem.HomeID, homemem.User } equals new { lobjs.HomeID, User = user }
                            select lobjs.ID).ToList().Count();

            // 1. Insert new Object
            var obj = new LearnObject()
            {
                HomeID     = hid,
                Name       = "Test_LOBJ_1_UT_" + hid.ToString(),
                CategoryID = ctgid.ID,
                Content    = "Content 1"
            };
            var rst1 = await control.Post(obj);

            Assert.NotNull(rst1);
            var rst2 = Assert.IsType <CreatedODataResult <LearnObject> >(rst1);

            Assert.Equal(obj.Name, rst2.Entity.Name);
            var firstordid = rst2.Entity.ID;

            Assert.True(firstordid > 0);
            objectsCreated.Add(firstordid);

            // 2. Now read the whole accounts (no home ID applied)
            var queryUrl = "http://localhost/api/LearnObjects";
            var httpctx  = UnitTestUtility.GetDefaultHttpContext(provider, userclaim);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };
            var req          = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl);
            var odatacontext = UnitTestUtility.GetODataQueryContext <LearnObject>(this.model);
            var options      = UnitTestUtility.GetODataQueryOptions <LearnObject>(odatacontext, req);
            var rst3         = control.Get(options);

            Assert.NotNull(rst3);
            Assert.Equal(objamt + 1, rst3.Cast <LearnObject>().Count());

            // 2a. Read the whole accounts (with home ID applied)
            queryUrl = "http://localhost/api/LearnObjects?$filter=HomeID eq " + hid.ToString();
            req      = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl);
            //var odatacontext = UnitTestUtility.GetODataQueryContext<FinanceAccount>(this.model);
            options  = UnitTestUtility.GetODataQueryOptions <LearnObject>(odatacontext, req);
            rst3     = control.Get(options);
            existamt = context.LearnObjects.Where(p => p.HomeID == hid).Count();
            Assert.NotNull(rst3);
            Assert.Equal(existamt, rst3.Cast <LearnObject>().Count());

            // 3. Change the object's name
            obj.Name = "Test 2";
            rst1     = await control.Put(firstordid, obj);

            var rst3a = Assert.IsType <UpdatedODataResult <LearnObject> >(rst1);

            Assert.Equal(obj.Name, rst3a.Entity.Name);

            // 4. Delete it
            var rst4 = await control.Delete(firstordid);

            Assert.NotNull(rst4);
            var rst6 = Assert.IsType <StatusCodeResult>(rst4);

            Assert.Equal(204, rst6.StatusCode);
            objectsCreated.Clear();

            // 5. Read all object again
            existamt = context.LearnObjects.Where(p => p.HomeID == hid).Count();
            var rst7 = control.Get(options);

            Assert.Equal(existamt, rst3.Cast <LearnObject>().Count());

            await context.DisposeAsync();
        }
Exemplo n.º 3
0
 public LearnObjectsControllerTest(SqliteDatabaseFixture fixture)
 {
     this.fixture  = fixture;
     this.provider = UnitTestUtility.GetServiceProvider();
     this.model    = UnitTestUtility.GetEdmModel <LearnObject>(provider, "LearnObjects");
 }
Exemplo n.º 4
0
        public async Task TestController1(int hid, string user)
        {
            var context = this.fixture.GetCurrentDataContext();
            var curr    = "";

            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
                curr = DataSetupUtility.Home1BaseCurrency;
            }
            if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
                curr = DataSetupUtility.Home2BaseCurrency;
            }
            if (hid == DataSetupUtility.Home3ID)
            {
                fixture.InitHome3TestData(context);
                curr = DataSetupUtility.Home3BaseCurrency;
            }
            if (hid == DataSetupUtility.Home4ID)
            {
                fixture.InitHome4TestData(context);
                curr = DataSetupUtility.Home4BaseCurrency;
            }
            if (hid == DataSetupUtility.Home5ID)
            {
                fixture.InitHome5TestData(context);
                curr = DataSetupUtility.Home5BaseCurrency;
            }

            // 1. Prepare dta
            var userclaim = DataSetupUtility.GetClaimForUser(user);
            var httpctx   = UnitTestUtility.GetDefaultHttpContext(provider, userclaim);

            var existamt = (from homemem in context.HomeMembers
                            join finplan in context.FinancePlan
                            on new { homemem.HomeID, homemem.User } equals new { finplan.HomeID, User = user }
                            select finplan.ID).ToList().Count();
            var existamt_curhome = context.FinancePlan.Where(p => p.HomeID == hid).Count();

            // 2. Create plan
            var control = new FinancePlansController(context);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };
            var plan = new FinancePlan()
            {
                HomeID        = hid,
                PlanType      = FinancePlanTypeEnum.Account,
                TranCurr      = curr,
                AccountID     = context.FinanceAccount.Where(p => p.HomeID == hid).FirstOrDefault().ID,
                StartDate     = DateTime.Today,
                TargetBalance = 10000,
                TargetDate    = DateTime.Today.AddDays(100),
                Description   = "Test",
            };
            var rst = await control.Post(plan);

            Assert.NotNull(rst);
            var rst2 = Assert.IsType <CreatedODataResult <FinancePlan> >(rst);

            Assert.Equal(rst2.Entity.TranCurr, plan.TranCurr);
            Assert.Equal(rst2.Entity.TargetBalance, plan.TargetBalance);
            var oid = rst2.Entity.ID;

            Assert.True(oid > 0);
            plansCreated.Add(oid);

            // 3. Read the plan out (without Home ID)
            var queryUrl     = "http://localhost/api/FinancePlans";
            var req          = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl);
            var odatacontext = UnitTestUtility.GetODataQueryContext <FinancePlan>(this.model);
            var options      = UnitTestUtility.GetODataQueryOptions <FinancePlan>(odatacontext, req);
            var rst3         = control.Get(options);

            Assert.NotNull(rst3);
            Assert.Equal(existamt + 1, rst3.Cast <FinancePlan>().Count());

            // 3a. Read the plan out (with Home ID)
            queryUrl = "http://localhost/api/FinancePlans?$filter=HomeID eq " + hid.ToString();
            req      = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl);
            //var odatacontext = UnitTestUtility.GetODataQueryContext<FinancePlan>(this.model);
            options = UnitTestUtility.GetODataQueryOptions <FinancePlan>(odatacontext, req);
            rst3    = control.Get(options);
            Assert.NotNull(rst3);
            Assert.Equal(existamt_curhome + 1, rst3.Cast <FinancePlan>().Count());

            // 4. Change one plan
            var nplan = rst2.Entity;

            nplan.Description = "Test > 2";
            rst = await control.Put(nplan.ID, nplan);

            Assert.NotNull(rst);
            var rst4 = Assert.IsType <UpdatedODataResult <FinancePlan> >(rst);

            Assert.Equal(nplan.Description, rst4.Entity.Description);

            // 5. Delete a plan
            var rst5 = await control.Delete(oid);

            Assert.NotNull(rst4);
            var rst6 = Assert.IsType <StatusCodeResult>(rst5);

            Assert.Equal(204, rst6.StatusCode);
            plansCreated.Clear();

            // 6. Read the plan again
            rst3 = control.Get(options);
            Assert.NotNull(rst3);
            Assert.Equal(existamt_curhome, rst3.Cast <FinancePlan>().Count());

            await context.DisposeAsync();
        }
        public async Task TestCase1_InterestFree(int hid, string currency, string user, short doctype)
        {
            var context = this.fixture.GetCurrentDataContext();

            // 0. Prepare the context for current home
            if (hid == DataSetupUtility.Home1ID)
            {
                fixture.InitHome1TestData(context);
            }
            else if (hid == DataSetupUtility.Home2ID)
            {
                fixture.InitHome2TestData(context);
            }
            else if (hid == DataSetupUtility.Home3ID)
            {
                fixture.InitHome3TestData(context);
            }
            else if (hid == DataSetupUtility.Home4ID)
            {
                fixture.InitHome4TestData(context);
            }
            else if (hid == DataSetupUtility.Home5ID)
            {
                fixture.InitHome5TestData(context);
            }
            var account = context.FinanceAccount.Where(p => p.HomeID == hid && p.Status != (Byte)FinanceAccountStatus.Closed).FirstOrDefault();
            var cc      = context.FinanceControlCenter.Where(p => p.HomeID == hid).FirstOrDefault();

            // 1. Create first Loan docs.
            var control   = new FinanceDocumentsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(user);
            var httpctx   = UnitTestUtility.GetDefaultHttpContext(provider, userclaim);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };

            // 1a. Prepare data
            var dpcontext = new FinanceLoanDocumentCreateContext();

            dpcontext.DocumentInfo = new FinanceDocument()
            {
                HomeID   = hid,
                DocType  = doctype,
                TranCurr = currency,
                Desp     = "Test 1"
            };
            var item = new FinanceDocumentItem()
            {
                DocumentHeader = dpcontext.DocumentInfo,
                ItemID         = 1,
                Desp           = "Item 1.1",
                TranType       = doctype == FinanceDocumentType.DocType_BorrowFrom
                        ? FinanceTransactionType.TranType_BorrowFrom
                        : FinanceTransactionType.TranType_LendTo,
                TranAmount      = 1200,
                AccountID       = account.ID,
                ControlCenterID = cc.ID,
            };

            dpcontext.DocumentInfo.Items.Add(item);
            dpcontext.AccountInfo = new FinanceAccount()
            {
                HomeID     = hid,
                Name       = "Account_8" + ".1",
                CategoryID = doctype == FinanceDocumentType.DocType_BorrowFrom
                    ? FinanceAccountCategory.AccountCategory_BorrowFrom
                    : FinanceAccountCategory.AccountCategory_LendTo,
                Owner  = user,
                Status = (Byte)FinanceAccountStatus.Normal,
            };
            var startdate = new DateTime(2020, 1, 10);
            var enddate   = new DateTime(2021, 1, 10);

            dpcontext.AccountInfo.ExtraLoan = new FinanceAccountExtraLoan()
            {
                StartDate       = startdate,
                EndDate         = enddate,
                TotalMonths     = 12,
                RepaymentMethod = LoanRepaymentMethod.EqualPrincipal,
                InterestFree    = true,
            };
            var rsts = CommonUtility.WorkoutRepeatedDatesWithAmountAndInterest(new RepeatDatesWithAmountAndInterestCalInput
            {
                RepaymentMethod  = dpcontext.AccountInfo.ExtraLoan.RepaymentMethod.Value,
                InterestFreeLoan = dpcontext.AccountInfo.ExtraLoan.InterestFree.Value,
                StartDate        = dpcontext.AccountInfo.ExtraLoan.StartDate,
                TotalAmount      = 1200,
                EndDate          = dpcontext.AccountInfo.ExtraLoan.EndDate,
                TotalMonths      = dpcontext.AccountInfo.ExtraLoan.TotalMonths.Value,
                FirstRepayDate   = new DateTime(2020, 2, 15)
            });
            var tmpdocid = 1;

            foreach (var rst in rsts)
            {
                var tmpdoc = new FinanceTmpLoanDocument
                {
                    DocumentID        = tmpdocid++,
                    TransactionAmount = rst.TranAmount,
                    InterestAmount    = rst.InterestAmount,
                    TransactionDate   = rst.TranDate,
                    HomeID            = hid,
                    ControlCenterID   = item.ControlCenterID,
                    OrderID           = item.OrderID,
                    Description       = item.Desp,
                };

                dpcontext.AccountInfo.ExtraLoan.LoanTmpDocs.Add(tmpdoc);
            }
            var resp = await control.PostLoanDocument(dpcontext);

            var doc = Assert.IsType <CreatedODataResult <FinanceDocument> >(resp).Entity;

            documentsCreated.Add(doc.ID);
            var loanacntid = -1;

            foreach (var docitem in doc.Items)
            {
                if (docitem.AccountID != account.ID)
                {
                    loanacntid = docitem.AccountID;
                    accountsCreated.Add(loanacntid);
                }
            }
            Assert.True(doc.Items.Count == 2);

            // 2. Switch to second controller
            var tmpcontrol = new FinanceTmpLoanDocumentsController(context);

            tmpcontrol.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };
            var tmpdocs = tmpcontrol.Get();

            Assert.NotEmpty(tmpdocs);
            Assert.Equal(rsts.Count, tmpdocs.Count());
            var dpdocs = tmpdocs.Cast <FinanceTmpLoanDocument>();

            // 3. Create repay document
            foreach (var dpdoc in dpdocs)
            {
                var contxt = new FinanceLoanRepayDocumentCreateContext();
                contxt.HomeID = hid;
                contxt.LoanTemplateDocumentID = dpdoc.DocumentID;
                contxt.DocumentInfo           = new FinanceDocument
                {
                    DocType  = FinanceDocumentType.DocType_Repay,
                    HomeID   = hid,
                    Desp     = dpdoc.Description,
                    TranCurr = currency,
                };
                contxt.DocumentInfo.Items.Add(new FinanceDocumentItem
                {
                    ItemID          = 1,
                    AccountID       = dpdoc.AccountID,
                    TranAmount      = dpdoc.TransactionAmount,
                    ControlCenterID = dpdoc.ControlCenterID,
                    OrderID         = dpdoc.OrderID,
                    TranType        = doctype == FinanceDocumentType.DocType_BorrowFrom
                        ? FinanceTransactionType.TranType_RepaymentIn
                        : FinanceTransactionType.TranType_RepaymentOut,
                });
                contxt.DocumentInfo.Items.Add(new FinanceDocumentItem
                {
                    ItemID          = 2,
                    AccountID       = account.ID,
                    TranAmount      = dpdoc.TransactionAmount,
                    ControlCenterID = dpdoc.ControlCenterID,
                    OrderID         = dpdoc.OrderID,
                    TranType        = doctype == FinanceDocumentType.DocType_BorrowFrom
                        ? FinanceTransactionType.TranType_RepaymentOut
                        : FinanceTransactionType.TranType_RepaymentIn,
                });
                var repaydocresp = await tmpcontrol.PostRepayDocument(contxt);

                var repaydoc = Assert.IsType <CreatedODataResult <FinanceDocument> >(repaydocresp);
                Assert.True(repaydoc.Entity.ID > 0);
                documentsCreated.Add(repaydoc.Entity.ID);

                // Check in the database
                var dpdocInDB = context.FinanceTmpLoanDocument.Where(p => p.DocumentID == dpdoc.DocumentID).SingleOrDefault();
                Assert.NotNull(dpdocInDB);
                Assert.NotNull(dpdocInDB.ReferenceDocumentID);
            }

            // 4. Now the account shall be closed automatically
            var account2 = context.FinanceAccount.Where(p => p.HomeID == hid && p.ID == loanacntid).FirstOrDefault();

            Assert.True(account2.Status == (Byte)FinanceAccountStatus.Closed);

            // Last, clear all created objects
            CleanupCreatedEntries();

            await context.DisposeAsync();
        }
 public FinanceTmpLoanDocumentsControllerTest(SqliteDatabaseFixture fixture)
 {
     this.fixture  = fixture;
     this.provider = UnitTestUtility.GetServiceProvider();
     this.model    = UnitTestUtility.GetEdmModel <FinanceDocument>(provider, "FinanceDocuments");
 }