aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Tests/Data/TechnologyRepository.Tests.cs
blob: 9a00401d28293adb3eb5c5e0012f0a79f2418e8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using DevHive.Data;
using DevHive.Data.Repositories;
using DevHive.Data.Models;
using System.Threading.Tasks;
using System.Linq;
using System;
using System.Security.Cryptography.X509Certificates;

namespace DevHive.Tests.Data
{
	[TestFixture]
	public class TechnologyRepositoryTests
	{
		private const string TECHNOLOGY_NAME = "Technology test name";

		protected DevHiveContext Context { get;set; }
		
		protected TechnologyRepository TechnologyRepository { get;set; }

		[SetUp]
		public void Setup()
		{
			var optionsBuilder = new DbContextOptionsBuilder<DevHiveContext>()
				.UseInMemoryDatabase(databaseName: "DevHive_Test_Database");			

			this.Context = new DevHiveContext(optionsBuilder.Options);

			TechnologyRepository = new TechnologyRepository(Context);			
		}

		[TearDown]
        public void TearDown()
        {
            this.Context.Database.EnsureDeleted();
        }

		[Test]
		public void AddAsync_AddsTheGivenTechnologyToTheDatabase()
		{
			AddEntity();
			
			int numberOfTechnologies = Context.Technologies.Count();

			Assert.True(numberOfTechnologies > 0, "Technologies repo does not store Technologies correctly");
		}

		[Test]
		public void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists()
		{
			Task.Run(async () =>
			{
				AddEntity();
				Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault();
				Guid id = technology.Id;

				Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id);

				Assert.AreEqual(TECHNOLOGY_NAME, technologyReturned.Name, "GetByIdAsync does not return the correct Technology when id is valid");
			}).GetAwaiter().GetResult();
		}

		[Test]		
		public void GetByIdAsync_ReturnsNull_IfIdDoesNotExists()
		{
			Task.Run(async () =>
			{
				Guid id = new Guid();
				
				Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id);

				Assert.IsNull(technologyReturned, "GetByIdAsync returns Technology when it should be null");
			}).GetAwaiter().GetResult();
		}

		[Test]
		public void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists()
		{
			Task.Run(async () =>
			{
				AddEntity();

				bool result = await this.TechnologyRepository.DoesTechnologyNameExist(TECHNOLOGY_NAME);

				Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist");
			}).GetAwaiter().GetResult();
		}

		[Test]
		public void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists()
		{
			Task.Run(async () =>
			{
				bool result = await this.TechnologyRepository.DoesTechnologyNameExist(TECHNOLOGY_NAME);

				Assert.False(result, "DoesTechnologyNameExist returns true when tehcnology name does not exist");
			}).GetAwaiter().GetResult();
		}

		private void AddEntity(string name = TECHNOLOGY_NAME)
		{
			Task.Run(async () =>
			{
				Technology technology = new Technology
				{
					Name = name
				};

				await this.TechnologyRepository.AddAsync(technology);		
			}).GetAwaiter().GetResult();
		}

		//Task.Run(async () =>
		//{
		//
		//}).GetAwaiter().GetResult();
	}
}