Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0f58bb56

Přidáno uživatelem Jakub Schenk před asi 2 roky(ů)

Testing of documents in backend

Add and get documents from database

Zobrazit rozdíly:

Backend/BackendTesting/DocumentManagementTesting.cs
1
using System;
1
using Microsoft.VisualStudio.TestTools.UnitTesting;
2
using Models.Enums;
3
using Core.Entities;
4
using Serilog;
5

  
6
using System;
2 7
using System.Collections.Generic;
3 8
using System.Linq;
4 9
using System.Text;
5 10
using System.Threading.Tasks;
11
using Microsoft.EntityFrameworkCore;
12
using AutoMapper;
13
using Core.MapperProfiles;
14
using Core.Contexts;
15
using Microsoft.Extensions.Configuration;
16
using Models.Documents;
6 17

  
7
namespace BackendTesting
18
namespace Core.Services
8 19
{
20
    //class responsible for testing user and backend autentication on login
21
    [TestClass]
9 22
    public class DocumentManagementTesting
10 23
    {
24
        private static readonly IConfiguration configuration = new ConfigurationBuilder().Build();
25
        public DatabaseContext ctx;
26
        public Core.Services.DocumentService.IDocumentService DS;
27
        public DocumentAddRequest docList;
28
        public User user;
29

  
30
        public DocumentManagementTesting()
31
        {
32
            //creating new database
33
            this.ctx = new DatabaseContext(configuration);
34
            this.DS = new DocumentService.DocumentServiceEF(this.ctx, TestingLogger.GetLogger(), TestingMapper.GetMapper());
35
            AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
36

  
37
            //ensure the database is fresh without unwanted users from previous testings
38
            ctx.Database.EnsureDeleted();
39
            ctx.Database.EnsureCreated();
40

  
41
            user = new() { Id = Guid.NewGuid(), Name = "aa", Surname = "aa", Password = "aa", Role = ERole.ADMINISTRATOR, Username = "aa" };
42
            ctx.Users.Add(user);
43
            ctx.SaveChanges();
44

  
45
            docList = makeRequest();
46
        }
47

  
48
        //method for testing Documents addition
49
        //if document is really in databse is tested in next method where its requested from there (GetDocumentsTest())
50
        [TestMethod]
51
        public void AddDocumentsTest()
52
        {
53
            try
54
            {
55
                DS.AddDocuments(docList, user.Id);
56
            }
57
            catch (Exception ex)
58
            {
59
                Assert.Fail(ex.Message);
60
            }
61
        }
62

  
63
        [TestMethod]
64
        [DataRow(2, 1)]
65
        [DataRow(0, 2)]
66
        [DataRow(3, 1)]
67
        //method for GetDocuments() testing
68
        //recieves same paramaters as GetDocuments()
69
        //numberOfDocs is number representing lenght of list got from method (in documents)
70
        //index represents index of those lists -> 2nd list (index 1) of lenght 2 contains two documents with indexes 2 and 3 (or so is expected)
71
        public void GetDocumentsTest(int index, int numberOfDocs)
72
        {
73
            //vlastne telo addDocument metody - potrebuju do databaze dostat data
74
            try
75
            {
76
                DS.AddDocuments(docList, user.Id);
77
            }
78
            catch (Exception ex)
79
            {
80
                Assert.Fail("Mistake in AddDocument - test AddDocumentsTest");
81
            }
82

  
83

  
84
            var documents = DS.GetDocuments(index, numberOfDocs);
85
            Assert.IsNotNull(documents);
86
            Assert.AreEqual(documents.Documents.Count, numberOfDocs);
87
            for (int i = 0; i < numberOfDocs; i++)
88
            {
89
                var doc1 = documents.Documents[i];
90
                var name = docList.Documents[numberOfDocs * index + i].Name;
91
                Assert.AreEqual(doc1.Name, name);
92
            }
93
        }
94

  
95

  
96
        //Method for preparing data for database creating DatabaseAssRequest from DocumentAddInfo witch contains documents strings
97
        private DocumentAddRequest makeRequest()
98
        {
99
            docList = new DocumentAddRequest() { Documents = new() };
100
            var dc = new DocumentContent();
101
            string content = "aaa <html> bbb";
102
            content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content));
103
            dc.Content = content;
104
            var dt = new DateTime(2022, 4, 11);
105
            DocumentAddInfo info = new DocumentAddInfo();
106
            info.Name = "a";
107
            info.Content = content;
108
            info.Format = EAddDocumentFormat.TEXTFILE;
109
            docList.Documents.Add(info);
110

  
111
            content = "aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb";
112
            dc.Content = content;
113
            dt = new DateTime(2022, 4, 12);
114
            docList.Documents.Add(new DocumentAddInfo() { Name = "b", Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)), Format = EAddDocumentFormat.TEXTFILE });
115

  
116
            content = "aaa <html> bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?>" +
117
                " bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb aaa <html> <xss> \n xdd<abc>" +
118
                " nonvalid xml<tag>abc <tag2?> bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb aaa <html>" +
119
                " <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?>" +
120
                " bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb aaa <html> <xss> \n xdd<abc> nonvalid xml<tag>abc <tag2?> bbb ";
121
            dc.Content = content;
122
            dt = new DateTime(2022, 4, 13);
123
            docList.Documents.Add(new DocumentAddInfo() { Name = "c", Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)), Format = EAddDocumentFormat.TEXTFILE });
124

  
125
            content = "aaa";
126
            dc.Content = content;
127
            dt = new DateTime(2022, 4, 14);
128
            docList.Documents.Add(new DocumentAddInfo() { Name = "d", Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)), Format = EAddDocumentFormat.TEXTFILE });
129

  
130
            content = "aaa <html> edfgckvhbjno§mpoizuzcjtjczkutvlzibůuoni§piuoůbzivlutkczjxzcktuvlziůuoi§ouůizvlutckzxjzckguvl h.gjcutfgýíáéphuoůizltukridýžáflizv bbb";
131
            dc.Content = content;
132
            dt = new DateTime(2022, 4, 15);
133
            docList.Documents.Add(new DocumentAddInfo() { Name = "e", Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(content)), Format = EAddDocumentFormat.TEXTFILE });
134

  
135
            return docList;
136
        }
11 137
    }
12
}
138
}
Backend/BackendTesting/Utils.cs
15 15
using Core.Contexts;
16 16
using Microsoft.Extensions.Options;
17 17
using Core.Authentication;
18
using Microsoft.EntityFrameworkCore.Design;
18 19

  
19 20
namespace Core.Services
20 21
{
......
39 40
    }
40 41
    public static class TestingLogger
41 42
    {
42
        public static ILogger GetLogger() 
43
        public static ILogger GetLogger()
43 44
        {
44 45
            return new LoggerConfiguration().WriteTo.Console().CreateLogger(); /* logger */
45 46
        }
......
48 49
    public static class TestingUserService
49 50
    {
50 51
        private static readonly IConfiguration configuration = new ConfigurationBuilder().Build();
51
       
52

  
52 53
        public static IUserService GetUserService()
53 54
        {
54
            return new UserServiceEF(new DatabaseContext(configuration), TestingLogger.GetLogger(), TestingMapper.GetMapper()); 
55
            return new UserServiceEF(new DatabaseContext(configuration), TestingLogger.GetLogger(), TestingMapper.GetMapper());
55 56
        }
56 57
    }
57 58

  

Také k dispozici: Unified diff