Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
make hardware_product.specification not nullable
Browse files Browse the repository at this point in the history
..and make the tests look a little more like reality
  • Loading branch information
karenetheridge committed Dec 3, 2020
1 parent 80a2ca5 commit 8806a80
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 43 deletions.
9 changes: 1 addition & 8 deletions docs/json-schema/request.json
Expand Up @@ -640,14 +640,7 @@
"title" : "SKU"
},
"specification" : {
"oneOf" : [
{
"$ref" : "common.json#/$defs/HardwareProductSpecification"
},
{
"type" : "null"
}
],
"$ref" : "common.json#/$defs/HardwareProductSpecification",
"title" : "Specification"
},
"usb_num" : {
Expand Down
9 changes: 1 addition & 8 deletions docs/json-schema/response.json
Expand Up @@ -1450,14 +1450,7 @@
"title" : "SKU"
},
"specification" : {
"oneOf" : [
{
"$ref" : "common.json#/$defs/HardwareProductSpecification"
},
{
"type" : "null"
}
],
"$ref" : "common.json#/$defs/HardwareProductSpecification",
"title" : "Specification"
},
"updated" : {
Expand Down
3 changes: 2 additions & 1 deletion docs/modules/Conch::DB::Result::HardwareProduct.md
Expand Up @@ -78,7 +78,8 @@ original: {default_value => \"now()"}

```
data_type: 'jsonb'
is_nullable: 1
default_value: '{}'
is_nullable: 0
```

### sku
Expand Down
4 changes: 1 addition & 3 deletions json-schema/request.yaml
Expand Up @@ -258,9 +258,7 @@ $defs:
$ref: common.yaml#/$defs/uuid
specification:
title: Specification
oneOf:
- $ref: common.yaml#/$defs/HardwareProductSpecification
- type: 'null'
$ref: common.yaml#/$defs/HardwareProductSpecification
sku:
title: SKU
$ref: common.yaml#/$defs/mojo_standard_placeholder
Expand Down
4 changes: 1 addition & 3 deletions json-schema/response.yaml
Expand Up @@ -654,9 +654,7 @@ $defs:
$ref: common.yaml#/$defs/mojo_standard_placeholder
specification:
title: Specification
oneOf:
- $ref: common.yaml#/$defs/HardwareProductSpecification
- type: 'null'
$ref: common.yaml#/$defs/HardwareProductSpecification
rack_unit_size:
title: Rack Unit Size (RU)
$ref: common.yaml#/$defs/positive_integer
Expand Down
7 changes: 4 additions & 3 deletions lib/Conch/DB/Result/HardwareProduct.pm
Expand Up @@ -79,7 +79,8 @@ __PACKAGE__->table("hardware_product");
=head2 specification
data_type: 'jsonb'
is_nullable: 1
default_value: '{}'
is_nullable: 0
=head2 sku
Expand Down Expand Up @@ -285,7 +286,7 @@ __PACKAGE__->add_columns(
original => { default_value => \"now()" },
},
"specification",
{ data_type => "jsonb", is_nullable => 1 },
{ data_type => "jsonb", default_value => "{}", is_nullable => 0 },
"sku",
{ data_type => "text", is_nullable => 0 },
"generation_name",
Expand Down Expand Up @@ -441,7 +442,7 @@ __PACKAGE__->has_many(


# Created by DBIx::Class::Schema::Loader v0.07049
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:9Q3v8Ag2aFFkitFegf0t2g
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:jh81nspu+x8IBhfHU063jQ

use experimental 'signatures';
use Mojo::JSON 'from_json';
Expand Down
@@ -0,0 +1,8 @@
SELECT run_migration(179, $$

update hardware_product set specification = '{}' where specification is null;

alter table hardware_product alter column specification set default '{}',
alter column specification set not null;

$$);
2 changes: 1 addition & 1 deletion sql/schema.sql
Expand Up @@ -380,7 +380,7 @@ CREATE TABLE public.hardware_product (
deactivated timestamp with time zone,
created timestamp with time zone DEFAULT now() NOT NULL,
updated timestamp with time zone DEFAULT now() NOT NULL,
specification jsonb,
specification jsonb DEFAULT '{}'::jsonb NOT NULL,
sku text NOT NULL,
generation_name text,
legacy_product_name text,
Expand Down
75 changes: 59 additions & 16 deletions t/integration/crud/hardware-product.t
Expand Up @@ -258,20 +258,24 @@ $t->get_ok('/hardware_product/sungo')
->status_is(409)
->json_is({ error => 'there is more than one match' });

my $base_specification = $hw_fields{specification} = {
disk_size => { _default => 0, AcmeCorp => 512 },
};

subtest 'manipulate hardware_product.specification' => sub {
$t->put_ok('/hardware_product/'.$new_hw_id.'/specification', json => {})
->status_is(400)
->json_schema_is('QueryParamsValidationError')
->json_cmp_deeply('/details', [ superhashof({ error => 'missing property: path' }) ]);

$t->put_ok('/hardware_product/'.$new_hw_id.'/specification?path=', json => {})
$t->put_ok('/hardware_product/'.$new_hw_id.'/specification?path=', json => $base_specification)
->status_is(204)
->location_is('/hardware_product/'.$new_hw_id);

$t->get_ok('/hardware_product/'.$new_hw_id)
->status_is(200)
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({ specification => {} }));
->json_cmp_deeply(superhashof({ specification => $base_specification }));

$t->put_ok('/hardware_product/'.$new_hw_id.'/specification?path=', json => 'hello')
->status_is(400)
Expand All @@ -298,7 +302,10 @@ subtest 'manipulate hardware_product.specification' => sub {
->status_is(200)
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({
specification => { disk_size => { _default => 128 } },
specification => {
$base_specification->%*,
disk_size => { _default => 128 },
},
}));

$t->put_ok('/hardware_product/'.$new_hw_id.'/specification?path=/disk_size/SEAGATE_8000',
Expand All @@ -319,7 +326,10 @@ subtest 'manipulate hardware_product.specification' => sub {
->status_is(200)
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({
specification => { disk_size => { _default => 128, SEAGATE_8000 => 1 } },
specification => {
$base_specification->%*,
disk_size => { _default => 128, SEAGATE_8000 => 1 },
},
}));

$t->put_ok('/hardware_product/'.$new_hw_id.'/specification?path=/disk_size/_default', json => 64)
Expand All @@ -330,7 +340,10 @@ subtest 'manipulate hardware_product.specification' => sub {
->status_is(200)
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({
specification => { disk_size => { _default => 64, SEAGATE_8000 => 1 } },
specification => {
$base_specification->%*,
disk_size => { _default => 64, SEAGATE_8000 => 1 },
},
}));

# the path we want to operate on is called .../~1~device/ and encodes as .../~01~0device/...
Expand All @@ -347,14 +360,18 @@ subtest 'manipulate hardware_product.specification' => sub {
->status_is(200)
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({
specification => { disk_size => {
_default => 64,
SEAGATE_8000 => 1,
'tilde~1~device' => 2,
} },
specification => {
$base_specification->%*,
disk_size => {
_default => 64,
SEAGATE_8000 => 1,
'tilde~1~device' => 2,
},
},
}));

$t->put_ok('/hardware_product/'.$new_hw_id.'/specification?path=/other_prop', json => [ 1,2,3 ])
$t->put_ok('/hardware_product/'.$new_hw_id.'/specification?path=/disks',
json => { usb_hdd_num => 0 })
->status_is(204)
->location_is('/hardware_product/'.$new_hw_id);

Expand All @@ -363,8 +380,25 @@ subtest 'manipulate hardware_product.specification' => sub {
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({
specification => {
$base_specification->%*,
disk_size => { _default => 64, SEAGATE_8000 => 1, 'tilde~1~device' => 2 },
other_prop => [ 1, 2, 3 ],
disks => { usb_hdd_num => 0 },
},
}));

$t->put_ok('/hardware_product/'.$new_hw_id.'/specification?path=/disks',
json => { usb_hdd_num => 0, sas_ssd_slots => '1,2,3' })
->status_is(204)
->location_is('/hardware_product/'.$new_hw_id);

$t->get_ok('/hardware_product/'.$new_hw_id)
->status_is(200)
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({
specification => {
$base_specification->%*,
disk_size => { _default => 64, SEAGATE_8000 => 1, 'tilde~1~device' => 2 },
disks => { usb_hdd_num => 0, sas_ssd_slots => '1,2,3' },
},
}));

Expand Down Expand Up @@ -393,8 +427,9 @@ subtest 'manipulate hardware_product.specification' => sub {
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({
specification => {
$base_specification->%*,
disk_size => { _default => 64, SEAGATE_8000 => 1 },
other_prop => [ 1, 2, 3 ],
disks => { usb_hdd_num => 0, sas_ssd_slots => '1,2,3' },
},
}));

Expand All @@ -406,17 +441,25 @@ subtest 'manipulate hardware_product.specification' => sub {
->status_is(200)
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({
specification => { other_prop => [ 1, 2, 3 ] },
specification => {
$base_specification->%{ grep $_ ne 'disk_size', keys $base_specification->%* },
disks => { usb_hdd_num => 0, sas_ssd_slots => '1,2,3' },
},
}));

$t->delete_ok('/hardware_product/'.$new_hw_id.'/specification?path=')
$t->delete_ok('/hardware_product/'.$new_hw_id.'/specification?path=/disks/usb_hdd_num')
->status_is(204)
->location_is('/hardware_product/'.$new_hw_id);

$t->get_ok('/hardware_product/'.$new_hw_id)
->status_is(200)
->json_schema_is('HardwareProduct')
->json_cmp_deeply(superhashof({ specification => undef }));
->json_cmp_deeply(superhashof({
specification => {
$base_specification->%{ grep $_ ne 'disk_size', keys $base_specification->%* },
disks => { sas_ssd_slots => '1,2,3' },
},
}));
};

subtest 'delete a hardware product' => sub {
Expand Down

0 comments on commit 8806a80

Please sign in to comment.