def test_to_dict(self): u = User({ 'first_name': 'Waylon', 'last_name': 'Jennings', 'nickname': 'Hoss', }) # have to save to get the created_dt not be None. db.session.add(u) db.session.commit() d = u.to_dict() self.assertEqual(d['last'], u.last_name) self.assertEqual(d['first'], u.first_name) self.assertEqual(d['nickname'], u.nickname)
def adduser(request): data = None name = None if(request.POST.get('name')): data = request.POST name = data.get('name') else: data = request.raw_post_data data = json.loads(data) name = data['name'] user = User(name=name, photo=request.FILES['photo']) user.save() return HttpResponse(user.id)
def create_user(): u = User.from_request_json(request.get_json()) db.session.add(u) db.session.commit() ret = {"error": False, "timestamp": int(time.time()), "userid": u.id} res = Response(json.dumps(ret), 200) res.headers['Content-Type'] = "application/json" return res
def _create_users(self): u1 = User({ "nickname": "skyl" }) u2 = User({ "nickname": "tony" }) db.session.add(u1) db.session.add(u2) db.session.commit() # sqlalchemy.orm.exc.DetachedInstanceError # why do we need to manually rehydrate these user objects? self.u1 = User({ "nickname": "skyl" }) self.u2 = User({ "nickname": "tony" })
def test_battle_callable_fields(self): u = User({"nickname": "foobar"}) self.db.session.add(u) self.db.session.commit() b = Battle({ "winner_id": lambda: u.id, "attacker_id": lambda: u.id, }) self.db.session.add(b) self.db.session.commit() self.assertEqual(b.id, 1)
def test_search_users(self): u = User({ "first_name": "foo", "last_name": "bar", "nickname": "baz", }) db.session.add(u) res = self.client.get("/users/search?nickname=%s" % "baz", headers=self.headers) self.assertEqual(res.status_code, 302) self.assertIn("/users/1", res.headers['location'])
def test_callable_attr(self): nickf = lambda: "foobar" u = User({ 'first_name': 'Dale', 'last_name': 'Earnhardt', 'nickname': nickf, }) db.session.add(u) db.session.commit() self.assertEqual( User.query.filter_by(nickname='foobar').first().first_name, 'Dale') self.assertEqual(repr(u), '<User %s>' % nickf())
def create_user(): u = User.from_request_json(request.get_json()) db.session.add(u) db.session.commit() ret = { "error": False, "timestamp": int(time.time()), "userid": u.id } res = Response(json.dumps(ret), 200) res.headers['Content-Type'] = "application/json" return res
def adduser(request): data = None fbid = None if(request.POST.get('fbid')): data = request.POST fbid = data.get('fbid') else: data = request.raw_post_data data = json.loads(data) fbid = data['fbid'] response = "WTF -- added more than once?" user = User.objects.filter(fb_id=fbid) if(user.count() == 1): user[0].active = True user[0].save() response = user[0].id elif(user.count() == 0): user = User(fb_id=fbid) user.save() response = user.id return HttpResponse(fbid)
def test_detail_user(self): u = User({ "first_name": "Merle", "last_name": "Haggard", "nickname": "Hag", }) db.session.add(u) res = self.client.get( "/users/1", headers=self.headers, ) self.assertEqual(res.status_code, 200) ret = json.loads(res.data.decode('utf-8')) self.assertEqual(ret["first"], "Merle") self.assertEqual(ret["last"], "Haggard") self.assertEqual(ret["nickname"], "Hag")
def test_create_user_update_last_seen(self): u = User({ 'first_name': 'Skylar', 'last_name': 'Saveland', 'nickname': 'skyl', }) db.session.add(u) db.session.commit() u = User.query.filter_by(nickname='skyl').first() self.assertEqual(u.first_name, "Skylar") # they are actually not the same # self.assertEqual(u.last_seen_dt, u.created_dt) u.wins += 1 db.session.add(u) db.session.commit() u = User.query.filter_by(nickname='skyl').first() self.assertGreater(u.last_seen_dt, u.created_dt) self.assertEqual(u.wins, 1)
def test_modify_user(self): u = User() db.session.add(u) db.session.commit() put = { "field": "last_name", "value": "Henderson", } res = self.client.put( "/users/1", data=json.dumps(put), content_type='application/json', headers=self.headers, ) self.assertEqual(res.status_code, 200) u = User.query.get(1) self.assertEqual(u.last_name, "Henderson")
def test_create_user_basic(self): u = User() db.session.add(u) db.session.commit()