Example #1
0
        public void GetInsertSqlTest()
        {
            var id         = HelperObjectFactory.GetRandomInt(0, 100);
            var value      = new Artist(id, StringExtensions.GetRandomStringAsync(23).Result);
            var parameters = new List <SQLiteParameter>();
            var table      = new ArtistTable();
            var expected   = $"INSERT OR IGNORE INTO {TableFactory<Artist>.GetTable<ArtistTable>().TableName} ({ArtistTable.IdColumnName}, {AlbumTable.NameColumnName}) VALUES(@{ArtistTable.IdColumnName}, @{ArtistTable.NameColumnName});";

            Assert.AreEqual(expected, table.GetInsertSql(value, ref parameters));
            Assert.AreEqual(2, parameters.Count);
        }
Example #2
0
        public async Task InitializeDatabaseNonUniqueIdsExceptionTest()
        {
            using (var container = Substitute.For <UnityContainer>())
            {
                using (var cursor = Substitute.For <ICursor>())
                {
                    cursor.GetInt(0).Returns(1);

                    var artistTable = new ArtistTable();
                    var albumTable  = new AlbumTable();
                    var genreTable  = new GenreTable();

                    var artistParameters = new List <SQLiteParameter>();
                    var albumParameters  = new List <SQLiteParameter>();
                    var genreParameters  = new List <SQLiteParameter>();

                    var artistSql = artistTable.GetInsertSql(new Artist(0, "Michael Jackson"), ref artistParameters);
                    var genreSql  = genreTable.GetInsertSql(new Genre(0, "Pop"), ref genreParameters);
                    var albumSql  = albumTable.GetInsertSql(new Album(0, 0, 0, "Thriller", 1982), ref albumParameters);

                    var dataAccess = Substitute.For <IDataAccess>();
                    dataAccess.OpenOrCreateDatabaseAsync().Returns(DatabaseState.New);
                    dataAccess.ExecuteQueryWithParametersAsync(Arg.Any <string>(), Arg.Any <List <SQLiteParameter> >()).Returns(cursor);

                    dataAccess.ExecuteSqlWithParametersAsync(artistSql, Arg.Any <List <SQLiteParameter> >()).Returns(1);
                    dataAccess.ExecuteSqlWithParametersAsync(genreSql, Arg.Any <List <SQLiteParameter> >()).Returns(0);
                    dataAccess.ExecuteSqlWithParametersAsync(albumSql, Arg.Any <List <SQLiteParameter> >()).Returns(1);

                    container.RegisterInstance(Resources.DataAccessObjectName, dataAccess, new ContainerControlledLifetimeManager());

                    container.RegisterType <AlbumController>(new InjectionFactory(s => new AlbumController(dataAccess)));
                    container.RegisterType <ArtistController>(new InjectionFactory(s => new ArtistController(dataAccess)));
                    container.RegisterType <GenreController>(new InjectionFactory(s => new GenreController(dataAccess)));
                    container.RegisterType <StatisticsController>(new InjectionFactory(s => new StatisticsController(dataAccess)));

                    try
                    {
                        await DatabaseHelpers.InitializeDatabase(container);
                    }
                    catch (Exception ex)
                    {
                        Assert.IsInstanceOf <SQLiteException>(ex);
                        Assert.AreEqual($"unknown error\r\n{Resources.ErrorAddingItemMessage}", ex.Message);
                    }
                }
            }
        }