Skip to content

Commit

Permalink
Add most factories and a POC test for model creation
Browse files Browse the repository at this point in the history
Refs #330
  • Loading branch information
lukasjuhrich committed Jan 20, 2022
1 parent aa592f6 commit 7601bd2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/factories/nat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from factory import Sequence, SubFactory, Faker

from pycroft.model.nat import NATDomain, OutsideIPAddress
from pycroft.model.nat import InsideNetwork, Translation, Forwarding, \
DHCPHostReservation
from .base import BaseFactory


class NatDomainFactory(BaseFactory):
class Meta:
model = NATDomain

name = Sequence(lambda n: f"NAT Domain {n + 1}")


class OutsideIpAddressFactory(BaseFactory):
class Meta:
model = OutsideIPAddress

nat_domain = SubFactory(NatDomainFactory)
ip_address = Faker('ipv4', network=False)
owner = None


class InsideNetworkFactory(BaseFactory):
# TODO this should be filled with all available inside networks
class Meta:
model = InsideNetwork

nat_domain = SubFactory(NatDomainFactory)
ip_network = Faker('ipv4', network=True)
gateway = Faker('ipv4', network=False)


class TranslationFactory(BaseFactory):
class Meta:
model = Translation

pass


class ForwardingFactory(BaseFactory):
class Meta:
model = Forwarding

pass


class DhcpHostReservationFactory(BaseFactory):
class Meta:
model = DHCPHostReservation

pass


37 changes: 37 additions & 0 deletions tests/model/test_nat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import List

from pycroft.model.nat import NATDomain
from tests import FactoryDataTestBase
from tests.factories import ConfigFactory, UserFactory
from tests.factories.nat import NatDomainFactory, OutsideIpAddressFactory


class NatTestBase(FactoryDataTestBase):
def create_factories(self):
ConfigFactory.create()
self.nat_domain = NatDomainFactory()


class TrivialNatModelTestCase(NatTestBase):
def create_factories(self):
super().create_factories()

def test_one_natdomain(self):
doms: List[NATDomain] = NATDomain.q.all()
self.assertEqual(len(doms), 1)
dom = doms[0]
self.assertEqual(dom.name, self.nat_domain.name)


class SimpleNatModelTestCase(NatTestBase):
def create_factories(self):
super().create_factories()
self.user = UserFactory.create()
self.outside_ip = OutsideIpAddressFactory(owner=self.user,
nat_domain=self.nat_domain)
# TODO later check that this doesn't work because every outside_ip needs a translation
# TODO add a pool of inside networks

def test_forwardings_correct(self):
domain: NATDomain = NATDomain.q.one()
self.assertEqual(domain.name, self.nat_domain.name)

0 comments on commit 7601bd2

Please sign in to comment.