コード例 #1
0
        public async Task TestFileIoUpload()
        {
            DirectoryEntry dir          = EnvironmentV2.instance.GetNewInMemorySystem();
            FileEntry      fileToUpload = dir.GetChild("test.txt");
            string         textInFile   = "I am a text";

            fileToUpload.SaveAsText(textInFile);
            Assert.Equal(textInFile, fileToUpload.LoadAs <string>());

            RestRequest uploadRequest = new Uri("https://file.io/?expires=1d").SendPOST().AddFileViaForm(fileToUpload);

            uploadRequest.WithRequestHeaderUserAgent("" + GuidV2.NewGuid());
            FileIoResponse result = await uploadRequest.GetResult <FileIoResponse>();

            Log.d(JsonWriter.AsPrettyString(result));
            Assert.True(result.success);
            Assert.NotEmpty(result.link);

            FileEntry fileToDownloadTo = dir.GetChild("test2.txt");
            var       downloadRequest  = new Uri(result.link).SendGET();

            downloadRequest.WithRequestHeaderUserAgent("" + GuidV2.NewGuid());
            await downloadRequest.DownloadTo(fileToDownloadTo);

            //Assert.True(textInFile == fileToDownloadTo.LoadAs<string>(), "Invalid textInFile from " + result.link);
            Assert.Equal(textInFile, fileToDownloadTo.LoadAs <string>());
        }
コード例 #2
0
ファイル: SystemTests.cs プロジェクト: cs-util-com/cscore
        public void TestGuidV2()
        {
            for (int i = 0; i < 1000; i++)
            {
                var a = GuidV2.NewGuid();
                var b = GuidV2.NewGuid();
                //Assert.True(b.CompareTo(a) > 0, $"Run {i}: a={a} was > then b={b}");
                Assert.True(b.CompareToV2(a) > 0, $"Run {i}: a={a} was > then b={b}");
                Assert.True(a.CompareToV2(b) < 0, $"Run {i}: a={a} was > then b={b}");
                Assert.True(a.CompareToV2(a) == 0, $"Run {i}: a != a : {b}");
            }
            var count = 1000000;
            var t1    = StopwatchV2.StartNewV2();

            {
                var isOrderedAfterCounter = RunIdTest(count, () => Guid.NewGuid());
                // Guid.NewGuid are not ordered:
                Assert.NotEqual(0, isOrderedAfterCounter);
                Assert.NotEqual(count, isOrderedAfterCounter);
            }
            t1.StopV2();
            var t2 = StopwatchV2.StartNewV2();

            {
                var isOrderedAfterCounter = RunIdTest(count, () => GuidV2.NewGuid());
                // GuidV2.NewGuid must be ordered:
                Assert.Equal(count, isOrderedAfterCounter);
            }
            t2.StopV2();
            // Check that the GuidV2.NewGuid is not much slower then Guid.NewGuid
            Assert.True(t1.ElapsedMilliseconds * 2 > t2.ElapsedMilliseconds);
        }
コード例 #3
0
ファイル: NewsManager.cs プロジェクト: quanliang2000/cscore
 public static News NewLocalNewsEvent(string title, string descr, string url, string urlText, NewsType type = News.NewsType.New, string id = null)
 {
     if (id == null)
     {
         id = GuidV2.NewGuid().ToString();
     }
     return(new News()
     {
         key = id,
         title = title,
         description = descr,
         detailsUrl = url,
         detailsUrlText = urlText,
         date = "" + DateTimeV2.UtcNow.ToReadableString_ISO8601(),
         type = EnumUtil.GetEntryName(type)
     });
 }
コード例 #4
0
ファイル: LiteDbTests.cs プロジェクト: quanliang2000/cscore
        void ExampleUsage1()
        {
            string testId = GuidV2.NewGuid().ToString();

            var dbFile = EnvironmentV2.instance.GetOrAddTempFolder("tests.io.db").GetChild("TestDB_" + testId);

            // Open database (or create if doesn't exist)
            using (var db = new UltraLiteDatabase(dbFile.OpenOrCreateForReadWrite(), disposeStream: true)) {
                var users = db.GetCollection <User>("users");

                var user1 = new User {
                    id   = testId,
                    name = "John Doe",
                    age  = 39
                };

                // Create unique index in Name field
                // https://github.com/mbdavid/LiteDB/wiki/Indexes#ensureindex
                users.EnsureIndex("name", true);

                users.Insert(user1);

                user1.name = "Joana Doe";
                users.Update(user1);
                Assert.Equal("Joana Doe", users.FindById(testId).name);

                user1.name = "Joana Doe 2";
                users.Upsert(user1); // insert or update if already found
                Assert.Equal("Joana Doe 2", users.FindById(testId).name);

                // Use LINQ to query documents (with no index)
                var queryResults = users.Find(Query.GT("age", 20));
                Assert.Single(queryResults);
                Assert.Equal("Joana Doe 2", queryResults.First().name);
            }
            Assert.True(dbFile.IsNotNullAndExists());
            dbFile.DeleteV2(); // cleanup after the test
        }
コード例 #5
0
        public async Task PerformanceTest1()
        {
            var dataTree = NewTreeLayer("1", 1000, () => NewTreeLayer("2", 2, () => NewTreeLayer("3", 4, () => NewTreeLayer("4", 1))));

            var testFolder = EnvironmentV2.instance.GetOrAddTempFolder("tests.io.db").CreateV2();

            Log.d("Path=" + testFolder);
            var dbFile = testFolder.GetChild("PerformanceTestDB_" + GuidV2.NewGuid().ToString());

            // Open database (or create if doesn't exist)
            using (var db = new UltraLiteDatabase(dbFile.OpenOrCreateForReadWrite(), disposeStream: true)) {
                await InsertIntoDb(dataTree, db);
                await ReadFromDb(dataTree, db);
            }

            await WriteFiles(dataTree, testFolder);
            await ReadFiles(dataTree, testFolder);

            // cleanup after the test
            await ParallelExec(dataTree, (elem) => { GetFileForElem(testFolder, elem).DeleteV2(); });

            Assert.True(dbFile.DeleteV2());
            Assert.False(dbFile.Exists);
        }
コード例 #6
0
 private TreeElem NewTreeElem(string nodeName, Func <List <TreeElem> > CreateChildren = null)
 {
     return(new TreeElem {
         id = GuidV2.NewGuid().ToString(), name = nodeName, children = CreateChildren?.Invoke()
     });
 }
コード例 #7
0
 public MyUserModel(string id = null)
 {
     this.id = id == null ? "" + GuidV2.NewGuid() : id;
 }
コード例 #8
0
        public void ExampleUsage1()
        {
            // A normal user model with a few example fields that users typically have:
            var user1 = new MyUserModel("" + GuidV2.NewGuid())
            {
                name       = "Tom",
                password   = "******",
                age        = 50,
                profilePic = new FileRef()
                {
                    url = "https://picsum.photos/128/128"
                },
                tags = new List <string>()
                {
                    "tag1"
                }
            };

            var        schemaGenerator = new ModelToJsonSchema();
            JsonSchema schema          = schemaGenerator.ToJsonSchema("MyUserModel", user1);

            Log.d("schema: " + JsonWriter.AsPrettyString(schema));

            var profilePicVm = schema.properties["profilePic"];

            Assert.Equal("string", profilePicVm.properties["dir"].type);
            Assert.Equal("string", profilePicVm.properties["url"].type);

            Assert.Contains("id", schema.required);
            Assert.Contains("name", schema.required);
            Assert.Equal(2, schema.required.Count);

            Assert.Equal("array", schema.properties["tags"].type);
            Assert.Equal("string", schema.properties["tags"].items.First().type);
            Assert.Null(schema.properties["tags"].items.First().properties);

            Assert.Equal("array", schema.properties["contacts"].type);
            Assert.True(schema.properties["id"].readOnly.Value);       // id has private setter
            Assert.True(schema.properties["contacts"].readOnly.Value); // contacts has only a getter

            Assert.Equal(2, schema.properties["name"].minLength);
            Assert.Equal(30, schema.properties["name"].maxLength);

            Assert.Equal("object", schema.properties["contacts"].items.First().type);
            // Contacts schema already resolve as part of the bestFried field, so here no properties are included:
            Assert.Null(schema.properties["contacts"].items.First().properties);

            var entrySchema = schema.properties["contacts"].items.First();

            Assert.Equal("" + typeof(MyUserModel.UserContact), entrySchema.modelType);
            Assert.Null(entrySchema.properties);

            Assert.Equal("" + typeof(MyUserModel.UserContact), schema.properties["bestFriend"].modelType);

            Assert.Equal("" + ContentFormat.password, schema.properties["password"].format);

            var userSchemaInUserContactClass = schema.properties["bestFriend"].properties["user"];

            Assert.Equal("" + typeof(MyUserModel), userSchemaInUserContactClass.modelType);
            // The other fields of this json schema are null since it was already defined:
            Assert.Null(userSchemaInUserContactClass.properties);

            Assert.Equal(0, schema.properties["age"].minimum);
            Assert.Equal(130, schema.properties["age"].maximum);
        }
コード例 #9
0
 private static string NewId()
 {
     return(GuidV2.NewGuid().ToString());
 }
コード例 #10
0
        public void ExampleUsage1()
        {
            var t = Log.MethodEntered("DataStoreExample1.ExampleUsage1");

            // Some initial state of the model (eg loaded from file when the app is started) is restored and put into the store:
            MyUser1 carl = new MyUser1(GuidV2.NewGuid(), "Carl", 99, null, MyUser1.MyEnum.State1);
            var     data = new MyAppState1(ImmutableDictionary <Guid, MyUser1> .Empty.Add(carl.id, carl), null);

            var store = new DataStore <MyAppState1>(MyReducers1.ReduceMyAppState1, data);

            var keepListenerAlive   = true;
            var usersChangedCounter = 0;

            store.AddStateChangeListener(state => state.users, (changedUsers) => {
                usersChangedCounter++;
                return(keepListenerAlive);
            }, triggerInstantToInit: false);

            ActionAddSomeId a1 = new ActionAddSomeId()
            {
                someId = GuidV2.NewGuid()
            };

            store.Dispatch(a1);
            Assert.Equal(0, usersChangedCounter); // no change happened in the users
            Assert.Equal(a1.someId, store.GetState().someUuids.Value.Single());

            MyUser1 carlsFriend = new MyUser1(GuidV2.NewGuid(), "Carls Friend", 50, null, MyUser1.MyEnum.State1);

            store.Dispatch(new ActionOnUser.AddContact()
            {
                targetUser = carl.id, newContact = carlsFriend
            });
            Assert.Equal(1, usersChangedCounter);
            Assert.Equal(carlsFriend, store.GetState().users[carlsFriend.id]);
            Assert.Contains(carlsFriend.id, store.GetState().users[carl.id].contacts);

            store.Dispatch(new ActionOnUser.ChangeName()
            {
                targetUser = carl.id, newName = "Karl"
            });
            Assert.Equal(2, usersChangedCounter);
            Assert.Equal("Karl", store.GetState().users[carl.id].name);


            store.Dispatch(new ActionOnUser.ChangeAge()
            {
                targetUser = carlsFriend.id, newAge = null
            });
            Assert.Equal(3, usersChangedCounter);
            Assert.Null(store.GetState().users[carlsFriend.id].age);

            Assert.NotEqual(MyUser1.MyEnum.State2, store.GetState().users[carl.id].myEnum);
            store.Dispatch(new ActionOnUser.ChangeEnumState()
            {
                targetUser = carl.id, newEnumValue = MyUser1.MyEnum.State2
            });
            Assert.Equal(MyUser1.MyEnum.State2, store.GetState().users[carl.id].myEnum);

            // Remove the listener from the store the next time an action is dispatched:
            keepListenerAlive = false;
            Assert.Equal(4, usersChangedCounter);
            store.Dispatch(new ActionOnUser.ChangeAge()
            {
                targetUser = carlsFriend.id, newAge = 22
            });
            Assert.Equal(5, usersChangedCounter);
            // Test that now the listener is removed and will not receive any updates anymore:
            store.Dispatch(new ActionOnUser.ChangeAge()
            {
                targetUser = carlsFriend.id, newAge = 33
            });
            Assert.Equal(5, usersChangedCounter);

            Log.MethodDone(t);
        }