Пример #1
0
        public void AddMemberToDB_AddMemberToDataBase_ResultIsTrue()
        {
            //Arrange
            var member = new Member
            {
                memberUsername         = "******",
                memberName             = "Unit Test",
                memberRowLevelId       = 1,
                memberAccessLevelId    = 1,
                memberSubscribedUntill = DateTime.Now.AddYears(1)
            };
            var result = false;

            //Act
            //Method is placed inside a try block, so if it cant connect the result is set to false
            try
            {
                Member.AddMemberToDb(member);
            }
            catch (Exception e)
            {
                result = false;
            }

            //Check if the member is actually in the database
            using (var context = new BootDB())
            {
                var members = from m in context.Members
                              where m.memberUsername == member.memberUsername
                              select m;

                if (members.ToList().Count > 0)
                {
                    result = true;
                }
            }

            //Remove test member form database
            using (var context = new BootDB())
            {
                context.Members.Attach(member);
                context.Members.Remove(member);
                context.SaveChanges();
            }

            //Assert
            Assert.True(result);
        }
Пример #2
0
        public void CheckIfMemberExists_EnterAMemberThatIsNotInTheDateBase_ResultIsTrue()
        {
            //Arrange
            var member1 = new Member
            {
                memberUsername         = "******",
                memberName             = "Unit Test",
                memberRowLevelId       = 1,
                memberAccessLevelId    = 1,
                memberSubscribedUntill = DateTime.Now.AddYears(1)
            };
            var member2 = new Member
            {
                memberUsername         = "******",
                memberName             = "Unit Tester",
                memberRowLevelId       = 2,
                memberAccessLevelId    = 2,
                memberSubscribedUntill = DateTime.Now.AddYears(1)
            };
            var result = false;

            //Act
            Member.AddMemberToDb(member1);

            try
            {
                //The method CheckIfMemberExists has a void return type and throws an Exception when the the member is already in the database
                Member.CheckIfMemberExists(member2);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }

            //Remove test member form database
            using (var context = new BootDB())
            {
                context.Members.Attach(member1);
                context.Members.Remove(member1);
                context.SaveChanges();
            }

            //Assert
            Assert.True(result);
        }
Пример #3
0
        //Method to execute when AddUser button is clicked
        private void AddUser_Click(object sender, RoutedEventArgs e)
        {
            //Save TextBox content to variables for easy access
            var name        = NameBox.Text;
            var userName    = UserNameBox.Text;
            var rowLevel    = RowLevelBox.SelectedIndex + 1;    //Add 1 because combobox index start at 0 and values in database vary from 1 to 4
            var accessLevel = AccesslevelBox.SelectedIndex + 1; //Add 1 because combobox index start at 0 and values in database vary from 1 to 5

            //Check for empty fields, if a field is left empty show an error dialog
            if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(userName))
            {
                try
                {
                    DateTime?memberUntil;
                    try
                    {
                        memberUntil = DatePicker.SelectedDate.Value;
                    }
                    catch (InvalidOperationException)
                    {
                        throw new InvalidDateException("Selecteer een datum");
                    }

                    //CHeck for invalid characters in the strings
                    if (Member.NameHasSpecialChars(name))
                    {
                        throw new FormatException();
                    }

                    if (Member.HasSpecialChars(userName))
                    {
                        throw new FormatException();
                    }

                    //Create new member to add to the DB
                    var member = new Member
                    {
                        memberUsername         = userName,
                        memberName             = name,
                        memberRowLevelId       = rowLevel,
                        memberAccessLevelId    = accessLevel,
                        memberSubscribedUntill = memberUntil
                    };

                    //Check if the member aleady exists
                    Member.CheckIfMemberExists(member);
                    //Add new member to database
                    Member.AddMemberToDb(member);

                    MessageBox.Show("Gebruiker is succesvol toegevoegd.", "Gebruiker toegevoegd", MessageBoxButton.OK, MessageBoxImage.Information);
                    Switcher.Switch(new EditUserScreen(FullName, AccessLevel, MemberId));
                }
                catch (FormatException)
                {
                    //Warning message for FormatException
                    MessageBox.Show("Een van de ingevulde waardes is niet geldig", "Ongeldige waarde", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                catch (InvalidDateException ide)
                {
                    //Warning message for an invalid date
                    MessageBox.Show(ide.Message, "Datum is niet geldig", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                catch (Exception ex)
                {
                    //Error message for any other exception that could occur
                    MessageBox.Show(ex.Message, "Een fout is opgetreden", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show("Vul alle velden in.", "Niet alle velden zijn ingevuld", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }