Skip to content

Commit

Permalink
Update test_cli
Browse files Browse the repository at this point in the history
Update our CLI tests to use our fixtures to ensure reproducible tests.
We'll use cli- as a prefix for our objects to ensure they don't otherwise
conflict with out fixture values.
  • Loading branch information
kfdm committed Dec 15, 2022
1 parent 26a6c13 commit 8f7516e
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions promgen/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,41 @@


class CLITests(PromgenTest):
fixtures = ["testcases.yaml", "extras.yaml"]

@mock.patch("promgen.signals._trigger_write_config")
def test_register_job(self, mock_signal):
# Assert when project doesn't exist
with self.assertRaises(CommandError):
management.call_command("register-job", "missing-project", "example", 1234)

# Create a Service and Project and then try adding our job
shard = models.Shard.objects.create(name="TestShard")
service = models.Service.objects.create(name="TestService")
_ = models.Project.objects.create(name="TestProject", service=service, shard=shard)
management.call_command("register-job", "TestProject", "example", 1234)
management.call_command("register-job", "test-project", "example", 1234)

# Ensure the jobs we expect exist
self.assertCount(models.Exporter, 1)

# Registering the same job again shouldn't change our count
management.call_command("register-job", "TestProject", "example", 1234)
management.call_command("register-job", "test-project", "example", 1234)
self.assertCount(models.Exporter, 1)

# But registering a new one will
management.call_command("register-job", "TestProject", "example", 4321)
management.call_command("register-job", "test-project", "example", 4321)
self.assertCount(models.Exporter, 2)

@mock.patch("promgen.signals._trigger_write_config")
def test_register_host(self, mock_signal):
# Assert when project doesn't exist
with self.assertRaises(CommandError):
management.call_command("register-host", "missing-project", "example.com")
management.call_command("register-host", "missing-project", "cli.example.com")

# Create a Service and Project and then try adding our job
shard = models.Shard.objects.create(name="TestShard")
service = models.Service.objects.create(name="TestService")
project = models.Project.objects.create(name="TestProject", service=service, shard=shard)
project = models.Project.objects.create(name="cli-project", service_id=1, shard_id=1)

# Still assert an error if there is no Farm
with self.assertRaises(CommandError):
management.call_command("register-host", "TestProject", "example.com")
management.call_command("register-host", "cli-project", "cli.example.com")

# Register farm and finally register host
project.farm = models.Farm.objects.create(name="TestFarm")
project.farm = models.Farm.objects.create(name="cli-farm")
project.save()
management.call_command("register-host", "TestProject", "example.com")
self.assertCount(models.Host, 1, "Should be a single host registered")
management.call_command("register-host", "cli-project", "cli.example.com")
self.assertCount(models.Host, 2, "Should be a new host registered (1 host from fixture)")

0 comments on commit 8f7516e

Please sign in to comment.