示例#1
0
        public void Test_ReadAll_Push_in_DB(string filename)
        {
            try
            {
                string textFromFile = BaseFileWorker.ReadAll(filename);
                byte[] fileContent  = Encoding.ASCII.GetBytes(textFromFile);
                string filenameTxtFromDB;
                byte[] fileContentFromDB;

                Assert.IsTrue(storageDatabase.AddFile(filename, fileContent),
                              $"The file {filename} was not added successfully");
                int?fileID = storageDatabase.GetIntBySql("SELECT MAX(FileID) FROM Files");
                Assert.IsTrue(storageDatabase.GetFile((int)fileID, out filenameTxtFromDB, out fileContentFromDB),
                              $"File {filename} not found in db, although add method returned true");
                string textFromDB = Encoding.ASCII.GetString(fileContentFromDB);
                Assert.AreEqual(filename, filenameTxtFromDB,
                                "The names of the files returned by the method and the database do not match");
                Assert.AreEqual(textFromFile, textFromDB,
                                "The content of the files returned by the method and the database do not match");
            }
            catch (Exception err)
            {
                Assert.Fail(err.Message);
            }
        }
示例#2
0
        public void TestGetFile()
        {
            storageDatabaseUtils.ExecSql("INSERT INTO dbo.Files (FileName, FileContent) values ('filename', CONVERT(varbinary(1024), 'sorry for what?'));");
            int fileId = (int)storageDatabaseUtils.GetIntBySql("SELECT FileID FROM dbo.Files WHERE FileName = 'filename';");

            Assert.True(storageDatabaseUtils.GetFile(fileId, out string fileName, out byte[] fileContent));
            Assert.Equal("filename", fileName);
            Assert.Equal("sorry for what?", Encoding.UTF8.GetString(fileContent));
            storageDatabaseUtils.ExecSql("DELETE FROM dbo.Files");
        }
        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);
        }
        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.
        }
示例#5
0
        public void GetFileErrorCheck()
        {
            byte[] emptyByte;
            string emptyFilename;

            Assert.False(sdu.GetFile(-1, out emptyFilename, out emptyByte));
        }
        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.");
        }
示例#7
0
        public void DBGetNonExistingFile()
        {
            string name;

            byte[] outputText;

            Assert.IsFalse(storageDatabaseUtils.GetFile(0, out name, out outputText));
            Assert.IsNull(name);
            Assert.IsNull(outputText);

            Assert.IsFalse(storageDatabaseUtils.GetFile(2021, out name, out outputText));
            Assert.IsNull(name);
            Assert.IsNull(outputText);

            Assert.IsFalse(storageDatabaseUtils.GetFile(-2021, out name, out outputText));
            Assert.IsNull(name);
            Assert.IsNull(outputText);
        }
示例#8
0
        public void getFileWithNoReferenceFromDBTest()
        {
            string filename_out;

            byte[] content_out;

            Assert.IsFalse(storageDatabaseUtils.GetFile(1, out filename_out, out content_out));
            Assert.IsNull(filename_out);
            Assert.IsNull(content_out);

            Assert.IsFalse(storageDatabaseUtils.GetFile(1000, out filename_out, out content_out));
            Assert.IsNull(filename_out);
            Assert.IsNull(content_out);

            Assert.IsFalse(storageDatabaseUtils.GetFile(0, out filename_out, out content_out));
            Assert.IsNull(filename_out);
            Assert.IsNull(content_out);

            Assert.IsFalse(storageDatabaseUtils.GetFile(-1000, out filename_out, out content_out));
            Assert.IsNull(filename_out);
            Assert.IsNull(content_out);
        }
        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);
        }