public void InsertSingle()
        {
            var json = "{ \"FirstName\":\"Mikael\", \"LastName\":\"Eliasson\",\"BirthDate\":\"1987-03-01\" }";

            var handler = new SisoDbManagementHandler();
            SetConfig();

            var adapter = new FakeContextAdapter()
            .WithPath("/insert")
            .WithEntityType<IPerson>()
            .WithJson(json);

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual("{ \"Success\" : true}", adapter.Response);
            Assert.AreEqual(200, adapter.StatusCode);

            var persons = Configuration.DB.UseOnceTo().Query<IPerson>().ToListOf<Person>();

            Assert.AreEqual(1, persons.Count);
            Assert.AreEqual("Mikael", persons[0].FirstName);
            Assert.AreEqual("Eliasson", persons[0].LastName);

        }
        public void DeleteSingle()
        {
            var handler = new SisoDbManagementHandler();
            var persons = new List<Person>
            {
                new Person{FirstName = "P1"},
                new Person{FirstName = "P2"},
                new Person{FirstName = "P3"}
            };
            SetConfig(session => session.InsertMany<IPerson>(persons));

            var adapter = new FakeContextAdapter()
            .WithPath("/delete")
            .WithEntityType<IPerson>()
            .WithId(persons[1].Id);

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual(200, adapter.StatusCode);

            var updatedPersons = Configuration.DB.UseOnceTo().Query<IPerson>().ToListOf<Person>();

            Assert.AreEqual(2, updatedPersons.Count);
            Assert.IsTrue(!updatedPersons.Any(p => p.FirstName == "P2"));
        }
        public void DeleteMatchingCriteria()
        {
            var handler = new SisoDbManagementHandler();
            var persons = new List<Person>
            {
                new Person{FirstName = "P1", Birthdate = DateTime.Parse("2012-01-02")},
                new Person{FirstName = "P2", Birthdate = DateTime.Parse("2011-01-02")},
                new Person{FirstName = "P3", Birthdate = DateTime.Parse("2010-01-02")},
            };
            SetConfig(session => session.InsertMany<IPerson>(persons));

            var adapter = new FakeContextAdapter()
            .WithPath("/deletebyquery")
            .WithEntityType<IPerson>()
            .WithPredicate("p => p.Birthdate < DateTime.Parse(\"2012-01-01\")");

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual("2", adapter.Response);
            Assert.AreEqual(200, adapter.StatusCode);

            var updatedPersons = Configuration.DB.UseOnceTo().Query<IPerson>().ToListOf<Person>();

            Assert.AreEqual(1, updatedPersons.Count);
            Assert.IsTrue(updatedPersons[0].FirstName == "P1");
        }
        public void WithExistingDb_ShouldNotDestoryDb()
        {
            var handler = new SisoDbManagementHandler();

            var persons = new List<Person>
            {
                new Person{FirstName = "P1"},
                new Person{FirstName = "P2"},
                new Person{FirstName = "P3"}
            };
            SetConfig(session => session.InsertMany<IPerson>(persons));

            var adapter = new FakeContextAdapter()
            .WithPath("/initdb")
            .WithEntityType<IPerson>();

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual(200, adapter.StatusCode);

            var updatedPersons = Configuration.DB.UseOnceTo().Query<IPerson>().ToListOf<Person>();

            Assert.AreEqual(3, updatedPersons.Count);

        }
        public void NoAuthorizeSet_Returns500()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/page");

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual(500, adapter.StatusCode);

            Assert.IsTrue(!adapter.Response.Contains("<html"));
        }
        public void AuthorizeReturnsFalse_Returns401()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/page");
            Configuration.Authorize = str => false;
            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual(401, adapter.StatusCode);

            Assert.IsTrue(!adapter.Response.Contains("<html"));
        }
        public void NoPredicate_NoOrderBy_ReturnsAll()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/query")
            .WithEntityType<IPerson>();

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            AssertThat(adapter)
            .IsSuccess()
            .HasEntityCount(501)
            .HasTotalMatches(501);
        }
        public void WithNoSchema_InsertsSchema()
        {
            var handler = new SisoDbManagementHandler();
            SetConfig();

            var adapter = new FakeContextAdapter()
            .WithPath("/insertschema")
            .WithEntityType<IPerson>();

            SisoDbManagementHandler.Context = adapter;

            Configuration.DB.Configure().ForProduction();
            handler.ProcessRequest(null);

            Assert.AreEqual(200, adapter.StatusCode);

            //Query persons to trigger exception if the schema wasn't already inserted
            var updatedPersons = Configuration.DB.UseOnceTo().Query<IPerson>().ToListOf<Person>();
        }
        public void WithoutStructures()
        {
            var handler = new SisoDbManagementHandler();

            Setup();

            var adapter = new FakeContextAdapter()
            .WithPath("/initdb")
            .WithEntityType<IPerson>();

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual(200, adapter.StatusCode);

            var db = "data source=.\\;initial catalog=SisoDb.Management.Tests;integrated security=SSPI;".CreateSql2008Db();
            Assert.IsTrue(db.Exists());
            
        }
        public void OrderByBirthdate_NoPage_NoSortorder_ReturnsAllOrderedAsc()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/query")
            .WithEntityType<IPerson>()
            .WithOrderBy("x => x.Birthdate");

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            AssertThat(adapter)
                .IsSuccess()
                .HasEntityCount(501)
                .HasTotalMatches(501)
                .FirstEntityShould(p => Assert.AreEqual(DateTime.Parse("1987-03-01"), p.Birthdate))
                .LastEntityShould(p => Assert.AreEqual(lastDate, p.Birthdate));
        }
        public void WithStructures()
        {
            var handler = new SisoDbManagementHandler();

            Setup();

            var adapter = new FakeContextAdapter()
            .WithPath("/initdbandstructures")
            .WithEntityType<IPerson>();

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual(200, adapter.StatusCode);

            var db = "data source=.\\;initial catalog=SisoDb.Management.Tests;integrated security=SSPI;".CreateSql2008Db();
            db.Configure().ForProduction();
            Assert.IsTrue(db.Exists());
            //Force exception if tables aren't already created
            var persons = db.UseOnceTo().Query<IPerson>().ToList();

        }
        public void DoesNotAlterEnabledUpsert()
        {
            var handler = new SisoDbManagementHandler();
            var persons = new List<Person>
            {
                new Person{FirstName = "P1"},
                new Person{FirstName = "P2"},
                new Person{FirstName = "P3"}
            };
            SetConfig(session => session.InsertMany<IPerson>(persons));

            var adapter = new FakeContextAdapter()
            .WithPath("/insertschema")
            .WithEntityType<IPerson>();

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            Assert.AreEqual(200, adapter.StatusCode);

            Assert.IsTrue(Configuration.DB.Settings.AllowDynamicSchemaCreation);
            Assert.IsTrue(Configuration.DB.Settings.AllowDynamicSchemaUpdates);
        }
 public PersonQueryResultEvaluator(FakeContextAdapter adapter)
 {
     this.adapter = adapter;
     if (adapter.StatusCode == 200)
     {
         QueryResult = JsonSerializer.DeserializeFromString<FakeQueryResult>(adapter.Response);
         Entities = QueryResult.Entities.ToList();
     }
 }
        public void FilterWithBadPredicate_ShouldReturnErrorAndMessageAboutThePredicateBeingBad()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/query")
            .WithEntityType<IPerson>()
            .WithPredicate("p => p.SomeNameThatDoesNotExist == 2");

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            AssertThat(adapter).FailedWithMessage("Mono could not parse the predicate");
        }
        public void FilterUsingASisoMethods()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/query")
            .WithEntityType<IPerson>()
            .WithPredicate("p => p.FirstName.QxStartsWith(\"Mik\")");

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            AssertThat(adapter)
                .IsSuccess()
                .HasEntityCount(1)
                .HasTotalMatches(1)
                .FirstEntityShould(p => Assert.AreEqual("Mikael", p.FirstName));
        }
        public void FilterOnDateWithSetup()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/query")
            .WithEntityType<IPerson>()
            .WithSetup("var date = DateTime.Parse(\"1987-03-01\");")
            .WithPredicate("p => p.Birthdate == date");

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            AssertThat(adapter)
                .IsSuccess()
                .HasEntityCount(1)
                .HasTotalMatches(1)
                .FirstEntityShould(p => Assert.AreEqual("Mikael", p.FirstName));
        }
        public void OrderByBirthdate_PageSize100_Page1_Asc_Returns101To200()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/query")
            .WithEntityType<IPerson>()
            .WithOrderBy("x => x.Birthdate")
            .WithPageSize(100)
            .WithPage(1)
            .WithSortOrder("asc");

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            AssertThat(adapter)
                .IsSuccess()
                .HasEntityCount(100)
                .HasTotalMatches(501)
                .FirstEntityShould(p => Assert.AreEqual("F99", p.FirstName))
                .LastEntityShould(p => Assert.AreEqual("F198", p.FirstName));
        }
        public void SortAndPredicateWorksToghether()
        {
            var handler = new SisoDbManagementHandler();

            var adapter = new FakeContextAdapter()
            .WithPath("/query")
            .WithEntityType<IPerson>()
            .WithOrderBy("x => x.Birthdate")
            .WithPageSize(100)
            .WithPredicate("p => p.FirstName.QxStartsWith(\"Mik\")");

            SisoDbManagementHandler.Context = adapter;

            handler.ProcessRequest(null);

            AssertThat(adapter)
                .IsSuccess()
                .HasEntityCount(1)
                .HasTotalMatches(1)
                .FirstEntityShould(p => Assert.AreEqual("Mikael", p.FirstName));
        }
 private PersonQueryResultEvaluator AssertThat(FakeContextAdapter adapter)
 {
     return new PersonQueryResultEvaluator(adapter);
 }