public void AddFile_WithContentFromLargeFile_AddsFile_But_TruncatesContent(int contentSizeBytes = MAX_CONTENT_LENGTH_BYTES)
        {
            // Arrange
            StorageDatabaseUtils db  = DatabaseHelper.ProvideStorageDatabaseUtils();
            string path              = InputHelper.GenerateInputFile(contentSizeBytes);
            string inputContent      = BaseFileWorker.ReadAll(path);
            var    inputContentBytes = DEFAULT_ENCODING.GetBytes(inputContent);

            Console.WriteLine("Working with file " + path + "\nContent: " + inputContent);
            Console.WriteLine("FileName length: " + path.Length + "\nContent length: " + inputContent.Length + "\nContent length (bytes): " + inputContentBytes.Length);
            int    initialFileCountInDB = db.GetFiles(path).Rows.Count;          // initial count of files with specified path in DB
            string result_fileName; byte[] result_fileContentBytes;
            // Act
            bool      result = db.AddFile(path, inputContentBytes);
            DataTable dt         = db.GetFiles(path);
            var       lastRow    = dt.Rows[dt.Rows.Count - 1];
            var       lastFileId = (int)lastRow.ItemArray[0];

            db.GetFile(lastFileId, out result_fileName, out result_fileContentBytes);
            // Assert
            Assert.IsTrue(result);                                                    // Entry added to database successfully
            Assert.AreEqual(initialFileCountInDB + 1, dt.Rows.Count);                 // Row count increased (new row was added)
            Assert.AreEqual(path, result_fileName);                                   // File path in DB is equal to inputs
            Assert.IsTrue(inputContentBytes.Length > result_fileContentBytes.Length); // Content is being truncated because it exceeds maximum length.
        }
        public void AddFile_WithContentFromReadLinesFirstLine_WithGetFullPath_AddsFileToDatabase()
        {
            // Arrange
            StorageDatabaseUtils db = DatabaseHelper.ProvideStorageDatabaseUtils();
            string contents = BaseFileWorker.ReadLines(FILE_STATIC_INPUT)[0];
            string path = BaseFileWorker.GetFullPath(FILE_STATIC_INPUT);
            var    contentBytes = DEFAULT_ENCODING.GetBytes(contents);
            int    initialFileCountInDB = db.GetFiles(path).Rows.Count;          // initial count of files with specified path
            string result_fileName, result_fileContent; byte[] result_fileContentBytes;

            // Act
            bool      result     = db.AddFile(path, contentBytes);
            DataTable dt         = db.GetFiles(path);
            var       lastRow    = dt.Rows[dt.Rows.Count - 1];
            var       lastFileId = (int)lastRow.ItemArray[0];

            db.GetFile(lastFileId, out result_fileName, out result_fileContentBytes);
            result_fileContent = DEFAULT_ENCODING.GetString(result_fileContentBytes);
            // Assert
            Assert.IsTrue(result);
            Assert.AreEqual(initialFileCountInDB + 1, dt.Rows.Count);
            Assert.AreEqual(path, result_fileName);
            CollectionAssert.AreEqual(contentBytes, result_fileContentBytes);
            Assert.AreEqual(contents, result_fileContent);
        }
Exemple #3
0
        public void Test_ReadAll_GetFiles_from_DB_NotNull()
        {
            try
            {
                string    textFromFile = BaseFileWorker.ReadAll(filenameTxt);
                byte[]    fileContent  = Encoding.ASCII.GetBytes(textFromFile);
                const int countFiles   = 3;

                for (int i = 0; i < countFiles; i++)
                {
                    Assert.IsTrue(storageDatabase.AddFile(filenameTxt, fileContent),
                                  $"The file filenameTxt was not added successfully for the {i + 1} time");
                }

                DataTable files = storageDatabase.GetFiles(filenameTxt);
                Assert.IsNotNull(files,
                                 "GetFiles returns zero values even when there are files with the same name in the database");
                Assert.AreEqual(files.Rows.Count, countFiles,
                                "The number of records for a certain file name is not returned correctly");
            }
            catch (Exception err)
            {
                Assert.Fail(err.Message);
            }
        }
Exemple #4
0
        public void TestGetFiles()
        {
            storageDatabaseUtils.ExecSql("INSERT INTO dbo.Files (FileName, FileContent) values ('filename', CONVERT(varbinary(1024), 'sorry for what?')), ('filename', CONVERT(varbinary(1024), 'our daddy told us not to be shame'));");
            string[][] fileIds = storageDatabaseUtils.GetLstBySql("SELECT FileID FROM dbo.Files WHERE FileName = 'filename';");
            int[]      ids     = { Int32.Parse(fileIds[0][0]), Int32.Parse(fileIds[1][0]) };

            string fileName = "filename";

            System.Data.DataTable table = storageDatabaseUtils.GetFiles(fileName);
            Assert.Equal(2, table.Rows.Count);
            foreach (System.Data.DataRow row in table.Rows)
            {
                int fileId = (int)row[0];
                Assert.True(Array.Exists(ids, id => id == fileId));
            }
            storageDatabaseUtils.ExecSql("DELETE FROM dbo.Files");
        }
        public void DeleteFile_WithExistingPathInDB_UsingGetFullPath_DeletesEntry(int fileNameSize = -1, int contentSize = MAX_CONTENT_LENGTH_BYTES)
        {
            // Arrange
            if (fileNameSize < 0)
            {
                fileNameSize = MAX_FILE_LENGTH;
            }
            string fileName = InputHelper.GenerateInputFile(size: contentSize, fileNameSize: fileNameSize);
            string content  = BaseFileWorker.ReadAll(fileName);

            byte[] fileContentBytes = DEFAULT_ENCODING.GetBytes(content);
            Console.WriteLine("Working with " + fileName + ".\nContent: " + content);
            Console.WriteLine("FileName length: " + fileName.Length + "\nContent length: " + content.Length + "\nContent length (bytes): " + fileContentBytes.Length);
            BaseFileWorker.Write(content, fileName);
            string fullPath = BaseFileWorker.GetFullPath(fileName);
            string result_fileName; byte[] result_fileContentBytes;

            StorageDatabaseUtils db = DatabaseHelper.ProvideStorageDatabaseUtils();

            db.AddFile(fullPath, fileContentBytes);
            DataTable dt = db.GetFiles(fullPath);
            int       initialEntryCount = dt.Rows.Count;
            var       lastRow           = dt.Rows[dt.Rows.Count - 1];
            var       lastFileId        = (int)lastRow.ItemArray[0];

            // Act
            db.GetFile(lastFileId, out result_fileName, out result_fileContentBytes);
            bool success          = db.DeleteFile(lastFileId);
            int  resultEntryCount = db.GetFiles(fullPath).Rows.Count;

            // Assert
            Assert.IsTrue(success);
            Assert.AreEqual(initialEntryCount - 1, resultEntryCount);
            CollectionAssert.AreEqual(fileContentBytes, result_fileContentBytes);
            // Cleanup
            File.Delete(fullPath);
            Console.WriteLine("File was deleted from database and from file system.");
        }
        public void AddFile_WithContentFromReadAll_WithGetFullPath_AddsFileToDatabase(int contentSize)
        {
            // Arrange
            StorageDatabaseUtils db = DatabaseHelper.ProvideStorageDatabaseUtils();
            string fullPath = InputHelper.GenerateInputFile(contentSize);
            string contents = BaseFileWorker.ReadAll(fullPath);
            var    contentBytes = DEFAULT_ENCODING.GetBytes(contents);
            int    initialFileCountInDB = db.GetFiles(fullPath).Rows.Count;          // initial count of files with path==fullPath
            string result_fileName, result_fileContent; byte[] result_fileContentBytes;
            // Act
            bool      result = db.AddFile(fullPath, contentBytes);        // Add file to Database
            DataTable dt         = db.GetFiles(fullPath);
            var       lastRow    = dt.Rows[dt.Rows.Count - 1];
            var       lastFileId = (int)lastRow.ItemArray[0];

            db.GetFile(lastFileId, out result_fileName, out result_fileContentBytes);
            result_fileContent = DEFAULT_ENCODING.GetString(result_fileContentBytes);
            // Assert
            Assert.IsTrue(result);
            Assert.AreEqual(initialFileCountInDB + 1, dt.Rows.Count);
            Assert.AreEqual(fullPath, result_fileName);
            CollectionAssert.AreEqual(contentBytes, result_fileContentBytes);
            Assert.AreEqual(contents, result_fileContent);
        }
Exemple #7
0
 public void DBGetFilesNotNull()
 {
     Assert.IsNotNull(storageDatabaseUtils.GetFiles());
     Assert.IsNotNull(storageDatabaseUtils.GetFiles("and here.txt"));
     Assert.IsNotNull(storageDatabaseUtils.GetFiles("Last Christmas.txt"));
 }
 public void getFilesFromDBTest()
 {
     Assert.IsNotNull(storageDatabaseUtils.GetFiles());
 }
Exemple #9
0
 public void GetFilesEmptyCheck()
 {
     Assert.True(sdu.GetFiles("no_such_filename.dot").Rows.Count == 0);
 }