Esempio n. 1
0
 public void TestConstructorInMemory()
 {
     using (var fs = new SqliteFileSystem(this.testFile.FullName, OpenMode.Memory))
     {
         this.testFile.Refresh();
         Assert.IsFalse(this.testFile.Exists);
         Assert.IsFalse(fs.IsReadOnly);
     }
 }
Esempio n. 2
0
 public void TestConstructorReadWriteCreate()
 {
     using (var fs = new SqliteFileSystem(this.testFile.FullName, OpenMode.ReadWriteCreate))
     {
         this.testFile.Refresh();
         Assert.IsTrue(this.testFile.Exists);
         Assert.IsFalse(fs.IsReadOnly);
     }
 }
        /// <summary>
        /// Determine what kind of filesystem to use.
        /// After this point we *should theoretically* not need to use System.IO.Path.
        /// </summary>
        public static (IFileSystem, DirectoryEntry) DetermineFileSystem(string path, bool readOnly = false)
        {
            var emptyPath = string.IsNullOrWhiteSpace(path);
            var extension = Path.GetExtension(path);

            Log.Debug($"Determining file system for {path}");

            IFileSystem    fileSystem;
            DirectoryEntry baseEntry;

            if (emptyPath)
            {
                fileSystem = new PhysicalFileSystem();
                baseEntry  = fileSystem.GetDirectoryEntry(
                    fileSystem.ConvertPathFromInternal(Directory.GetCurrentDirectory()));
            }
            else
            {
                // resolve path (relative to absolute)
                path = Path.GetFullPath(path);

                if (Directory.Exists(path) || extension == string.Empty)
                {
                    var physicalFileSystem = new PhysicalFileSystem();
                    var internalPath       = physicalFileSystem.ConvertPathFromInternal(path);
                    physicalFileSystem.CreateDirectory(internalPath);
                    fileSystem = new SubFileSystem(physicalFileSystem, internalPath);
                    baseEntry  = fileSystem.GetDirectoryEntry(UPath.Root);
                }
                else
                {
                    // ensure parent directory exists on disk
                    Directory.CreateDirectory(Path.GetDirectoryName(path));

                    switch (extension)
                    {
                    case "." + SqlitePattern:
                        fileSystem = new SqliteFileSystem(
                            path,
                            readOnly ? OpenMode.ReadOnly : OpenMode.ReadWriteCreate);
                        break;

                    default:
                        throw new NotSupportedException(
                                  $"Cannot determine file system for given extension {extension}");
                    }

                    baseEntry = new DirectoryEntry(fileSystem, UPath.Root);
                }
            }

            Log.Debug($"Filesystem for {path} is {fileSystem.GetType().Name}");

            return(fileSystem, baseEntry);
        }
Esempio n. 4
0
        public void EnsureSchemaMismatchesThrow()
        {
            // create a new empty database
            using (var fs = new SqliteFileSystem(this.testFile.FullName, OpenMode.ReadWriteCreate))
            {
            }

            // ensure a schema has been created
            int?affected = (int?)TestHelper.DirectQuery(
                "UPDATE meta SET version = '0.5.0';",
                this.testFile.FullName,
                "ReadWrite");


            Assert.AreEqual(affected, null);

            Assert.ThrowsException <SqliteFileSystemException>(
                () => new SqliteFileSystem(this.testFile.FullName, OpenMode.ReadOnly),
                "Schema version 0.5.0 does not match library required version 1.0.0");
        }
Esempio n. 5
0
        public void TestGenerateTilesSqlite()
        {
            // generate the zooming spectrograms
            var zoomOutput = this.outputDirectory.Combine("Zooming");

            DrawZoomingSpectrograms.Execute(
                new DrawZoomingSpectrograms.Arguments()
            {
                Output                   = zoomOutput.FullName,
                OutputFormat             = "sqlite3",
                SourceDirectory          = ResultsDirectory.FullName,
                SpectrogramZoomingConfig = PathHelper.ResolveConfigFile("SpectrogramZoomingConfig.yml").FullName,
                ZoomAction               = DrawZoomingSpectrograms.Arguments.ZoomActionType.Tile,
            });

            var tiles = zoomOutput.CombineFile("OxleyCreek_site_1_1060_244333_20140529T081358+1000_120_0__Tiles.sqlite3");

            Assert.IsTrue(tiles.Exists);

            using (var fs = new SqliteFileSystem(tiles.FullName, OpenMode.ReadOnly))
            {
                var filesProduced = fs.EnumerateFiles(UPath.Root).ToArray();

                // there are 12 zoom levels in the default config, but the test recording is only 2min long
                // we're also split right at a natural boundary, so many tiles won't be full, but will be split
                // at scale 240, we're rendering <1px of content and that tile is not generated
                // for scales 120,60,30,15,7.5 only one tile is produced (subtotal: 5)
                // for 3.2, 1.6, 0.8: 2 tiles (subtotal: 11)
                // for 0.4: 3 tiles (subtotal: 14)
                // for 0.2: 4 tiles (subtotal: 18)
                // for 0.1: 8 tiles (subtotal: 26)
                Assert.AreEqual(26, filesProduced.Length);

                // 6.66 tiles (1200px / 180px tiles) - with padding either side -> 8
                Assert.AreEqual(8, filesProduced.Count(x => x.GetName().Contains("0.1")));
            }

            // not sure what else to test - generally exceptions should be thrown if anything goes wrong
        }
        /// <summary>
        ///  manually add a blob so we can test other parts of the code
        /// </summary>
        /// <param name="random"></param>
        /// <returns></returns>
        public static (string ConnectionString, UPath blobPath, byte[] blobData) PrepareDatabaseAndBlob(
            string testFile,
            System.Random random)
        {
            var testData = TestHelper.GenerateTestData(random, "/test.blob");

            // create a new empty database - mainly doing this to get a schema
            using (var fs = new SqliteFileSystem(testFile, OpenMode.ReadWriteCreate))
            {
            }

            var connectionString = $"Data source='{testFile}';Mode={OpenMode.ReadWrite}";

            using (var connection = new SqliteConnection(connectionString))
            {
                connection.Open();

                InsertBlobManually(connection, testData);
            }

            return(connectionString, testData.Path, testData.Data);
        }
Esempio n. 7
0
        public void TestConstructorReadWrite()
        {
            Assert.ThrowsException <SqliteException>(
                () => new SqliteFileSystem(this.testFile.FullName, OpenMode.ReadWrite),
                "unable to open database file");

            this.testFile.Refresh();
            Assert.IsFalse(this.testFile.Exists);

            using (new SqliteFileSystem(this.testFile.FullName, OpenMode.ReadWriteCreate))
            {
                // touch the db first
            }

            // try again
            using (var fs = new SqliteFileSystem(this.testFile.FullName, OpenMode.ReadWrite))
            {
                this.testFile.Refresh();
                Assert.IsTrue(this.testFile.Exists);
                Assert.IsFalse(fs.IsReadOnly);
            }
        }
Esempio n. 8
0
        public void TestAutomaticSchemaCreation()
        {
            // create a new empty database
            using (var fs = new SqliteFileSystem(this.testFile.FullName, OpenMode.ReadWriteCreate))
            {
            }

            // ensure a schema has been created
            string hasFileTable = (string)TestHelper.DirectQuery("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'files';", this.testFile.FullName);
            string hasMetaTable = (string)TestHelper.DirectQuery("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'meta';", this.testFile.FullName);

            Assert.AreEqual("files", hasFileTable);
            Assert.AreEqual("meta", hasMetaTable);

            string versionMatches = (string)TestHelper.DirectQuery("SELECT version FROM meta LIMIT 1;", this.testFile.FullName);

            Assert.AreEqual(Adapter.SchemaVersion, versionMatches);

            long pageSize = (long)TestHelper.DirectQuery("PRAGMA page_size;", this.testFile.FullName);

            Assert.AreEqual(Adapter.PageSize, pageSize);

            Debug.WriteLine($"Version: {versionMatches}, Page Size: {pageSize}");
        }