public ExpressionTest() { _db = new AppDb(); // initialize simple data types _simple = new DataTypesSimple { TypeDateTime = new DateTime(2017, 1, 1, 0, 0, 0), TypeDouble = 3.1415, TypeDoubleN = -3.1415 }; _db.DataTypesSimple.Add(_simple); // initialize simple data types _simple2 = new DataTypesSimple { TypeDouble = 1, TypeDoubleN = -1 }; _db.DataTypesSimple.Add(_simple2); // initialize variable data types _variable = DataTypesVariable.CreateEmpty(); _variable.TypeString = "EntityFramework"; _db.DataTypesVariable.Add(_variable); _db.SaveChanges(); }
public async Task TestDataTypesVariable() { void TestEmpty(DataTypesVariable valueDb) { // string not null Assert.Equal("", valueDb.TypeString); Assert.Equal("", valueDb.TypeString255); // string null Assert.Equal(null, valueDb.TypeStringN); Assert.Equal(null, valueDb.TypeString255N); // binary not null Assert.Equal(DataTypesVariable.EmptyByteArray, valueDb.TypeByteArray); Assert.Equal(DataTypesVariable.EmptyByteArray, valueDb.TypeByteArray255); // binary null Assert.Equal(null, valueDb.TypeByteArrayN); Assert.Equal(null, valueDb.TypeByteArray255N); // json not null Assert.Equal(DataTypesVariable.EmptyJsonArray.Json, valueDb.TypeJsonArray.Json); Assert.Equal(DataTypesVariable.EmptyJsonObject.Json, valueDb.TypeJsonObject.Json); // json null Assert.Equal(null, valueDb.TypeJsonArrayN); Assert.Equal(null, valueDb.TypeJsonObjectN); } var string255 = new string('a', 255); var string4K = new string('a', 4000); var byte255 = new byte[255]; var byte10K = new byte[10000]; for (var i = 0; i < byte10K.Length; i++) { if (i < 255) { byte255[i] = (byte)'a'; } byte10K[i] = (byte)'a'; } var jsonArray = new JsonObject <List <string> >(new List <string> { "test" }); var jsonObject = new JsonObject <Dictionary <string, string> >(new Dictionary <string, string> { { "test", "test" } }); // test each data type with a valid value DataTypesVariable NewValueMem() => new DataTypesVariable { // string not null TypeString = string4K, TypeString255 = string255, // should be truncated by DBMS // string null TypeStringN = string4K, TypeString255N = string255, // should be truncated by DBMS // binary not null TypeByteArray = byte10K, TypeByteArray255 = byte255, // should be truncated by DBMS // binary null TypeByteArrayN = byte10K, TypeByteArray255N = byte255, // should be truncated by DBMS // json not null TypeJsonArray = jsonArray, TypeJsonObject = jsonObject, // json null TypeJsonArrayN = jsonArray, TypeJsonObjectN = jsonObject, }; void TestValue(DataTypesVariable valueDb) { // string not null Assert.Equal(string4K, valueDb.TypeString); Assert.Equal(string255, valueDb.TypeString255); // string null Assert.Equal(string4K, valueDb.TypeStringN); Assert.Equal(string255, valueDb.TypeString255N); // binary not null Assert.Equal(byte10K, valueDb.TypeByteArray); Assert.Equal(byte255, valueDb.TypeByteArray255); // binary null Assert.Equal(byte10K, valueDb.TypeByteArrayN); Assert.Equal(byte255, valueDb.TypeByteArray255N); // json not null Assert.Equal(jsonArray.Json, valueDb.TypeJsonArray.Json); Assert.Equal(jsonObject.Json, valueDb.TypeJsonObject.Json); // json null Assert.Equal(jsonArray.Json, valueDb.TypeJsonArrayN.Json); Assert.Equal(jsonObject.Json, valueDb.TypeJsonObjectN.Json); } // create test data objects var emptyMemAsync = DataTypesVariable.CreateEmpty(); var emptyMemSync = DataTypesVariable.CreateEmpty(); var valueMemAsync = NewValueMem(); var valueMemSync = NewValueMem(); // save them to the database using (var db = new AppDb()){ db.DataTypesVariable.Add(emptyMemAsync); db.DataTypesVariable.Add(valueMemAsync); await db.SaveChangesAsync(); db.DataTypesVariable.Add(emptyMemSync); db.DataTypesVariable.Add(valueMemSync); db.SaveChanges(); } // load them from the database and run tests using (var db = new AppDb()) { // ReSharper disable once AccessToDisposedClosure async Task <DataTypesVariable> FromDbAsync(DataTypesVariable dt) => await db.DataTypesVariable.FirstOrDefaultAsync(m => m.Id == dt.Id); // ReSharper disable once AccessToDisposedClosure DataTypesVariable FromDbSync(DataTypesVariable dt) => db.DataTypesVariable.FirstOrDefault(m => m.Id == dt.Id); TestEmpty(await FromDbAsync(emptyMemAsync)); TestEmpty(FromDbSync(emptyMemSync)); TestValue(await FromDbAsync(valueMemAsync)); TestValue(FromDbSync(valueMemSync)); } }