<file_sep>using AssessmentTest03.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace AssessmentTest03
{
    [TestClass]
    public class Level1EfTests
    {
        [TestMethod]
        public void add_parties_and_make_queries()
        {
            EfMethods.RecreateDatabase(); // To student: comment this line when the database is created
            EfMethods.ClearDatabase();

            var lib = new PoliticalParty
            {
                Name = "Liberalerna",
                AbbreviatedName = "L",
                Founded = new DateTime(1934, 8, 5),
                Members = 15390,
            };

            var miljo = new PoliticalParty
            {
                Name = "<NAME>",
                AbbreviatedName = "MP",
                Founded = new DateTime(1981, 9, 20),
                Members = 10719,
            };

            var soc = new PoliticalParty
            {
                Name = "<NAME>",
                AbbreviatedName = "S",
                Founded = new DateTime(1889, 4, 23),
                Members = 89010,
            };

            EfMethods.AddParty(miljo);
            EfMethods.AddParty(lib);
            EfMethods.AddParty(soc);

            string[] result = EfMethods.GetAllAbbreviatedNamesOrderedByFoundedDate();

            string[] expected = new[] { "S", "L", "MP" };

            CollectionAssert.AreEqual(expected, result);

        }

    }
}
<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AssessmentTest03
{
    public class CSharpMethods
    {
        public static int SumEdges(int[] arr, bool add10000)
        {
            var eka = arr.First();
            var vika = arr.Last();
            int tonni = 10000;

            if (add10000)
            {
                return eka + vika + tonni;
            }
           
            return eka + vika;
        }
    }
}
<file_sep>﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AssessmentTest03
{
    [TestClass]
    public class Level1CSharpTest
    {
        /*
         Create the method "SumEdges" so the following tests will pass.

         The method should add the first and last number in the array

         If add10000 is true then the method should add 10000 to the result
             
         (you don't need to take care of edge cases like null or arrays with 0 or 1 elements)

         */

        [TestMethod]
        public void test1()
        {
            var arr = new[] { 4, 11, 15 };
            bool add10000 = false;
            int expected = 19; // 4+15
            int result = CSharpMethods.SumEdges(arr, add10000);
            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void test2()
        {
            var arr = new[] { 4, 11, 15 };
            bool add10000 = true;
            int expected = 10019; // 4+15+10000
            int result = CSharpMethods.SumEdges(arr, add10000);
            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void test3()
        {
            var arr = new[] { 6, 2 };
            bool add10000 = false;
            int expected = 8; // 6+2
            int result = CSharpMethods.SumEdges(arr, add10000);
            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void test4()
        {
            var arr = new[] { 6, 2 };
            bool add10000 = true;
            int expected = 10008; // 6+2+10000
            int result = CSharpMethods.SumEdges(arr, add10000);
            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void test5()
        {
            var arr = new[] { 100, 200, 500, 77 };
            bool add10000 = false;
            int expected = 177; // 100+77
            int result = CSharpMethods.SumEdges(arr, add10000);
            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void test6()
        {
            var arr = new[] { 100, 200, 500, 77 };
            bool add10000 = true;
            int expected = 10177; // 100+77+10000
            int result = CSharpMethods.SumEdges(arr, add10000);
            Assert.AreEqual(expected, result);
        }
    }
}
<file_sep>﻿using AssessmentTest03.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AssessmentTest03
{
    public class EfMethods
    {
        internal static void AddParty(PoliticalParty party)
        {
            var context = new PoliticalContext();
            context.PoliticalParties.Add(party);
            context.SaveChanges();
        }

        internal static string[] GetAllAbbreviatedNamesOrderedByFoundedDate()
        {
            var context = new PoliticalContext();
            return context.PoliticalParties.OrderBy(p => p.Founded).Select(c => c.AbbreviatedName).ToArray();

        }

        internal static void RecreateDatabase()
        {
            var context = new PoliticalContext();
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
        }

        internal static void ClearDatabase()
        {
            var context = new PoliticalContext();
            context.RemoveRange(context.PoliticalParties);
            context.SaveChanges();
        }

    }
}
<file_sep>﻿USE PoliticalParties
select * from person
-- (1) Remove all rows from Person, Country, PoliticalParties
DELETE FROM Person;
DELETE FROM Country;
DELETE FROM PoliticalParties

SET IDENTITY_INSERT Person ON 
INSERT INTO Person (Id, FirstName, LastName)
VALUES (5000, 'Jonas', 'Sjöstedt')
INSERT INTO Person (Id, FirstName, LastName)
VALUES (5001, 'Ebba', '<NAME>')
INSERT INTO Person (Id, FirstName, LastName)
VALUES (5002, 'Ulf', 'Kristersson')
-- (2) Add three people (Jonas, Ebba, Ulf)
SET IDENTITY_INSERT Person OFF

SET IDENTITY_INSERT PoliticalParties ON 
insert into PoliticalParties (Id, Name) values (5000, 'Väntsterpartiet')
insert into PoliticalParties (Id, Name) values (5001, 'Kristdemokraterna')
insert into PoliticalParties (Id, Name) values (5002, 'Moderaterna')
-- (3) Add three political parties 
SET IDENTITY_INSERT PoliticalParties OFF 
select * from Person
select  Person.Id, Name, FirstName, LastName from PoliticalParties join Person on PoliticalParties.Id = Person.Id
-- (4) Write a "select" which gives info about the political parties 
-- Verify that you get exactly the same result as in SQL-output.png
<file_sep>﻿using Microsoft.EntityFrameworkCore;

namespace AssessmentTest03.Domain
{
    public class PoliticalContext : DbContext
    {
        public DbSet<PoliticalParty> PoliticalParties { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
                optionsBuilder.UseSqlServer("Server = (localdb)\\mssqllocaldb; Database = PoliticalParties; Trusted_Connection = True; ");
        }
    }

}
<file_sep>﻿using System;

namespace AssessmentTest03.Domain
{
    public class PoliticalParty
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime? Founded { get; set; }
        public int? Members { get; set; }
        public string AbbreviatedName { get; set; }
        public Person Chairman { get; set; }
        public Country Country { get; set; }
    }
}
