public void GetItem_DynamicAndTyped_DateTypeSecure()
        {
            var newFilePath = UTHelpers.Up(encryptionKey: EncryptionPassword);

            var store = new DataStore(path: newFilePath, encryptionKey: EncryptionPassword);

            var test = DateTime.Now.ToShortDateString();

            var itemDynamic = store.GetItem("myDate_string");
            var itemTyped   = store.GetItem <DateTime>("myDate_string");

            Assert.Equal(2009, itemTyped.Year);

            var itemDynamic2 = store.GetItem("myDate_date");
            var itemTyped2   = store.GetItem <DateTime>("myDate_date");

            Assert.Equal(2015, itemDynamic2.Year);
            Assert.Equal(2015, itemTyped2.Year);

            UTHelpers.Down(newFilePath);
        }
        public void ReplaceMany_TypedUser()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var collection = store.GetCollection <User>("user");

            Assert.Equal(3, collection.Count);

            var newUser1 = new User {
                Id = 11, Name = "Teddy"
            };
            var newUser2 = new User {
                Id = 11, Name = "Teddy2"
            };

            collection.InsertOne(newUser1);
            collection.InsertOne(newUser2);
            Assert.Equal(5, collection.Count);

            var store2 = new DataStore(newFilePath);

            var collection2 = store2.GetCollection <User>("user");

            Assert.Equal(5, collection2.Count);

            collection2.ReplaceMany(e => e.Name.Contains("Teddy"), new User {
                Id = 11, Name = "Theodor"
            });

            var store3      = new DataStore(newFilePath);
            var collection3 = store3.GetCollection <User>("user");
            var updated     = collection3.Find(e => e.Id == 11 && e.Name == "Theodor");

            Assert.Equal(2, updated.Count());

            UTHelpers.Down(newFilePath);
        }
        public async Task UpdateOneAsync_TypedUser()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var collection = store.GetCollection <User>("user");

            Assert.Equal(3, collection.Count);

            var newUser = new User {
                Id = 11, Name = "Teddy", Age = 21
            };
            var insertResult = collection.InsertOne(newUser);

            Assert.True(insertResult);
            Assert.Equal(4, collection.Count);

            var store2      = new DataStore(newFilePath);
            var collection2 = store2.GetCollection <User>("user");

            Assert.Equal(4, collection2.Count);

            await collection2.UpdateOneAsync(e => e.Id == newUser.Id, new { Age = 22 });

            var store3      = new DataStore(newFilePath);
            var collection3 = store3.GetCollection <User>("user");
            var updated     = collection3.Find(e => e.Id == newUser.Id).First();

            Assert.Equal(22, updated.Age);
            Assert.Equal("Teddy", updated.Name);

            // Try to update property that doesn't exist
            var notThereResult = await collection2.UpdateOneAsync(e => e.Id == newUser.Id, new { SomeThatIsNotThere = "No" });

            Assert.True(notThereResult);

            UTHelpers.Down(newFilePath);
        }
        public async Task ReplaceItem_TypedUser()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var result = store.InsertItem <User>("myUser2", new User {
                Id = 12, Name = "Teddy"
            });

            Assert.True(result);

            result = await store.ReplaceItemAsync("myUser2", new User { Id = 2, Name = "James" });

            Assert.True(result);

            var user = store.GetItem("myUser2");

            Assert.Equal("James", user.name);

            UTHelpers.Down(newFilePath);
        }
        public async Task InsertItem_DynamicUser()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var result = await store.InsertItemAsync("myUser2", new { id = 12, name = "Teddy" });

            Assert.True(result);

            var user = store.GetItem("myUser2");

            Assert.Equal("Teddy", user.name);

            var store2 = new DataStore(newFilePath);

            var user2 = store2.GetItem("myUser2");

            Assert.Equal("Teddy", user2.name);

            UTHelpers.Down(newFilePath);
        }
Exemplo n.º 6
0
        public async Task Readme_Example()
        {
            var pathToJson = UTHelpers.Up();

            var store = new DataStore(pathToJson);

            var dynamicCollection = store.GetCollection("user");

            var userDynamic = dynamicCollection
                              .AsQueryable()
                              .Single(p => p.name == "Phil");

            await dynamicCollection.InsertOneAsync(new { id = 14, name = "Raymond", age = 32 });

            await dynamicCollection.ReplaceOneAsync(e => e.id == 14, new { id = 14, name = "Barry", age = 32 });

            await dynamicCollection.DeleteOneAsync(e => e.name == "Barry");

            var typedCollection = store.GetCollection <User>();

            var userTyped = typedCollection
                            .AsQueryable()
                            .Single(p => p.Name == "Phil");

            typedCollection.InsertOne(new User {
                Id = 15, Name = "Jim", Age = 52
            });
            typedCollection.ReplaceOne(e => e.Id == 15, new User {
                Id = 15, Name = "Barry", Age = 52
            });
            typedCollection.DeleteOne(e => e.Name == "Barry");
            typedCollection.DeleteMany(e => e.Age < 31);

            Assert.Equal("Phil", userDynamic.name);
            Assert.Equal("Phil", userTyped.Name);

            UTHelpers.Down(pathToJson);
        }
        public void GetNextIdValue_IntegerId()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var collection = store.GetCollection("user");
            var lastItem   = collection
                             .AsQueryable()
                             .OrderBy(e => e.id)
                             .Last();

            Assert.Equal(3, lastItem.id);

            var nextId = collection.GetNextIdValue();

            Assert.Equal(4, nextId);

            collection.InsertOne(new { id = nextId });

            nextId = collection.GetNextIdValue();
            Assert.Equal(5, nextId);
        }
        public async Task InsertOne_100Async()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var collection = store.GetCollection <User>("user");

            Assert.Equal(3, collection.Count);

            var tasks = Enumerable.Range(0, 100).Select(i => collection.InsertOneAsync(new User {
                Id = i, Name = "Teddy"
            }));

            await Task.WhenAll(tasks);

            var store2      = new DataStore(newFilePath);
            var collection2 = store2.GetCollection <User>("user");

            Assert.Equal(103, collection2.Count);

            UTHelpers.Down(newFilePath);
        }
        public async Task ReplaceItem_ValueType()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var result = await store.ReplaceItemAsync("counter", 2);

            Assert.False(result);

            result = await store.ReplaceItemAsync("counter", 2, true);

            Assert.True(result);

            var counter = store.GetItem <int>("counter");

            Assert.Equal(2, counter);

            var updateResult = await store.ReplaceItemAsync <string>("counter", "2");

            Assert.True(result);

            var store2 = new DataStore(newFilePath);

            var c2 = store2.GetItem("counter");

            Assert.Equal("2", c2);

            updateResult = await store2.ReplaceItemAsync("counter", "4");

            Assert.True(result);

            c2 = store2.GetItem("counter");
            Assert.Equal("4", c2);

            UTHelpers.Down(newFilePath);
        }
        public void FindAndAsQueryable_Linq()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var itemDynamic = store.GetCollection("user")
                              .AsQueryable()
                              .Single(p => p.name == "Phil");

            var itemTyped1 = store.GetCollection <User>()
                             .AsQueryable()
                             .Single(p => p.Name == "Phil");

            var itemTyped2 = store.GetCollection <User>()
                             .Find(p => p.Name == "Phil")
                             .First();

            Assert.Equal("Phil", itemDynamic.name);
            Assert.Equal("Phil", itemTyped1.Name);
            Assert.Equal("Phil", itemTyped2.Name);

            UTHelpers.Down(newFilePath);
        }
        public async Task UpdateItem_TypedUser()
        {
            var newFilePath = UTHelpers.Up();

            var store = new DataStore(newFilePath);

            var result = await store.InsertItemAsync("myUser2", new User { Id = 12, Name = "Teddy" });

            Assert.True(result);

            var user = store.GetItem <User>("myUser2");

            Assert.Equal("Teddy", user.Name);

            var updateResult = await store.UpdateItemAsync("myUser2", new { name = "Harold" });

            var store2 = new DataStore(newFilePath);

            var user2 = store2.GetItem <User>("myUser2");

            Assert.Equal("Harold", user2.Name);

            UTHelpers.Down(newFilePath);
        }
Exemplo n.º 12
0
        public async Task Readme_Example2()
        {
            var pathToJson = UTHelpers.Up();

            // Open database (create new if file doesn't exist)
            var store = new DataStore(pathToJson);

            // Get employee collection
            var collection = store.GetCollection("employee");

            // Create new employee instance
            var employee = new
            {
                id   = collection.GetNextIdValue(),
                name = "John",
                age  = 46
            };

            // Example with JSON object
            var employeeJson = JToken.Parse("{ 'id': 2, 'name': 'Raymond', 'age': 32 }");

            // Example with JSON object
            var employeeDict = new Dictionary <string, object>
            {
                ["id"]   = 3,
                ["name"] = "Andy",
                ["age"]  = 32
            };

            var employeeExpando = new ExpandoObject();
            var items           = employeeExpando as IDictionary <string, object>;

            items.Add("id", 3);
            items.Add("name", "Karl");
            items.Add("age", 29);

            // Insert new employee
            await collection.InsertOneAsync(employee);

            await collection.InsertOneAsync(employeeJson);

            await collection.InsertOneAsync(employeeDict);

            await collection.InsertOneAsync(employeeExpando);

            // As anonymous types property is read only we can use new anonymous type to update data
            var updateData = new { name = "John Doe" };
            await collection.UpdateOneAsync(e => e.id == employee.id, updateData);

            var updateJson = JObject.Parse("{ 'name': 'Raymond Doe' }");
            await collection.UpdateOneAsync(e => e.id == 1, updateJson);

            var updateDict = new Dictionary <string, object> {
                ["name"] = "Andy Doe"
            };
            await collection.UpdateOneAsync(e => e.id == 2, updateDict);

            // Use LINQ to query items
            var results = collection.AsQueryable().Where(x => x.age < 40);

            Assert.True(results.Count() == 3);
            Assert.Single(results.Where(e => e.name == "Raymond Doe"));
            Assert.Single(results.Where(e => e.name == "Andy Doe"));
            Assert.Single(results.Where(e => e.name == "Karl"));

            UTHelpers.Down(pathToJson);
        }