예제 #1
0
        public static string CopyBundledFileToDocuments(string realmName, string destPath = null)
        {
            destPath = RealmConfigurationBase.GetPathToRealm(destPath);  // any relative subdir or filename works

            var assembly     = typeof(TestHelpers).Assembly;
            var resourceName = assembly.GetManifestResourceNames().SingleOrDefault(s => s.EndsWith(realmName));

            if (resourceName == null)
            {
                throw new Exception($"Couldn't find embedded resource '{realmName}' in the RealmTests assembly");
            }

            using var stream      = assembly.GetManifestResourceStream(resourceName);
            using var destination = new FileStream(destPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

            stream.CopyTo(destination);

            return(destPath);
        }
예제 #2
0
        public static void CopyBundledDatabaseToDocuments(string realmName, string destPath = null, bool overwrite = true)
        {
            destPath = RealmConfigurationBase.GetPathToRealm(destPath);  // any relative subdir or filename works

#if __ANDROID__
            using (var asset = Application.Context.Assets.Open(realmName))
                using (var destination = File.OpenWrite(destPath))
                {
                    asset.CopyTo(destination);
                }
#else
#if __IOS__
            var sourceDir = Foundation.NSBundle.MainBundle.BundlePath;
#else
            var sourceDir = NUnit.Framework.TestContext.CurrentContext.TestDirectory;
#endif

            File.Copy(Path.Combine(sourceDir, realmName), destPath, overwrite);
#endif
        }
예제 #3
0
        public void Migrate()
        {
            var uri = RealmConfigurationBase.GetPathToRealm(DATABASE_PATH);

            bool databaseExist = File.Exists(uri);

            var configuration = new RealmConfiguration(DATABASE_PATH)
            {
                SchemaVersion     = DATABASE_SCHEMA_VERSION,
                MigrationCallback = (migration, oldSchemaVersion) => OnMigration(migration, oldSchemaVersion, migration.NewRealm.Config.SchemaVersion)
            };

            Realm database = Realm.GetInstance(configuration);

            if (!databaseExist)
            {
                var transaction = database.BeginWrite();
                this.OnDataBaseCreated(database);
                transaction.Commit();
            }

            database.Dispose();
        }