Esempio n. 1
0
        public void CheckTableScheme_TableSchemeHasNoAttributes_ThrowsInvalidTableAttributesException()
        {
            // Arrange
            TableScheme tableScheme = new TableScheme("testTable", new List <Models.Attribute>());

            // Arrange - create target
            DatabaseValidation target = new DatabaseValidation(this._dbValidationSettings);

            // Act and Assert
            Assert.Throws <InvalidTableAttributesException>(() => target.CheckTableScheme(tableScheme));
        }
Esempio n. 2
0
        public ITree CreateObject(params object[] args)
        {
            IDbDataSource source = ObjectUtil.ConfirmQueryObject <IDbDataSource>(this, args);

            ITableScheme scheme = TableScheme.CreateObject();
            NormalDbTree dbTree = new NormalDbTree(scheme, DbTree, source);

            if (DataRight != null)
            {
                dbTree.DataRight = DataRight.CreateObject(dbTree);
            }
            return(dbTree);
        }
Esempio n. 3
0
        ///
        public static TableQuery <InstanceCountEntity> GetQuery(DateTime startTime, DateTime endTime)
        {
            if (startTime > endTime)
            {
                throw new InvalidOperationException("Start time must be less than or equal to end time");
            }
            string rowKeyStart = string.Format(CultureInfo.InvariantCulture, RowKeyPrefix, startTime.Ticks);
            string rowKeyEnd   = string.Format(CultureInfo.InvariantCulture, RowKeyPrefix, endTime.Ticks);

            var query = TableScheme.GetRowsInRange <InstanceCountEntity>(PartitionKeyFormat, rowKeyStart, rowKeyEnd);

            return(query);
        }
Esempio n. 4
0
        public TableRecords GetTableRecords(string tableName)
        {
            _sqlConnection.Open();

            // Table records
            var cmd = _sqlConnection.CreateCommand();

            cmd.CommandText = $"SELECT * FROM {tableName} FOR JSON AUTO";
            var reader        = cmd.ExecuteReader();
            var stringBuilder = new StringBuilder();

            while (reader.Read())
            {
                stringBuilder.Append(reader[0]);
            }
            var recordsJson = JToken.Parse(stringBuilder.ToString());

            // Table scheme
            var table       = _sqlConnection.GetSchema("Tables", new[] { _sqlConnection.Database, null, tableName });
            var columns     = _sqlConnection.GetSchema("Columns", new[] { _sqlConnection.Database, null, tableName });
            var tableScheme = new TableScheme()
            {
                Name    = tableName,
                Type    = table.Rows[0].ItemArray[3].ToString(),
                Columns = new List <ColumnScheme>(),
            };

            foreach (DataRow column in columns.Rows)
            {
                tableScheme.Columns.Add(new ColumnScheme()
                {
                    Name            = column.ItemArray[3].ToString(),
                    OrdinalPosition = column.ItemArray[4].ToString(),
                    DefaultValue    = column.ItemArray[5].ToString(),
                    IsNullable      = column.ItemArray[6].ToString() == "YES" ? true : false,
                    DataType        = column.ItemArray[7].ToString(),
                    CharLength      = column.ItemArray[8].ToString()
                });
            }

            var tableRecords = new TableRecords()
            {
                TableName   = tableName,
                Records     = recordsJson,
                TableScheme = tableScheme
            };

            _sqlConnection.Close();
            return(tableRecords);
        }
Esempio n. 5
0
        public void CreateTable_TableWithSuchNameAlreadyExists_ThrowsTableAlreadyExistsException()
        {
            // Arrange
            string      dbName      = this._testDb.Name;
            TableScheme tableScheme = new TableScheme(this._testTable.Name, new List <Models.Attribute>());

            // Arrange - create target
            IDatabaseService target = new DatabaseService(this._dbRepositoryMock.Object, this._dbValidationMock.Object);

            // Act and Assert
            Assert.Throws <TableAlreadyExistsException>(() => target.CreateTable(dbName, tableScheme));

            this._dbRepositoryMock.Verify(r => r.Update(It.Is <Database>(db => db.Name == dbName)), Times.Never);
        }
Esempio n. 6
0
        public void CreateTable_ArgumentsAreNullOrEmpty_ThrowsArgumentExceptions()
        {
            // Arrange
            string      dbName      = "testDatabase";
            TableScheme tableScheme = new TableScheme("testTable", new List <Models.Attribute>());

            // Arrange - create target
            IDatabaseService target = new DatabaseService(this._dbRepositoryMock.Object, this._dbValidationMock.Object);

            // Act and Assert
            Assert.Throws <ArgumentNullException>(() => target.CreateTable(null, tableScheme));
            Assert.Throws <ArgumentNullException>(() => target.CreateTable(dbName, null));

            Assert.Throws <ArgumentException>(() => target.CreateTable("", tableScheme));
        }
Esempio n. 7
0
        public void CheckTableScheme_TableSchemeIsValid_DoesNotThrowAnyException()
        {
            // Arrange
            TableScheme tableScheme = new TableScheme("testTable",
                                                      new List <Models.Attribute>
            {
                new Models.Attribute {
                    Name = "testAttribute", Type = this._dbValidationSettings.DataTypes.Keys.First()
                }
            });

            // Arrange - create target
            DatabaseValidation target = new DatabaseValidation(this._dbValidationSettings);

            // Act and Assert
            Assert.DoesNotThrow(() => target.CheckTableScheme(tableScheme));
        }
Esempio n. 8
0
        public void CreateTable_NonexistentDatabaseName_ThrowsDatabaseNotFoundException()
        {
            // Arrange
            string      dbName      = "testDatabase";
            TableScheme tableScheme = new TableScheme("testTable", new List <Models.Attribute>());

            // Arrange - mock dbRepository
            this._dbRepositoryMock.Setup(r => r.GetByName(dbName))
            .Returns((Database)null);

            // Arrange - create target
            IDatabaseService target = new DatabaseService(this._dbRepositoryMock.Object, this._dbValidationMock.Object);

            // Act and Assert
            Assert.Throws <DatabaseNotFoundException>(() => target.CreateTable(dbName, tableScheme));

            this._dbRepositoryMock.Verify(r => r.Update(It.Is <Database>(db => db.Name == dbName)), Times.Never);
        }
Esempio n. 9
0
        public void CreateTable_DbRepositoryThrowsDbRepositoryException_ThrowsDbServiceException()
        {
            // Arrange
            string      dbName      = this._testDb.Name;
            TableScheme tableScheme = new TableScheme("testTable", new List <Models.Attribute>());

            DbRepositoryException innerException = new DbRepositoryException();

            // Arrange - mock dbRepository
            this._dbRepositoryMock.Setup(r => r.Update(It.IsAny <Database>()))
            .Throws(innerException);

            // Arrange - create target
            IDatabaseService target = new DatabaseService(this._dbRepositoryMock.Object, this._dbValidationMock.Object);

            // Act and Assert
            DbServiceException ex = Assert.Throws <DbServiceException>(() => target.CreateTable(dbName, tableScheme));

            Assert.AreSame(innerException, ex.InnerException);
        }
Esempio n. 10
0
        public void CheckTableScheme_TableSchemeHasInvalidName_ThrowsInvalidTableNameException()
        {
            // Arrange
            string[]           tableNames = { "", " ", new string(Path.GetInvalidFileNameChars()) };
            Models.Attribute[] attributes =
            {
                new Models.Attribute {
                    Name = "testAttribute", Type = this._dbValidationSettings.DataTypes.Keys.First()
                }
            };

            // Arrange - create target
            DatabaseValidation target = new DatabaseValidation(this._dbValidationSettings);

            // Act and Assert
            foreach (string tableName in tableNames)
            {
                TableScheme tableScheme = new TableScheme(tableName, attributes);

                Assert.Throws <InvalidTableNameException>(() => target.CheckTableScheme(tableScheme));
            }
        }
Esempio n. 11
0
        public void CreateTable_TableSchemeIsInvalid_ThrowsInvalidTableSchemeException()
        {
            // Arrange
            string      dbName      = this._testDb.Name;
            TableScheme tableScheme = new TableScheme("testTable", new List <Models.Attribute>());

            Exception innerException = new Exception();

            // Arrange - mock dbValidation
            this._dbValidationMock.Setup(v => v.CheckTableScheme(tableScheme))
            .Throws(innerException);

            // Arrange - create target
            IDatabaseService target = new DatabaseService(this._dbRepositoryMock.Object, this._dbValidationMock.Object);

            // Act and Assert
            InvalidTableSchemeException exeption =
                Assert.Throws <InvalidTableSchemeException>(() => target.CreateTable(dbName, tableScheme));

            Assert.AreSame(exeption.InnerException, innerException);

            this._dbRepositoryMock.Verify(r => r.Update(It.Is <Database>(db => db.Name == dbName)), Times.Never);
        }
Esempio n. 12
0
        public void CreateTable_DatabaseServiceCreatesTable_ReturnsCreatedAtRouteResult()
        {
            // Arrange
            string      dbName      = "testDatabase";
            TableScheme tableScheme = new TableScheme("testTable", new List <Domain.Models.Attribute>());

            // Arrange - create target
            DatabaseApiController target = new DatabaseApiController(this._dbServiceMock.Object);

            // Act
            IHttpActionResult actionResult = target.CreateTable(dbName, tableScheme);

            CreatedAtRouteNegotiatedContentResult <TableScheme> createdResult =
                actionResult as CreatedAtRouteNegotiatedContentResult <TableScheme>;

            // Assert
            Assert.IsNotNull(createdResult);
            Assert.AreEqual("GetTable", createdResult.RouteName);
            Assert.AreEqual(dbName, createdResult.RouteValues["dbName"]);
            Assert.AreEqual(tableScheme.Name, createdResult.RouteValues["tableName"]);

            this._dbServiceMock.Verify(s => s.CreateTable(dbName, tableScheme), Times.Once);
        }
Esempio n. 13
0
        public void Parse(string rowKey, int termNumber, string expected)
        {
            string actual = TableScheme.GetNthTerm(rowKey, termNumber);

            Assert.Equal(expected, actual);
        }