public void UpdateDocumentGroup() { DocumentGroup existingDocumentGroup; using (var uow = new XbtContext()) { var repo = new GenericRepository <DocumentGroup>(uow); //[retrieve any existing document group] existingDocumentGroup = repo.Get().FirstOrDefault(); //Check if there was any document group retreived Assert.IsNotNull(existingDocumentGroup); //edit an existing class existingDocumentGroup.Name = "Corporate Documents"; existingDocumentGroup.Description = "Documents used by the corporate department"; repo.Update(existingDocumentGroup); try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); //If we have reached here the test has failed Assert.Fail("Test failed"); } }; //Retreive the modified document group using a new unit of work and repository //to ensure values are retreived from database instead of in memory graph var uow1 = new XbtContext(); var repo1 = new GenericRepository <DocumentGroup>(uow1); var modifiedDocumentGroup = repo1.GetById(existingDocumentGroup.Id); //Establish if the changes to the document group were pushed to the database Assert.AreEqual(modifiedDocumentGroup.Name, "Corporate Documents"); Assert.AreEqual(modifiedDocumentGroup.Description, "Documents used by the corporate department"); }
public void CreateDocumentGroup() { using (var uow = new XbtContext()) { var repo = new GenericRepository <DocumentGroup>(uow); //Create new document group var newDocumentGroup = new DocumentGroup() { Name = "Credit Documents", Description = "Documents used in the credit department" }; repo.Insert(newDocumentGroup); try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); //If we have reached here the test has failed Assert.Fail("Test failed"); } //Retrieve saved document using a new unit of work and repository //to ensure values are retreived from database instead of in memory graph var uow1 = new XbtContext(); var repo1 = new GenericRepository <DocumentGroup>(uow1); var savedDocumentGroup = repo1.GetById(newDocumentGroup.Id); //Establish if the saved document group has the same name as the saved one Assert.AreEqual(savedDocumentGroup.Name, newDocumentGroup.Name); }; }
public void AddNewCustomer() { var customer = new Customer() { CustomerNo = 235, FirstName = "Timo", LastName = "Lukyamuzi", DateOfBirth = DateTime.Now.ToLocalTime(), Email = "*****@*****.**", Address = "Ebbs" }; using (var uow = new XbtContext()) { uow.DocumentOwners.Add(customer); try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); //If we have reached here the test has failed Assert.Fail("Test failed"); } } }
/// <summary> /// Saves a document to the database. The document is also moved from its current location to the specified archive forlder /// </summary> /// <param name="document"></param> /// <returns></returns> public bool SaveDocument(Document document) { //Generate serial number document.SerialNo = GenerateDocumentSerialNumber(); //Generate security hash for the document file to prevent swapping of stored files in the archive //TODO: //Move file from current directory to archive //TODO: //Save document metadata var repo = new GenericRepository <Document>(uow); repo.Insert(document); int saveStatus = 0; try { saveStatus = uow.SaveChanges(); } catch (DbEntityValidationException ve) { //Catch validation errors } catch (Exception ex) //Other sql, connectivity exceptions. { throw ex; //The rest will be handled by controll Handle error methods } //The rest of the exception //if insert was not successful if (saveStatus < 1) { return(false); } return(true); }
public void CreateDocumentTag() { DocumentTag newDocTag = new DocumentTag(); using (var uow = new XbtContext()) { var repoDocGroup = new GenericRepository <DocumentGroup>(uow); var newDocumentGroup = new DocumentGroup() { Name = "Credit Documents", Description = "Documents used in the credit department" }; repoDocGroup.Insert(newDocumentGroup); //Retrieve Inserted document var insertedDocumentGroup = repoDocGroup.GetById(newDocumentGroup.Id); var repoDocType = new GenericRepository <DocumentType>(uow); var newDocumentType = new DocumentType() { Name = "Document Type 3", Description = "Document type description 3", DocumentGroup = insertedDocumentGroup, DocumentDirectory = @"\TestDocuments", }; repoDocType.Insert(newDocumentType); var repoDocTag = new GenericRepository <DocumentTag>(uow); var insertedDocumentType = repoDocType.GetById(newDocumentType.Id); newDocTag.Name = "New Doc Tag"; newDocTag.Description = "New Doc Tag Description"; newDocTag.DocumentTypes = new List <DocumentType>() { insertedDocumentType }; repoDocTag.Insert(newDocTag); try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); //If we have reached here the test has failed Assert.Fail("Test failed"); } } //Retreive the modified document tag using a new unit of work and repository //to ensure values are retreived from database instead of in memory graph var uowDoctag = new XbtContext(); var repoExistingDocTag = new GenericRepository <DocumentTag>(uowDoctag); var retrievedDocumentTag = repoExistingDocTag.Get().FirstOrDefault(); Assert.AreEqual(retrievedDocumentTag.Name, newDocTag.Name); }
public void Should_Create_Document() { using (var uow = new XbtContext()) { //retrieve document owner var docOwner = from rc in uow.DocumentOwners.OfType <Customer>() select rc; var result = docOwner.FirstOrDefault(); //retrieve document type int doctypeId = 1; var repoDocType = new GenericRepository <DocumentType>(uow); var docType = repoDocType.GetById(doctypeId); var newDocument = new Document() { Description = "New Doc Description", SerialNo = "35-899-hello-world", FileName = "New Document-hello-world", DocumentType = docType, DocumentOwner = result }; uow.Documents.Add(newDocument); try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); //If we have reached here the test has failed Assert.Fail("Test failed"); } //Assert.IsNotNull(newDocument); //Retreive the inserted document using a new unit of work and repository //to ensure values are retreived from database instead of in memory graph var uow1 = new XbtContext(); var repo1 = new GenericRepository <Document>(uow1); var insertedDocument = repo1.GetById(newDocument.Id); Assert.NotNull(insertedDocument); } }
public void UpdateDocumentType() { DocumentType existingdocumentType; using (var uow = new XbtContext()) { var repo = new GenericRepository <DocumentType>(uow); //[retrieve any existing document type] existingdocumentType = repo.Get().FirstOrDefault(); //Check if there was any document type retreived Assert.IsNotNull(existingdocumentType); //Create new document group var repoDoc = new GenericRepository <DocumentGroup>(uow); var newDocumentGroup = new DocumentGroup() { Name = "updates Credit Documents", Description = "updated Documents used in the credit department" }; repoDoc.Insert(newDocumentGroup); uow.SaveChanges(); Assert.IsNotNull(newDocumentGroup); //edit an existing document type properties existingdocumentType.Name = "New document type name"; existingdocumentType.Description = "New document type description"; existingdocumentType.DocumentGroup = newDocumentGroup; repo.Update(existingdocumentType); try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); //If we have reached here the test has failed Assert.Fail("Test failed"); } } //Retreive the modified document type using a new unit of work and repository //to ensure values are retreived from database instead of in memory graph var uow1 = new XbtContext(); var repo1 = new GenericRepository <DocumentType>(uow1); var modifiedDocumentType = repo1.Get(d => d.Id == existingdocumentType.Id, includeProperties: "DocumentGroup").FirstOrDefault(); Assert.NotNull(modifiedDocumentType); Assert.AreEqual(modifiedDocumentType.Name, existingdocumentType.Name); Assert.AreEqual(modifiedDocumentType.Description, existingdocumentType.Description); Assert.AreEqual(modifiedDocumentType.DocumentGroup.Id, existingdocumentType.DocumentGroup.Id); }