def test_update_bars(self, mock_api): """Latest bars for list of symbols is fetched and saved.""" mock_api().get_bars.return_value = self.bars tesla = AssetFactory(symbol="TSLA") microsoft = AssetFactory(symbol="AAPL") symbols = [tesla.symbol, microsoft.symbol] bars = update_bars(symbols=symbols, timeframe="1D", limit=2) # Bars are fetched self.assertEqual(len(bars), 2) self.assertTrue(isinstance(bars["TSLA"][0], AlpacaBar)) self.assertTrue(isinstance(bars["AAPL"][0], AlpacaBar)) # Bars are saved to db correctly self.assertEqual(Bar.objects.filter(asset=tesla).count(), 2) self.assertEqual(Bar.objects.filter(asset=microsoft).count(), 2)
def setUp(self): self.user = UserFactory() self.order_1 = OrderFactory() self.order_2 = OrderFactory() self.order_3 = OrderFactory() self.order_4 = OrderFactory() self.asset = AssetFactory(symbol="TEST") self.strategy = StrategyFactory(asset=self.asset)
def test_create_invalid_strategy_order(self): """An order asset_id and strategy asset must be the same.""" invalid_asset = AssetFactory() with self.assertRaisesRegex( ValidationError, "Strategy ``asset`` and order ``asset_id`` must be the same", ): OrderFactory(strategy=self.strategy, asset_id=invalid_asset)
def setUp(self): self.admin = AdminFactory() self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin.auth_token.key) self.exchange = ExchangeFactory() self.asset_class = AssetClassFactory() self.asset = AssetFactory(name="Asset", exchange=self.exchange, asset_class=self.asset_class) self.data = { "id": str(uuid.uuid4()), "name": "Asset Name", "asset_class": self.asset_class.name, "easy_to_borrow": True, "exchange": self.exchange.name, "marginable": True, "shortable": True, "status": Asset.ACTIVE, "symbol": "SYMBOL", "tradable": True, }
def setUp(self): self.admin = AdminFactory() self.user = UserFactory() self.asset = AssetFactory(symbol="AAPL") self.strategy = StrategyFactory(asset=self.asset) self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin.auth_token.key) self.order_1 = OrderFactory( user=self.user, strategy=self.strategy, asset_id=self.asset ) self.order_2 = OrderFactory( user=self.user, strategy=self.strategy, asset_id=self.asset ) self.order_3 = OrderFactory() self.order_4 = OrderFactory()
def setUp(self): self.tsla = AssetFactory(symbol="TSLA") self.user_1 = UserFactory() self.user_2 = UserFactory() self.user_3 = UserFactory() self.strategy_1 = StrategyFactory(asset=self.tsla, user=self.user_1, trade_value=1000) self.strategy_2 = StrategyFactory(asset=self.tsla, user=self.user_2, trade_value=1000) self.inactive_strategy = StrategyFactory( user=self.user_3, start_date=timezone.now() - timedelta(days=7), end_date=timezone.now() - timedelta(days=5), )
def setUp(self): self.admin = AdminFactory() self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin.auth_token.key) self.asset = AssetFactory(symbol="AAPL") self.bar_1 = BarFactory(asset=self.asset, t=100000) self.bar_2 = BarFactory(asset=self.asset, t=150000) self.bar_3 = BarFactory(asset=self.asset, t=200000) self.data = { "asset": self.asset.symbol, "t": 2100000000, "o": 100.01, "h": 110.05, "l": 99.05, "c": 105.01, "v": 200000, }
class VideoFactory(factory.django.DjangoModelFactory): """Default Video factory.""" class Meta(object): """VideoFactory Meta class.""" model = Video class Params(object): """VideoFactory Params class. user is the shared user owning the asset and the session. """ user = factory.SubFactory(UserFactory) session = factory.LazyAttribute(lambda o: SessionFactory(owner=o.user)) asset = factory.LazyAttribute(lambda o: AssetFactory(owner=o.user)) start_date = make_aware(datetime.datetime(2018, 3, 7, 11, 37)) index = 0
def setUp(self): self.user = UserFactory() self.strategy = StrategyFactory() self.asset_1 = AssetFactory(symbol="AAPL") self.asset_2 = AssetFactory(symbol="TLSA") self.asset_3 = AssetFactory(symbol="MFST") self.uuid_1 = str(uuid.uuid4()) self.uuid_2 = str(uuid.uuid4()) self.uuid_3 = str(uuid.uuid4()) self.uuid_4 = str(uuid.uuid4()) self.uuid_5 = str(uuid.uuid4()) self.uuid_6 = str(uuid.uuid4()) self.order_data = [ AlpacaOrder( { "status": "filled", "asset_id": self.asset_1.pk, "symbol": self.asset_1.symbol, "asset_class": self.asset_1.asset_class.name, "qty": 10.01, "filled_qty": 10.01, "side": "buy", "type": "market", "time_in_force": "day", "client_order_id": self.uuid_1, "id": self.uuid_2, "created_at": str(timezone.now()), } ), AlpacaOrder( { "status": "filled", "asset_id": self.asset_2.pk, "symbol": self.asset_2.symbol, "asset_class": self.asset_1.asset_class.name, "qty": 101.05, "filled_qty": 10.01, "side": "buy", "type": "market", "time_in_force": "day", "client_order_id": self.uuid_3, "id": self.uuid_4, "created_at": str(timezone.now()), } ), AlpacaOrder( { "status": "filled", "asset_id": self.asset_3.pk, "symbol": self.asset_3.symbol, "asset_class": self.asset_1.asset_class.name, "qty": 105.08, "filled_qty": 10.01, "side": "buy", "type": "market", "time_in_force": "day", "client_order_id": self.uuid_5, "id": self.uuid_6, "created_at": str(timezone.now()), } ), ]
def setUp(self): self.user = UserFactory() self.asset = AssetFactory(symbol="AAPL") self.strategy = StrategyFactory(asset=self.asset) self.order = OrderFactory(strategy=self.strategy, asset_id=self.asset)
class AssetViewTests(APITestCase): def setUp(self): self.admin = AdminFactory() self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin.auth_token.key) self.exchange = ExchangeFactory() self.asset_class = AssetClassFactory() self.asset = AssetFactory(name="Asset", exchange=self.exchange, asset_class=self.asset_class) self.data = { "id": str(uuid.uuid4()), "name": "Asset Name", "asset_class": self.asset_class.name, "easy_to_borrow": True, "exchange": self.exchange.name, "marginable": True, "shortable": True, "status": Asset.ACTIVE, "symbol": "SYMBOL", "tradable": True, } def test_list_assets_admin(self): """Assets are listed for admins.""" response = self.client.get(reverse("v1:assets-list")) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 1) def test_list_assets_user(self): """Assets are listed for users.""" user = UserFactory() self.client.credentials(HTTP_AUTHORIZATION="Token " + user.auth_token.key) response = self.client.get(reverse("v1:assets-list")) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 1) def test_create_asset(self): """Assets can be created by admins.""" response = self.client.post(reverse("v1:assets-list"), self.data) asset = Asset.objects.filter(id=self.data["id"]) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue(asset.exists()) asset = asset.first() self.assertEqual(asset.name, self.data["name"]) self.assertEqual(asset.asset_class.name, self.data["asset_class"]) self.assertEqual(asset.easy_to_borrow, self.data["easy_to_borrow"]) self.assertEqual(asset.exchange.name, self.data["exchange"]) self.assertEqual(asset.marginable, self.data["marginable"]) self.assertEqual(asset.shortable, self.data["shortable"]) self.assertEqual(asset.status, self.data["status"]) self.assertEqual(asset.symbol, self.data["symbol"]) self.assertEqual(asset.tradable, self.data["tradable"]) def test_create_asset_invalid(self): """Assets can not be created by users.""" user = UserFactory() self.client.credentials(HTTP_AUTHORIZATION="Token " + user.auth_token.key) response = self.client.post(reverse("v1:assets-list"), self.data) asset = Asset.objects.filter(id=self.data["id"]) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertFalse(asset.exists()) def test_asset_partial_update(self): """Assets can be partially updated by admins.""" data = {"name": "New Name"} response = self.client.patch( reverse("v1:assets-detail", args=[self.asset.pk]), data) self.asset.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.asset.name, data["name"]) def test_asset_partial_update_asset_class(self): """Assets can asset class updated by admins.""" new_asset_class = AssetClassFactory() data = {"asset_class": new_asset_class.name} response = self.client.patch( reverse("v1:assets-detail", args=[self.asset.pk]), data) self.asset.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.asset.asset_class.name, data["asset_class"]) def test_asset_partial_update_exchange(self): """Assets can have exchange updated by admins.""" new_exchange = ExchangeFactory() data = {"exchange": new_exchange.name} response = self.client.patch( reverse("v1:assets-detail", args=[self.asset.pk]), data) self.asset.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(self.asset.exchange.name, data["exchange"]) def test_asset_partial_update_invalid(self): """Assets can not be partially updated by users.""" user = UserFactory() self.client.credentials(HTTP_AUTHORIZATION="Token " + user.auth_token.key) data = {"name": "New Name"} response = self.client.patch( reverse("v1:assets-detail", args=[self.asset.pk]), data) self.asset.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(self.asset.name, "Asset") def test_delete_asset(self): """Assets can be deleted by admins.""" response = self.client.delete( reverse("v1:assets-detail", args=[self.asset.pk])) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(Asset.objects.filter(pk=self.asset.pk).exists()) def test_delete_asset_invalid(self): """Assets can not be deleted by users.""" user = UserFactory() self.client.credentials(HTTP_AUTHORIZATION="Token " + user.auth_token.key) response = self.client.delete( reverse("v1:assets-detail", args=[self.asset.pk])) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertTrue(Asset.objects.filter(pk=self.asset.pk).exists())
def setUp(self): self.user = UserFactory() self.asset = AssetFactory()