Example #1
0
        public void GetItems_UseResultObject_offsetlimit()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings {
                UseResultObject = true
            });

            var controller = new DynamicController(ds, apiSettings);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();
            controller.ControllerContext.HttpContext.Request.QueryString = new QueryString("?offset=5&limit=12");

            var result = controller.GetItems("families") as OkObjectResult;

            var resultObject = JsonConvert.DeserializeObject <dynamic>(JsonConvert.SerializeObject(result.Value));

            Assert.Equal(12, resultObject.results.Count);
            Assert.Equal(5, resultObject.offset.Value);
            Assert.Equal(12, resultObject.limit.Value);
            Assert.Equal(20, resultObject.count.Value);

            UTHelpers.Down(filePath);
        }
Example #2
0
        public async Task PutItem_Upsert()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings {
                UpsertOnPut = true
            });

            var controller = new DynamicController(ds, apiSettings);

            var result = await controller.ReplaceItem("my_test", "2", JToken.Parse("{ 'id': 2, 'name': 'Raymond', 'age': 32 }"));

            Assert.IsType <NoContentResult>(result);

            var itemResult = controller.GetItem("my_test", "2");

            Assert.IsType <OkObjectResult>(itemResult);

            var     okObjectResult = itemResult as OkObjectResult;
            dynamic item           = okObjectResult.Value as ExpandoObject;

            Assert.Equal("Raymond", item.name);

            UTHelpers.Down(filePath);
        }
Example #3
0
        public void Query_Families()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            var q = @"
                    query {
                          families {
                            familyName
                            children(age: 4) {
                              name
                              age
                            }
                          }
                    }";

            var results = GraphQL.GraphQL.HandleQuery(q, ds, _idFieldName);

            var js = JsonConvert.SerializeObject(results);

            Assert.NotEmpty(js);

            Assert.Equal(4, results.Data["families"].Count);

            UTHelpers.Down(filePath);
        }
Example #4
0
        public void Mutation_Add_User()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            var muation = @"
                    mutation {
                          addUsers ( input: {
                            users: {
                              name: Jimmy
                              age: 22
                            }
                          }) {
                            users {
                              id
                              name
                              age
                            }
                          }
                    }";

            var results = GraphQL.GraphQL.HandleQuery(muation, ds, _idFieldName);

            var id   = ((dynamic)results.Data["users"]).id;
            var item = ds.GetCollection("users").AsQueryable().First(e => e.id == id);

            Assert.Equal("Jimmy", item.name);
            Assert.Equal(22, item.age);

            UTHelpers.Down(filePath);
        }
        public async Task Query_Families()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            var q = @"
                    query {
                          families {
                            familyName
                            children(age: 4) {
                              name
                              age
                            }
                          }
                    }";

            var results = await GraphQL.GraphQL.HandleQuery(q, ds);

            var js = JsonConvert.SerializeObject(results);

            Assert.NotEmpty(js);

            UTHelpers.Down(filePath);
        }
Example #6
0
        public void GetItems_EmptyCollection()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings {
                UseResultObject = true
            });
            var dsSettings = Options.Create(new DataStoreSettings());

            var controller = new DynamicController(ds, apiSettings, dsSettings);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();

            var result = controller.GetItems("empty_collection") as OkObjectResult;

            var resultObject = JsonConvert.DeserializeObject <dynamic>(JsonConvert.SerializeObject(result.Value));

            Assert.Equal(0, resultObject.results.Count);
            Assert.Equal(0, resultObject.skip.Value);
            Assert.Equal(512, resultObject.take.Value);
            Assert.Equal(0, resultObject.count.Value);

            UTHelpers.Down(filePath);
        }
Example #7
0
        public void GetItems_UseResultObject()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings {
                UseResultObject = true
            });
            var dsSettings = Options.Create(new DataStoreSettings());

            var controller = new DynamicController(ds, apiSettings, dsSettings);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();
            controller.ControllerContext.HttpContext.Request.QueryString = new QueryString("");

            var result = controller.GetItems("families", 4, 10) as OkObjectResult;

            dynamic resultObject = JsonConvert.DeserializeObject <dynamic>(JsonConvert.SerializeObject(result.Value));

            Assert.Equal(10, resultObject.results.Count);
            Assert.Equal(4, resultObject.skip.Value);
            Assert.Equal(10, resultObject.take.Value);
            Assert.Equal(20, resultObject.count.Value);

            UTHelpers.Down(filePath);
        }
Example #8
0
        public void Mutation_Delete_User()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            dynamic original = ds.GetCollection("users").AsQueryable().First(e => e.id == 2);

            Assert.NotNull(original);

            var muation = @"
                    mutation {
                          deleteUsers ( input: {
                            id: 2
                          })
                    }";

            var results = GraphQL.GraphQL.HandleQuery(muation, ds, _idFieldName);

            var success = results.Data["Result"];

            Assert.True(success);

            var item = ds.GetCollection("users").AsQueryable().FirstOrDefault(e => e.id == 2);

            Assert.Null(item);

            UTHelpers.Down(filePath);
        }
Example #9
0
        public void Query_Users(string q)
        {
            var filePath = UTHelpers.Up();

            var ds      = new DataStore(filePath);
            var results = GraphQL.GraphQL.HandleQuery(q, ds, _idFieldName);

            Assert.Equal(4, results.Data["users"].Count);

            UTHelpers.Down(filePath);
        }
        public async Task Query_Users(string q)
        {
            var filePath = UTHelpers.Up();

            var ds      = new DataStore(filePath);
            var results = await GraphQL.GraphQL.HandleQuery(q, ds);

            Assert.Equal(4, results.Data["users"].Count);

            UTHelpers.Down(filePath);
        }
Example #11
0
        public void Query_Errors()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            var q = @" {  }";

            var results = GraphQL.GraphQL.HandleQuery(q, ds, _idFieldName);

            Assert.Single(results.Errors);

            UTHelpers.Down(filePath);
        }
        public async Task Query_Errors()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            var q = @" {  }";

            var results = await GraphQL.GraphQL.HandleQuery(q, ds);

            Assert.Equal(1, results.Errors.Count);

            UTHelpers.Down(filePath);
        }
Example #13
0
        public void GetNested_ParentsSingle()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings());

            var controller = new DynamicController(ds, apiSettings);

            var result = controller.GetNested("families", 1, "parents/1") as OkObjectResult;

            Assert.Equal("Kim", ((dynamic)result.Value).name);

            UTHelpers.Down(filePath);
        }
Example #14
0
        public void GetNested_ParentsList()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings());

            var controller = new DynamicController(ds, apiSettings);

            var result = controller.GetNested("families", 1, "parents") as OkObjectResult;

            Assert.Equal(2, ((IEnumerable <dynamic>)result.Value).Count());

            UTHelpers.Down(filePath);
        }
        public void GetNested_Single_BadRequest()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings());

            var controller = new DynamicController(ds, apiSettings);

            var result = controller.GetNested("configuration", 0, "ip");

            Assert.IsType <BadRequestResult>(result);

            UTHelpers.Down(filePath);
        }
Example #16
0
        public void GetCollections()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings());

            var controller = new DynamicController(ds, apiSettings);

            var collections = controller.GetKeys();

            Assert.Equal(3, collections.Count());

            UTHelpers.Down(filePath);
        }
Example #17
0
        public void Mutation_Replace_User_With_Child()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            var original = ds.GetCollection("users").AsQueryable().First(e => e.id == 2);

            Assert.NotNull(original);
            Assert.Equal("London", original.location);

            var muation = @"
                    mutation {
                          replaceUsers ( input: {
                            id: 2
                            users: {
                              name: Newton
                              age: 45
                              work: {
                                name: ACME
                              }
                            }
                          }) {
                            users {
                              id
                              name
                              age
                              work {
                                name
                              }
                            }
                          }
                    }";

            var results = GraphQL.GraphQL.HandleQuery(muation, ds, _idFieldName);

            var item = ds.GetCollection("users").AsQueryable().First(e => e.id == 2);

            Assert.Equal("Newton", item.name);
            Assert.Equal(45, item.age);
            Assert.Equal("ACME", item.work.name);

            var itemDict = item as IDictionary <string, object>;

            Assert.False(itemDict.ContainsKey("location"));

            UTHelpers.Down(filePath);
        }
        public async Task AddItem_Single_Conflict()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings());

            var controller = new DynamicController(ds, apiSettings);

            var item = new { value = "hello" };

            var result = await controller.AddNewItem("configuration", JToken.FromObject(item));

            Assert.IsType <ConflictResult>(result);

            UTHelpers.Down(filePath);
        }
Example #19
0
        public async Task PutItem_NoUpsert()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings {
                UpsertOnPut = false
            });

            var controller = new DynamicController(ds, apiSettings);

            var result = await controller.ReplaceItem("my_test", "2", JToken.Parse("{ 'id': 2, 'name': 'Raymond', 'age': 32 }"));

            Assert.IsType <NotFoundResult>(result);

            UTHelpers.Down(filePath);
        }
        public void Stop()
        {
            if (Client != null)
            {
                Client.Dispose();
                Client = null;
            }

            if (_factory != null)
            {
                _factory.Dispose();
                _factory = null;
            }

            UTHelpers.Down(_newFilePath);
        }
        public void GetItem_Single_BadRequest()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings {
                UpsertOnPut = true
            });

            var controller = new DynamicController(ds, apiSettings);

            var itemResult = controller.GetItem("configuration", "0");

            Assert.IsType <BadRequestResult>(itemResult);

            UTHelpers.Down(filePath);
        }
Example #22
0
        public void Mutation_Add_User_With_ChildList()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            var muation = @"
                    mutation {
                          addFamilies ( input: {
                            families: {
                              familyName: Newtons
                              children: [
                                {
                                  name: Mary
                                  age: 12
                                },
                                {
                                  name: James
                                  age: 16
                                }
                              ]
                            }
                          }) {
                            families {
                              id
                              familyName
                              notFoundProperty
                              children {
                                name
                                age
                              }
                            }
                          }
                    }";

            var results = GraphQL.GraphQL.HandleQuery(muation, ds, _idFieldName);

            var id   = ((dynamic)results.Data["families"]).id;
            var item = ds.GetCollection("families").AsQueryable().First(e => e.id == id);

            Assert.Equal("Newtons", item.familyName);
            Assert.Equal("Mary", item.children[0].name);
            Assert.Equal("James", item.children[1].name);
            Assert.Equal(16, item.children[1].age);

            UTHelpers.Down(filePath);
        }
        public void StartServer(string authenticationType = "")
        {
            var path     = Path.GetDirectoryName(typeof(IntegrationFixture).GetTypeInfo().Assembly.Location);
            var fileName = Guid.NewGuid().ToString();

            _newFilePath = UTHelpers.Up(fileName);

            var mainConfiguration = new Dictionary <string, string>
            {
                { "currentPath", path },
                { "file", _newFilePath },
                { "DataStore:IdField", "id" },
                { "Caching:ETag:Enabled", "true" }
            };

            if (!string.IsNullOrEmpty(authenticationType))
            {
                mainConfiguration.Add("Authentication:Enabled", "true");
                mainConfiguration.Add("Authentication:AuthenticationType", authenticationType);

                if (authenticationType == "apikey")
                {
                    mainConfiguration.Add("Authentication:ApiKey", "correct-api-key");
                }
                else
                {
                    mainConfiguration.Add("Authentication:Users:0:Username", "admin");
                    mainConfiguration.Add("Authentication:Users:0:Password", "root");
                }
            }

            _factory = new WebApplicationFactory <Startup>()
                       .WithWebHostBuilder(builder =>
            {
                builder.UseEnvironment("IntegrationTest")
                .ConfigureAppConfiguration((ctx, b) =>
                {
                    b.SetBasePath(path)
                    .Add(new MemoryConfigurationSource
                    {
                        InitialData = mainConfiguration
                    });
                });
            });

            Client = _factory.CreateClient();
        }
Example #24
0
        public void GetItems_FriendsWithQueryString()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings());

            var controller = new DynamicController(ds, apiSettings);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();
            controller.ControllerContext.HttpContext.Request.QueryString = new QueryString("?children.friends.name=Castillo");

            var result = controller.GetItems("families") as OkObjectResult;

            Assert.Equal(2, ((IEnumerable <dynamic>)result.Value).Count());

            UTHelpers.Down(filePath);
        }
Example #25
0
        public void GetItems_FavouriteMoveiWithQueryString()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings());

            var controller = new DynamicController(ds, apiSettings);

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext();
            controller.ControllerContext.HttpContext.Request.QueryString = new QueryString("?parents.favouriteMovie=Predator");

            // NOTE: Can't but skip and take to querystring with tests
            var result = controller.GetItems("families", 0, 100) as OkObjectResult;

            Assert.Equal(11, ((IEnumerable <dynamic>)result.Value).Count());

            UTHelpers.Down(filePath);
        }
Example #26
0
        public void Mutation_Update_User()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            dynamic original = ds.GetCollection("users").AsQueryable().First(e => e.id == 2);

            var muation = @"
                    mutation {
                          updateUsers ( input: {
                            id: 2
                            patch: {
                              name: Jimmy
                              age: 87
                            }
                          }) {
                            users {
                              id
                            }
                          }
                    }";

            var results = GraphQL.GraphQL.HandleQuery(muation, ds, _idFieldName);

            var id = ((dynamic)results.Data["users"]).id;

            Assert.Equal(2, id);

            var item = ds.GetCollection("users").AsQueryable().First(e => e.id == id);

            Assert.Equal("Phil", original.name);
            Assert.Equal(25, original.age);
            Assert.Equal("London", original.location);

            Assert.Equal("Jimmy", item.name);
            Assert.Equal(87, item.age);
            Assert.Equal("London", item.location);

            UTHelpers.Down(filePath);
        }
        public void GetItems_Single()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings {
                UpsertOnPut = true
            });

            var controller = new DynamicController(ds, apiSettings);

            var itemResult = controller.GetItems("configuration");

            Assert.IsType <OkObjectResult>(itemResult);

            var     okObjectResult = itemResult as OkObjectResult;
            dynamic item           = okObjectResult.Value as ExpandoObject;

            Assert.Equal("abba", item.password);

            UTHelpers.Down(filePath);
        }
Example #28
0
        public void Mutation_Add_User_With_Child()
        {
            var filePath = UTHelpers.Up();

            var ds = new DataStore(filePath);

            var muation = @"
                    mutation {
                          addUsers ( input: {
                            users: {
                              name: Newton
                              age: 45
                              work: {
                                name: ACME
                              }
                            }
                          }) {
                            users {
                              id
                              name
                              age
                              work {
                                name
                              }
                            }
                          }
                    }";

            var results = GraphQL.GraphQL.HandleQuery(muation, ds, _idFieldName);

            var id   = ((dynamic)results.Data["users"]).id;
            var item = ds.GetCollection("users").AsQueryable().First(e => e.id == id);

            Assert.Equal("Newton", item.name);
            Assert.Equal(45, item.age);
            Assert.Equal("ACME", item.work.name);

            UTHelpers.Down(filePath);
        }
Example #29
0
        public async Task PutItem_Upsert_Id_String()
        {
            var filePath    = UTHelpers.Up();
            var ds          = new DataStore(filePath);
            var apiSettings = Options.Create(new ApiSettings {
                UpsertOnPut = true
            });

            var controller = new DynamicController(ds, apiSettings);

            var result = await controller.ReplaceItem("my_test_string", "acdc", JToken.Parse("{ 'id': 2, 'text': 'Hello' }")) as NoContentResult;

            Assert.Equal(204, result.StatusCode);

            var itemResult = controller.GetItem("my_test_string", "acdc") as OkObjectResult;

            dynamic item = itemResult.Value as ExpandoObject;

            Assert.Equal("acdc", item.id);
            Assert.Equal("Hello", item.text);

            UTHelpers.Down(filePath);
        }
Example #30
0
        public void StartServer(string authenticationType = "")
        {
            if (_serverTask != null)
            {
                return;
            }

            var dir = Path.GetDirectoryName(typeof(IntegrationFixture).GetTypeInfo().Assembly.Location);

            var fileName = Guid.NewGuid().ToString();

            _newFilePath = UTHelpers.Up(fileName);

            Port    = 5001;
            BaseUrl = $"http://localhost:{Port}";

            _serverTask = Task.Run(() =>
            {
                TestServer.Run(BaseUrl, dir, $"{fileName}.json", authenticationType);
            });

            var success = Task.Run(() => WaitForServer()).GetAwaiter().GetResult();
        }