public void TestAddUser() { using (var db = new KptRepository()) { Assert.Equal(0, db.GetUsers().Count); db.AddUser(new User { Name = "Test01" }); Assert.Equal(1, db.GetUsers().Count); db.AddUser(new User { Name = "Test02", Age = 2 }); Assert.Equal(2, db.GetUsers().Count); } }
private static string PostApi(KptRepository db, HttpListenerRequest req, Command cmd) { if (!req.HasEntityBody) { throw new ArgumentException("指定したAPIに必要なデータがありません。POST " + cmd.ToString()); } var body = new StreamReader(req.InputStream).ReadToEnd(); var keyValues = HttpUtility.ParseQueryString(body); if (cmd.IsUsers && cmd.UserId == 0 && !cmd.IsKptBoards && cmd.KptBoardId == 0) { // POST /v1/users/ Console.WriteLine("人追加"); int?age = null; if (keyValues.Get("age") != null) { age = int.Parse(keyValues.Get("age")); } var user = new User { Name = keyValues.Get("name"), Age = age }; db.AddUser(user); return(""); } if (cmd.IsUsers && cmd.UserId != 0 && cmd.IsKptBoards && cmd.KptBoardId == 0) { // POST /v1/users/n/kptboards/ Console.WriteLine("ボード追加"); var kptBoard = new KptBoard { Time = DateTime.Now, Keep = keyValues.Get("keep"), Problem = keyValues.Get("problem"), Try = keyValues.Get("try"), UserId = cmd.UserId }; db.AddKptBoard(kptBoard); return(""); } throw new ArgumentException("指定したAPIがありません。POST " + cmd.ToString()); }
public void TestAddUserWithKptBoard() { using (var db = new KptRepository()) { var user = new User { Name = "Test01", KptBoards = { new KptBoard { Time = DateTime.Now, Keep = "K011", Problem = "P011", Try = "T011" } } }; Assert.Equal(0, user.UserId); Assert.Equal(0, db.GetUsers().Count); db.AddUser(user); Assert.NotEqual(0, user.UserId); // AddUserした時点でUserIdが設定される Assert.Equal(1, db.GetUsers().Count); Assert.Equal(1, db.GetKptBoardsByUserId(user.UserId).Count); } }