Beispiel #1
0
        public void TestSaveLateBoundTable()
        {
            // test saving and reloading
            var expected = new TestLateBoundTable {
                Id = 1, Data = Guid.NewGuid().ToString()
            };

            _databaseInstance.RegisterTableDefinition(
                _databaseInstance.CreateTableDefinition <TestLateBoundTable, int>(t => t.Id));

            _databaseInstance.SaveAsync(expected).Wait();

            var actual = _databaseInstance.LoadAsync <TestLateBoundTable>(expected.Id).Result;

            Assert.NotNull(actual);                   //Load failed.");

            Assert.Equal(expected.Id, actual.Id);     //Load failed: key mismatch.");
            Assert.Equal(expected.Data, actual.Data); //Load failed: data mismatch.");

            _databaseInstance.FlushAsync().Wait();

            _engine.Dispose();
            var driver = _databaseInstance.Driver;

            _databaseInstance = null;

            // bring it back up
            _engine = Factory.NewEngine();
            _engine.Activate();
            _databaseInstance =
                _engine.SterlingDatabase.RegisterDatabase <TestDatabaseInstance>(TestContext.TestName, driver);

            // do this in a different order
            _databaseInstance.RegisterTableDefinition(
                _databaseInstance.CreateTableDefinition <TestSecondLateBoundTable, int>(t => t.Id));

            _databaseInstance.RegisterTableDefinition(
                _databaseInstance.CreateTableDefinition <TestLateBoundTable, int>(t => t.Id));

            actual = _databaseInstance.LoadAsync <TestLateBoundTable>(expected.Id).Result;

            Assert.NotNull(actual);                   //Load failed after restart.");

            Assert.Equal(expected.Id, actual.Id);     //Load failed: key mismatch after restart.");
            Assert.Equal(expected.Data, actual.Data); //Load failed: data mismatch after restart.");
        }
        public void TestAddBill()
        {
            _database.PurgeAsync().Wait();

            var bill = new Bill
            {
                Id   = Guid.NewGuid(),
                Name = "Test"
            };

            _database.SaveAsync(bill).Wait();

            var person1 = new Person
            {
                Id   = Guid.NewGuid(),
                Name = "Martin"
            };

            _database.SaveAsync(person1).Wait();

            var partaker1 = new Partaker
            {
                Id     = Guid.NewGuid(),
                Paid   = 42,
                Person = person1
            };

            bill.Partakers.Add(partaker1);

            _database.SaveAsync(bill).Wait();

            var person2 = new Person
            {
                Id   = Guid.NewGuid(),
                Name = "Jeremy"
            };

            _database.SaveAsync(person2).Wait();

            var partaker2 = new Partaker
            {
                Id     = Guid.NewGuid(),
                Paid   = 0,
                Person = person2
            };

            bill.Partakers.Add(partaker2);

            _database.SaveAsync(bill).Wait();

            var partaker3 = new Partaker()
            {
                Id     = Guid.NewGuid(),
                Paid   = 1,
                Person = person1
            };

            bill.Partakers.Add(partaker3);

            _database.SaveAsync(bill).Wait();

            _database.FlushAsync().Wait();

            var billKeys = _database.Query <Bill, Guid>();

            Assert.IsTrue(billKeys.Count == 1);
            Assert.AreEqual(billKeys[0].Key, bill.Id);

            var freshBill = billKeys[0].LazyValue.Value;

            Assert.IsTrue(freshBill.Partakers.Count == 3, "Bill should have exactly 3 partakers.");

            var personKeys = _database.Query <Person, Guid>();

            Assert.IsTrue(personKeys.Count == 2, "Failed to save exactly 2 persons.");

            // Compare loaded instances and verify they are equal
            var persons = (from p in freshBill.Partakers where p.Person.Id.Equals(person1.Id) select p.Person).ToList();

            // should be two of these
            Assert.AreEqual(2, persons.Count, "Failed to grab two instances of the same person.");
            Assert.AreEqual(persons[0], persons[1], "Instances were not equal.");
        }